diff --git a/notifications/tests/test_admin.py b/notifications/tests/test_admin.py new file mode 100644 index 0000000..2e95ee7 --- /dev/null +++ b/notifications/tests/test_admin.py @@ -0,0 +1,13 @@ +from django.urls import reverse + +from notifications.tests.factories.notifications import NotificationFullFactory + + +def test_admin(admin_user, client): + app_name = "notifications" + NotificationFullFactory.create_batch(10) + + client.force_login(admin_user) + + response = client.get(reverse(f"admin:{app_name}_notification_changelist")) + assert response.status_code == 200 diff --git a/notifications/tests/test_models.py b/notifications/tests/test_models.py index 73d3121..daf8ee6 100644 --- a/notifications/tests/test_models.py +++ b/notifications/tests/test_models.py @@ -111,9 +111,32 @@ def test_build_url(): ), ) @pytest.mark.django_db -def test_natural_day(increase, expected_result): +def test_naturalday(increase, expected_result): initial_date = datetime(2023, 1, 1, 0, 0, 0) with freeze_time(initial_date): notification = NotificationShortFactory() with freeze_time(initial_date + timedelta(**increase)): assert notification.naturalday() == expected_result + + +@pytest.mark.parametrize( + "increase,expected_result", + ( + ({"minutes": 10}, "10\xa0minutes ago"), + ({"days": 1}, "1\xa0day ago"), + ), +) +@pytest.mark.django_db +def test_naturaltime(increase, expected_result): + initial_date = datetime(2023, 1, 1, 0, 0, 0) + with freeze_time(initial_date): + notification = NotificationShortFactory() + with freeze_time(initial_date + timedelta(**increase)): + assert notification.naturaltime() == expected_result + + +@pytest.mark.django_db +def test_extra_data(): + data = {"url": "/learn/ask-a-pro/q/test-question-9/299/", "other_content": "Hello my 'world'"} + notification = NotificationFullFactory(data=data) + assert notification.data == data diff --git a/notifications/tests/test_template_tags.py b/notifications/tests/test_template_tags.py index ef25815..b882a96 100644 --- a/notifications/tests/test_template_tags.py +++ b/notifications/tests/test_template_tags.py @@ -1,16 +1,13 @@ from unittest.mock import Mock, patch import pytest +from django.template import Context, Template from django.urls import reverse_lazy from freezegun import freeze_time from swapper import load_model from notifications.templatetags.notifications_tags import ( - has_notification, live_notify_badge, - live_notify_list, - notifications_unread, - register_notify_callbacks, user_context, ) from notifications.tests.factories.notifications import NotificationFullFactory @@ -19,27 +16,38 @@ Notification = load_model("notifications", "Notification") +def render_tag(template, context): + template = Template("{% load notifications_tags %}" + template) + return template.render(Context(context)) + + @pytest.mark.django_db def test_notifications_unread(): - with patch("notifications.templatetags.notifications_tags.user_context") as user_context_mock: - user_context_mock.return_value = None - assert notifications_unread({}) == "" + assert render_tag("{% notifications_unread %}", {}) == "" - notification = NotificationFullFactory() - user_context_mock.return_value = notification.recipient - assert notifications_unread({}) == 1 + assert ( + render_tag("{% notifications_unread %}", {"user": Mock(), "request": Mock(user=Mock(is_anonymous=True))}) == "" + ) + + notification = NotificationFullFactory() + assert ( + render_tag( + "{% notifications_unread %}", {"user": notification.recipient, "request": Mock(user=notification.recipient)} + ) + == "1" + ) @pytest.mark.django_db def test_has_notification(): user = RecipientFactory() - assert has_notification(user) is False + assert render_tag("{{ user|has_notification }}", {"user": user}) == "False" notification = NotificationFullFactory(recipient=user) - assert has_notification(user) is True + assert render_tag("{{ user|has_notification }}", {"user": user}) == "True" notification.mark_as_read() - assert has_notification(user) is False + assert render_tag("{{ user|has_notification }}", {"user": user}) == "False" @pytest.mark.parametrize( @@ -50,26 +58,19 @@ def test_has_notification(): ), ) def test_register_notify_callbacks(_type, url): - callback = register_notify_callbacks( - badge_class="badge1", - menu_class="menu2", - refresh_period=10, - callbacks="cb1,cb2", - api_name=_type, - fetch=50, - nonce="123", - mark_as_read=True, - ) + tag = f"{{% register_notify_callbacks badge_class='badge1' menu_class='menu2' refresh_period=10 callbacks='cb1,cb2' api_name='{_type}' fetch=50 nonce='123' mark_as_read=True %}}" # pylint: disable=line-too-long + + render = render_tag(tag, {}) - assert "notify_badge_class='badge1'" in callback - assert "notify_menu_class='menu2'" in callback - assert "notify_refresh_period=10" in callback - assert "register_notifier(cb1);" in callback - assert "register_notifier(cb2);" in callback - assert "notify_fetch_count='50'" in callback - assert 'nonce="123"' in callback - assert "true" in callback - assert str(url) in callback + assert "notify_badge_class='badge1'" in render + assert "notify_menu_class='menu2'" in render + assert "notify_refresh_period=10" in render + assert "register_notifier(cb1);" in render + assert "register_notifier(cb2);" in render + assert "notify_fetch_count='50'" in render + assert 'nonce="123"' in render + assert "true" in render + assert str(url) in render @patch("notifications.templatetags.notifications_tags.user_context") @@ -80,26 +81,27 @@ def test_live_notify_badge(cache_mock, user_context_mock): user_context_mock.return_value = None assert live_notify_badge({}) == "" + assert render_tag("{% live_notify_badge %}", {}) == "" user_context_mock.return_value = user - badge = live_notify_badge({}, badge_class="blafoo") + badge = render_tag("{% live_notify_badge badge_class='blafoo' %}", {}) assert "blafoo" in badge assert "0" in badge with freeze_time("2024-01-01 00:00:00"): NotificationFullFactory(recipient=user) - badge = live_notify_badge({}, badge_class="blafoo") + badge = render_tag("{% live_notify_badge badge_class='blafoo' %}", {}) assert "blafoo" in badge assert "0" in badge # Because of cache cache_mock.side_effect = lambda user: user.notifications.unread().count() - badge = live_notify_badge({}, badge_class="blafoo") + badge = render_tag("{% live_notify_badge badge_class='blafoo' %}", {}) assert "blafoo" in badge assert "1" in badge def test_live_notify_list(): - resp = live_notify_list("blafoo") + resp = render_tag("{% live_notify_list list_class='blafoo' %}", {}) assert "