mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-30 22:46:30 +02:00
Unit tests
This commit is contained in:
parent
1b292ce640
commit
cdf46044d7
1 changed files with 109 additions and 0 deletions
|
@ -1,8 +1,12 @@
|
||||||
"""Test form validation requirements."""
|
"""Test form validation requirements."""
|
||||||
|
|
||||||
|
import json
|
||||||
from django.test import TestCase, RequestFactory
|
from django.test import TestCase, RequestFactory
|
||||||
|
from django.urls import reverse
|
||||||
|
from api.views import available
|
||||||
|
|
||||||
from registrar.forms.application_wizard import (
|
from registrar.forms.application_wizard import (
|
||||||
|
AlternativeDomainForm,
|
||||||
CurrentSitesForm,
|
CurrentSitesForm,
|
||||||
DotGovDomainForm,
|
DotGovDomainForm,
|
||||||
AuthorizingOfficialForm,
|
AuthorizingOfficialForm,
|
||||||
|
@ -23,6 +27,7 @@ from django.contrib.auth import get_user_model
|
||||||
class TestFormValidation(MockEppLib):
|
class TestFormValidation(MockEppLib):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
|
self.API_BASE_PATH = "/api/v1/available/?domain="
|
||||||
self.user = get_user_model().objects.create(username="username")
|
self.user = get_user_model().objects.create(username="username")
|
||||||
self.factory = RequestFactory()
|
self.factory = RequestFactory()
|
||||||
|
|
||||||
|
@ -74,6 +79,110 @@ class TestFormValidation(MockEppLib):
|
||||||
["Enter the .gov domain you want without any periods."],
|
["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. <a class='usa-link' "
|
||||||
|
"href='https://beta.get.gov/domains/choosing' target='_blank'>Read more about "
|
||||||
|
"choosing your .gov domain.</a>",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
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. <a class='usa-link' "
|
||||||
|
"href='https://beta.get.gov/domains/choosing' target='_blank'>Read more about "
|
||||||
|
"choosing your .gov domain.</a>",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
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):
|
def test_requested_domain_two_dots_invalid(self):
|
||||||
"""don't accept domains that are subdomains"""
|
"""don't accept domains that are subdomains"""
|
||||||
form = DotGovDomainForm(data={"requested_domain": "sub.top-level-agency.gov"})
|
form = DotGovDomainForm(data={"requested_domain": "sub.top-level-agency.gov"})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue