mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-22 02:36:02 +02:00
Clean up and add unit tests
This commit is contained in:
parent
1876f08ca6
commit
b3d4631d45
4 changed files with 73 additions and 76 deletions
|
@ -1,2 +1,7 @@
|
|||
from .application_wizard import *
|
||||
from .domain import DomainAddUserForm, NameserverFormset, DomainSecurityEmailForm, ContactForm
|
||||
from .domain import (
|
||||
DomainAddUserForm,
|
||||
NameserverFormset,
|
||||
DomainSecurityEmailForm,
|
||||
ContactForm,
|
||||
)
|
||||
|
|
|
@ -33,8 +33,8 @@ class DomainSecurityEmailForm(forms.Form):
|
|||
"""Form for adding or editing a security email to a domain."""
|
||||
|
||||
security_email = forms.EmailField(label="Security email")
|
||||
|
||||
|
||||
|
||||
|
||||
class ContactForm(forms.ModelForm):
|
||||
|
||||
"""Form for updating contacts."""
|
||||
|
@ -54,21 +54,13 @@ class ContactForm(forms.ModelForm):
|
|||
# the database fields have blank=True so ModelForm doesn't create
|
||||
# required fields by default. Use this list in __init__ to mark each
|
||||
# of these fields as required
|
||||
required = [
|
||||
"first_name",
|
||||
"last_name",
|
||||
"title",
|
||||
"email",
|
||||
"phone"
|
||||
]
|
||||
required = ["first_name", "last_name", "title", "email", "phone"]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
# take off maxlength attribute for the phone number field
|
||||
# which interferes with out input_with_errors template tag
|
||||
self.fields['phone'].widget.attrs.pop('maxlength', None)
|
||||
self.fields["phone"].widget.attrs.pop("maxlength", None)
|
||||
|
||||
for field_name in self.required:
|
||||
self.fields[field_name].required = True
|
||||
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ import boto3_mocking # type: ignore
|
|||
from registrar.models import (
|
||||
DomainApplication,
|
||||
Domain,
|
||||
DomainInformation,
|
||||
DomainInvitation,
|
||||
Contact,
|
||||
Website,
|
||||
|
@ -1029,12 +1030,16 @@ class TestWithDomainPermissions(TestWithUser):
|
|||
def setUp(self):
|
||||
super().setUp()
|
||||
self.domain, _ = Domain.objects.get_or_create(name="igorville.gov")
|
||||
self.domain_information, _ = DomainInformation.objects.get_or_create(
|
||||
creator=self.user, domain=self.domain
|
||||
)
|
||||
self.role, _ = UserDomainRole.objects.get_or_create(
|
||||
user=self.user, domain=self.domain, role=UserDomainRole.Roles.ADMIN
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
try:
|
||||
self.domain_information.delete()
|
||||
self.domain.delete()
|
||||
self.role.delete()
|
||||
except ValueError: # pass if already deleted
|
||||
|
@ -1045,55 +1050,37 @@ class TestWithDomainPermissions(TestWithUser):
|
|||
class TestDomainPermissions(TestWithDomainPermissions):
|
||||
def test_not_logged_in(self):
|
||||
"""Not logged in gets a redirect to Login."""
|
||||
response = self.client.get(reverse("domain", kwargs={"pk": self.domain.id}))
|
||||
self.assertEqual(response.status_code, 302)
|
||||
|
||||
response = self.client.get(
|
||||
reverse("domain-users", kwargs={"pk": self.domain.id})
|
||||
)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
|
||||
response = self.client.get(
|
||||
reverse("domain-users-add", kwargs={"pk": self.domain.id})
|
||||
)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
|
||||
response = self.client.get(
|
||||
reverse("domain-nameservers", kwargs={"pk": self.domain.id})
|
||||
)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
|
||||
response = self.client.get(
|
||||
reverse("domain-security-email", kwargs={"pk": self.domain.id})
|
||||
)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
for view_name in [
|
||||
"domain",
|
||||
"domain-users",
|
||||
"domain-users-add",
|
||||
"domain-nameservers",
|
||||
"domain-your-contact-information" "domain-security-email",
|
||||
]:
|
||||
with self.subTest(view_name=view_name):
|
||||
response = self.client.get(
|
||||
reverse(view_name, kwargs={"pk": self.domain.id})
|
||||
)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
|
||||
def test_no_domain_role(self):
|
||||
"""Logged in but no role gets 403 Forbidden."""
|
||||
self.client.force_login(self.user)
|
||||
self.role.delete() # user no longer has a role on this domain
|
||||
|
||||
with less_console_noise():
|
||||
response = self.client.get(reverse("domain", kwargs={"pk": self.domain.id}))
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
with less_console_noise():
|
||||
response = self.client.get(
|
||||
reverse("domain-users", kwargs={"pk": self.domain.id})
|
||||
)
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
with less_console_noise():
|
||||
response = self.client.get(
|
||||
reverse("domain-users-add", kwargs={"pk": self.domain.id})
|
||||
)
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
with less_console_noise():
|
||||
response = self.client.get(
|
||||
reverse("domain-nameservers", kwargs={"pk": self.domain.id})
|
||||
)
|
||||
self.assertEqual(response.status_code, 403)
|
||||
for view_name in [
|
||||
"domain",
|
||||
"domain-users",
|
||||
"domain-users-add",
|
||||
"domain-nameservers",
|
||||
"domain-your-contact-information" "domain-security-email",
|
||||
]:
|
||||
with self.subTest(view_name=view_name):
|
||||
with less_console_noise():
|
||||
response = self.client.get(
|
||||
reverse(view_name, kwargs={"pk": self.domain.id})
|
||||
)
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
|
||||
class TestDomainDetail(TestWithDomainPermissions, WebTest):
|
||||
|
@ -1287,6 +1274,23 @@ class TestDomainDetail(TestWithDomainPermissions, WebTest):
|
|||
# the field.
|
||||
self.assertContains(result, "This field is required", count=2, status_code=200)
|
||||
|
||||
def test_domain_your_contact_information(self):
|
||||
"""Can load domain's your contact information page."""
|
||||
page = self.client.get(
|
||||
reverse("domain-your-contact-information", kwargs={"pk": self.domain.id})
|
||||
)
|
||||
self.assertContains(page, "Domain contact information")
|
||||
|
||||
def test_domain_your_contact_information(self):
|
||||
"""Your contact information appears on the page."""
|
||||
self.domain_information.submitter = Contact(first_name="Testy")
|
||||
self.domain_information.submitter.save()
|
||||
self.domain_information.save()
|
||||
page = self.app.get(
|
||||
reverse("domain-your-contact-information", kwargs={"pk": self.domain.id})
|
||||
)
|
||||
self.assertContains(page, "Testy")
|
||||
|
||||
def test_domain_security_email(self):
|
||||
"""Can load domain's security email page."""
|
||||
page = self.client.get(
|
||||
|
|
|
@ -10,9 +10,20 @@ from django.urls import reverse
|
|||
from django.views.generic import DetailView
|
||||
from django.views.generic.edit import DeleteView, FormMixin
|
||||
|
||||
from registrar.models import Domain, DomainInvitation, User, UserDomainRole, DomainInformation
|
||||
from registrar.models import (
|
||||
Domain,
|
||||
DomainInvitation,
|
||||
User,
|
||||
UserDomainRole,
|
||||
DomainInformation,
|
||||
)
|
||||
|
||||
from ..forms import DomainAddUserForm, NameserverFormset, DomainSecurityEmailForm, ContactForm
|
||||
from ..forms import (
|
||||
DomainAddUserForm,
|
||||
NameserverFormset,
|
||||
DomainSecurityEmailForm,
|
||||
ContactForm,
|
||||
)
|
||||
from ..utility.email import send_templated_email, EmailSendingError
|
||||
from .utility import DomainPermission
|
||||
|
||||
|
@ -94,8 +105,8 @@ class DomainNameserversView(DomainPermission, FormMixin, DetailView):
|
|||
)
|
||||
# superclass has the redirect
|
||||
return super().form_valid(formset)
|
||||
|
||||
|
||||
|
||||
|
||||
class DomainYourContactInformationView(DomainPermission, FormMixin, DetailView):
|
||||
|
||||
"""Domain your contact information editing view."""
|
||||
|
@ -104,19 +115,7 @@ class DomainYourContactInformationView(DomainPermission, FormMixin, DetailView):
|
|||
template_name = "domain_your_contact_information.html"
|
||||
context_object_name = "domain"
|
||||
form_class = ContactForm
|
||||
|
||||
# def get_initial(self):
|
||||
# """The initial value for the form."""
|
||||
# domainInformation = self.get_object()
|
||||
# initial = super().get_initial()
|
||||
# initial["first_name"] = domainInformation.submitter.first_name
|
||||
# initial["middle_name"] = domainInformation.submitter.middle_name
|
||||
# initial["last_name"] = domainInformation.submitter.last_name
|
||||
# initial["title"] = domainInformation.submitter.title
|
||||
# initial["email"] = domainInformation.submitter.email
|
||||
# initial["phone"] = domainInformation.submitter.phone
|
||||
# return initial
|
||||
|
||||
|
||||
def get_form_kwargs(self, *args, **kwargs):
|
||||
"""Add domain_info.submitter instance to make a bound form."""
|
||||
form_kwargs = super().get_form_kwargs(*args, **kwargs)
|
||||
|
@ -141,9 +140,6 @@ class DomainYourContactInformationView(DomainPermission, FormMixin, DetailView):
|
|||
"""The form is valid, call setter in model."""
|
||||
|
||||
# Post to DB using values from the form
|
||||
# new_email = form.cleaned_data["security_email"]
|
||||
# domain = self.get_object()
|
||||
# domain.set_security_email(new_email)
|
||||
domain = self.get_object()
|
||||
form.save()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue