diff --git a/src/registrar/models/domain.py b/src/registrar/models/domain.py index 8a8ba8b9e..55539cc11 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -49,7 +49,6 @@ class Domain(TimeStampedModel, DomainHelper): self._cache = {} super(Domain, self).__init__(*args, **kwargs) - class Status(models.TextChoices): """ The status codes we can receive from the registry. @@ -103,7 +102,6 @@ class Domain(TimeStampedModel, DomainHelper): PENDING_TRANSFER = "pendingTransfer" PENDING_UPDATE = "pendingUpdate" - class State(models.TextChoices): """These capture (some of) the states a domain object can be in.""" @@ -116,7 +114,6 @@ class Domain(TimeStampedModel, DomainHelper): # the state is indeterminate UNKNOWN = "unknown" - class Cache(property): """ Python descriptor to turn class methods into properties. @@ -149,7 +146,6 @@ class Domain(TimeStampedModel, DomainHelper): """Called during delete. Example: `del domain.registrant`.""" super().__delete__(obj) - @classmethod def available(cls, domain: str) -> bool: """Check if a domain is available.""" @@ -260,8 +256,8 @@ class Domain(TimeStampedModel, DomainHelper): @registrant_contact.setter # type: ignore def registrant_contact(self, contact: PublicContact): - #get id from PublicContact->.registry_id - #call UpdateDomain() command with registrant as parameter + # get id from PublicContact->.registry_id + # call UpdateDomain() command with registrant as parameter raise NotImplementedError() @Cache @@ -271,10 +267,10 @@ class Domain(TimeStampedModel, DomainHelper): @administrative_contact.setter # type: ignore def administrative_contact(self, contact: PublicContact): - #call CreateContact, if contact doesn't exist yet for domain + # call CreateContact, if contact doesn't exist yet for domain # call UpdateDomain with contact, # type options are[admin, billing, tech, security] - #use admin as type parameter for this contact + # use admin as type parameter for this contact raise NotImplementedError() @Cache @@ -541,7 +537,9 @@ class Domain(TimeStampedModel, DomainHelper): "tr_date": getattr(data, "tr_date", ...), "up_date": getattr(data, "up_date", ...), } - cleaned["hosts"].append({k: v for k, v in host.items() if v is not ...}) + cleaned["hosts"].append( + {k: v for k, v in host.items() if v is not ...} + ) # replace the prior cache with new data self._cache = cleaned @@ -560,7 +558,7 @@ class Domain(TimeStampedModel, DomainHelper): fetch_hosts=(property == "hosts"), fetch_contacts=(property == "contacts"), ) - + if property in self._cache: return self._cache[property] else: diff --git a/src/registrar/tests/test_models_domain.py b/src/registrar/tests/test_models_domain.py index c9523c6a6..b96dbdb04 100644 --- a/src/registrar/tests/test_models_domain.py +++ b/src/registrar/tests/test_models_domain.py @@ -2,40 +2,47 @@ from django.test import TestCase from django.db.utils import IntegrityError from unittest.mock import patch, MagicMock import datetime -from registrar.models import ( - DomainApplication, - User, - Domain -) +from registrar.models import DomainApplication, User, Domain from unittest import skip from epplibwrapper import commands + class TestDomain(TestCase): class fakedEppObject(object): """""" - def __init__(self, auth_info=..., cr_date=..., contacts=..., hosts=...): - self.auth_info=auth_info - self.cr_date=cr_date - self.contacts=contacts - self.hosts=hosts - mockDataInfoDomain=fakedEppObject("fakepw",cr_date= datetime.datetime(2023, 5, 25, 19, 45, 35), contacts=["123"], hosts=["fake.host.com"]) - mockDataInfoContact=fakedEppObject("anotherPw", cr_date=datetime.datetime(2023, 7, 25, 19, 45, 35)) - mockDataInfoHosts=fakedEppObject("lastPw", cr_date=datetime.datetime(2023, 8, 25, 19, 45, 35)) - + def __init__(self, auth_info=..., cr_date=..., contacts=..., hosts=...): + self.auth_info = auth_info + self.cr_date = cr_date + self.contacts = contacts + self.hosts = hosts + + mockDataInfoDomain = fakedEppObject( + "fakepw", + cr_date=datetime.datetime(2023, 5, 25, 19, 45, 35), + contacts=["123"], + hosts=["fake.host.com"], + ) + mockDataInfoContact = fakedEppObject( + "anotherPw", cr_date=datetime.datetime(2023, 7, 25, 19, 45, 35) + ) + mockDataInfoHosts = fakedEppObject( + "lastPw", cr_date=datetime.datetime(2023, 8, 25, 19, 45, 35) + ) + def mockSend(self, _request, cleaned): """""" - if isinstance(_request,commands.InfoDomain): + if isinstance(_request, commands.InfoDomain): return MagicMock(res_data=[self.mockDataInfoDomain]) elif isinstance(_request, commands.InfoContact): return MagicMock(res_data=[self.mockDataInfoContact]) return MagicMock(res_data=[self.mockDataInfoHosts]) - + def setUp(self): """mock epp send function as this will fail locally""" - self.patcher = patch ("registrar.models.domain.registry.send") + self.patcher = patch("registrar.models.domain.registry.send") self.mock_foo = self.patcher.start() - self.mock_foo.side_effect=self.mockSend + self.mock_foo.side_effect = self.mockSend def tearDown(self): self.patcher.stop() @@ -55,61 +62,64 @@ class TestDomain(TestCase): def test_cache_sets_resets(self): """Cache should be set on getter and reset on setter calls""" domain, _ = Domain.objects.get_or_create(name="igorville.gov") - #trigger getter - _val=domain.creation_date - - #getter should set the domain cache with a InfoDomain object (see InfoDomainResult) - self.assertEquals(domain._cache["auth_info"],self.mockDataInfoDomain.auth_info ) - self.assertEquals(domain._cache["cr_date"],self.mockDataInfoDomain.cr_date ) + # trigger getter + _ = domain.creation_date + + # getter should set the domain cache with a InfoDomain object + # (see InfoDomainResult) + self.assertEquals(domain._cache["auth_info"], self.mockDataInfoDomain.auth_info) + self.assertEquals(domain._cache["cr_date"], self.mockDataInfoDomain.cr_date) self.assertFalse("avail" in domain._cache.keys()) - #using a setter should clear the cache - domain.nameservers=[("","")] + # using a setter should clear the cache + domain.nameservers = [("", "")] self.assertEquals(domain._cache, {}) - #send should have been called only once + # send should have been called only once self.mock_foo.assert_called_once() def test_cache_used_when_avail(self): """Cache is pulled from if the object has already been accessed""" domain, _ = Domain.objects.get_or_create(name="igorville.gov") - cr_date=domain.creation_date - - #repeat the getter call - cr_date=domain.creation_date + cr_date = domain.creation_date - #value should still be set correctly - self.assertEqual(cr_date, self.mockDataInfoDomain.cr_date ) - self.assertEqual(domain._cache["cr_date"], self.mockDataInfoDomain.cr_date ) + # repeat the getter call + cr_date = domain.creation_date - #send was only called once & not on the second getter call + # value should still be set correctly + self.assertEqual(cr_date, self.mockDataInfoDomain.cr_date) + self.assertEqual(domain._cache["cr_date"], self.mockDataInfoDomain.cr_date) + + # send was only called once & not on the second getter call self.mock_foo.assert_called_once() - def test_cache_nested_elements(self): """Cache works correctly with the nested objects cache and hosts""" domain, _ = Domain.objects.get_or_create(name="igorville.gov") - - #the cached contacts and hosts should be dictionaries of what is passed to them - expectedContactsDict={'id':self.mockDataInfoDomain.contacts[0], - 'auth_info':self.mockDataInfoContact.auth_info, - 'cr_date':self.mockDataInfoContact.cr_date - } - expectedHostsDict={'name':self.mockDataInfoDomain.hosts[0], - 'cr_date':self.mockDataInfoHosts.cr_date} - - #this can be changed when the getter for contacts is implemented - domain._get_property("contacts") - - #check domain info is still correct and not overridden - self.assertEqual(domain._cache["auth_info"], self.mockDataInfoDomain.auth_info ) - self.assertEqual(domain._cache["cr_date"], self.mockDataInfoDomain.cr_date ) - #check contacts - self.assertEqual(domain._cache["_contacts"], self.mockDataInfoDomain.contacts ) + # the cached contacts and hosts should be dictionaries of what is passed to them + expectedContactsDict = { + "id": self.mockDataInfoDomain.contacts[0], + "auth_info": self.mockDataInfoContact.auth_info, + "cr_date": self.mockDataInfoContact.cr_date, + } + expectedHostsDict = { + "name": self.mockDataInfoDomain.hosts[0], + "cr_date": self.mockDataInfoHosts.cr_date, + } + + # this can be changed when the getter for contacts is implemented + domain._get_property("contacts") + + # check domain info is still correct and not overridden + self.assertEqual(domain._cache["auth_info"], self.mockDataInfoDomain.auth_info) + self.assertEqual(domain._cache["cr_date"], self.mockDataInfoDomain.cr_date) + + # check contacts + self.assertEqual(domain._cache["_contacts"], self.mockDataInfoDomain.contacts) self.assertEqual(domain._cache["contacts"], [expectedContactsDict]) - - #get and check hosts is set correctly + + # get and check hosts is set correctly domain._get_property("hosts") self.assertEqual(domain._cache["hosts"], [expectedHostsDict]) diff --git a/src/registrar/views/domain.py b/src/registrar/views/domain.py index 16b007af5..6a33ec994 100644 --- a/src/registrar/views/domain.py +++ b/src/registrar/views/domain.py @@ -137,7 +137,6 @@ class DomainNameserversView(DomainPermissionView, FormMixin): domain = self.get_object() return [{"server": name} for name, *ip in domain.nameservers] - def get_success_url(self): """Redirect to the nameservers page for the domain.""" return reverse("domain-nameservers", kwargs={"pk": self.object.pk})