Skip to content

Commit

Permalink
test attachment excluded fields: set intersection into a variable for…
Browse files Browse the repository at this point in the history
… readability
  • Loading branch information
NC-jsAhonen committed Feb 6, 2025
1 parent 7793ddd commit ea0ae37
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
9 changes: 6 additions & 3 deletions forms/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,13 @@ def test_attachment_post_public(
response = admin_client.post(url, data=payload)
assert response.status_code == 201

# When attachments are created, the HTTP response should not return any sensitive or unnecessary data
# Checks that the response does not contain any of the keys in EXCLUDED_ATTACHMENT_FIELDS
# When attachments are created,
# the HTTP response should not return any sensitive or unnecessary data
attachment_keys = response.json().keys()
assert len(set(EXCLUDED_ATTACHMENT_FIELDS).intersection(set(attachment_keys))) == 0
unwanted_fields_in_response = set(EXCLUDED_ATTACHMENT_FIELDS).intersection(
set(attachment_keys)
)
assert len(unwanted_fields_in_response) == 0

attachment_id = response.json()["id"]
url = reverse("v1:pub_answer-list")
Expand Down
31 changes: 14 additions & 17 deletions plotsearch/tests/api/test_plot_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,15 +824,14 @@ def test_area_search_create_simple_public(
response = admin_client.post(
url, json.dumps(data, cls=DjangoJSONEncoder), content_type="application/json"
)

# When attachments are created,
# the HTTP response should not return any sensitive or unnecessary data
attachments = response.json().get("area_search_attachments")
assert (
len(
set(attachments[0].keys()).intersection(
set(EXCLUDED_AREA_SEARCH_ATTACHMENT_FIELDS)
)
)
== 0
unwanted_fields_in_response = set(attachments[0].keys()).intersection(
set(EXCLUDED_AREA_SEARCH_ATTACHMENT_FIELDS)
)
assert len(unwanted_fields_in_response) == 0

assert response.status_code == 201, "%s %s" % (response.status_code, response.data)
assert AreaSearch.objects.filter(id=response.data["id"]).exists()
Expand Down Expand Up @@ -905,17 +904,15 @@ def test_area_search_attachment_create_public(

assert response.status_code == 201

# When attachments are created, the HTTP response should not return any sensitive or unnecessary data
# Checks that the response does not contain any of the keys in EXCLUDED_ATTACHMENT_FIELDS
# When attachments are created,
# the HTTP response should not return any sensitive or unnecessary data
attachment_keys = response.json().keys()
assert (
len(
set(EXCLUDED_AREA_SEARCH_ATTACHMENT_FIELDS).intersection(
set(attachment_keys)
)
)
== 0
)

unwanted_fields_in_response = set(
EXCLUDED_AREA_SEARCH_ATTACHMENT_FIELDS
).intersection(set(attachment_keys))

assert len(unwanted_fields_in_response) == 0
assert AreaSearchAttachment.objects.filter(
area_search=area_search_test_data
).exists()
Expand Down
15 changes: 9 additions & 6 deletions plotsearch/tests/serializers/test_plot_search_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,19 @@ def test_get_plan_unit_or_custom_detailed_plan_badrequest(


@pytest.mark.django_db
def test_attachment_public_serializer(attachment_factory, user_factory):
def test_attachment_public_serializer_unwanted_fields(attachment_factory, user_factory):
user = user_factory()
example_file = SimpleUploadedFile(name="example.txt", content=b"Lorem lipsum")
attachment = attachment_factory(attachment=example_file, user=user)
data = AttachmentPublicSerializer(attachment).data
assert len(set(data.keys()).intersection(set(EXCLUDED_ATTACHMENT_FIELDS))) == 0
unwanted_fields_in_data = set(data.keys()).intersection(
set(EXCLUDED_ATTACHMENT_FIELDS)
)
assert len(unwanted_fields_in_data) == 0


@pytest.mark.django_db
def test_area_search_attachment_public_serializer(
def test_area_search_attachment_public_serializer_unwanted_fields(
area_search_attachment_factory, user_factory
):
user = user_factory()
Expand All @@ -98,7 +101,7 @@ def test_area_search_attachment_public_serializer(
attachment=example_file, user=user
)
data = AreaSearchAttachmentPublicSerializer(area_search_attachment).data
assert (
len(set(data.keys()).intersection(set(EXCLUDED_AREA_SEARCH_ATTACHMENT_FIELDS)))
== 0
unwanted_fields_in_data = set(data.keys()).intersection(
set(EXCLUDED_AREA_SEARCH_ATTACHMENT_FIELDS)
)
assert len(unwanted_fields_in_data) == 0

0 comments on commit ea0ae37

Please sign in to comment.