Add unit tests

This commit is contained in:
zandercymatics 2024-01-05 12:32:08 -07:00
parent 0bbbc895b0
commit 90760e6d43
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
4 changed files with 83 additions and 4 deletions

View file

@ -94,6 +94,86 @@ class LoggedInTests(TestWithUser):
# clean up
application.delete()
def test_home_deletes_withdrawn_domain_application(self):
"""Tests if the user can delete a DomainApplication in the 'withdrawn' status"""
site = DraftDomain.objects.create(name="igorville.gov")
application = DomainApplication.objects.create(
creator=self.user, requested_domain=site, status=DomainApplication.ApplicationStatus.WITHDRAWN
)
# Ensure that igorville.gov exists on the page
home_page = self.client.get("/")
self.assertContains(home_page, "igorville.gov")
# Check if the delete button exists. We can do this by checking for its id and text content.
self.assertContains(home_page, "Delete")
self.assertContains(home_page, "button-toggle-delete-domain-alert-1")
# Trigger the delete logic
response = self.client.post(reverse("application-delete", kwargs={"pk": application.pk}), follow=True)
self.assertNotContains(response, "igorville.gov")
# clean up
application.delete()
def test_home_deletes_started_domain_application(self):
"""Tests if the user can delete a DomainApplication in the 'started' status"""
site = DraftDomain.objects.create(name="igorville.gov")
application = DomainApplication.objects.create(
creator=self.user, requested_domain=site, status=DomainApplication.ApplicationStatus.STARTED
)
# Ensure that igorville.gov exists on the page
home_page = self.client.get("/")
self.assertContains(home_page, "igorville.gov")
# Check if the delete button exists. We can do this by checking for its id and text content.
self.assertContains(home_page, "Delete")
self.assertContains(home_page, "button-toggle-delete-domain-alert-1")
# Trigger the delete logic
response = self.client.post(reverse("application-delete", kwargs={"pk": application.pk}), follow=True)
self.assertNotContains(response, "igorville.gov")
# clean up
application.delete()
def test_home_doesnt_delete_other_domain_applications(self):
"""Tests to ensure the user can't delete Applications not in the status of STARTED or WITHDRAWN"""
# Given that we are including a subset of items that can be deleted while excluding the rest,
# subTest is appropriate here as otherwise we would need many duplicate tests for the same reason.
draft_domain = DraftDomain.objects.create(name="igorville.gov")
for status in DomainApplication.ApplicationStatus:
if status not in [
DomainApplication.ApplicationStatus.STARTED,
DomainApplication.ApplicationStatus.WITHDRAWN,
]:
with self.subTest(status=status):
application = DomainApplication.objects.create(
creator=self.user, requested_domain=draft_domain, status=status
)
# Trigger the delete logic
response = self.client.post(
reverse("application-delete", kwargs={"pk": application.pk}), follow=True
)
# Check for a 403 error - the end user should not be allowed to do this
self.assertEqual(response.status_code, 403)
desired_application = DomainApplication.objects.filter(requested_domain=draft_domain)
# Make sure the DomainApplication wasn't deleted
self.assertEqual(desired_application.count(), 1)
# clean up
application.delete()
def test_home_lists_domains(self):
response = self.client.get("/")
domain, _ = Domain.objects.get_or_create(name="igorville.gov")

View file

@ -1,5 +1,4 @@
import logging
from django.forms import ValidationError
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect, render
@ -578,6 +577,7 @@ class ApplicationWithdrawn(DomainApplicationPermissionWithdrawView):
class DomainApplicationDeleteView(DomainApplicationPermissionDeleteView):
"""Delete view for home that allows the end user to delete DomainApplications"""
object: DomainApplication # workaround for type mismatch in DeleteView
def has_permission(self):
@ -593,4 +593,4 @@ class DomainApplicationDeleteView(DomainApplicationPermissionDeleteView):
return True
def get_success_url(self):
return reverse("home")
return reverse("home")

View file

@ -31,6 +31,5 @@ def index(request):
'name="delete-application">Yes, delete request</button>'
)
context["modal_button"] = modal_button
return render(request, "home.html", context)

View file

@ -129,4 +129,4 @@ class DomainApplicationPermissionDeleteView(DomainApplicationPermission, DeleteV
"""Abstract view for deleting a DomainApplication."""
model = DomainApplication
object: DomainApplication
object: DomainApplication