Add to tests

This commit is contained in:
Rebecca Hsieh 2023-09-21 22:09:23 -07:00
parent f2948a77de
commit 803a469ba0
No known key found for this signature in database
GPG key ID: 644527A2F375A379
2 changed files with 217 additions and 32 deletions

View file

@ -228,18 +228,19 @@ class Domain(TimeStampedModel, DomainHelper):
"""
try:
hosts = self._get_property("hosts")
# PRINT THIS -- host response object?
print("HOST IS ")
print(hosts)
except Exception as err:
# Don't throw error as this is normal for a new domain
# TODO - 433 error handling ticket should address this
# TODO-848: Check/add to error handling ticket if it's not addressed
# (Don't throw error as this is normal for a new domain?)
logger.info("Domain is missing nameservers %s" % err)
return []
hostList = []
for host in hosts:
# TODO - this should actually have a second tuple value with the ip address
# TODO-848: This should actually have a second tuple value with the ip address
# ignored because uncertain if we will even have a way to display mult.
# and adresses can be a list of mult address
# and adresses can be a list of mult address
hostList.append((host["name"],))
return hostList
@ -247,13 +248,12 @@ class Domain(TimeStampedModel, DomainHelper):
def _check_host(self, hostnames: list[str]):
"""check if host is available, True if available
returns boolean"""
# Double check this implementation is needed bc it's untested code
# TODO-848: Double check this implementation is needed bc it's untested code
# Check if the IP address is available/real
checkCommand = commands.CheckHost(hostnames)
try:
response = registry.send(checkCommand, cleaned=True)
return response.res_data[0].avail
# there will be a .available property on object -- boolean
except RegistryError as err:
logger.warning(
"Couldn't check hosts %s. Errorcode was %s, error was %s",
@ -270,14 +270,14 @@ class Domain(TimeStampedModel, DomainHelper):
returns ErrorCode (int)"""
logger.info("Creating host")
if addrs is not None:
# UNIT TEST: make sure to have 1 with ip address + 1 without
# TODO-848: Make sure to have 1 with ip address + 1 without
addresses = [epp.Ip(addr=addr) for addr in addrs]
request = commands.CreateHost(name=host, addrs=addresses)
else:
# ip is a specification within the nameserver
# NOTE-848: ip is a specification within the nameserver
request = commands.CreateHost(name=host)
# if you talk to registry you MUST do try/except
# NOTE-848: if you talk to registry you MUST do try/except
try:
logger.info("_create_host()-> sending req as %s" % request)
response = registry.send(request, cleaned=True)
@ -291,9 +291,8 @@ class Domain(TimeStampedModel, DomainHelper):
"""host should be a tuple of type str, str,... where the elements are
Fully qualified host name, addresses associated with the host
example: [(ns1.okay.gov, 127.0.0.1, others ips)]"""
# TODO: ticket #848 finish this implementation
# must delete nameservers as well or update
# ip version checking may need to be added in a different ticket
# TODO-848: Finish this implementation of delete + update nameserver
# TODO-848: ip version checking may need to be added in a different ticket
# We currently don't have IP address functionality
# We can have multiple IP addresses
@ -315,25 +314,23 @@ class Domain(TimeStampedModel, DomainHelper):
addrs = None
if len(hostTuple) > 1:
addrs = hostTuple[1:] # list of all the ip address
# do we want to clean the addresses (strip it if not null?)
# is the host a .gov (do .split on the last item), isdotgov can be a boolean
# if you are dotgov and don't have an IP address then raise error
# TRY logger.info() or print()
# TODO-848: Do we want to clean the addresses (strip it if not null?)
# TODO-848: Check if the host a .gov (do .split on the last item), isdotgov can be a boolean function
# TODO-848: if you are dotgov and don't have an IP address then raise error
# NOTE-848: TRY logger.info() or print()
avail = self._check_host([host])
if avail:
createdCode = self._create_host(host=host, addrs=addrs) # creates in registry
# DOUBLE CHECK: _create_host should handle duplicates?
# update the domain obj
# if createdCode == ErrorCode.OBJECT_EXISTS:
# duplication check if it's already on the domain -- self.nameservers
# Is it possible for a nameserver to exist and not be on a domain yet? (can one have duplicate name servers)
# TODO-848: Go through code flow to figure out why count is not incrementing
createdCode = self._create_host(host=host, addrs=addrs) # creates in registry
# TODO-848: Double check if _create_host should handle duplicates + update domain obj?
# NOTE-848: if createdCode == ErrorCode.OBJECT_EXISTS: --> self.nameservers
# TODO: There could be an error here???
count += 1
# host can be used by multiple domains
# NOTE-848: Host can be used by multiple domains
if createdCode == ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY:
# add host to domain (domain already created, just adding to it)
# NOTE-848: Add host to domain (domain already created, just adding to it)
request = commands.UpdateDomain(
name=self.name, add=[epp.HostObjSet([host])]
)
@ -350,6 +347,8 @@ class Domain(TimeStampedModel, DomainHelper):
# count += 1
try:
print("COUNT IS ")
print(count)
if len(count) >= 2 or len(count) <= 13:
self.ready()
self.save()
@ -358,8 +357,7 @@ class Domain(TimeStampedModel, DomainHelper):
"nameserver setter checked for create state "
"and it did not succeed. Error: %s" % err
)
# TODO - handle removed nameservers here will need to change the state
# then go back to DNS_NEEDED
# TODO-848: Handle removed nameservers here, will need to change the state then go back to DNS_NEEDED
@Cache
def statuses(self) -> list[str]:
@ -967,6 +965,10 @@ class Domain(TimeStampedModel, DomainHelper):
raise NotImplementedError()
def _delete_host(self, host):
# if len(nameserver_list) < 2:
# change from READY to DNS_NEEDED state
# Check host to nameserver list, and then use delete command?
raise NotImplementedError()
def _fetch_cache(self, fetch_hosts=False, fetch_contacts=False):

View file

@ -567,7 +567,6 @@ class TestRegistrantNameservers(MockEppLib):
# check that status is still NOT READY
self.assertFalse(self.domain.is_active())
@skip("not implemented yet")
def test_user_adds_two_nameservers(self):
"""
Scenario: Registrant adds 2 or more nameservers, thereby activating the domain
@ -577,9 +576,39 @@ class TestRegistrantNameservers(MockEppLib):
to the registry
And `domain.is_active` returns True
"""
raise
@skip("not implemented yet")
# set 2 nameservers
nameserver1 = "ns1.my-nameserver-1.com"
nameserver2 = "ns1.my-nameserver-2.com"
self.domain.nameservers = [(nameserver1,), (nameserver2,)]
# when you create a host, you also have to update at same time
created_host1 = commands.CreateHost(nameserver1)
update_domain_with_created1 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host1.name])])
created_host2 = commands.CreateHost(nameserver2)
update_domain_with_created2 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host2.name])])
# checking if commands were sent (commands have to be sent in order)
expectedCalls = [
call(
commands.CheckHost([created_host1.name]), cleaned=True
),
call(created_host1, cleaned=True),
call(update_domain_with_created1, cleaned=True),
call(
commands.CheckHost([created_host2.name]), cleaned=True
),
call(created_host2, cleaned=True),
call(update_domain_with_created2, cleaned=True),
]
self.mockedSendFunction.assert_has_calls(expectedCalls)
# check that status is READY
# TO-FIX: This is currently failing because we are not incrementing count?
self.assertTrue(self.domain.is_active())
def test_user_adds_too_many_nameservers(self):
"""
Scenario: Registrant adds 14 or more nameservers
@ -587,7 +616,161 @@ class TestRegistrantNameservers(MockEppLib):
When `domain.nameservers` is set to an array of length 14
Then Domain raises a user-friendly error
"""
raise
# set 13+ nameservers
nameserver1 = "ns1.cats-are-superior1.com"
nameserver2 = "ns1.cats-are-superior2.com"
nameserver3 = "ns1.cats-are-superior3.com"
nameserver4 = "ns1.cats-are-superior4.com"
nameserver5 = "ns1.cats-are-superior5.com"
nameserver6 = "ns1.cats-are-superior6.com"
nameserver7 = "ns1.cats-are-superior7.com"
nameserver8 = "ns1.cats-are-superior8.com"
nameserver9 = "ns1.cats-are-superior9.com"
nameserver10 = "ns1.cats-are-superior10.com"
nameserver11 = "ns1.cats-are-superior11.com"
nameserver12 = "ns1.cats-are-superior12.com"
nameserver13 = "ns1.cats-are-superior13.com"
nameserver14 = "ns1.cats-are-superior14.com"
self.domain.nameservers = [(nameserver1,), (nameserver2,), (nameserver3,), (nameserver4,),
(nameserver5,), (nameserver6,), (nameserver7,), (nameserver8,), (nameserver9), (nameserver10,),
(nameserver11,), (nameserver12,), (nameserver13,), (nameserver14,)]
# when you create a host, you also have to update at same time
created_host1 = commands.CreateHost(nameserver1)
update_domain_with_created1 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host1.name])])
created_host2 = commands.CreateHost(nameserver2)
update_domain_with_created2 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host2.name])])
created_host3 = commands.CreateHost(nameserver3)
update_domain_with_created3 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host3.name])])
created_host4 = commands.CreateHost(nameserver4)
update_domain_with_created4 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host4.name])])
created_host5 = commands.CreateHost(nameserver5)
update_domain_with_created5 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host5.name])])
created_host6 = commands.CreateHost(nameserver6)
update_domain_with_created6 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host6.name])])
created_host7 = commands.CreateHost(nameserver7)
update_domain_with_created7 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host7.name])])
created_host8 = commands.CreateHost(nameserver8)
update_domain_with_created8 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host8.name])])
created_host9 = commands.CreateHost(nameserver9)
update_domain_with_created9 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host5.name])])
created_host10 = commands.CreateHost(nameserver10)
update_domain_with_created10 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host10.name])])
created_host11 = commands.CreateHost(nameserver11)
update_domain_with_created11 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host11.name])])
created_host12 = commands.CreateHost(nameserver12)
update_domain_with_created12 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host12.name])])
created_host13 = commands.CreateHost(nameserver13)
update_domain_with_created13 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host13.name])])
created_host14 = commands.CreateHost(nameserver14)
update_domain_with_created14 = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host14.name])])
# checking if commands were sent (commands have to be sent in order)
expectedCalls = [
call(
commands.CheckHost([created_host1.name]), cleaned=True
),
call(created_host1, cleaned=True),
call(update_domain_with_created1, cleaned=True),
call(
commands.CheckHost([created_host2.name]), cleaned=True
),
call(created_host2, cleaned=True),
call(update_domain_with_created2, cleaned=True),
call(
commands.CheckHost([created_host3.name]), cleaned=True
),
call(created_host3, cleaned=True),
call(update_domain_with_created3, cleaned=True),
call(
commands.CheckHost([created_host4.name]), cleaned=True
),
call(created_host4, cleaned=True),
call(update_domain_with_created4, cleaned=True),
call(
commands.CheckHost([created_host5.name]), cleaned=True
),
call(created_host5, cleaned=True),
call(update_domain_with_created5, cleaned=True),
call(
commands.CheckHost([created_host6.name]), cleaned=True
),
call(created_host6, cleaned=True),
call(update_domain_with_created6, cleaned=True),
call(
commands.CheckHost([created_host7.name]), cleaned=True
),
call(created_host7, cleaned=True),
call(update_domain_with_created7, cleaned=True),
call(
commands.CheckHost([created_host8.name]), cleaned=True
),
call(created_host8, cleaned=True),
call(update_domain_with_created8, cleaned=True),
call(
commands.CheckHost([created_host9.name]), cleaned=True
),
call(created_host9, cleaned=True),
call(update_domain_with_created9, cleaned=True),
call(
commands.CheckHost([created_host10.name]), cleaned=True
),
call(created_host10, cleaned=True),
call(update_domain_with_created10, cleaned=True),
call(
commands.CheckHost([created_host11.name]), cleaned=True
),
call(created_host11, cleaned=True),
call(update_domain_with_created11, cleaned=True),
call(
commands.CheckHost([created_host12.name]), cleaned=True
),
call(created_host12, cleaned=True),
call(update_domain_with_created12, cleaned=True),
call(
commands.CheckHost([created_host13.name]), cleaned=True
),
call(created_host13, cleaned=True),
call(update_domain_with_created13, cleaned=True),
call(
commands.CheckHost([created_host14.name]), cleaned=True
),
call(created_host14, cleaned=True),
call(update_domain_with_created14, cleaned=True),
]
self.mockedSendFunction.assert_has_calls(expectedCalls)
# TO-FIX: This is borked because it hits the error as soon as we set up 14
self.assertRaises(ValueError, namservers)
@skip("not implemented yet")
def test_user_removes_some_nameservers(self):