Add test for all views, update fields for form_to_objects

This commit is contained in:
igorkorenfeld 2022-11-21 13:33:40 -08:00
commit 1211ba163d
No known key found for this signature in database
GPG key ID: 826947A4B867F659
3 changed files with 242 additions and 31 deletions

View file

@ -326,16 +326,22 @@ class ApplicationWizard(LoginRequiredMixin, NamedUrlSessionWizardView):
"""Unpack the form responses onto the model object properties.""" """Unpack the form responses onto the model object properties."""
application = DomainApplication.objects.create(creator=self.request.user) application = DomainApplication.objects.create(creator=self.request.user)
# organization information # organization type information
organization_data = form_dict["organization"].cleaned_data organization_type_data = form_dict["organization_type"].cleaned_data
application.organization_type = organization_data["organization_type"] application.organization_type = organization_type_data["organization_type"]
application.federal_branch = organization_data["federal_type"]
application.is_election_office = organization_data["is_election_board"] # federal branch information
federal_branch_data = form_dict["organization_federal"].cleaned_data
application.federal_type = federal_branch_data["federal_type"]
# election board information
election_board_data = form_dict["organization_election"].cleaned_data
application.is_election_office = election_board_data["is_election_board"]
# contact information # contact information
contact_data = form_dict["contact"].cleaned_data contact_data = form_dict["organization_contact"].cleaned_data
application.organization_name = contact_data["organization_name"] application.organization_name = contact_data["organization_name"]
application.street_address = contact_data["street_address"] application.street_address = contact_data["address_line1"]
# TODO: add the rest of these fields when they are created in the forms # TODO: add the rest of these fields when they are created in the forms
# This isn't really the requested_domain field # This isn't really the requested_domain field
@ -350,5 +356,5 @@ class ApplicationWizard(LoginRequiredMixin, NamedUrlSessionWizardView):
application = self.forms_to_object(form_dict) application = self.forms_to_object(form_dict)
application.submit() # change the status to submitted application.submit() # change the status to submitted
application.save() application.save()
logger.debug("Application object saved:", application.id) logger.debug("Application object saved: %s", application.id)
return redirect("home") return redirect("home")

View file

@ -5,6 +5,9 @@
{% block form_content %} {% block form_content %}
<form id="step__{{wizard.steps.current}}" class="usa-form usa-form--large" method="post"> <form id="step__{{wizard.steps.current}}" class="usa-form usa-form--large" method="post">
{{ wizard.management_form }}
{% csrf_token %}
{% for this_step in wizard.steps.all|slice:":-1" %} {% for this_step in wizard.steps.all|slice:":-1" %}
<div class="review__step margin-top-2"> <div class="review__step margin-top-2">
<div class="review__step__title display-flex flex-justify"> <div class="review__step__title display-flex flex-justify">
@ -16,7 +19,7 @@
</div> </div>
</div> </div>
{% endfor %} {% endfor %}
{{ block.super }} {{ block.super }}
</form> </form>

View file

@ -1,9 +1,12 @@
from django.conf import settings
from django.test import Client, TestCase from django.test import Client, TestCase
from django.urls import reverse from django.urls import reverse
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django_webtest import WebTest # type: ignore from django_webtest import WebTest # type: ignore
from registrar.models import DomainApplication
class TestViews(TestCase): class TestViews(TestCase):
def setUp(self): def setUp(self):
@ -81,6 +84,11 @@ class FormTests(TestWithUser, WebTest):
super().setUp() super().setUp()
self.app.set_user(self.user.username) self.app.set_user(self.user.username)
def tearDown(self):
# delete any applications we made so that users can be deleted\
DomainApplication.objects.all().delete()
super().tearDown()
def test_application_form_empty_submit(self): def test_application_form_empty_submit(self):
# 302 redirect to the first form # 302 redirect to the first form
page = self.app.get(reverse("application")).follow() page = self.app.get(reverse("application")).follow()
@ -95,27 +103,221 @@ class FormTests(TestWithUser, WebTest):
form["organization_type-organization_type"] = "Federal" form["organization_type-organization_type"] = "Federal"
result = page.form.submit().follow() result = page.form.submit().follow()
# Got the next form page # Got the next form page
self.assertIn("contact information", result) self.assertContains(result, "contact information")
# def test_application_form_submission(self): def test_application_form_submission(self):
# """Can fill out the entire form and submit. """Can fill out the entire form and submit.
As we add additional form pages, we need to include them here to make
this test work.
"""
type_page = self.app.get(reverse("application")).follow()
# 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]
# As we add additional form pages, we need to include them here to make # ---- TYPE PAGE ----
# this test work. type_form = type_page.form
# """ type_form["organization_type-organization_type"] = "Federal"
# page = self.app.get(reverse("application")).follow()
# form = page.form # set the session ID before .submit()
# form["organization-organization_type"] = "Federal" self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
# form["organization-federal_type"] = "Executive" type_result = type_page.form.submit()
# result = page.form.submit().follow()
# # Got the next form page # the post request should return a redirect to the next form in
# contact_form = result.form # the application
# contact_form["contact-organization_name"] = "test" self.assertEquals(type_result.status_code, 302)
# contact_form["contact-street_address"] = "100 Main Street" self.assertEquals(type_result["Location"], "/register/organization_federal/")
# result = page.form.submit()
# # final submission results in a redirect # TODO: In the future this should be conditionally dispalyed based on org type
# self.assertEquals(result.status_code, 302)
# page = result.follow() # ---- FEDERAL BRANCH PAGE ----
# self.assertContains(page, "registrar") # Follow the redirect to the next form page
# # TODO: when we have a page that lists applications, visit it and federal_page = type_result.follow()
# # make sure that the new one exists federal_form = federal_page.form
federal_form["organization_federal-federal_type"] = "Executive"
# set the session ID before .submit()
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
federal_result = federal_form.submit()
self.assertEquals(federal_result.status_code, 302)
self.assertEquals(federal_result["Location"], "/register/organization_election/")
# ---- ELECTION BOARD BRANCH PAGE ----
# Follow the redirect to the next form page
election_page = federal_result.follow()
election_form = election_page.form
election_form["organization_election-is_election_board"] = True
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
election_result = election_form.submit()
self.assertEquals(election_result.status_code, 302)
self.assertEquals(election_result["Location"], "/register/organization_contact/")
# ---- ORG CONTACT PAGE ----
# Follow the redirect to the next form page
org_contact_page = election_result.follow()
org_contact_form = org_contact_page.form
org_contact_form["organization_contact-organization_name"] = "Testorg"
org_contact_form["organization_contact-address_line1"] = "address 1"
org_contact_form["organization_contact-us_state"] = "NY"
org_contact_form["organization_contact-zipcode"] = "10002"
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
org_contact_result = org_contact_form.submit()
self.assertEquals(org_contact_result.status_code, 302)
self.assertEquals(org_contact_result["Location"], "/register/authorizing_official/")
# ---- AUTHORIZING OFFICIAL PAGE ----
# Follow the redirect to the next form page
ao_page = org_contact_result.follow()
ao_form = ao_page.form
ao_form["authorizing_official-first_name"] = "Testy"
ao_form["authorizing_official-last_name"] = "Tester"
ao_form["authorizing_official-title"] = "Chief Tester"
ao_form["authorizing_official-email"] = "testy@town.com"
ao_form["authorizing_official-phone"] = "(555) 555 5555"
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
ao_result = ao_form.submit()
self.assertEquals(ao_result.status_code, 302)
self.assertEquals(ao_result["Location"], "/register/current_sites/")
# ---- CURRENT SITES PAGE ----
# Follow the redirect to the next form page
current_sites_page = ao_result.follow()
current_sites_form = current_sites_page.form
current_sites_form["current_sites-current_site"] = "www.city.com"
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
current_sites_result = current_sites_form.submit()
self.assertEquals(current_sites_result.status_code, 302)
self.assertEquals(current_sites_result["Location"], "/register/dotgov_domain/")
# ---- DOTGOV DOMAIN PAGE ----
# Follow the redirect to the next form page
dotgov_page = current_sites_result.follow()
dotgov_form = dotgov_page.form
dotgov_form["dotgov_domain-dotgov_domain"] = "city"
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
dotgov_result = dotgov_form.submit()
self.assertEquals(dotgov_result.status_code, 302)
self.assertEquals(dotgov_result["Location"], "/register/purpose/")
# ---- PURPOSE DOMAIN PAGE ----
# Follow the redirect to the next form page
purpose_page = dotgov_result.follow()
purpose_form = purpose_page.form
purpose_form["purpose-purpose_field"] = "Purpose of the site"
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
purpose_result = purpose_form.submit()
self.assertEquals(purpose_result.status_code, 302)
self.assertEquals(purpose_result["Location"], "/register/your_contact/")
# ---- YOUR CONTACT INFO PAGE ----
# Follow the redirect to the next form page
your_contact_page = purpose_result.follow()
your_contact_form = your_contact_page.form
your_contact_form["your_contact-first_name"] = "Testy you"
your_contact_form["your_contact-last_name"] = "Tester you"
your_contact_form["your_contact-title"] = "Admin Tester"
your_contact_form["your_contact-email"] = "testy-admin@town.com"
your_contact_form["your_contact-phone"] = "(555) 555 5556"
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
your_contact_result = your_contact_form.submit()
self.assertEquals(your_contact_result.status_code, 302)
self.assertEquals(your_contact_result["Location"], "/register/other_contacts/")
# ---- OTHER CONTACTS PAGE ----
# Follow the redirect to the next form page
other_contacts_page = your_contact_result.follow()
other_contacts_form = other_contacts_page.form
other_contacts_form["other_contacts-first_name"] = "Testy2"
other_contacts_form["other_contacts-last_name"] = "Tester2"
other_contacts_form["other_contacts-title"] = "Another Tester"
other_contacts_form["other_contacts-email"] = "testy2@town.com"
other_contacts_form["other_contacts-phone"] = "(555) 555 5557"
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
other_contacts_result = other_contacts_form.submit()
self.assertEquals(other_contacts_result.status_code, 302)
self.assertEquals(other_contacts_result["Location"], "/register/security_email/")
# ---- SECURITY EMAIL PAGE ----
# Follow the redirect to the next form page
security_email_page = other_contacts_result.follow()
security_email_form = security_email_page.form
security_email_form["security_email-email"] = "security@city.com"
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
security_email_result = security_email_form.submit()
self.assertEquals(security_email_result.status_code, 302)
self.assertEquals(security_email_result["Location"], "/register/anything_else/")
# ---- ANYTHING ELSE PAGE ----
# Follow the redirect to the next form page
anything_else_page = security_email_result.follow()
anything_else_form = anything_else_page.form
anything_else_form["anything_else-anything_else"] = "No"
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
anything_else_result = anything_else_form.submit()
self.assertEquals(anything_else_result.status_code, 302)
self.assertEquals(anything_else_result["Location"], "/register/requirements/")
# ---- REQUIREMENTS PAGE ----
# Follow the redirect to the next form page
requirements_page = anything_else_result.follow()
requirements_form = requirements_page.form
requirements_form["requirements-agree_check"] = True
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
requirements_result = requirements_form.submit()
self.assertEquals(requirements_result.status_code, 302)
self.assertEquals(requirements_result["Location"], "/register/review/")
# ---- REVIEW AND FINSIHED PAGES ----
# Follow the redirect to the next form page
review_page = requirements_result.follow()
review_form = review_page.form
# final submission results in a redirect to the "finished" URL
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
review_result = review_form.submit()
self.assertEquals(review_result.status_code, 302)
self.assertEquals(review_result["Location"], "/register/finished/")
# the finished URL (for now) returns a redirect to /
# following this redirect is a GET request, so include the cookie
# here too.
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
root_page = review_result.follow()
self.assertEquals(root_page.status_code, 302)
self.assertEquals(root_page["Location"], "/")
self.assertContains(root_page.follow(), "Welcome to the .gov registrar")
# TODO: when we have a page that lists applications, visit it and
# make sure that the new one exists