From 5cbfc94560c3f69b95640e17631b2826d06ef223 Mon Sep 17 00:00:00 2001 From: Domejko Date: Sat, 16 Mar 2024 10:17:09 +0100 Subject: [PATCH 1/2] Tear down PermissionError bug fix for Windows OS --- tests/views/test_receipts.py | 46 +------------------ tests/views/test_reciepts_download.py | 65 +++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 45 deletions(-) create mode 100644 tests/views/test_reciepts_download.py diff --git a/tests/views/test_receipts.py b/tests/views/test_receipts.py index d3b37b3f..ed0fbdd2 100644 --- a/tests/views/test_receipts.py +++ b/tests/views/test_receipts.py @@ -1,10 +1,8 @@ -import uuid - from django.contrib.messages import get_messages from django.core.files.uploadedfile import SimpleUploadedFile from django.urls import reverse, resolve -from backend.models import Receipt, ReceiptDownloadToken +from backend.models import Receipt from tests.handler import ViewTestCase @@ -84,45 +82,3 @@ def test_receipt_create_post_with_invalid_image(self): messages = list(get_messages(response.wsgi_request)) self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0]), "No image found") - - -class ReceiptDownloadEndpointsTest(ViewTestCase): - def setUp(self): - super().setUp() - self.receipt = Receipt.objects.create( - user=self.log_in_user, image=SimpleUploadedFile("mock_image.jpg", b"image_content", "image/jpeg") - ) - self.token = ReceiptDownloadToken.objects.create(user=self.log_in_user, file=self.receipt) - self.download_receipt_url = reverse("api:receipts:download_receipt", args=[self.token.token]) - self.generate_download_link_url = reverse("api:receipts:generate_download_link", args=[self.receipt.id]) - - def test_download_receipt_valid_token(self): - self.login_user() - response = self.client.get(self.download_receipt_url) - self.assertEqual(response.status_code, 200) - self.assertEqual(response["Content-Type"], "image/jpeg") - - def test_download_receipt_invalid_token(self): - self.login_user() - invalid_token = uuid.uuid4() # Generate a valid UUID - invalid_url = reverse("api:receipts:download_receipt", args=[invalid_token]) - response = self.client.get(invalid_url) - self.assertEqual(response.status_code, 404) # Update expected status code - - def test_download_receipt_used_token(self): - self.login_user() - self.client.get(self.download_receipt_url) # Use the token once - response = self.client.get(self.download_receipt_url) # Try to use the token again - self.assertEqual(response.status_code, 404) # Expect a 404 response - - def test_generate_download_link_valid_receipt(self): - self.login_user() - response = self.client.get(self.generate_download_link_url) - self.assertEqual(response.status_code, 200) - self.assertEqual(response["Content-Type"], "application/json") - - def test_generate_download_link_invalid_receipt(self): - self.login_user() - invalid_url = reverse("api:receipts:generate_download_link", args=[9999]) # Assuming 9999 is an invalid receipt id - response = self.client.get(invalid_url) - self.assertEqual(response.status_code, 404) diff --git a/tests/views/test_reciepts_download.py b/tests/views/test_reciepts_download.py new file mode 100644 index 00000000..01fa3efc --- /dev/null +++ b/tests/views/test_reciepts_download.py @@ -0,0 +1,65 @@ +import os +import shutil +import uuid + +from django.core.files.uploadedfile import SimpleUploadedFile +from django.urls import reverse +from django.test import TestCase + +from backend.models import Receipt, ReceiptDownloadToken, User + + +class ReceiptDownloadEndpointsTest(TestCase): + @classmethod + def tearDownClass(cls): + directory = "media/receipts" + try: + shutil.rmtree(directory) + os.mkdir(directory) + except PermissionError: + print( + '"ReceiptDownloadEndpointsTest" tearDownClass() failed due to random PermissionError ' + "(only on Windows). Files in /media/receipts have not been deleted. Run tests again to delete files." + ) + pass + super().tearDownClass() + + def setUp(self): + super().setUp() + self.log_in_user = User.objects.create_user(username="test@example.com", password="user", email="test@example.com") + self.client.login(username="test@example.com", password="user") + + self.receipt = Receipt.objects.create( + user=self.log_in_user, image=SimpleUploadedFile("mock_image.jpg", b"image_content", "image/jpeg") + ) + self.token = ReceiptDownloadToken.objects.create(user=self.log_in_user, file=self.receipt) + self.download_receipt_url = reverse("api:receipts:download_receipt", args=[self.token.token]) + self.generate_download_link_url = reverse("api:receipts:generate_download_link", args=[self.receipt.id]) + + def test_download_receipt_valid_token(self): + response = self.client.get(self.download_receipt_url) + self.assertEqual(response.status_code, 200) + self.assertEqual(response["Content-Type"], "image/jpeg") + + def test_download_receipt_invalid_token(self): + invalid_token = uuid.uuid4() # Generate a valid UUID + invalid_url = reverse("api:receipts:download_receipt", args=[invalid_token]) + response = self.client.get(invalid_url) + self.assertEqual(response.status_code, 404) # Update expected status code + + def test_download_receipt_used_token(self): + self.client.get(self.download_receipt_url) # Use the token once + response = self.client.get(self.download_receipt_url) # Try to use the token again + self.assertEqual(response.status_code, 404) # Expect a 404 response + + def test_generate_download_link_valid_receipt(self): + response = self.client.get(self.generate_download_link_url) + self.assertEqual(response.status_code, 200) + self.assertEqual(response["Content-Type"], "application/json") + self.client.logout() + + def test_generate_download_link_invalid_receipt(self): + invalid_url = reverse("api:receipts:generate_download_link", args=[9999]) # Assuming 9999 is an invalid receipt id + response = self.client.get(invalid_url) + self.assertEqual(response.status_code, 404) + self.client.logout() From f84c0ddf49b8331f4071f5b2991af1314f414c00 Mon Sep 17 00:00:00 2001 From: Domejko Date: Sat, 16 Mar 2024 10:45:28 +0100 Subject: [PATCH 2/2] Tear down PermissionError bug fix for Windows OS --- tests/views/test_reciepts_download.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/views/test_reciepts_download.py b/tests/views/test_reciepts_download.py index 01fa3efc..e69411ad 100644 --- a/tests/views/test_reciepts_download.py +++ b/tests/views/test_reciepts_download.py @@ -21,7 +21,6 @@ def tearDownClass(cls): '"ReceiptDownloadEndpointsTest" tearDownClass() failed due to random PermissionError ' "(only on Windows). Files in /media/receipts have not been deleted. Run tests again to delete files." ) - pass super().tearDownClass() def setUp(self):