Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tear down PermissionError bug fix for Windows OS #218

Merged
merged 4 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 1 addition & 45 deletions tests/views/test_receipts.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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)
64 changes: 64 additions & 0 deletions tests/views/test_reciepts_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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."
)
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()
Loading