From cdf46044d7b57d697ba1204109a9a03d2ac89ba1 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Wed, 10 Jan 2024 11:06:40 -0700 Subject: [PATCH] Unit tests --- src/registrar/tests/test_forms.py | 109 ++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/src/registrar/tests/test_forms.py b/src/registrar/tests/test_forms.py index e0afb2d71..0187a7636 100644 --- a/src/registrar/tests/test_forms.py +++ b/src/registrar/tests/test_forms.py @@ -1,8 +1,12 @@ """Test form validation requirements.""" +import json from django.test import TestCase, RequestFactory +from django.urls import reverse +from api.views import available from registrar.forms.application_wizard import ( + AlternativeDomainForm, CurrentSitesForm, DotGovDomainForm, AuthorizingOfficialForm, @@ -23,6 +27,7 @@ from django.contrib.auth import get_user_model class TestFormValidation(MockEppLib): def setUp(self): super().setUp() + self.API_BASE_PATH = "/api/v1/available/?domain=" self.user = get_user_model().objects.create(username="username") self.factory = RequestFactory() @@ -74,6 +79,110 @@ class TestFormValidation(MockEppLib): ["Enter the .gov domain you want without any periods."], ) + def test_requested_domain_errors_consistent(self): + """Tests if the errors on submit and with the check availability buttons are consistent + for requested_domains + """ + test_cases = [ + # extra_dots + ("top-level-agency.com", "Enter the .gov domain you want without any periods."), + # invalid + ( + "underscores_forever", + "Enter a domain using only letters, numbers, " "or hyphens (though we don't recommend using hyphens).", + ), + # required + ("", "Enter the .gov domain you want. Don’t include “www” or “.gov.”"), + # unavailable + ( + "whitehouse.gov", + "That domain isn’t available. Read more about " + "choosing your .gov domain.", + ), + ] + + for domain, expected_error in test_cases: + with self.subTest(domain=domain, error=expected_error): + form = DotGovDomainForm(data={"requested_domain": domain}) + + form_error = list(form.errors["requested_domain"]) + + # Ensure the form returns what we expect + self.assertEqual( + form_error, + [expected_error], + ) + + request = self.factory.get(self.API_BASE_PATH + domain) + request.user = self.user + response = available(request, domain=domain) + + # Ensure that we're getting the right kind of response + self.assertContains(response, "available") + + response_object = json.loads(response.content) + + json_error = response_object["message"] + # Test if the message is what we expect + self.assertEqual(json_error, expected_error) + + # While its implied, + # for good measure, test if the two objects are equal anyway + self.assertEqual([json_error], form_error) + + def test_alternate_domain_errors_consistent(self): + """Tests if the errors on submit and with the check availability buttons are consistent + for alternative_domains + """ + test_cases = [ + # extra_dots + ("top-level-agency.com", "Enter the .gov domain you want without any periods."), + # invalid + ( + "underscores_forever", + "Enter a domain using only letters, numbers, " "or hyphens (though we don't recommend using hyphens).", + ), + # required + ("", "Enter the .gov domain you want. Don’t include “www” or “.gov.”"), + # unavailable + ( + "whitehouse.gov", + "That domain isn’t available. Read more about " + "choosing your .gov domain.", + ), + ] + + for domain, expected_error in test_cases: + with self.subTest(domain=domain, error=expected_error): + form = AlternativeDomainForm(data={"alternative_domain": domain}) + + form_error = list(form.errors["alternative_domain"]) + + # Ensure the form returns what we expect + self.assertEqual( + form_error, + [expected_error], + ) + + request = self.factory.get(self.API_BASE_PATH + domain) + request.user = self.user + response = available(request, domain=domain) + + # Ensure that we're getting the right kind of response + self.assertContains(response, "available") + + response_object = json.loads(response.content) + + json_error = response_object["message"] + # Test if the message is what we expect + self.assertEqual(json_error, expected_error) + + # While its implied, + # for good measure, test if the two objects are equal anyway + self.assertEqual([json_error], form_error) + def test_requested_domain_two_dots_invalid(self): """don't accept domains that are subdomains""" form = DotGovDomainForm(data={"requested_domain": "sub.top-level-agency.gov"})