mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-06-29 07:43:32 +02:00
cleaned up test cases and formatting
This commit is contained in:
parent
a7ba33b523
commit
d532449b3d
4 changed files with 55 additions and 61 deletions
|
@ -7,7 +7,7 @@ from django.forms import formset_factory
|
|||
from phonenumber_field.widgets import RegionalPhoneNumberWidget
|
||||
from registrar.utility.errors import (
|
||||
NameserverError,
|
||||
NameserverErrorCodes as nsErrorCodes
|
||||
NameserverErrorCodes as nsErrorCodes,
|
||||
)
|
||||
|
||||
from ..models import Contact, DomainInformation, Domain
|
||||
|
@ -26,26 +26,9 @@ class DomainAddUserForm(forms.Form):
|
|||
|
||||
|
||||
class IPAddressField(forms.CharField):
|
||||
|
||||
|
||||
|
||||
def validate(self, value):
|
||||
super().validate(value) # Run the default CharField validation
|
||||
|
||||
# ip_list = [ip.strip() for ip in value.split(",")] # Split IPs and remove whitespace
|
||||
|
||||
# # TODO: pass hostname from view?
|
||||
|
||||
# hostname = self.form.cleaned_data.get("server", "")
|
||||
|
||||
# print(f"hostname {hostname}")
|
||||
|
||||
# # Call the IP validation method from Domain
|
||||
# try:
|
||||
# Domain.checkHostIPCombo(hostname, ip_list)
|
||||
# except NameserverError as e:
|
||||
# raise forms.ValidationError(str(e))
|
||||
|
||||
|
||||
class DomainNameserverForm(forms.Form):
|
||||
"""Form for changing nameservers."""
|
||||
|
@ -65,9 +48,9 @@ class DomainNameserverForm(forms.Form):
|
|||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
server = cleaned_data.get('server', '')
|
||||
ip = cleaned_data.get('ip', '')
|
||||
domain = cleaned_data.get('domain', '')
|
||||
server = cleaned_data.get("server", "")
|
||||
ip = cleaned_data.get("ip", "")
|
||||
domain = cleaned_data.get("domain", "")
|
||||
print(f"clean is called on {domain} {server}")
|
||||
|
||||
# make sure there's a nameserver if an ip is passed
|
||||
|
@ -75,7 +58,9 @@ class DomainNameserverForm(forms.Form):
|
|||
ip_list = [ip.strip() for ip in ip.split(",")]
|
||||
if not server and len(ip_list) > 0:
|
||||
# If 'server' is empty, disallow 'ip' input
|
||||
self.add_error('server', NameserverError(code=nsErrorCodes.MISSING_HOST))
|
||||
self.add_error(
|
||||
"server", NameserverError(code=nsErrorCodes.MISSING_HOST)
|
||||
)
|
||||
|
||||
# if there's a nameserver and an ip, validate nameserver/ip combo
|
||||
|
||||
|
@ -83,22 +68,19 @@ class DomainNameserverForm(forms.Form):
|
|||
if ip:
|
||||
ip_list = [ip.strip() for ip in ip.split(",")]
|
||||
else:
|
||||
ip_list = ['']
|
||||
ip_list = [""]
|
||||
try:
|
||||
Domain.checkHostIPCombo(domain, server, ip_list)
|
||||
except NameserverError as e:
|
||||
if e.code == nsErrorCodes.GLUE_RECORD_NOT_ALLOWED:
|
||||
self.add_error(
|
||||
'server',
|
||||
NameserverError(code=nsErrorCodes.GLUE_RECORD_NOT_ALLOWED)
|
||||
"server",
|
||||
NameserverError(code=nsErrorCodes.GLUE_RECORD_NOT_ALLOWED),
|
||||
)
|
||||
elif e.code == nsErrorCodes.MISSING_IP:
|
||||
self.add_error(
|
||||
'ip',
|
||||
NameserverError(code=nsErrorCodes.MISSING_IP)
|
||||
)
|
||||
self.add_error("ip", NameserverError(code=nsErrorCodes.MISSING_IP))
|
||||
else:
|
||||
self.add_error('ip', str(e))
|
||||
self.add_error("ip", str(e))
|
||||
|
||||
return cleaned_data
|
||||
|
||||
|
|
|
@ -314,15 +314,16 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
NameserverError (if exception hit)
|
||||
Returns:
|
||||
None"""
|
||||
if cls.isSubdomain(name, nameserver) and (ip is None or ip == [] or ip == ['']):
|
||||
|
||||
if cls.isSubdomain(name, nameserver) and (ip is None or ip == [] or ip == [""]):
|
||||
raise NameserverError(code=nsErrorCodes.MISSING_IP, nameserver=nameserver)
|
||||
|
||||
elif not cls.isSubdomain(name, nameserver) and (ip is not None and ip != [] and ip != ['']):
|
||||
elif not cls.isSubdomain(name, nameserver) and (
|
||||
ip is not None and ip != [] and ip != [""]
|
||||
):
|
||||
raise NameserverError(
|
||||
code=nsErrorCodes.GLUE_RECORD_NOT_ALLOWED, nameserver=nameserver, ip=ip
|
||||
)
|
||||
elif ip is not None and ip != [] and ip != ['']:
|
||||
elif ip is not None and ip != [] and ip != [""]:
|
||||
for addr in ip:
|
||||
if not cls._valid_ip_addr(addr):
|
||||
raise NameserverError(
|
||||
|
@ -384,7 +385,9 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
if newHostDict[prevHost] is not None and set(
|
||||
newHostDict[prevHost]
|
||||
) != set(addrs):
|
||||
self.__class__.checkHostIPCombo(name=self.name, nameserver=prevHost, ip=newHostDict[prevHost])
|
||||
self.__class__.checkHostIPCombo(
|
||||
name=self.name, nameserver=prevHost, ip=newHostDict[prevHost]
|
||||
)
|
||||
updated_values.append((prevHost, newHostDict[prevHost]))
|
||||
|
||||
new_values = {
|
||||
|
@ -394,7 +397,9 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
}
|
||||
|
||||
for nameserver, ip in new_values.items():
|
||||
self.__class__.checkHostIPCombo(name=self.name, nameserver=nameserver, ip=ip)
|
||||
self.__class__.checkHostIPCombo(
|
||||
name=self.name, nameserver=nameserver, ip=ip
|
||||
)
|
||||
|
||||
return (deleted_values, updated_values, new_values, previousHostDict)
|
||||
|
||||
|
@ -605,7 +610,11 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
if len(hosts) > 13:
|
||||
raise NameserverError(code=nsErrorCodes.TOO_MANY_HOSTS)
|
||||
|
||||
if self.state not in [self.State.DNS_NEEDED, self.State.READY, self.State.UNKNOWN]:
|
||||
if self.state not in [
|
||||
self.State.DNS_NEEDED,
|
||||
self.State.READY,
|
||||
self.State.UNKNOWN,
|
||||
]:
|
||||
raise ActionNotAllowed("Nameservers can not be " "set in the current state")
|
||||
|
||||
logger.info("Setting nameservers")
|
||||
|
|
|
@ -10,9 +10,7 @@ class TestNameserverError(TestCase):
|
|||
def test_with_no_ip(self):
|
||||
"""Test NameserverError when no ip address is passed"""
|
||||
nameserver = "nameserver val"
|
||||
expected = (
|
||||
"Subdomains require an IP address"
|
||||
)
|
||||
expected = "Subdomains require an IP address"
|
||||
|
||||
nsException = NameserverError(
|
||||
code=nsErrorCodes.MISSING_IP, nameserver=nameserver
|
||||
|
|
|
@ -15,7 +15,6 @@ from django.shortcuts import redirect
|
|||
from django.template import RequestContext
|
||||
from django.urls import reverse
|
||||
from django.views.generic.edit import FormMixin
|
||||
from django.forms import BaseFormSet
|
||||
|
||||
from registrar.models import (
|
||||
Domain,
|
||||
|
@ -215,7 +214,7 @@ class DomainDNSView(DomainBaseView):
|
|||
template_name = "domain_dns.html"
|
||||
|
||||
|
||||
class DomainNameserversView(DomainFormBaseView, BaseFormSet):
|
||||
class DomainNameserversView(DomainFormBaseView):
|
||||
"""Domain nameserver editing view."""
|
||||
|
||||
template_name = "domain_nameservers.html"
|
||||
|
@ -229,7 +228,9 @@ class DomainNameserversView(DomainFormBaseView, BaseFormSet):
|
|||
|
||||
if nameservers is not None:
|
||||
# Add existing nameservers as initial data
|
||||
initial_data.extend({"server": name, "ip": ','.join(ip)} for name, ip in nameservers)
|
||||
initial_data.extend(
|
||||
{"server": name, "ip": ",".join(ip)} for name, ip in nameservers
|
||||
)
|
||||
|
||||
# Ensure at least 3 fields, filled or empty
|
||||
while len(initial_data) < 2:
|
||||
|
@ -284,7 +285,7 @@ class DomainNameserversView(DomainFormBaseView, BaseFormSet):
|
|||
def form_valid(self, formset):
|
||||
"""The formset is valid, perform something with it."""
|
||||
|
||||
self.request.session['nameservers_form_domain'] = self.object
|
||||
self.request.session["nameservers_form_domain"] = self.object
|
||||
|
||||
# Set the nameservers from the formset
|
||||
nameservers = []
|
||||
|
@ -292,7 +293,7 @@ class DomainNameserversView(DomainFormBaseView, BaseFormSet):
|
|||
try:
|
||||
ip_string = form.cleaned_data["ip"]
|
||||
# Split the string into a list using a comma as the delimiter
|
||||
ip_list = ip_string.split(',')
|
||||
ip_list = ip_string.split(",")
|
||||
# Remove any leading or trailing whitespace from each IP in the list
|
||||
# this will return [''] if no ips have been entered, which is taken
|
||||
# into account in the model in checkHostIPCombo
|
||||
|
@ -311,7 +312,7 @@ class DomainNameserversView(DomainFormBaseView, BaseFormSet):
|
|||
self.object.nameservers = nameservers
|
||||
except NameserverError as Err:
|
||||
# TODO: move into literal
|
||||
messages.error(self.request, 'Whoops, Nameservers Error')
|
||||
messages.error(self.request, "Whoops, Nameservers Error")
|
||||
# messages.error(self.request, GENERIC_ERROR)
|
||||
logger.error(f"Nameservers error: {Err}")
|
||||
# TODO: registry is not throwing an error when no connection
|
||||
|
@ -325,7 +326,11 @@ class DomainNameserversView(DomainFormBaseView, BaseFormSet):
|
|||
logger.error(f"Registry error: {Err}")
|
||||
else:
|
||||
messages.success(
|
||||
self.request, "The name servers for this domain have been updated. Keep in mind that DNS changes may take some time to propagate across the internet. It can take anywhere from a few minutes to 48 hours for your changes to take place."
|
||||
self.request,
|
||||
"The name servers for this domain have been updated. "
|
||||
"Keep in mind that DNS changes may take some time to "
|
||||
"propagate across the internet. It can take anywhere "
|
||||
"from a few minutes to 48 hours for your changes to take place.",
|
||||
)
|
||||
|
||||
# superclass has the redirect
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue