-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to download OrderItem results by token UUID
- Loading branch information
Andrey Rusakov
committed
Nov 6, 2024
1 parent
b92140e
commit b0b6b0e
Showing
3 changed files
with
80 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,110 @@ | ||
import os | ||
import json | ||
from django.conf import settings | ||
import tempfile | ||
from uuid import uuid4 | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
from api.models import Order | ||
from api.models import Order, OrderItem | ||
from api.tests.factories import BaseObjectsFactory | ||
|
||
UUID_EXISTS = str(uuid4()) | ||
UUID_FILE_NOTFOUND = str(uuid4()) | ||
UUID_ORDER_NOTFOUND = str(uuid4()) | ||
# Order | ||
ORDER_EXISTS_UUID = str(uuid4()) | ||
ORDER_FILE_NOTFOUND_UUID = str(uuid4()) | ||
ORDER_NOTFOUND_UUID = str(uuid4()) | ||
|
||
# Order item | ||
ITEM_EXISTS_UUID = str(uuid4()) | ||
ITEM_FILE_NOTFOUND_UUID = str(uuid4()) | ||
ITEM_NOTFOUND_UUID = str(uuid4()) | ||
|
||
TMP_CONTENT="Hello world" | ||
|
||
|
||
class TestResponseFile(APITestCase): | ||
""" | ||
Test sending extract result files | ||
""" | ||
order_data = { | ||
"order_type": "Privé", | ||
"items": [ | ||
{"product": "Maquette 3D"}, | ||
{"product": "Maquette 3D"}, | ||
{"product": "Maquette 3D"}, | ||
], | ||
"title": "Test file exists", | ||
"description": "Nice order", | ||
"geom": {"type": "Polygon", "coordinates": [[[0,0], [0, 1], [1, 1], [0, 0]]]}, | ||
} | ||
|
||
def addOrder(self, updates) -> Order: | ||
data = self.order_data.copy() | ||
data.update(updates) | ||
id = json.loads(self.client.post(reverse("order-list"), data, format="json").content)["id"] | ||
return Order.objects.get(id=id) | ||
|
||
def setUp(self): | ||
self.config = BaseObjectsFactory(self.client) | ||
settings.MEDIA_ROOT = tempfile.mkdtemp() | ||
with open(os.path.join(settings.MEDIA_ROOT, "demo_file"), "w") as tmpfile: | ||
tmpfile.write(TMP_CONTENT) | ||
order_data = { | ||
"order_type": "Privé", | ||
"items": [], | ||
"title": "Test file exists", | ||
"description": "Nice order", | ||
"geom": {"type": "Polygon", "coordinates": [[[0,0], [0, 1], [1, 1], [0, 0]]]}, | ||
} | ||
with open(os.path.join(settings.MEDIA_ROOT, "another_demo_file"), "w") as tmpfile: | ||
tmpfile.write(TMP_CONTENT[::-1]) | ||
self.client.credentials(HTTP_AUTHORIZATION="Bearer " + self.config.client_token) | ||
self.client.post(reverse("order-list"), order_data, format="json").content | ||
order_data["title"] = "Test file not found" | ||
self.client.post(reverse("order-list"), order_data, format="json").content | ||
|
||
obj = Order.objects.filter(title="Test file exists")[0] | ||
obj.download_guid = UUID_EXISTS | ||
obj = self.addOrder({"title": "Test file exists"}) | ||
obj.download_guid = ORDER_EXISTS_UUID | ||
obj.extract_result.name = "demo_file" | ||
obj.save() | ||
|
||
obj = Order.objects.filter(title="Test file not found")[0] | ||
obj.download_guid = UUID_FILE_NOTFOUND | ||
obj = self.addOrder({"title": "Test file not found"}) | ||
obj.download_guid = ORDER_FILE_NOTFOUND_UUID | ||
obj.extract_result.name = "missing_demo_file" | ||
obj.save() | ||
|
||
def testSendFileSuccess(self): | ||
url = reverse("download_by_uuid", kwargs={"guid": UUID_EXISTS}) | ||
obj = self.addOrder({"title": "Order with tokened items"}) | ||
items = obj.items.all() | ||
item = OrderItem.objects.get(id=items[0].id) | ||
item.token = ITEM_EXISTS_UUID | ||
item.extract_result.name = "another_demo_file" | ||
item.save() | ||
|
||
item = OrderItem.objects.get(id=items[1].id) | ||
item.token = ITEM_FILE_NOTFOUND_UUID | ||
item.extract_result.name = "missing_another_demo_file" | ||
item.save() | ||
|
||
def testSendOrderFileSuccess(self): | ||
url = reverse("download_by_uuid", kwargs={"guid": ORDER_EXISTS_UUID}) | ||
resp = self.client.get(url) | ||
self.assertEqual(TMP_CONTENT, str(resp.content, "utf8")) | ||
|
||
def testSendOrderNotFound(self): | ||
url = reverse("download_by_uuid", kwargs={"guid": UUID_ORDER_NOTFOUND}) | ||
url = reverse("download_by_uuid", kwargs={"guid": ORDER_NOTFOUND_UUID}) | ||
resp = self.client.get(url) | ||
self.assertTrue("No Order matches" in str(resp.content)) | ||
self.assertTrue("No object matches" in str(resp.content)) | ||
self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
def testSendFileNotFound(self): | ||
url = reverse("download_by_uuid", kwargs={"guid": UUID_FILE_NOTFOUND}) | ||
url = reverse("download_by_uuid", kwargs={"guid": ORDER_FILE_NOTFOUND_UUID}) | ||
resp = self.client.get(url) | ||
self.assertTrue("file not found" in str(resp.content)) | ||
self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
def testSendItemFileSuccess(self): | ||
url = reverse("download_by_uuid", kwargs={"guid": ITEM_EXISTS_UUID}) | ||
resp = self.client.get(url) | ||
self.assertEqual(TMP_CONTENT[::-1], str(resp.content, "utf8")) | ||
|
||
def testSendItemNotFound(self): | ||
url = reverse("download_by_uuid", kwargs={"guid": ITEM_NOTFOUND_UUID}) | ||
resp = self.client.get(url) | ||
self.assertTrue("No object matches" in str(resp.content)) | ||
self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
def testSendItemFileNotFound(self): | ||
url = reverse("download_by_uuid", kwargs={"guid": ITEM_FILE_NOTFOUND_UUID}) | ||
resp = self.client.get(url) | ||
self.assertTrue("file not found" in str(resp.content)) | ||
self.assertEqual(resp.status_code, status.HTTP_404_NOT_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
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