2021-03-13 19:11:06 +00:00
|
|
|
""" test for app action functionality """
|
|
|
|
import json
|
2021-03-23 20:02:45 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2021-12-10 02:16:34 +00:00
|
|
|
from django.http import Http404, JsonResponse
|
2021-03-13 19:11:06 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
|
|
|
|
|
|
|
from bookwyrm import models, views
|
|
|
|
|
|
|
|
|
|
|
|
class UpdateViews(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""lets the ui check for unread notification"""
|
2021-03-13 19:11:06 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""we need basic test data and mocks"""
|
2021-08-02 23:05:40 +00:00
|
|
|
self.factory = RequestFactory()
|
2021-09-06 21:48:45 +00:00
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
2021-12-09 23:03:01 +00:00
|
|
|
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
|
2021-08-03 17:25:53 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
"mouse@local.com",
|
|
|
|
"mouse@mouse.mouse",
|
|
|
|
"password",
|
|
|
|
local=True,
|
|
|
|
localname="mouse",
|
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
models.SiteSettings.objects.create()
|
2021-03-13 19:11:06 +00:00
|
|
|
|
2021-03-23 20:02:45 +00:00
|
|
|
def test_get_notification_count(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-03-13 19:11:06 +00:00
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
|
2021-03-23 20:02:45 +00:00
|
|
|
result = views.get_notification_count(request)
|
2021-03-13 19:11:06 +00:00
|
|
|
self.assertIsInstance(result, JsonResponse)
|
|
|
|
data = json.loads(result.getvalue())
|
2021-03-23 20:02:45 +00:00
|
|
|
self.assertEqual(data["count"], 0)
|
2021-03-13 19:11:06 +00:00
|
|
|
|
|
|
|
models.Notification.objects.create(
|
|
|
|
notification_type="BOOST", user=self.local_user
|
|
|
|
)
|
2021-03-23 20:02:45 +00:00
|
|
|
result = views.get_notification_count(request)
|
|
|
|
self.assertIsInstance(result, JsonResponse)
|
|
|
|
data = json.loads(result.getvalue())
|
|
|
|
self.assertEqual(data["count"], 1)
|
|
|
|
|
|
|
|
def test_get_unread_status_count(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-03-23 20:02:45 +00:00
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
|
2021-11-24 19:54:53 +00:00
|
|
|
with patch(
|
|
|
|
"bookwyrm.activitystreams.ActivityStream.get_unread_count"
|
|
|
|
) as mock_count:
|
|
|
|
with patch(
|
2021-12-10 02:16:34 +00:00
|
|
|
# pylint:disable=line-too-long
|
2021-11-24 19:54:53 +00:00
|
|
|
"bookwyrm.activitystreams.ActivityStream.get_unread_count_by_status_type"
|
|
|
|
) as mock_count_by_status:
|
2021-11-24 19:52:30 +00:00
|
|
|
mock_count.return_value = 3
|
|
|
|
mock_count_by_status.return_value = {"review": 5}
|
|
|
|
result = views.get_unread_status_count(request, "home")
|
2021-03-23 20:02:45 +00:00
|
|
|
|
2021-03-13 19:11:06 +00:00
|
|
|
self.assertIsInstance(result, JsonResponse)
|
|
|
|
data = json.loads(result.getvalue())
|
2021-03-23 20:02:45 +00:00
|
|
|
self.assertEqual(data["count"], 3)
|
2021-11-24 19:52:30 +00:00
|
|
|
self.assertEqual(data["count_by_type"]["review"], 5)
|
2021-12-10 02:16:34 +00:00
|
|
|
|
|
|
|
def test_get_unread_status_count_invalid_stream(self):
|
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
with self.assertRaises(Http404):
|
|
|
|
views.get_unread_status_count(request, "fish")
|