diff --git a/makedoc/tests/conftest.py b/makedoc/tests/conftest.py index e1dcc06..b4200a7 100644 --- a/makedoc/tests/conftest.py +++ b/makedoc/tests/conftest.py @@ -1,9 +1,12 @@ import pytest +from faker import Faker from rest_framework.test import APIClient from rest_framework_simplejwt.tokens import RefreshToken from users.models import CustomUser +fake = Faker() + @pytest.fixture def user(django_user_model): @@ -19,3 +22,22 @@ def authorized_client(user): client.credentials(HTTP_AUTHORIZATION=f"Bearer {str(refresh.access_token)}") client.user = user return client + + +@pytest.fixture +def test_data(): + return { + "delivery_type": fake.random_element(elements=("auto", "manual")), + "client_id": fake.random_int(min=1, max=100), + "items": [ + {"product_id": fake.random_int(min=1, max=100), + "quantity": fake.random_int(min=1, max=10), + "discount": fake.random_int(min=0, max=50)}, + {"product_id": fake.random_int(min=1, max=100), + "quantity": fake.random_int(min=1, max=10), + "discount": fake.random_int(min=0, max=50)}, + ], + "factory_id": fake.random_int(min=1, max=100), + "destination": fake.city(), + "delivery_cost": fake.random_int(min=100, max=2000), + } diff --git a/makedoc/tests/test_views_makedoc.py b/makedoc/tests/test_views_makedoc.py index d482fb2..20f8dda 100644 --- a/makedoc/tests/test_views_makedoc.py +++ b/makedoc/tests/test_views_makedoc.py @@ -7,77 +7,34 @@ @pytest.mark.django_db -def test__data_doc_view__unauthorized_user_cannot_post_data() -> None: +def test__data_doc_view__unauthorized_user_cannot_post_data(test_data): client = APIClient() url = reverse("data") - data = { - "delivery_type": "auto", - "client_id": 1, - "items": [ - {"product_id": 1, "quantity": 2, "discount": 10}, - {"product_id": 2, "quantity": 5, "discount": 4}, - ], - "factory_id": 1, - "destination": "New York", - "delivery_cost": 1500, - } - response = client.post(url, data, format="json") - assert response.status_code == 401 # Unauthorized + response = client.post(url, test_data, format="json") + assert response.status_code == 401 @pytest.mark.django_db -def test__data_doc_view__authorized_user_can_post_data(authorized_client) -> None: +def test__data_doc_view__authorized_user_can_post_data(authorized_client, test_data): url = reverse("data") - data = { - "delivery_type": "auto", - "client_id": 1, - "items": [ - {"product_id": 1, "quantity": 2, "discount": 10}, - {"product_id": 2, "quantity": 5, "discount": 4}, - ], - "factory_id": 1, - "destination": "New York", - "delivery_cost": 1500, - } - response = authorized_client.post(url, data, format="json") + response = authorized_client.post(url, test_data, format="json") assert response.status_code == 200 @pytest.mark.django_db -def test__data_doc_view__authorized_user_post_data_response_is_correct(authorized_client) -> None: +def test__data_doc_view__authorized_user_post_data_response_is_correct(authorized_client, + test_data): url = reverse("data") - data = { - "delivery_type": "auto", - "client_id": 1, - "items": [ - {"product_id": 1, "quantity": 2, "discount": 10}, - {"product_id": 2, "quantity": 5, "discount": 4}, - ], - "factory_id": 1, - "destination": "New York", - "delivery_cost": 1500, - } - response = authorized_client.post(url, data, format="json") + response = authorized_client.post(url, test_data, format="json") assert response.status_code == 200 assert response.json() == {"Success": True} @pytest.mark.django_db -def test_create_docs_view_get_valid_data(authorized_client): +def test__create_docs_view__get_valid_data(authorized_client, test_data): url = reverse("file") cache_key = f"validated_data_{authorized_client.user.id}" - data = { - "delivery_type": "auto", - "client_id": 1, - "items": [ - {"product_id": 1, "quantity": 2, "discount": 10}, - {"product_id": 2, "quantity": 5, "discount": 4}, - ], - "factory_id": 1, - "destination": "New York", - "delivery_cost": 1500, - } - cache.set(cache_key, json.dumps(data), timeout=120) + cache.set(cache_key, json.dumps(test_data), timeout=120) response = authorized_client.get(url) assert response.status_code == 200 assert response.json() == {"message": "Documents saved"} @@ -85,7 +42,7 @@ def test_create_docs_view_get_valid_data(authorized_client): @pytest.mark.django_db -def test_create_docs_view_get_no_data(authorized_client): +def test__create_docs_view__get_no_data(authorized_client): url = reverse("file") response = authorized_client.get(url) assert response.status_code == 400 @@ -93,8 +50,8 @@ def test_create_docs_view_get_no_data(authorized_client): @pytest.mark.django_db -def test_download_doc_view(authorized_client): - url = reverse("downloadxls") +def test__download_doc_view__returns_404_when_no_file_exists(authorized_client): + url = reverse("downloadfile") response = authorized_client.get(url) assert response.status_code == 404 assert response.json() == {"error": "No file found"} diff --git a/makedoc/urls.py b/makedoc/urls.py index 4e480fd..ce10cec 100644 --- a/makedoc/urls.py +++ b/makedoc/urls.py @@ -4,6 +4,6 @@ urlpatterns = [ path("filemake/", CreateDocsView.as_view(), name="file"), - path("downloadxls/", DownloadDocView.as_view(), name="downloadxls"), + path("downloadfile/", DownloadDocView.as_view(), name="downloadfile"), path("data/", DataDocView.as_view(), name="data"), ]