Testing views

This commit is contained in:
Rachid Mrad 2024-01-04 16:56:31 -05:00
parent d854f8327f
commit a76d4c9da1
No known key found for this signature in database
GPG key ID: EF38E4CEC4A8F3CF
3 changed files with 175 additions and 22 deletions

View file

@ -757,6 +757,11 @@ class NoOtherContactsForm(RegistrarForm):
message="Response must be less than 1000 characters.",
)
],
error_messages={
"required": (
"Rationale for no other employees is required."
)
},
)
def __init__(self, *args, **kwargs):

View file

@ -40,10 +40,6 @@
<h2>Organization contact {{ forloop.counter }} (optional)</h2>
</legend>
{% if forms.1.can_delete %}
{{ form.DELETE }}
{% endif %}
{% input_with_errors form.first_name %}
{% input_with_errors form.middle_name %}

View file

@ -1,4 +1,5 @@
from unittest import skip
import unittest
from unittest.mock import MagicMock, ANY, patch
from django.conf import settings
@ -170,8 +171,8 @@ class DomainApplicationTests(TestWithUser, WebTest):
in the modal header on the submit page.
"""
num_pages_tested = 0
# elections, type_of_work, tribal_government, no_other_contacts
SKIPPED_PAGES = 4
# elections, type_of_work, tribal_government
SKIPPED_PAGES = 3
num_pages = len(self.TITLES) - SKIPPED_PAGES
type_page = self.app.get(reverse("application:")).follow()
@ -367,14 +368,19 @@ class DomainApplicationTests(TestWithUser, WebTest):
# Follow the redirect to the next form page
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
other_contacts_page = your_contact_result.follow()
other_contacts_form = other_contacts_page.forms[0]
# This page has 3 forms in 1.
# Let's set the yes/no radios to enable the other contacts fieldsets
other_contacts_form = other_contacts_page.forms[0]
other_contacts_form["other_contacts-has_other_contacts"] = "True"
other_contacts_form["other_contacts-0-first_name"] = "Testy2"
other_contacts_form["other_contacts-0-last_name"] = "Tester2"
other_contacts_form["other_contacts-0-title"] = "Another Tester"
other_contacts_form["other_contacts-0-email"] = "testy2@town.com"
other_contacts_form["other_contacts-0-phone"] = "(201) 555 5557"
# test next button
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
other_contacts_result = other_contacts_form.submit()
@ -506,8 +512,8 @@ class DomainApplicationTests(TestWithUser, WebTest):
@skip("WIP")
def test_application_form_started_allsteps(self):
num_pages_tested = 0
# elections, type_of_work, tribal_government, no_other_contacts
SKIPPED_PAGES = 4
# elections, type_of_work, tribal_government
SKIPPED_PAGES = 3
DASHBOARD_PAGE = 1
num_pages = len(self.TITLES) - SKIPPED_PAGES + DASHBOARD_PAGE
@ -708,25 +714,171 @@ class DomainApplicationTests(TestWithUser, WebTest):
contact_page = type_result.follow()
self.assertContains(contact_page, self.TITLES[Step.ABOUT_YOUR_ORGANIZATION])
def test_application_no_other_contacts(self):
"""Applicants with no other contacts have to give a reason."""
contacts_page = self.app.get(reverse("application:other_contacts"))
def test_yes_no_form_inits_blank_for_new_application(self):
""""""
other_contacts_page = self.app.get(reverse("application:other_contacts"))
other_contacts_form = other_contacts_page.forms[0]
self.assertEquals(other_contacts_form["other_contacts-has_other_contacts"].value, None)
def test_yes_no_form_inits_yes_for_application_with_other_contacts(self):
""""""
# Application has other contacts by default
application = completed_application(user=self.user)
# prime the form by visiting /edit
self.app.get(reverse("edit-application", kwargs={"id": application.pk}))
# django-webtest does not handle cookie-based sessions well because it keeps
# resetting the session key on each new request, thus destroying the concept
# of a "session". We are going to do it manually, saving the session ID here
# and then setting the cookie on each request.
session_id = self.app.cookies[settings.SESSION_COOKIE_NAME]
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
result = contacts_page.forms[0].submit()
# follow first redirect
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
no_contacts_page = result.follow()
expected_url_slug = str(Step.NO_OTHER_CONTACTS)
actual_url_slug = no_contacts_page.request.path.split("/")[-2]
self.assertEqual(expected_url_slug, actual_url_slug)
other_contacts_page = self.app.get(reverse("application:other_contacts"))
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
other_contacts_form = other_contacts_page.forms[0]
self.assertEquals(other_contacts_form["other_contacts-has_other_contacts"].value, "True")
def test_yes_no_form_inits_no_for_application_with_no_other_contacts_rationale(self):
""""""
# Application has other contacts by default
application = completed_application(user=self.user, has_other_contacts=False)
application.no_other_contacts_rationale = "Hello!"
application.save()
# prime the form by visiting /edit
self.app.get(reverse("edit-application", kwargs={"id": application.pk}))
# django-webtest does not handle cookie-based sessions well because it keeps
# resetting the session key on each new request, thus destroying the concept
# of a "session". We are going to do it manually, saving the session ID here
# and then setting the cookie on each request.
session_id = self.app.cookies[settings.SESSION_COOKIE_NAME]
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
other_contacts_page = self.app.get(reverse("application:other_contacts"))
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
other_contacts_form = other_contacts_page.forms[0]
self.assertEquals(other_contacts_form["other_contacts-has_other_contacts"].value, "False")
def test_submitting_other_contacts_deletes_no_other_contacts_rationale(self):
""""""
# Application has other contacts by default
application = completed_application(user=self.user, has_other_contacts=False)
application.no_other_contacts_rationale = "Hello!"
application.save()
# prime the form by visiting /edit
self.app.get(reverse("edit-application", kwargs={"id": application.pk}))
# django-webtest does not handle cookie-based sessions well because it keeps
# resetting the session key on each new request, thus destroying the concept
# of a "session". We are going to do it manually, saving the session ID here
# and then setting the cookie on each request.
session_id = self.app.cookies[settings.SESSION_COOKIE_NAME]
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
other_contacts_page = self.app.get(reverse("application:other_contacts"))
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
other_contacts_form = other_contacts_page.forms[0]
self.assertEquals(other_contacts_form["other_contacts-has_other_contacts"].value, "False")
other_contacts_form["other_contacts-has_other_contacts"] = "True"
other_contacts_form["other_contacts-0-first_name"] = "Testy"
other_contacts_form["other_contacts-0-middle_name"] = ""
other_contacts_form["other_contacts-0-last_name"] = "McTesterson"
other_contacts_form["other_contacts-0-title"] = "Lord"
other_contacts_form["other_contacts-0-email"] = "testy@abc.org"
other_contacts_form["other_contacts-0-phone"] = "(201) 555-0123"
# Submit the now empty form
other_contacts_form.submit()
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
# Verify that the no_other_contacts_rationale we saved earlier has been removed from the database
application = DomainApplication.objects.get()
self.assertEqual(
application.other_contacts.count(),
1,
)
self.assertEquals(
application.no_other_contacts_rationale,
None,
)
def test_submitting_no_other_contacts_rationale_deletes_other_contacts(self):
""""""
# Application has other contacts by default
application = completed_application(user=self.user)
# prime the form by visiting /edit
self.app.get(reverse("edit-application", kwargs={"id": application.pk}))
# django-webtest does not handle cookie-based sessions well because it keeps
# resetting the session key on each new request, thus destroying the concept
# of a "session". We are going to do it manually, saving the session ID here
# and then setting the cookie on each request.
session_id = self.app.cookies[settings.SESSION_COOKIE_NAME]
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
other_contacts_page = self.app.get(reverse("application:other_contacts"))
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
other_contacts_form = other_contacts_page.forms[0]
self.assertEquals(other_contacts_form["other_contacts-has_other_contacts"].value, "True")
other_contacts_form["other_contacts-has_other_contacts"] = "False"
other_contacts_form["other_contacts-no_other_contacts_rationale"] = "Hello again!"
# Submit the now empty form
other_contacts_form.submit()
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
# Verify that the no_other_contacts_rationale we saved earlier has been removed from the database
application = DomainApplication.objects.get()
self.assertEqual(
application.other_contacts.count(),
0,
)
self.assertEquals(
application.no_other_contacts_rationale,
"Hello again!",
)
def test_if_yes_no_form_is_no_then_no_other_contacts_required(self):
"""Applicants with no other contacts have to give a reason."""
other_contacts_page = self.app.get(reverse("application:other_contacts"))
other_contacts_form = other_contacts_page.forms[0]
other_contacts_form["other_contacts-has_other_contacts"] = "False"
response = other_contacts_page.forms[0].submit()
# The textarea for no other contacts returns this error message
# Assert that it is returned, ie the no other contacts form is required
self.assertContains(response, "Rationale for no other employees is required.")
# The first name field for other contacts returns this error message
# Assert that it is not returned, ie the contacts form is not required
self.assertNotContains(response, "Enter the first name / given name of this contact.")
def test_if_yes_no_form_is_yes_then_other_contacts_required(self):
"""Applicants with other contacts do not have to give a reason."""
other_contacts_page = self.app.get(reverse("application:other_contacts"))
other_contacts_form = other_contacts_page.forms[0]
other_contacts_form["other_contacts-has_other_contacts"] = "True"
response = other_contacts_page.forms[0].submit()
# The textarea for no other contacts returns this error message
# Assert that it is not returned, ie the no other contacts form is not required
self.assertNotContains(response, "Rationale for no other employees is required.")
# The first name field for other contacts returns this error message
# Assert that it is returned, ie the contacts form is required
self.assertContains(response, "Enter the first name / given name of this contact.")
@skip("Repurpose when working on ticket 903")
def test_application_delete_other_contact(self):
"""Other contacts can be deleted after being saved to database."""
# Populate the databse with a domain application that