Initial commit from ZIP
This commit is contained in:
commit
378d4692a9
454 files changed
+40367
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user