Initial commit from ZIP

This commit is contained in:
kusz committed 2024-11-27 13:18:16 +01:00
commit 378d4692a9
454 files changed
+40367

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
import pytest
from rest_framework import status
from rest_framework.utils import json
@pytest.mark.django_db
class UserLoginE2ETests:
def test_missing_username_and_password_fail_validation(self, client):
# Arrange
# Act
response = client.post(
"/token/", data=json.dumps({}), content_type="application/json"
)
# Assert
assert response.status_code == status.HTTP_400_BAD_REQUEST
def test_valid_credentials_return_token(self, user_factory, client):
# Arrange
user_factory(username="username", password="valid_password")
# Act
response = client.post(
"/token/",
data=json.dumps({"username": "username", "password": "valid_password"}),
content_type="application/json",
)
data = response.json()
# Assert
assert response.status_code == status.HTTP_200_OK
assert "token" in data
@@ -0,0 +1,60 @@
import pytest
from pytest import fixture
from pytest_factoryboy import LazyFixture
from rest_framework import status
@pytest.mark.django_db
class GetProductBriefE2ETests:
@fixture(autouse=True)
def product_stages(self, product_stages):
pass
@fixture
def brief_url(self, product):
return f"/briefs/{product.id}/"
@fixture
def published_brief_url(self, published_product):
return f"/briefs/{published_product.id}/"
def test_unauthorized_cannot_open_brief(self, token, client, brief_url):
response = client.get(path=brief_url, content_type="application/json",)
assert response.status_code == status.HTTP_401_UNAUTHORIZED
@pytest.mark.parametrize(
"token__user,expected_status_code",
[
(LazyFixture("client_user"), status.HTTP_403_FORBIDDEN),
(LazyFixture("agency_user"), status.HTTP_200_OK),
(LazyFixture("supplier_user"), status.HTTP_200_OK),
],
)
def test_only_supplier_and_agency_can_open_nonpublished_brief(
self, token, client, brief_url, expected_status_code
):
response = client.get(
path=brief_url,
content_type="application/json",
HTTP_AUTHORIZATION=f"Bearer {token.key}",
)
assert response.status_code == expected_status_code
@pytest.mark.parametrize(
"token__user,expected_status_code",
[
(LazyFixture("client_user"), status.HTTP_200_OK),
(LazyFixture("agency_user"), status.HTTP_200_OK),
(LazyFixture("supplier_user"), status.HTTP_200_OK),
],
)
def test_everyone_can_open_published_brief(
self, token, client, published_brief_url, expected_status_code
):
response = client.get(
path=published_brief_url,
content_type="application/json",
HTTP_AUTHORIZATION=f"Bearer {token.key}",
)
assert response.status_code == expected_status_code
@@ -0,0 +1,38 @@
import pytest
from pytest import fixture
from pytest_factoryboy import LazyFixture
from rest_framework import status
@pytest.mark.django_db
class GetProductE2ETests:
@fixture(autouse=True)
def product_stages(self, product_stages):
pass
@fixture
def request_url(self, product):
return f"/products/{product.id}/"
@pytest.mark.parametrize(
"token__user,project__published,expected_status_code",
[
(LazyFixture("agency_user"), False, status.HTTP_200_OK),
(LazyFixture("client_user"), False, status.HTTP_403_FORBIDDEN),
(LazyFixture("supplier_user"), False, status.HTTP_200_OK),
(LazyFixture("agency_user"), True, status.HTTP_200_OK),
(LazyFixture("client_user"), True, status.HTTP_200_OK),
(LazyFixture("supplier_user"), True, status.HTTP_200_OK),
],
)
def test_user_can_see_product(
self, client, request_url, expected_status_code, token, project
):
response = client.get(
path=request_url,
content_type="application/json",
HTTP_AUTHORIZATION=f"Bearer {token.key}",
)
data = response.json()
assert response.status_code == expected_status_code
@@ -0,0 +1,56 @@
import pytest
from pytest import fixture
from pytest_factoryboy import LazyFixture
from rest_framework import status
@pytest.mark.django_db
class GetProductsListForProjectE2ETests:
@fixture(autouse=True)
def product_stages(self, product_stages):
pass
@fixture(autouse=True)
def project_with_two_products(self, project_factory):
return project_factory(products_amount=2, published=False)
@fixture(autouse=True)
def another_project_with_three_products(self, project_factory):
project_factory(products_amount=3, published=False)
@fixture
def request_url(self, project_with_two_products):
return f"/products/?project={project_with_two_products.id}"
def test_anonymous_user_is_not_authorized(self, client, request_url):
response = client.get(request_url, content_type="application/json")
# Assert
assert (
response.status_code == status.HTTP_401_UNAUTHORIZED
), "Wrong HTTP response code."
@pytest.mark.parametrize(
"token__user,expected_count",
[
(LazyFixture("agency_user"), 2),
(LazyFixture("client_user"), 0),
(LazyFixture("supplier_user"), 2),
],
)
def test_user_sees_only_published_products(
self, client, request_url, token, expected_count
):
# Arrange
# Act
response = client.get(
path=request_url,
content_type="application/json",
HTTP_AUTHORIZATION=f"Bearer {token.key}",
)
data = response.json()
# Assert
assert response.status_code == status.HTTP_200_OK
assert len(data) == expected_count
@@ -0,0 +1,21 @@
import pytest
from pytest import fixture
from pytest_factoryboy import LazyFixture
from rest_framework import status
@pytest.mark.skip("Not yet implemented")
@pytest.mark.django_db
class UpdateProductBriefE2ETests:
@fixture(autouse=True)
def product_stages(self, product_stages):
pass
@fixture
def brief_url(self, product):
return f"/briefs/{product.id}/"
@fixture
def published_brief_url(self, published_product):
return f"/briefs/{published_product.id}/"
@@ -0,0 +1,54 @@
import pytest
from pytest import fixture
from pytest_factoryboy import LazyFixture
@pytest.mark.django_db
class GetProductByIdE2ETests:
@fixture(autouse=True)
def product_stages(self, product_stages):
pass
@fixture
def request_url(self, project):
return f"/projects/{project.id}/"
@pytest.mark.parametrize(
"token__user,project__published,expected_status_code",
[
(LazyFixture("agency_user"), False, 200),
(LazyFixture("supplier_user"), False, 200),
(LazyFixture("agency_user"), True, 200),
(LazyFixture("client_user"), True, 200),
(LazyFixture("supplier_user"), True, 200),
],
)
def test_user_can_see_project(
self, client, request_url, expected_status_code, token
):
response = client.get(
path=request_url,
content_type="application/json",
HTTP_AUTHORIZATION=f"Bearer {token.key}",
)
assert response.status_code == expected_status_code
@pytest.mark.parametrize(
"token__user,project__published",
[
(LazyFixture("agency_user"), False),
(LazyFixture("supplier_user"), False),
(LazyFixture("agency_user"), True),
(LazyFixture("client_user"), True),
(LazyFixture("supplier_user"), True),
],
)
def test_user_sees_correct_project(self, client, request_url, token, project):
response = client.get(
path=request_url,
content_type="application/json",
HTTP_AUTHORIZATION=f"Bearer {token.key}",
)
data = response.json()
assert data["slug"] == project.slug
@@ -0,0 +1,45 @@
import pytest
from pytest import fixture
from pytest_factoryboy import LazyFixture
from rest_framework import status
@pytest.mark.django_db
class GetProjectsListE2ETests:
@fixture(autouse=True)
def product_stages(self, product_stages):
pass
@fixture
def request_url(self):
return f"/projects/"
@fixture(autouse=True)
def published_and_unpublished_projects(self, project_factory):
project_factory(published=True)
project_factory(published=True)
project_factory(published=False)
@pytest.mark.parametrize(
"token__user,expected_count",
[
(LazyFixture("agency_user"), 3),
(LazyFixture("client_user"), 2),
(LazyFixture("supplier_user"), 3),
],
)
def test_user_can_only_see_visible_projects(
self, client, request_url, token, expected_count
):
# Act
response = client.get(
path=request_url,
content_type="application/json",
HTTP_AUTHORIZATION=f"Bearer {token.key}",
)
data = response.json()
assert response.status_code == status.HTTP_200_OK
assert len(data) == expected_count
+281
View File
@@ -0,0 +1,281 @@
from typing import TYPE_CHECKING
import pytest
from pytest import fixture
from rest_framework import status
from rest_framework.utils import json
from outbo.apps.projects.models import Project
from tests import utils
if TYPE_CHECKING:
from outbo.apps.authentication.models import User
from rest_framework.authtoken.models import Token
@pytest.mark.django_db
class ProjectCreationE2ETests:
@fixture(autouse=True)
def product_stages(self, product_stages):
pass
BASE_FIELDS = (
"brand",
"category",
"description",
"name",
"printing_files_date",
"promo_date",
)
CREATE_FIELDS = BASE_FIELDS + ("products_amount",)
RETRIEVE_FIELDS = BASE_FIELDS + ("slug", "publishable", "published", "products")
FIELD_REQUIRED_MESSAGE = "This field is required."
AT_LEAST_ONE_PRODUCT = "Ensure this value is greater than or equal to 1."
def test_anonymous_user_is_not_authorized(self, client):
response = client.post("/projects/", content_type="application/json")
# Assert
assert (
response.status_code == status.HTTP_401_UNAUTHORIZED
), "Wrong HTTP response code."
def test_agency_user_not_allowed_to_create_project(
self, client, agency_user, token_factory, unauthorized_message
):
token: "Token" = token_factory(user=agency_user)
response = client.post(
path="/projects/",
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_create_project(
self, client, client_user, token_factory, unauthorized_message
):
# Arrange
token: "Token" = token_factory(user=client_user)
# Act
response = client.post(
path="/projects/",
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_create_project(
self, client, user_factory, token_factory, unauthorized_message
):
# Arrange
user: "User" = user_factory(is_staff=True)
token: "Token" = token_factory(user=user)
# Act
response = client.post(
path="/projects/",
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_supplier_user_fails_to_create_project_with_empty_data(
self, client, supplier_user, token_factory,
):
# Arrange
token: "Token" = token_factory(user=supplier_user)
# Act
response = client.post(
path="/projects/",
content_type="application/json",
HTTP_AUTHORIZATION=f"Bearer {token.key}",
)
data = response.json()
# Assert
assert (
response.status_code == status.HTTP_400_BAD_REQUEST
), "Invalid HTTP response code."
assert Project.objects.count() == 0, "Product was created and should not be."
for item in self.CREATE_FIELDS:
assert item in data
assert data[item] == [
self.FIELD_REQUIRED_MESSAGE
], f"""Invalid error message for field "{item}"."""
def test_supplier_user_creates_project_with_valid_data(
self, client, supplier_user, category_factory, brand_factory, token_factory,
):
# Arrange
token: "Token" = token_factory(user=supplier_user)
category = category_factory(name="Sample category")
brand = brand_factory(name="Sample brand")
project_data = {
"name": "Sample name",
"description": "Sample description",
"products_amount": 1,
"category": category.pk,
"brand": brand.pk,
"printing_files_date": "2019-10-30",
"promo_date": "2020-01-01",
}
expected_data = {
"name": "Sample name",
"description": "Sample description",
"products": "http://testserver/products/?project=1",
"category": category.pk,
"brand": brand.pk,
"printing_files_date": "2019-10-30",
"promo_date": "2020-01-01",
"slug": "#SASA1-0020",
"publishable": False,
"published": False,
}
# Act
response = client.post(
path="/projects/",
data=json.dumps(project_data),
content_type="application/json",
HTTP_AUTHORIZATION=f"Bearer {token.key}",
)
data = response.json()
# Assert
assert response.status_code == status.HTTP_201_CREATED
assert utils.is_subset(subset=expected_data, superset=data)
@pytest.mark.parametrize("products_amount", [1, 2, 4])
def test_supplier_user_creates_products_for_project(
self,
client,
supplier_user,
category_factory,
brand_factory,
token_factory,
products_amount,
):
# Arrange
token: "Token" = token_factory(user=supplier_user)
category = category_factory(name="Sample category")
brand = brand_factory(name="Sample brand")
project_data = {
"name": "Sample name",
"description": "Sample description",
"products_amount": products_amount,
"category": category.pk,
"brand": brand.pk,
"printing_files_date": "2019-10-30",
"promo_date": "2020-01-01",
}
# Act
response = client.post(
path="/projects/",
data=json.dumps(project_data),
content_type="application/json",
HTTP_AUTHORIZATION=f"Bearer {token.key}",
)
# Assert
assert response.status_code == status.HTTP_201_CREATED
assert Project.objects.count() == 1
project = Project.objects.first()
assert project.products.count() == products_amount
def test_supplier_user_fails_when_zero_products(
self, client, supplier_user, category_factory, brand_factory, token_factory,
):
# Arrange
token: "Token" = token_factory(user=supplier_user)
category = category_factory(name="Sample category")
brand = brand_factory(name="Sample brand")
project_data = {
"name": "Sample name",
"description": "Sample description",
"products_amount": 0,
"category": category.pk,
"brand": brand.pk,
"printing_files_date": "2019-10-30",
"promo_date": "2020-01-01",
}
expected_data = {"products_amount": [self.AT_LEAST_ONE_PRODUCT]}
# Act
response = client.post(
path="/projects/",
data=json.dumps(project_data),
content_type="application/json",
HTTP_AUTHORIZATION=f"Bearer {token.key}",
)
data = response.json()
# Assert
assert (
response.status_code == status.HTTP_400_BAD_REQUEST
), "Invalid HTTP response code."
assert data == expected_data, "Error message is invalid."
assert Project.objects.count() == 0, "Product was created and should not be."
@pytest.mark.parametrize("products_amount", [1, 2, 4])
def test_supplier_user_products_in_project_have_valid_stage(
self,
client,
supplier_user,
category_factory,
brand_factory,
token_factory,
products_amount,
):
# Arrange
token: "Token" = token_factory(user=supplier_user)
category = category_factory(name="Sample category")
brand = brand_factory(name="Sample brand")
project_data = {
"name": "Sample name",
"description": "Sample description",
"products_amount": products_amount,
"category": category.pk,
"brand": brand.pk,
"printing_files_date": "2019-10-30",
"promo_date": "2020-01-01",
}
# Act
response = client.post(
path="/projects/",
data=json.dumps(project_data),
content_type="application/json",
HTTP_AUTHORIZATION=f"Bearer {token.key}",
)
# Assert
assert response.status_code == status.HTTP_201_CREATED
assert Project.objects.count() == 1
project = Project.objects.first()
for product in project.products.all():
assert (
product.stage_id == "waiting_for_brief"
), f"""Product "{product}" has invalid stage."""
@@ -0,0 +1,87 @@
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