Clean up and add unit tests

This commit is contained in:
rachidatecs 2023-05-23 18:51:52 -04:00
parent 1876f08ca6
commit b3d4631d45
No known key found for this signature in database
GPG key ID: 3CEBBFA7325E5525
4 changed files with 73 additions and 76 deletions

View file

@ -1,2 +1,7 @@
from .application_wizard import * from .application_wizard import *
from .domain import DomainAddUserForm, NameserverFormset, DomainSecurityEmailForm, ContactForm from .domain import (
DomainAddUserForm,
NameserverFormset,
DomainSecurityEmailForm,
ContactForm,
)

View file

@ -33,8 +33,8 @@ class DomainSecurityEmailForm(forms.Form):
"""Form for adding or editing a security email to a domain.""" """Form for adding or editing a security email to a domain."""
security_email = forms.EmailField(label="Security email") security_email = forms.EmailField(label="Security email")
class ContactForm(forms.ModelForm): class ContactForm(forms.ModelForm):
"""Form for updating contacts.""" """Form for updating contacts."""
@ -54,21 +54,13 @@ class ContactForm(forms.ModelForm):
# the database fields have blank=True so ModelForm doesn't create # the database fields have blank=True so ModelForm doesn't create
# required fields by default. Use this list in __init__ to mark each # required fields by default. Use this list in __init__ to mark each
# of these fields as required # of these fields as required
required = [ required = ["first_name", "last_name", "title", "email", "phone"]
"first_name",
"last_name",
"title",
"email",
"phone"
]
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# take off maxlength attribute for the phone number field # take off maxlength attribute for the phone number field
# which interferes with out input_with_errors template tag # 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: for field_name in self.required:
self.fields[field_name].required = True self.fields[field_name].required = True

View file

@ -13,6 +13,7 @@ import boto3_mocking # type: ignore
from registrar.models import ( from registrar.models import (
DomainApplication, DomainApplication,
Domain, Domain,
DomainInformation,
DomainInvitation, DomainInvitation,
Contact, Contact,
Website, Website,
@ -1029,12 +1030,16 @@ class TestWithDomainPermissions(TestWithUser):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.domain, _ = Domain.objects.get_or_create(name="igorville.gov") 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( self.role, _ = UserDomainRole.objects.get_or_create(
user=self.user, domain=self.domain, role=UserDomainRole.Roles.ADMIN user=self.user, domain=self.domain, role=UserDomainRole.Roles.ADMIN
) )
def tearDown(self): def tearDown(self):
try: try:
self.domain_information.delete()
self.domain.delete() self.domain.delete()
self.role.delete() self.role.delete()
except ValueError: # pass if already deleted except ValueError: # pass if already deleted
@ -1045,55 +1050,37 @@ class TestWithDomainPermissions(TestWithUser):
class TestDomainPermissions(TestWithDomainPermissions): class TestDomainPermissions(TestWithDomainPermissions):
def test_not_logged_in(self): def test_not_logged_in(self):
"""Not logged in gets a redirect to Login.""" """Not logged in gets a redirect to Login."""
response = self.client.get(reverse("domain", kwargs={"pk": self.domain.id})) for view_name in [
self.assertEqual(response.status_code, 302) "domain",
"domain-users",
response = self.client.get( "domain-users-add",
reverse("domain-users", kwargs={"pk": self.domain.id}) "domain-nameservers",
) "domain-your-contact-information" "domain-security-email",
self.assertEqual(response.status_code, 302) ]:
with self.subTest(view_name=view_name):
response = self.client.get( response = self.client.get(
reverse("domain-users-add", kwargs={"pk": self.domain.id}) reverse(view_name, kwargs={"pk": self.domain.id})
) )
self.assertEqual(response.status_code, 302) 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)
def test_no_domain_role(self): def test_no_domain_role(self):
"""Logged in but no role gets 403 Forbidden.""" """Logged in but no role gets 403 Forbidden."""
self.client.force_login(self.user) self.client.force_login(self.user)
self.role.delete() # user no longer has a role on this domain self.role.delete() # user no longer has a role on this domain
with less_console_noise(): for view_name in [
response = self.client.get(reverse("domain", kwargs={"pk": self.domain.id})) "domain",
self.assertEqual(response.status_code, 403) "domain-users",
"domain-users-add",
with less_console_noise(): "domain-nameservers",
response = self.client.get( "domain-your-contact-information" "domain-security-email",
reverse("domain-users", kwargs={"pk": self.domain.id}) ]:
) with self.subTest(view_name=view_name):
self.assertEqual(response.status_code, 403) with less_console_noise():
response = self.client.get(
with less_console_noise(): reverse(view_name, kwargs={"pk": self.domain.id})
response = self.client.get( )
reverse("domain-users-add", kwargs={"pk": self.domain.id}) self.assertEqual(response.status_code, 403)
)
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)
class TestDomainDetail(TestWithDomainPermissions, WebTest): class TestDomainDetail(TestWithDomainPermissions, WebTest):
@ -1287,6 +1274,23 @@ class TestDomainDetail(TestWithDomainPermissions, WebTest):
# the field. # the field.
self.assertContains(result, "This field is required", count=2, status_code=200) 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): def test_domain_security_email(self):
"""Can load domain's security email page.""" """Can load domain's security email page."""
page = self.client.get( page = self.client.get(

View file

@ -10,9 +10,20 @@ from django.urls import reverse
from django.views.generic import DetailView from django.views.generic import DetailView
from django.views.generic.edit import DeleteView, FormMixin 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.email import send_templated_email, EmailSendingError
from .utility import DomainPermission from .utility import DomainPermission
@ -94,8 +105,8 @@ class DomainNameserversView(DomainPermission, FormMixin, DetailView):
) )
# superclass has the redirect # superclass has the redirect
return super().form_valid(formset) return super().form_valid(formset)
class DomainYourContactInformationView(DomainPermission, FormMixin, DetailView): class DomainYourContactInformationView(DomainPermission, FormMixin, DetailView):
"""Domain your contact information editing view.""" """Domain your contact information editing view."""
@ -104,19 +115,7 @@ class DomainYourContactInformationView(DomainPermission, FormMixin, DetailView):
template_name = "domain_your_contact_information.html" template_name = "domain_your_contact_information.html"
context_object_name = "domain" context_object_name = "domain"
form_class = ContactForm 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): def get_form_kwargs(self, *args, **kwargs):
"""Add domain_info.submitter instance to make a bound form.""" """Add domain_info.submitter instance to make a bound form."""
form_kwargs = super().get_form_kwargs(*args, **kwargs) 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.""" """The form is valid, call setter in model."""
# Post to DB using values from the form # 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() domain = self.get_object()
form.save() form.save()