-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from dev-lymar/feat/dev-add-document-download-…
…view Feat/dev add document download view
- Loading branch information
Showing
4 changed files
with
95 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,57 @@ | ||
import json | ||
|
||
import pytest | ||
from django.core.cache import cache | ||
from django.urls import reverse | ||
from rest_framework.test import APIClient | ||
|
||
|
||
@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(make_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, make_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, make_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, make_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, | ||
make_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") | ||
assert response.json() == {"success": True, "data": data} | ||
response = authorized_client.post(url, make_test_data, format="json") | ||
assert response.status_code == 200 | ||
assert response.json() == {"Success": True} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test__create_docs_view__authorized_user_get_valid_data(authorized_client, make_test_data): | ||
url = reverse("file") | ||
cache_key = f"validated_data_{authorized_client.user.id}" | ||
cache.set(cache_key, json.dumps(make_test_data), timeout=120) | ||
response = authorized_client.get(url) | ||
assert response.status_code == 200 | ||
assert response.json() == {"message": "Documents saved"} | ||
cache.delete(cache_key) | ||
|
||
|
||
@pytest.mark.django_db | ||
def test__create_docs_view__authorized_user_get_no_data(authorized_client): | ||
url = reverse("file") | ||
response = authorized_client.get(url) | ||
assert response.status_code == 400 | ||
assert response.json() == {"error": "No data found in cache"} | ||
|
||
|
||
@pytest.mark.django_db | ||
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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
from django.urls import path | ||
|
||
from .views import DataDocView, CreateDocsView | ||
from .views import DataDocView, CreateDocsView, DownloadDocView | ||
|
||
urlpatterns = [ | ||
path("filemake/", CreateDocsView.as_view(), name="file"), | ||
path("downloadfile/", DownloadDocView.as_view(), name="downloadfile"), | ||
path("data/", DataDocView.as_view(), name="data"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters