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 phonenumber_field.widgets import RegionalPhoneNumberWidget
|
||||||
from registrar.utility.errors import (
|
from registrar.utility.errors import (
|
||||||
NameserverError,
|
NameserverError,
|
||||||
NameserverErrorCodes as nsErrorCodes
|
NameserverErrorCodes as nsErrorCodes,
|
||||||
)
|
)
|
||||||
|
|
||||||
from ..models import Contact, DomainInformation, Domain
|
from ..models import Contact, DomainInformation, Domain
|
||||||
|
@ -26,26 +26,9 @@ class DomainAddUserForm(forms.Form):
|
||||||
|
|
||||||
|
|
||||||
class IPAddressField(forms.CharField):
|
class IPAddressField(forms.CharField):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
super().validate(value) # Run the default CharField validation
|
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):
|
class DomainNameserverForm(forms.Form):
|
||||||
"""Form for changing nameservers."""
|
"""Form for changing nameservers."""
|
||||||
|
@ -65,9 +48,9 @@ class DomainNameserverForm(forms.Form):
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
cleaned_data = super().clean()
|
cleaned_data = super().clean()
|
||||||
server = cleaned_data.get('server', '')
|
server = cleaned_data.get("server", "")
|
||||||
ip = cleaned_data.get('ip', '')
|
ip = cleaned_data.get("ip", "")
|
||||||
domain = cleaned_data.get('domain', '')
|
domain = cleaned_data.get("domain", "")
|
||||||
print(f"clean is called on {domain} {server}")
|
print(f"clean is called on {domain} {server}")
|
||||||
|
|
||||||
# make sure there's a nameserver if an ip is passed
|
# 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(",")]
|
ip_list = [ip.strip() for ip in ip.split(",")]
|
||||||
if not server and len(ip_list) > 0:
|
if not server and len(ip_list) > 0:
|
||||||
# If 'server' is empty, disallow 'ip' input
|
# 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
|
# if there's a nameserver and an ip, validate nameserver/ip combo
|
||||||
|
|
||||||
|
@ -83,22 +68,19 @@ class DomainNameserverForm(forms.Form):
|
||||||
if ip:
|
if ip:
|
||||||
ip_list = [ip.strip() for ip in ip.split(",")]
|
ip_list = [ip.strip() for ip in ip.split(",")]
|
||||||
else:
|
else:
|
||||||
ip_list = ['']
|
ip_list = [""]
|
||||||
try:
|
try:
|
||||||
Domain.checkHostIPCombo(domain, server, ip_list)
|
Domain.checkHostIPCombo(domain, server, ip_list)
|
||||||
except NameserverError as e:
|
except NameserverError as e:
|
||||||
if e.code == nsErrorCodes.GLUE_RECORD_NOT_ALLOWED:
|
if e.code == nsErrorCodes.GLUE_RECORD_NOT_ALLOWED:
|
||||||
self.add_error(
|
self.add_error(
|
||||||
'server',
|
"server",
|
||||||
NameserverError(code=nsErrorCodes.GLUE_RECORD_NOT_ALLOWED)
|
NameserverError(code=nsErrorCodes.GLUE_RECORD_NOT_ALLOWED),
|
||||||
)
|
)
|
||||||
elif e.code == nsErrorCodes.MISSING_IP:
|
elif e.code == nsErrorCodes.MISSING_IP:
|
||||||
self.add_error(
|
self.add_error("ip", NameserverError(code=nsErrorCodes.MISSING_IP))
|
||||||
'ip',
|
|
||||||
NameserverError(code=nsErrorCodes.MISSING_IP)
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
self.add_error('ip', str(e))
|
self.add_error("ip", str(e))
|
||||||
|
|
||||||
return cleaned_data
|
return cleaned_data
|
||||||
|
|
||||||
|
|
|
@ -314,15 +314,16 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
NameserverError (if exception hit)
|
NameserverError (if exception hit)
|
||||||
Returns:
|
Returns:
|
||||||
None"""
|
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)
|
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(
|
raise NameserverError(
|
||||||
code=nsErrorCodes.GLUE_RECORD_NOT_ALLOWED, nameserver=nameserver, ip=ip
|
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:
|
for addr in ip:
|
||||||
if not cls._valid_ip_addr(addr):
|
if not cls._valid_ip_addr(addr):
|
||||||
raise NameserverError(
|
raise NameserverError(
|
||||||
|
@ -384,7 +385,9 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
if newHostDict[prevHost] is not None and set(
|
if newHostDict[prevHost] is not None and set(
|
||||||
newHostDict[prevHost]
|
newHostDict[prevHost]
|
||||||
) != set(addrs):
|
) != 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]))
|
updated_values.append((prevHost, newHostDict[prevHost]))
|
||||||
|
|
||||||
new_values = {
|
new_values = {
|
||||||
|
@ -394,7 +397,9 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
}
|
}
|
||||||
|
|
||||||
for nameserver, ip in new_values.items():
|
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)
|
return (deleted_values, updated_values, new_values, previousHostDict)
|
||||||
|
|
||||||
|
@ -605,7 +610,11 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
if len(hosts) > 13:
|
if len(hosts) > 13:
|
||||||
raise NameserverError(code=nsErrorCodes.TOO_MANY_HOSTS)
|
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")
|
raise ActionNotAllowed("Nameservers can not be " "set in the current state")
|
||||||
|
|
||||||
logger.info("Setting nameservers")
|
logger.info("Setting nameservers")
|
||||||
|
|
|
@ -10,9 +10,7 @@ class TestNameserverError(TestCase):
|
||||||
def test_with_no_ip(self):
|
def test_with_no_ip(self):
|
||||||
"""Test NameserverError when no ip address is passed"""
|
"""Test NameserverError when no ip address is passed"""
|
||||||
nameserver = "nameserver val"
|
nameserver = "nameserver val"
|
||||||
expected = (
|
expected = "Subdomains require an IP address"
|
||||||
"Subdomains require an IP address"
|
|
||||||
)
|
|
||||||
|
|
||||||
nsException = NameserverError(
|
nsException = NameserverError(
|
||||||
code=nsErrorCodes.MISSING_IP, nameserver=nameserver
|
code=nsErrorCodes.MISSING_IP, nameserver=nameserver
|
||||||
|
|
|
@ -15,7 +15,6 @@ from django.shortcuts import redirect
|
||||||
from django.template import RequestContext
|
from django.template import RequestContext
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.views.generic.edit import FormMixin
|
from django.views.generic.edit import FormMixin
|
||||||
from django.forms import BaseFormSet
|
|
||||||
|
|
||||||
from registrar.models import (
|
from registrar.models import (
|
||||||
Domain,
|
Domain,
|
||||||
|
@ -215,7 +214,7 @@ class DomainDNSView(DomainBaseView):
|
||||||
template_name = "domain_dns.html"
|
template_name = "domain_dns.html"
|
||||||
|
|
||||||
|
|
||||||
class DomainNameserversView(DomainFormBaseView, BaseFormSet):
|
class DomainNameserversView(DomainFormBaseView):
|
||||||
"""Domain nameserver editing view."""
|
"""Domain nameserver editing view."""
|
||||||
|
|
||||||
template_name = "domain_nameservers.html"
|
template_name = "domain_nameservers.html"
|
||||||
|
@ -229,7 +228,9 @@ class DomainNameserversView(DomainFormBaseView, BaseFormSet):
|
||||||
|
|
||||||
if nameservers is not None:
|
if nameservers is not None:
|
||||||
# Add existing nameservers as initial data
|
# 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
|
# Ensure at least 3 fields, filled or empty
|
||||||
while len(initial_data) < 2:
|
while len(initial_data) < 2:
|
||||||
|
@ -284,7 +285,7 @@ class DomainNameserversView(DomainFormBaseView, BaseFormSet):
|
||||||
def form_valid(self, formset):
|
def form_valid(self, formset):
|
||||||
"""The formset is valid, perform something with it."""
|
"""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
|
# Set the nameservers from the formset
|
||||||
nameservers = []
|
nameservers = []
|
||||||
|
@ -292,7 +293,7 @@ class DomainNameserversView(DomainFormBaseView, BaseFormSet):
|
||||||
try:
|
try:
|
||||||
ip_string = form.cleaned_data["ip"]
|
ip_string = form.cleaned_data["ip"]
|
||||||
# Split the string into a list using a comma as the delimiter
|
# 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
|
# Remove any leading or trailing whitespace from each IP in the list
|
||||||
# this will return [''] if no ips have been entered, which is taken
|
# this will return [''] if no ips have been entered, which is taken
|
||||||
# into account in the model in checkHostIPCombo
|
# into account in the model in checkHostIPCombo
|
||||||
|
@ -311,7 +312,7 @@ class DomainNameserversView(DomainFormBaseView, BaseFormSet):
|
||||||
self.object.nameservers = nameservers
|
self.object.nameservers = nameservers
|
||||||
except NameserverError as Err:
|
except NameserverError as Err:
|
||||||
# TODO: move into literal
|
# TODO: move into literal
|
||||||
messages.error(self.request, 'Whoops, Nameservers Error')
|
messages.error(self.request, "Whoops, Nameservers Error")
|
||||||
# messages.error(self.request, GENERIC_ERROR)
|
# messages.error(self.request, GENERIC_ERROR)
|
||||||
logger.error(f"Nameservers error: {Err}")
|
logger.error(f"Nameservers error: {Err}")
|
||||||
# TODO: registry is not throwing an error when no connection
|
# TODO: registry is not throwing an error when no connection
|
||||||
|
@ -325,7 +326,11 @@ class DomainNameserversView(DomainFormBaseView, BaseFormSet):
|
||||||
logger.error(f"Registry error: {Err}")
|
logger.error(f"Registry error: {Err}")
|
||||||
else:
|
else:
|
||||||
messages.success(
|
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
|
# superclass has the redirect
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue