Source code for tests.notifications.test_views

from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase

from apps.notifications.models import Notification
from tests.notifications.factories import CommentNotificationFactory, FollowNotificationFactory, LikeNotificationFactory
from tests.users.factories import UserFactory  # type: ignore


[docs] class TestNotificationAPIView(APITestCase): """Test Notification api view"""
[docs] def setUp(self) -> None: self.user = UserFactory() self.url = reverse("notifications:notification") CommentNotificationFactory.create_batch(3, receiver=self.user) LikeNotificationFactory.create_batch(3, receiver=self.user) FollowNotificationFactory.create_batch(3, receiver=self.user)
[docs] def test_resolve_url(self): """Test resolve url""" url = "/api/notifications/" response = self.client.get(self.url) self.assertNotIn(response.status_code, [status.HTTP_404_NOT_FOUND, status.HTTP_405_METHOD_NOT_ALLOWED]) self.assertEqual(self.url, url)
[docs] def test_not_authorizated(self): """Test not authorizated""" self.client.force_authenticate(user=None) response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
[docs] def test_success(self): self.client.force_authenticate(user=self.user) response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data["results"]), 9)
[docs] class TestReadNotificationAPIView(APITestCase): """Test Notification api view"""
[docs] def setUp(self) -> None: self.user = UserFactory() self.url = reverse("notifications:read") CommentNotificationFactory.create_batch(3, receiver=self.user, is_read=False) LikeNotificationFactory.create_batch(3, receiver=self.user, is_read=False) FollowNotificationFactory.create_batch(3, receiver=self.user, is_read=False)
[docs] def test_resolve_url(self): """Test resolve url""" url = "/api/notifications/read/" response = self.client.post(self.url) self.assertNotIn(response.status_code, [status.HTTP_404_NOT_FOUND, status.HTTP_405_METHOD_NOT_ALLOWED]) self.assertEqual(self.url, url)
[docs] def test_not_authorizated(self): """Test not authorizated""" self.client.force_authenticate(user=None) response = self.client.post(self.url) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
[docs] def test_success(self): self.client.force_authenticate(user=self.user) response = self.client.post(self.url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(Notification.objects.filter(receiver=self.user, is_read=True).count(), 9)