""" notification test file """ # third party imports from fcm_django.models import FCMDevice # django imports from django.urls import reverse from rest_framework import status from account.models import UserNotification # local imports from account.serializers import GuardianSerializer from notifications.models import Notification from web_admin.tests.test_set_up import AnalyticsSetUp class NotificationTestCase(AnalyticsSetUp): """ test notification """ def setUp(self) -> None: """ test data up :return: """ super(NotificationTestCase, self).setUp() # notification settings create UserNotification.objects.create(user=self.user) # notification create self.notification = Notification.objects.create(notification_to=self.user, notification_from=self.user_3) # to get guardian/user auth token self.guardian_data = GuardianSerializer( self.guardian, context={'user_type': 2} ).data self.auth_token = self.guardian_data['auth_token'] # api header self.header = { 'HTTP_AUTHORIZATION': f'Bearer {self.auth_token}', 'Content-Type': "Application/json" } def test_notification_list(self): """ test notification list :return: """ url = reverse('notifications:notifications-list') response = self.client.get(url, **self.header) self.assertEqual(response.status_code, status.HTTP_200_OK) # Assuming only one notification exists in the database self.assertEqual(Notification.objects.filter(notification_to=self.user).count(), 1) def test_fcm_register(self): """ test fcm register :return: """ url = reverse('notifications:notifications-device') data = { 'registration_id': 'registration_id', 'device_id': 'device_id', 'type': 'ios' } response = self.client.post(url, data, **self.header) self.assertEqual(response.status_code, status.HTTP_200_OK) # device created for user self.assertEqual(FCMDevice.objects.count(), 1) def test_send_test_notification(self): """ test send test notification :return: """ url = reverse('notifications:notifications-test') response = self.client.get(url, **self.header) self.assertEqual(response.status_code, status.HTTP_200_OK) # Assuming one notification exists in the database and two created after api run self.assertEqual(Notification.objects.filter(notification_to=self.user).count(), 3) def test_mark_as_read(self): """ test mark as read :return: """ url = reverse('notifications:notifications-mark-as-read') data = { 'id': [self.notification.id] } response = self.client.patch(url, data, **self.header) self.notification.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(self.notification.is_read, True)