58 lines
2.2 KiB
Python
58 lines
2.2 KiB
Python
import pytest
|
|
from django.urls import reverse
|
|
from rest_framework import status
|
|
|
|
from notifications.models import Notification
|
|
|
|
|
|
@pytest.mark.unit
|
|
@pytest.mark.django_db
|
|
class TestNotificationsAPI:
|
|
"""Test Notifications API"""
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def setup_test(self, add_user_with_permissions, django_user_model):
|
|
self.admin = django_user_model.objects.get()
|
|
self.user = add_user_with_permissions(username='test_user', password='TesTPasWord1', is_superuser=True)
|
|
self.notification = Notification.objects.create(text='Notification')
|
|
|
|
def test_notifications_list(self, api_client):
|
|
api_client.force_authenticate(self.user)
|
|
url = reverse('notifications-list')
|
|
|
|
response = api_client.get(url)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert response.json()['count'] == 1
|
|
|
|
Notification.objects.create(text='Notification2')
|
|
response = api_client.get(url)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert response.json()['count'] == 2
|
|
|
|
def test_user_sees_only_his_notification(self, api_client):
|
|
api_client.force_authenticate(self.admin)
|
|
url = reverse('notifications-list')
|
|
Notification.objects.create(text='Notification2', recipient=self.user)
|
|
|
|
assert Notification.objects.count() == 2
|
|
response = api_client.get(url)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert response.json()['count'] == 1
|
|
|
|
def test_read_notification(self, api_client):
|
|
api_client.force_authenticate(self.user)
|
|
url = reverse('notifications-read', args=[self.notification.pk])
|
|
|
|
assert self.notification.is_read is False
|
|
response = api_client.post(url)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert response.json()['is_read'] is True
|
|
assert Notification.objects.get().is_read is True
|
|
|
|
def test_read_notification_but_not_found(self, api_client):
|
|
api_client.force_authenticate(self.user)
|
|
url = reverse('notifications-read', args=[555])
|
|
|
|
response = api_client.post(url)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
assert Notification.objects.get().is_read is False
|