Skip to content

Commit

Permalink
attachments: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NC-jsAhonen committed Feb 4, 2025
1 parent 8da9bb4 commit 69f6c16
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 14 deletions.
61 changes: 48 additions & 13 deletions forms/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def test_meeting_memo_create(


@pytest.mark.django_db
def test_attachment_upload_and_download(
def test_attachment_post(
django_db_setup, admin_client, admin_user, plot_search_target, basic_form
):
example_file = SimpleUploadedFile(name="example.txt", content=b"Lorem lipsum")
Expand Down Expand Up @@ -402,14 +402,26 @@ def test_attachment_upload_and_download(
"ready": True,
}
response = admin_client.post(url, data=payload, content_type="application/json")
id = response.json()["id"]
assert response.status_code == 201

# Attachment should exist
assert Attachment.objects.filter(answer=response.json()["id"]).exists()


@pytest.mark.django_db
def test_attachment_get(
django_db_setup, admin_client, admin_user, answer_factory, attachment_factory
):
answer = answer_factory()
answer_id = answer.id
example_file = SimpleUploadedFile(name="example.txt", content=b"Lorem lipsum")
attachment = attachment_factory(
user=admin_user, answer=answer, attachment=example_file
)
attachment_id = attachment.id

# Should get attachments
url = reverse("v1:answer-attachments", kwargs={"pk": id})
url = reverse("v1:answer-attachments", kwargs={"pk": answer_id})
response = admin_client.get(url)
assert response.status_code == 200

Expand All @@ -424,12 +436,10 @@ def test_attachment_upload_and_download(

@pytest.mark.django_db
def test_attachment_delete(
django_db_setup, admin_client, admin_user, plot_search_target, basic_form
django_db_setup, admin_client, admin_user, attachment_factory
):
test_attachment_upload_and_download(
django_db_setup, admin_client, admin_user, plot_search_target, basic_form
)
attachment = Attachment.objects.all().first()
example_file = SimpleUploadedFile(name="example.txt", content=b"Lorem lipsum")
attachment = attachment_factory(attachment=example_file, user=admin_user)
url = reverse("v1:attachment-detail", kwargs={"pk": attachment.pk})
file_path = attachment.attachment.path
assert os.path.isfile(file_path) is True
Expand All @@ -439,7 +449,7 @@ def test_attachment_delete(


@pytest.mark.django_db
def test_attachment_upload_and_download_public(
def test_attachment_post_public(
django_db_setup, admin_client, admin_user, plot_search_target, basic_form
):
example_file = SimpleUploadedFile(name="example.txt", content=b"Lorem lipsum")
Expand Down Expand Up @@ -510,15 +520,40 @@ def test_attachment_upload_and_download_public(
assert response.status_code == 201
assert Attachment.objects.filter(answer=response.json()["id"]).exists()


@pytest.mark.django_db
def test_attachment_get_public(
django_db_setup, admin_client, admin_user, answer_factory, attachment_factory
):
answer = answer_factory()
answer_id = answer.id
example_file = SimpleUploadedFile(name="example.txt", content=b"Lorem lipsum")
attachment = attachment_factory(
user=admin_user, answer=answer, attachment=example_file
)
attachment_id = attachment.id

# Public endpoint should not allow getting attachments
with pytest.raises(NoReverseMatch):
url = reverse("v1:pub_answer-attachments", kwargs={"pk": response.json()["id"]})
url = reverse("v1:pub_answer-attachments", kwargs={"pk": answer_id})

# Public endpoint should not allow downloading attachments
url = reverse("v1:pub_attachment-download", kwargs={"pk": attachment_id})
file_response: FileResponse = admin_client.get(url)
assert len(response.json()) != 0
assert file_response.status_code == 405 # Method not allowed
response: FileResponse = admin_client.get(url)
assert response.status_code == 405 # Method not allowed


@pytest.mark.django_db
def test_attachment_delete_public(
django_db_setup, admin_user, admin_client, attachment_factory
):
example_file = SimpleUploadedFile(name="example.txt", content=b"Lorem lipsum")
attachment = attachment_factory(attachment=example_file, user=admin_user)
url = reverse("v1:pub_attachment-detail", kwargs={"pk": attachment.pk})
file_path = attachment.attachment.path
assert os.path.isfile(file_path) is True
response = admin_client.delete(url)
assert response.status_code == 405


@pytest.mark.django_db
Expand Down
44 changes: 43 additions & 1 deletion plotsearch/tests/api/test_plot_search.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import os

import pytest
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.serializers.json import DjangoJSONEncoder
from django.urls import reverse
from django.urls import NoReverseMatch, reverse
from django.utils import timezone
from django.utils.crypto import get_random_string
from django.utils.text import slugify
Expand Down Expand Up @@ -867,6 +868,26 @@ def test_area_search_attachment_create(
assert len(response.data["area_search_attachments"]) == 1


@pytest.mark.django_db
def test_area_search_attachment_delete(
area_search_attachment_factory, admin_user, admin_client
):
example_file = SimpleUploadedFile(content=b"Lorem Impsum", name="test.txt")
attachment = area_search_attachment_factory(
attachment=example_file, user=admin_user
)

url = reverse(
"v1:areasearchattachment-detail",
kwargs={"pk": attachment.id},
)
file_path = attachment.attachment.path
assert os.path.isfile(file_path) is True
response = admin_client.delete(url)

assert response.status_code == 204


@pytest.mark.django_db
def test_area_search_attachment_create_public(
django_db_setup, admin_client, area_search_test_data
Expand Down Expand Up @@ -900,6 +921,27 @@ def test_area_search_attachment_create_public(
).exists()


@pytest.mark.django_db
def test_area_search_attachment_delete_public(
area_search_attachment_factory, admin_user, admin_client
):
example_file = SimpleUploadedFile(content=b"Lorem Impsum", name="test.txt")
attachment = area_search_attachment_factory(
attachment=example_file, user=admin_user
)

url = reverse(
"v1:pub_area_search_attachment-detail",
kwargs={"pk": attachment.id},
)

file_path = attachment.attachment.path
assert os.path.isfile(file_path) is True
response = admin_client.delete(url)

assert response.status_code == 4059


@pytest.mark.django_db
def test_opening_record_create(
django_db_setup,
Expand Down

0 comments on commit 69f6c16

Please sign in to comment.