75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
|
import pytest
|
||
|
import uuid
|
||
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
||
|
from django.urls import reverse
|
||
|
from django.core.files.storage import default_storage
|
||
|
|
||
|
from ..models import ImageMemo, User
|
||
|
|
||
|
|
||
|
@pytest.mark.django_db
|
||
|
class TestDeleteDocument:
|
||
|
|
||
|
@pytest.fixture
|
||
|
def setup_users(self):
|
||
|
self.user1 = User.objects.create_user(
|
||
|
email="user1@test.com", password="password1"
|
||
|
)
|
||
|
self.user2 = User.objects.create_user(
|
||
|
email="user2@test.com", password="password2"
|
||
|
)
|
||
|
|
||
|
@pytest.fixture
|
||
|
def setup_document(self, setup_users):
|
||
|
img_content = b"fake image content"
|
||
|
test_image = SimpleUploadedFile(
|
||
|
name="test_image.jpg", content=img_content, content_type="image/jpeg"
|
||
|
)
|
||
|
self.image_memo = ImageMemo.objects.create(
|
||
|
author=self.user1,
|
||
|
image=test_image,
|
||
|
)
|
||
|
|
||
|
def test_delete_document_success(self, client, setup_document):
|
||
|
client.force_login(self.user1)
|
||
|
url = reverse("delete_document", kwargs={"pk": str(self.image_memo.id)})
|
||
|
response = client.post(url)
|
||
|
|
||
|
assert response.status_code == 302
|
||
|
assert response.url == reverse("dashboard")
|
||
|
assert not ImageMemo.objects.filter(id=self.image_memo.id).exists()
|
||
|
assert not default_storage.exists(self.image_memo.image.name)
|
||
|
|
||
|
def test_delete_nonexistent_document(self, client, setup_users):
|
||
|
client.force_login(self.user1)
|
||
|
url = reverse(
|
||
|
"delete_document", kwargs={"pk": "facade00-0000-4000-a000-000000000000"}
|
||
|
)
|
||
|
response = client.post(url)
|
||
|
|
||
|
assert response.status_code == 404
|
||
|
assert response.content == b"Document not found"
|
||
|
|
||
|
def test_delete_other_users_document(self, client, setup_document):
|
||
|
client.force_login(self.user2)
|
||
|
url = reverse("delete_document", kwargs={"pk": str(self.image_memo.id)})
|
||
|
response = client.post(url)
|
||
|
|
||
|
assert response.status_code == 404
|
||
|
assert response.content == b"Document not found"
|
||
|
assert ImageMemo.objects.filter(id=self.image_memo.id).exists()
|
||
|
|
||
|
def test_delete_document_missing_file(self, client, setup_document):
|
||
|
# Remove the file manually
|
||
|
default_storage.delete(self.image_memo.image.name)
|
||
|
|
||
|
client.force_login(self.user1)
|
||
|
url = reverse("delete_document", kwargs={"pk": str(self.image_memo.id)})
|
||
|
response = client.post(url)
|
||
|
|
||
|
assert response.status_code == 302
|
||
|
assert response.url == reverse("dashboard")
|
||
|
assert not ImageMemo.objects.filter(id=self.image_memo.id).exists()
|
||
|
|
||
|
# ...
|