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)
|
|
|
|
|
2022-01-28 02:23:31 +00:00
|
|
|
def test_get_unread_status_string(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"
|
2022-01-28 02:23:31 +00:00
|
|
|
) as mock_count, patch(
|
|
|
|
"bookwyrm.activitystreams.ActivityStream.get_unread_count_by_status_type"
|
|
|
|
) as mock_count_by_status:
|
|
|
|
mock_count.return_value = 3
|
|
|
|
mock_count_by_status.return_value = {"review": 5}
|
|
|
|
result = views.get_unread_status_string(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())
|
2022-01-28 02:23:31 +00:00
|
|
|
self.assertEqual(data["count"], "Load 5 unread statuses")
|
2021-12-10 02:16:34 +00:00
|
|
|
|
2022-01-28 02:23:31 +00:00
|
|
|
def test_get_unread_status_string_with_filters(self):
|
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
|
|
|
self.local_user.feed_status_types = ["comment", "everything"]
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"bookwyrm.activitystreams.ActivityStream.get_unread_count"
|
|
|
|
) as mock_count, patch(
|
|
|
|
"bookwyrm.activitystreams.ActivityStream.get_unread_count_by_status_type"
|
|
|
|
) as mock_count_by_status:
|
|
|
|
mock_count.return_value = 3
|
|
|
|
mock_count_by_status.return_value = {
|
|
|
|
"generated_note": 1,
|
|
|
|
"comment": 1,
|
|
|
|
"review": 10,
|
|
|
|
}
|
|
|
|
result = views.get_unread_status_string(request, "home")
|
|
|
|
|
|
|
|
self.assertIsInstance(result, JsonResponse)
|
|
|
|
data = json.loads(result.getvalue())
|
|
|
|
self.assertEqual(data["count"], "Load 2 unread statuses")
|
|
|
|
|
|
|
|
def test_get_unread_status_string_invalid_stream(self):
|
2021-12-10 02:16:34 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
with self.assertRaises(Http404):
|
2022-01-28 02:23:31 +00:00
|
|
|
views.get_unread_status_string(request, "fish")
|