|
| 1 | +"""Endpoint Tests for News App.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from rest_framework import status |
| 5 | + |
| 6 | +from apps.utils.functions import generate_test_image |
| 7 | +from ...models import News |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.django_db |
| 11 | +def test_list_news(anonymous_user, news): |
| 12 | + endpoint = "/api/v1/news/" |
| 13 | + response = anonymous_user.get(endpoint) |
| 14 | + assert response.status_code == status.HTTP_200_OK |
| 15 | + assert response.reason_phrase == "OK" |
| 16 | + assert len(response.data["results"]) > 0 |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.django_db |
| 20 | +def test_retrieve_news(anonymous_user, news): |
| 21 | + endpoint = f"/api/v1/news/{news.id}/" |
| 22 | + response = anonymous_user.get(endpoint) |
| 23 | + assert response.status_code == status.HTTP_200_OK |
| 24 | + assert response.reason_phrase == "OK" |
| 25 | + assert str(response.data["id"]) == str(news.id) |
| 26 | + assert response.data["name"] == news.name |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.django_db |
| 30 | +def test_create_news(moderator_user, news): |
| 31 | + image = generate_test_image(size=(600, 600)) |
| 32 | + endpoint = "/api/v1/news/" |
| 33 | + data = { |
| 34 | + "name": "Lorem ipsum", |
| 35 | + "description": news.description, |
| 36 | + "content": news.content, |
| 37 | + "image": image, |
| 38 | + "source": news.source, |
| 39 | + "tag": news.tag, |
| 40 | + "author_id": str(news.author_id.id), |
| 41 | + } |
| 42 | + response = moderator_user.post(endpoint, data, format="multipart") |
| 43 | + assert response.status_code == status.HTTP_201_CREATED |
| 44 | + assert response.reason_phrase == "Created" |
| 45 | + assert News.objects.filter(name="Lorem ipsum").exists() |
| 46 | + assert response.data["name"] == "Lorem ipsum" |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.django_db |
| 50 | +def test_create_news_errors(member_user): |
| 51 | + endpoint = "/api/v1/news/" |
| 52 | + data = {} |
| 53 | + member_response = member_user.post(endpoint, data, format="json") |
| 54 | + assert member_response.status_code == status.HTTP_403_FORBIDDEN |
| 55 | + assert member_response.reason_phrase == "Forbidden" |
| 56 | + member_user.logout() |
| 57 | + anonymus_response = member_user.post(endpoint, data, format="json") |
| 58 | + assert anonymus_response.status_code == status.HTTP_401_UNAUTHORIZED |
| 59 | + assert anonymus_response.reason_phrase == "Unauthorized" |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.django_db |
| 63 | +def test_update_news(moderator_user, news): |
| 64 | + image = generate_test_image(size=(600, 600)) |
| 65 | + endpoint = f"/api/v1/news/{news.id}/" |
| 66 | + data = { |
| 67 | + "name": "Updated News", |
| 68 | + "description": news.description, |
| 69 | + "content": news.content, |
| 70 | + "image": image, |
| 71 | + "source": news.source, |
| 72 | + "tag": news.tag, |
| 73 | + "author_id": str(news.author_id.id), |
| 74 | + } |
| 75 | + response = moderator_user.put(endpoint, data, format="multipart") |
| 76 | + assert response.status_code == status.HTTP_200_OK |
| 77 | + assert response.reason_phrase == "OK" |
| 78 | + news.refresh_from_db() |
| 79 | + assert news.name == "Updated News" |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.django_db |
| 83 | +def test_partial_update_news(moderator_user, news): |
| 84 | + endpoint = f"/api/v1/news/{news.id}/" |
| 85 | + data = {"name": "Partially Updated News"} |
| 86 | + response = moderator_user.patch(endpoint, data, format="json") |
| 87 | + assert response.status_code == status.HTTP_200_OK |
| 88 | + assert response.reason_phrase == "OK" |
| 89 | + news.refresh_from_db() |
| 90 | + assert news.name == "Partially Updated News" |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.django_db |
| 94 | +def test_delete_news(moderator_user, news): |
| 95 | + assert news.is_available |
| 96 | + endpoint = f"/api/v1/news/{news.id}/" |
| 97 | + response = moderator_user.delete(endpoint) |
| 98 | + news.refresh_from_db() |
| 99 | + assert response.status_code == status.HTTP_204_NO_CONTENT |
| 100 | + assert response.reason_phrase == "No Content" |
| 101 | + assert News.objects.filter(id=news.id).exists() |
| 102 | + assert not news.is_available |
0 commit comments