from typing import TYPE_CHECKING import pytest from pytest import fixture from rest_framework import status if TYPE_CHECKING: from outbo.apps.authentication.models import User from rest_framework.authtoken.models import Token @pytest.mark.django_db class ProjectPublishingE2ETests: @fixture(autouse=True) def product_stages(self, product_stages): pass @fixture(autouse=True) def project_with_two_products(self, project, product_factory): product_factory(project=project) product_factory(project=project) def test_anonymous_user_is_not_authorized(self, client, project): response = client.patch( f"/projects/{project.pk}/", content_type="application/json" ) # Assert assert ( response.status_code == status.HTTP_401_UNAUTHORIZED ), "Wrong HTTP response code." def test_agency_user_not_allowed_to_publish_project( self, client, agency_user, token_factory, project, unauthorized_message ): # Arrange token: "Token" = token_factory(user=agency_user) # Act response = client.patch( path=f"/projects/{project.pk}/", content_type="application/json", HTTP_AUTHORIZATION=f"Bearer {token.key}", ) data = response.json() # Assert assert ( response.status_code == status.HTTP_403_FORBIDDEN ), "Invalid HTTP response code." assert data["detail"] == unauthorized_message, "Invalid error message." def test_client_user_not_allowed_to_publish_project( self, client, client_user, token_factory, project, unauthorized_message ): # Arrange token: "Token" = token_factory(user=client_user) # Act response = client.patch( path=f"/projects/{project.pk}/", content_type="application/json", HTTP_AUTHORIZATION=f"Bearer {token.key}", ) data = response.json() # Assert assert response.status_code == status.HTTP_403_FORBIDDEN assert data["detail"] == unauthorized_message def test_admin_user_not_allowed_to_publish_project( self, client, user_factory, token_factory, project, unauthorized_message ): # Arrange user: "User" = user_factory(is_staff=True) token: "Token" = token_factory(user=user) # Act response = client.patch( path=f"/projects/{project.pk}/", content_type="application/json", HTTP_AUTHORIZATION=f"Bearer {token.key}", ) data = response.json() # Assert assert response.status_code == status.HTTP_403_FORBIDDEN assert data["detail"] == unauthorized_message