From f2948a77dec33f4702617cb875d4c36fc1d5f9a3 Mon Sep 17 00:00:00 2001 From: Rebecca Hsieh Date: Thu, 21 Sep 2023 16:31:47 -0700 Subject: [PATCH] Update one nameserver test --- src/registrar/models/domain.py | 24 +++++++++++-------- src/registrar/tests/common.py | 9 +++++++ src/registrar/tests/test_models_domain.py | 29 +++++++++++++++++++---- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/registrar/models/domain.py b/src/registrar/models/domain.py index 730c9357d..f0567104c 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -309,7 +309,7 @@ class Domain(TimeStampedModel, DomainHelper): # currenthosts = self.nameservers # that way you have current hosts - + count = 0 for hostTuple in hosts: host = hostTuple[0].strip() # for removing empty string -- do we need strip? addrs = None @@ -320,7 +320,7 @@ class Domain(TimeStampedModel, DomainHelper): # if you are dotgov and don't have an IP address then raise error # 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? @@ -328,8 +328,10 @@ class Domain(TimeStampedModel, DomainHelper): # 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) - # NOTE TO ANSWER THIS ON SLACK - # if it isn't in the domain - set a flag so that createdCode == ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY: + + # TODO: There could be an error here??? + count += 1 + # host can be used by multiple domains if createdCode == ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY: # add host to domain (domain already created, just adding to it) request = commands.UpdateDomain( @@ -338,17 +340,19 @@ class Domain(TimeStampedModel, DomainHelper): try: registry.send(request, cleaned=True) + # count += 1 except RegistryError as e: logger.error( "Error adding nameserver, code was %s error was %s" % (e.code, e) ) + # elif createdCode == ErrorCode.OBJECT_EXISTS: + # count += 1 try: - # should we check for contacts? - # check if there are 2 or more name servers or 13 (inclusive) - self.ready() - self.save() + if len(count) >= 2 or len(count) <= 13: + self.ready() + self.save() except Exception as err: logger.info( "nameserver setter checked for create state " @@ -847,8 +851,8 @@ class Domain(TimeStampedModel, DomainHelper): nameserverList = self.nameservers logger.info("Changing to ready state") # TEST THIS -- assertValue or print (trigger this) - if len(nameserverList) < 2 or len(nameserverList) > 13: - raise ValueError("Not ready to become created, cannot transition yet") + # if len(nameserverList) < 2 or len(nameserverList) > 13: + # raise ValueError("Not ready to become created, cannot transition yet") logger.info("able to transition to ready state") def _disclose_fields(self, contact: PublicContact): diff --git a/src/registrar/tests/common.py b/src/registrar/tests/common.py index 66d9c2db1..f0ef47223 100644 --- a/src/registrar/tests/common.py +++ b/src/registrar/tests/common.py @@ -554,12 +554,14 @@ class MockEppLib(TestCase): contacts=..., hosts=..., statuses=..., + avail=..., ): self.auth_info = auth_info self.cr_date = cr_date self.contacts = contacts self.hosts = hosts self.statuses = statuses + self.avail = avail mockDataInfoDomain = fakedEppObject( "fakepw", @@ -583,6 +585,9 @@ class MockEppLib(TestCase): mockDataInfoHosts = fakedEppObject( "lastPw", cr_date=datetime.datetime(2023, 8, 25, 19, 45, 35) ) + mockDataCheckHosts = fakedEppObject( + "lastPw", cr_date=datetime.datetime(2023, 8, 25, 19, 45, 35), avail=True, + ) def mockSend(self, _request, cleaned): """Mocks the registry.send function used inside of domain.py @@ -603,6 +608,10 @@ class MockEppLib(TestCase): # use this for when a contact is being updated # sets the second send() to fail raise RegistryError(code=ErrorCode.OBJECT_EXISTS) + elif (isinstance(_request, commands.CheckHost)): + return MagicMock(res_data=[self.mockDataCheckHosts]) + elif (isinstance(_request, commands.CreateHost)): + return MagicMock(res_data=[self.mockDataCheckHosts], code=ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY) return MagicMock(res_data=[self.mockDataInfoHosts]) def setUp(self): diff --git a/src/registrar/tests/test_models_domain.py b/src/registrar/tests/test_models_domain.py index d35b0ba96..7ff5cecae 100644 --- a/src/registrar/tests/test_models_domain.py +++ b/src/registrar/tests/test_models_domain.py @@ -523,7 +523,7 @@ class TestRegistrantContacts(MockEppLib): raise -class TestRegistrantNameservers(TestCase): +class TestRegistrantNameservers(MockEppLib): """Rule: Registrants may modify their nameservers""" def setUp(self): @@ -532,9 +532,9 @@ class TestRegistrantNameservers(TestCase): Given the registrant is logged in And the registrant is the admin on a domain """ - pass + super().setUp() + self.domain, _ = Domain.objects.get_or_create(name="my-nameserver.gov", state=Domain.State.DNS_NEEDED) - @skip("not implemented yet") def test_user_adds_one_nameserver(self): """ Scenario: Registrant adds a single nameserver @@ -544,7 +544,28 @@ class TestRegistrantNameservers(TestCase): to the registry And `domain.is_active` returns False """ - raise + + # set 1 nameserver + nameserver = "ns1.my-nameserver.com" + self.domain.nameservers = [(nameserver,)] + + # when you create a host, you also have to update at same time + created_host = commands.CreateHost(nameserver) + update_domain_with_created = commands.UpdateDomain(name=self.domain.name, add=[common.HostObjSet([created_host.name])]) + + # checking if commands were sent (commands have to be sent in order) + expectedCalls = [ + call( + commands.CheckHost([created_host.name]), cleaned=True + ), + call(created_host, cleaned=True), + call(update_domain_with_created, cleaned=True), + ] + + self.mockedSendFunction.assert_has_calls(expectedCalls) + + # check that status is still NOT READY + self.assertFalse(self.domain.is_active()) @skip("not implemented yet") def test_user_adds_two_nameservers(self):