Initial commit from ZIP
This commit is contained in:
commit
378d4692a9
454 files changed
+40367
No files matched your search
Executable
Whitespace-only changes.
Executable
+58
@@ -0,0 +1,58 @@
|
||||
from pytest import fixture
|
||||
|
||||
from pytest_factoryboy import register, LazyFixture
|
||||
from tests.factories.authentication import (
|
||||
UserFactory,
|
||||
AgencyUserFactory,
|
||||
ClientUserFactory,
|
||||
SupplierUserFactory,
|
||||
)
|
||||
from tests.factories.organizations import (
|
||||
AgencyOrganizationFactory,
|
||||
ClientOrganizationFactory,
|
||||
SupplierOrganizationFactory,
|
||||
)
|
||||
from tests.factories.products import ProductStageFactory, ProductFactory
|
||||
from tests.factories.projects import (
|
||||
CategoryFactory,
|
||||
BrandFactory,
|
||||
ProjectFactory,
|
||||
)
|
||||
from tests.factories.token import TokenFactory
|
||||
|
||||
|
||||
register(UserFactory)
|
||||
register(AgencyUserFactory, "agency_user")
|
||||
register(ClientUserFactory, "client_user")
|
||||
register(SupplierUserFactory, "supplier_user")
|
||||
|
||||
register(TokenFactory)
|
||||
|
||||
register(AgencyOrganizationFactory)
|
||||
register(ClientOrganizationFactory)
|
||||
register(SupplierOrganizationFactory)
|
||||
|
||||
register(CategoryFactory)
|
||||
register(BrandFactory)
|
||||
register(ProjectFactory)
|
||||
register(ProjectFactory, "published_project", published=True)
|
||||
|
||||
register(ProductFactory)
|
||||
register(ProductFactory, "published_product", project=LazyFixture("published_project"))
|
||||
register(ProductStageFactory)
|
||||
|
||||
|
||||
@fixture
|
||||
def product_stages(product_stage_factory):
|
||||
product_stage_factory(
|
||||
id="waiting_for_brief",
|
||||
label="1",
|
||||
group=1,
|
||||
description="Briefing",
|
||||
next_action="Waiting for brief",
|
||||
)
|
||||
|
||||
|
||||
@fixture
|
||||
def unauthorized_message():
|
||||
return "You do not have permission to perform this action."
|
||||
@@ -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
@@ -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
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
import factory
|
||||
from faker import Factory as FakerFactory
|
||||
|
||||
from outbo.apps.authentication import models
|
||||
from tests.factories.organizations import (
|
||||
AgencyOrganizationFactory,
|
||||
ClientOrganizationFactory,
|
||||
SupplierOrganizationFactory,
|
||||
)
|
||||
|
||||
faker = FakerFactory.create()
|
||||
|
||||
|
||||
class UserFactory(factory.django.DjangoModelFactory):
|
||||
username = factory.LazyAttribute(lambda x: faker.profile()["username"])
|
||||
first_name = factory.LazyAttribute(lambda x: faker.first_name())
|
||||
last_name = factory.LazyAttribute(lambda x: faker.last_name())
|
||||
email = factory.LazyAttribute(lambda x: faker.profile()["mail"])
|
||||
is_staff = False
|
||||
is_superuser = False
|
||||
password = "password"
|
||||
|
||||
@classmethod
|
||||
def _create(cls, model_class, *args, **kwargs):
|
||||
manager = cls._get_manager(model_class)
|
||||
return manager.create_user(*args, **kwargs)
|
||||
|
||||
class Meta:
|
||||
model = models.User
|
||||
django_get_or_create = ("username",)
|
||||
|
||||
|
||||
class AgencyUserFactory(UserFactory):
|
||||
organization = factory.SubFactory(AgencyOrganizationFactory)
|
||||
|
||||
|
||||
class ClientUserFactory(UserFactory):
|
||||
organization = factory.SubFactory(ClientOrganizationFactory)
|
||||
|
||||
|
||||
class SupplierUserFactory(UserFactory):
|
||||
organization = factory.SubFactory(SupplierOrganizationFactory)
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
import factory
|
||||
from faker import Factory as FakerFactory
|
||||
|
||||
from outbo.apps.organizations import models
|
||||
|
||||
faker = FakerFactory.create()
|
||||
|
||||
|
||||
class AgencyOrganizationFactory(factory.django.DjangoModelFactory):
|
||||
name = factory.LazyAttribute(lambda x: faker.company())
|
||||
|
||||
class Meta:
|
||||
model = models.AgencyOrganization
|
||||
|
||||
|
||||
class ClientOrganizationFactory(factory.django.DjangoModelFactory):
|
||||
name = factory.LazyAttribute(lambda x: faker.company())
|
||||
|
||||
class Meta:
|
||||
model = models.ClientOrganization
|
||||
|
||||
|
||||
class SupplierOrganizationFactory(factory.django.DjangoModelFactory):
|
||||
name = factory.LazyAttribute(lambda x: faker.company())
|
||||
|
||||
class Meta:
|
||||
model = models.SupplierOrganization
|
||||
|
||||
|
||||
class CategoryFactory(factory.django.DjangoModelFactory):
|
||||
name = factory.LazyAttribute(lambda x: faker.word())
|
||||
|
||||
class Meta:
|
||||
model = models.Category
|
||||
|
||||
|
||||
class BrandFactory(factory.django.DjangoModelFactory):
|
||||
name = factory.LazyAttribute(lambda x: faker.word())
|
||||
client = factory.SubFactory(ClientOrganizationFactory)
|
||||
|
||||
class Meta:
|
||||
model = models.Brand
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
import factory
|
||||
|
||||
from outbo.apps.products import models
|
||||
from tests.factories.projects import ProjectFactory
|
||||
from faker import Factory as FakerFactory
|
||||
|
||||
faker = FakerFactory.create()
|
||||
|
||||
|
||||
class ProductStageFactory(factory.django.DjangoModelFactory):
|
||||
id = ""
|
||||
label = ""
|
||||
group = 0
|
||||
description = ""
|
||||
next_action = ""
|
||||
order = 0
|
||||
|
||||
class Meta:
|
||||
model = models.ProductStage
|
||||
|
||||
|
||||
class PackagingTypeFactory(factory.django.DjangoModelFactory):
|
||||
name = factory.LazyAttribute(lambda x: faker.word())
|
||||
|
||||
class Meta:
|
||||
model = models.PackagingType
|
||||
|
||||
|
||||
class ProductFactory(factory.django.DjangoModelFactory):
|
||||
project = factory.SubFactory(ProjectFactory)
|
||||
|
||||
class Meta:
|
||||
model = models.Product
|
||||
Executable
+30
@@ -0,0 +1,30 @@
|
||||
import factory
|
||||
from faker import Factory as FakerFactory
|
||||
|
||||
from outbo.apps.projects import models
|
||||
from tests.factories.organizations import CategoryFactory, BrandFactory
|
||||
|
||||
faker = FakerFactory.create()
|
||||
|
||||
|
||||
class ProjectFactory(factory.django.DjangoModelFactory):
|
||||
name = factory.LazyAttribute(lambda x: faker.sentence())
|
||||
description = factory.LazyAttribute(lambda x: " ".join(faker.sentences(nb=3)))
|
||||
category = factory.SubFactory(CategoryFactory)
|
||||
brand = factory.SubFactory(BrandFactory)
|
||||
printing_files_date = factory.LazyAttribute(
|
||||
lambda x: faker.date_between(start_date="today", end_date="+60d")
|
||||
)
|
||||
promo_date = factory.LazyAttribute(
|
||||
lambda x: faker.date_between(start_date="+60d", end_date="+120d")
|
||||
)
|
||||
products_amount = factory.LazyAttribute(lambda x: faker.random_int(min=1, max=10))
|
||||
published = False
|
||||
|
||||
@classmethod
|
||||
def _create(cls, model_class, *args, **kwargs):
|
||||
manager = cls._get_manager(model_class)
|
||||
return manager.create(*args, **kwargs)
|
||||
|
||||
class Meta:
|
||||
model = models.Project
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
import binascii
|
||||
import os
|
||||
|
||||
import factory
|
||||
from faker import Factory as FakerFactory
|
||||
from rest_framework.authtoken import models
|
||||
|
||||
from tests.factories.authentication import UserFactory
|
||||
|
||||
faker = FakerFactory.create()
|
||||
|
||||
|
||||
class TokenFactory(factory.django.DjangoModelFactory):
|
||||
user = factory.SubFactory(UserFactory)
|
||||
key = factory.LazyAttribute(lambda x: binascii.hexlify(os.urandom(20)).decode())
|
||||
|
||||
class Meta:
|
||||
model = models.Token
|
||||
@@ -0,0 +1,41 @@
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_user_has_proper_username(user_factory):
|
||||
user = user_factory(username="username")
|
||||
assert user.username == "username"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_user_is_admin(user_factory):
|
||||
admin_user = user_factory(is_staff=True)
|
||||
|
||||
assert admin_user.is_staff is True
|
||||
assert admin_user.is_agency is False
|
||||
assert admin_user.is_client is False
|
||||
assert admin_user.is_supplier is False
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_user_has_agency(agency_user):
|
||||
assert agency_user.is_staff is False
|
||||
assert agency_user.is_agency is True
|
||||
assert agency_user.is_client is False
|
||||
assert agency_user.is_supplier is False
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_user_has_client(client_user):
|
||||
assert client_user.is_staff is False
|
||||
assert client_user.is_agency is False
|
||||
assert client_user.is_client is True
|
||||
assert client_user.is_supplier is False
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_user_has_supplier(supplier_user):
|
||||
assert supplier_user.is_staff is False
|
||||
assert supplier_user.is_agency is False
|
||||
assert supplier_user.is_client is False
|
||||
assert supplier_user.is_supplier is True
|
||||
Executable
+2
@@ -0,0 +1,2 @@
|
||||
def test_pytest_running():
|
||||
assert True
|
||||
Executable
+25
@@ -0,0 +1,25 @@
|
||||
def is_subset(*, superset, subset):
|
||||
for key, value in subset.items():
|
||||
if key not in superset:
|
||||
return False
|
||||
|
||||
if isinstance(value, dict):
|
||||
if not is_subset(superset=superset[key], subset=value):
|
||||
return False
|
||||
|
||||
elif isinstance(value, str):
|
||||
if value not in superset[key]:
|
||||
return False
|
||||
|
||||
elif isinstance(value, list):
|
||||
if not set(value) <= set(superset[key]):
|
||||
return False
|
||||
elif isinstance(value, set):
|
||||
if not value <= superset[key]:
|
||||
return False
|
||||
|
||||
else:
|
||||
if not value == superset[key]:
|
||||
return False
|
||||
|
||||
return True
|
||||
Reference in new issue
Block a user