Skip to content

Commit 9ae8d11

Browse files
committed
Added more tests, fixed settings
1 parent c9157e4 commit 9ae8d11

4 files changed

Lines changed: 92 additions & 24 deletions

File tree

hypha/apply/funds/templates/submissions/all.html

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -500,15 +500,17 @@
500500
{% heroicon_outline "trash" aria_hidden="true" size=14 class="stroke-base-content/80" %}
501501
{% trans "Delete" %}
502502
</button>
503-
<button
504-
hx-post="{% url 'apply:submissions:bulk-skeleton' %}"
505-
hx-include="[name='selectedSubmissionIds']"
506-
hx-confirm="{% trans 'Are you sure you want to anonymize the selected submissions? All draft submission will be deleted. This action cannot be undone.' %}"
507-
class="btn btn-sm"
508-
>
509-
{% heroicon_outline "user-minus" aria_hidden="true" size=14 class="stroke-base-content/80" %}
510-
{% trans "Anonymize" %}
511-
</button>
503+
{% if SUBMISSION_SKELETONING_ENABLED %}
504+
<button
505+
hx-post="{% url 'apply:submissions:bulk-skeleton' %}"
506+
hx-include="[name='selectedSubmissionIds']"
507+
hx-confirm="{% trans 'Are you sure you want to anonymize the selected submissions? All draft submission will be deleted. This action cannot be undone.' %}"
508+
class="btn btn-sm"
509+
>
510+
{% heroicon_outline "user-minus" aria_hidden="true" size=14 class="stroke-base-content/80" %}
511+
{% trans "Anonymize" %}
512+
</button>
513+
{% endif %}
512514
{% endif %}
513515
</section>
514516
</header>

hypha/apply/funds/tests/views/test_submission_delete.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from django.test import override_settings
12
from django.urls import reverse
23

4+
from hypha.apply.funds.models.submissions import ApplicationSubmissionSkeleton
35
from hypha.apply.funds.tests.factories.models import ApplicationSubmissionFactory
46
from hypha.apply.funds.workflows import DRAFT_STATE
57
from hypha.apply.users.tests.factories import AdminFactory, ApplicantFactory
@@ -26,8 +28,9 @@ def test_submission_delete_by_admin(db, client):
2628
assert res.status_code == 200
2729
assert "<form" in res.content.decode()
2830
assert f'action="{delete_url}"' in res.content.decode()
31+
assert "id_anon_or_delete" not in res.content.decode()
2932

30-
res = client.post(delete_url, data={"delete": "delete"})
33+
res = client.post(delete_url, data={"delete": "delete", "anon_or_delete": "DELETE"})
3134
assert res.status_code == 302
3235
assert res.url == "/apply/submissions/all/"
3336

@@ -36,6 +39,61 @@ def test_submission_delete_by_admin(db, client):
3639
assert res.status_code == 404
3740

3841

42+
@override_settings(SUBMISSION_SKELETONING_ENABLED=True)
43+
def test_submission_delete_by_admin_skeleton_enabled(db, client):
44+
# Check admin can delete submission
45+
user = AdminFactory()
46+
submission = ApplicationSubmissionFactory()
47+
48+
client.force_login(user)
49+
delete_url = reverse("apply:submissions:delete", kwargs={"pk": submission.pk})
50+
res = client.get(delete_url)
51+
assert res.status_code == 200
52+
assert "<form" in res.content.decode()
53+
assert f'action="{delete_url}"' in res.content.decode()
54+
55+
res = client.post(delete_url, data={"delete": "delete", "anon_or_delete": "DELETE"})
56+
assert res.status_code == 302
57+
assert res.url == "/apply/submissions/all/"
58+
59+
# Check submission is deleted
60+
res = client.get(delete_url)
61+
assert res.status_code == 404
62+
63+
# Ensure no skeleton submission was created
64+
assert ApplicationSubmissionSkeleton.objects.all().count() == 0
65+
66+
67+
@override_settings(SUBMISSION_SKELETONING_ENABLED=True)
68+
def test_submission_skeleton_by_admin_skeleton_enabled(db, client):
69+
user = AdminFactory()
70+
submission = ApplicationSubmissionFactory()
71+
72+
client.force_login(user)
73+
delete_url = reverse("apply:submissions:delete", kwargs={"pk": submission.pk})
74+
res = client.get(delete_url)
75+
assert res.status_code == 200
76+
assert "<form" in res.content.decode()
77+
assert f'action="{delete_url}"' in res.content.decode()
78+
79+
# Add the skeleton option to the delete form
80+
res = client.post(
81+
delete_url, data={"delete": "delete", "anon_or_delete": "ANONYMIZE"}
82+
)
83+
assert res.status_code == 302
84+
assert res.url == "/apply/submissions/all/"
85+
86+
# Check submission is deleted
87+
res = client.get(delete_url)
88+
assert res.status_code == 404
89+
90+
# Ensure a new skeleton submission was created
91+
assert ApplicationSubmissionSkeleton.objects.all().count() == 1
92+
93+
last_skeleton = ApplicationSubmissionSkeleton.objects.last()
94+
assert last_skeleton.value == submission.form_data["value"]
95+
96+
3997
def test_submission_delete_by_applicant(db, client):
4098
user = ApplicantFactory()
4199
submission = ApplicationSubmissionFactory(user=user, status=DRAFT_STATE)

hypha/apply/funds/views/all.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
from django.core.exceptions import PermissionDenied
1010
from django.core.paginator import Paginator
1111
from django.db import models
12-
from django.http import HttpRequest, HttpResponse, HttpResponseForbidden
12+
from django.http import (
13+
HttpRequest,
14+
HttpResponse,
15+
HttpResponseForbidden,
16+
HttpResponseNotAllowed,
17+
)
1318
from django.shortcuts import render
1419
from django.urls import reverse_lazy
1520
from django.utils.translation import gettext as _
@@ -383,21 +388,24 @@ def bulk_delete_submissions(request):
383388
@login_required
384389
@require_http_methods(["POST"])
385390
def bulk_skeleton_submissions(request):
386-
if not permissions.can_bulk_delete_submissions(request.user):
387-
return HttpResponseForbidden()
391+
if settings.SUBMISSION_SKELETONING_ENABLED:
392+
if not permissions.can_bulk_delete_submissions(request.user):
393+
return HttpResponseForbidden()
388394

389-
submission_ids = request.POST.getlist("selectedSubmissionIds")
390-
submissions = ApplicationSubmission.objects.filter(id__in=submission_ids).values(
391-
"id", "form_data", "page_id", "round_id", "status", "submit_time"
392-
)
395+
submission_ids = request.POST.getlist("selectedSubmissionIds")
396+
submissions = ApplicationSubmission.objects.filter(
397+
id__in=submission_ids
398+
).values("id", "form_data", "page_id", "round_id", "status", "submit_time")
393399

394-
services.bulk_covert_to_skeleton_submissions(
395-
submissions=submissions,
396-
user=request.user,
397-
request=request,
398-
)
400+
services.bulk_covert_to_skeleton_submissions(
401+
submissions=submissions,
402+
user=request.user,
403+
request=request,
404+
)
399405

400-
return HttpResponseClientRefresh()
406+
return HttpResponseClientRefresh()
407+
408+
return HttpResponseNotAllowed()
401409

402410

403411
@login_required

hypha/settings/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
# Enable Projects in Hypha. Contracts and invoicing that comes after a submission is approved.
9191
PROJECTS_ENABLED = env.bool("PROJECTS_ENABLED", False)
9292

93-
SUBMISSION_SKELETONING_ENABLED = True
93+
SUBMISSION_SKELETONING_ENABLED = env.bool("SUBMISSION_SKELETONING_ENABLED", False)
9494

9595
# Auto create projects for approved applications.
9696
PROJECTS_AUTO_CREATE = env.bool("PROJECTS_AUTO_CREATE", False)

0 commit comments

Comments
 (0)