minor formatting and linting changes

This commit is contained in:
Alysia Broddrick 2023-06-15 08:11:33 -07:00
parent 5398a81d24
commit 7e18f7f8d1
No known key found for this signature in database
GPG key ID: 03917052CD0F06B7
3 changed files with 73 additions and 66 deletions

View file

@ -49,7 +49,6 @@ class Domain(TimeStampedModel, DomainHelper):
self._cache = {} self._cache = {}
super(Domain, self).__init__(*args, **kwargs) super(Domain, self).__init__(*args, **kwargs)
class Status(models.TextChoices): class Status(models.TextChoices):
""" """
The status codes we can receive from the registry. The status codes we can receive from the registry.
@ -103,7 +102,6 @@ class Domain(TimeStampedModel, DomainHelper):
PENDING_TRANSFER = "pendingTransfer" PENDING_TRANSFER = "pendingTransfer"
PENDING_UPDATE = "pendingUpdate" PENDING_UPDATE = "pendingUpdate"
class State(models.TextChoices): class State(models.TextChoices):
"""These capture (some of) the states a domain object can be in.""" """These capture (some of) the states a domain object can be in."""
@ -116,7 +114,6 @@ class Domain(TimeStampedModel, DomainHelper):
# the state is indeterminate # the state is indeterminate
UNKNOWN = "unknown" UNKNOWN = "unknown"
class Cache(property): class Cache(property):
""" """
Python descriptor to turn class methods into properties. Python descriptor to turn class methods into properties.
@ -149,7 +146,6 @@ class Domain(TimeStampedModel, DomainHelper):
"""Called during delete. Example: `del domain.registrant`.""" """Called during delete. Example: `del domain.registrant`."""
super().__delete__(obj) super().__delete__(obj)
@classmethod @classmethod
def available(cls, domain: str) -> bool: def available(cls, domain: str) -> bool:
"""Check if a domain is available.""" """Check if a domain is available."""
@ -260,8 +256,8 @@ class Domain(TimeStampedModel, DomainHelper):
@registrant_contact.setter # type: ignore @registrant_contact.setter # type: ignore
def registrant_contact(self, contact: PublicContact): def registrant_contact(self, contact: PublicContact):
#get id from PublicContact->.registry_id # get id from PublicContact->.registry_id
#call UpdateDomain() command with registrant as parameter # call UpdateDomain() command with registrant as parameter
raise NotImplementedError() raise NotImplementedError()
@Cache @Cache
@ -271,10 +267,10 @@ class Domain(TimeStampedModel, DomainHelper):
@administrative_contact.setter # type: ignore @administrative_contact.setter # type: ignore
def administrative_contact(self, contact: PublicContact): 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, # call UpdateDomain with contact,
# type options are[admin, billing, tech, security] # 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() raise NotImplementedError()
@Cache @Cache
@ -541,7 +537,9 @@ class Domain(TimeStampedModel, DomainHelper):
"tr_date": getattr(data, "tr_date", ...), "tr_date": getattr(data, "tr_date", ...),
"up_date": getattr(data, "up_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 # replace the prior cache with new data
self._cache = cleaned self._cache = cleaned

View file

@ -2,30 +2,37 @@ from django.test import TestCase
from django.db.utils import IntegrityError from django.db.utils import IntegrityError
from unittest.mock import patch, MagicMock from unittest.mock import patch, MagicMock
import datetime import datetime
from registrar.models import ( from registrar.models import DomainApplication, User, Domain
DomainApplication,
User,
Domain
)
from unittest import skip from unittest import skip
from epplibwrapper import commands from epplibwrapper import commands
class TestDomain(TestCase): class TestDomain(TestCase):
class fakedEppObject(object): 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"]) def __init__(self, auth_info=..., cr_date=..., contacts=..., hosts=...):
mockDataInfoContact=fakedEppObject("anotherPw", cr_date=datetime.datetime(2023, 7, 25, 19, 45, 35)) self.auth_info = auth_info
mockDataInfoHosts=fakedEppObject("lastPw", cr_date=datetime.datetime(2023, 8, 25, 19, 45, 35)) 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): def mockSend(self, _request, cleaned):
"""""" """"""
if isinstance(_request,commands.InfoDomain): if isinstance(_request, commands.InfoDomain):
return MagicMock(res_data=[self.mockDataInfoDomain]) return MagicMock(res_data=[self.mockDataInfoDomain])
elif isinstance(_request, commands.InfoContact): elif isinstance(_request, commands.InfoContact):
return MagicMock(res_data=[self.mockDataInfoContact]) return MagicMock(res_data=[self.mockDataInfoContact])
@ -33,9 +40,9 @@ class TestDomain(TestCase):
def setUp(self): def setUp(self):
"""mock epp send function as this will fail locally""" """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 = self.patcher.start()
self.mock_foo.side_effect=self.mockSend self.mock_foo.side_effect = self.mockSend
def tearDown(self): def tearDown(self):
self.patcher.stop() self.patcher.stop()
@ -55,61 +62,64 @@ class TestDomain(TestCase):
def test_cache_sets_resets(self): def test_cache_sets_resets(self):
"""Cache should be set on getter and reset on setter calls""" """Cache should be set on getter and reset on setter calls"""
domain, _ = Domain.objects.get_or_create(name="igorville.gov") domain, _ = Domain.objects.get_or_create(name="igorville.gov")
#trigger getter # trigger getter
_val=domain.creation_date _ = domain.creation_date
#getter should set the domain cache with a InfoDomain object (see InfoDomainResult) # getter should set the domain cache with a InfoDomain object
self.assertEquals(domain._cache["auth_info"],self.mockDataInfoDomain.auth_info ) # (see InfoDomainResult)
self.assertEquals(domain._cache["cr_date"],self.mockDataInfoDomain.cr_date ) 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()) self.assertFalse("avail" in domain._cache.keys())
#using a setter should clear the cache # using a setter should clear the cache
domain.nameservers=[("","")] domain.nameservers = [("", "")]
self.assertEquals(domain._cache, {}) self.assertEquals(domain._cache, {})
#send should have been called only once # send should have been called only once
self.mock_foo.assert_called_once() self.mock_foo.assert_called_once()
def test_cache_used_when_avail(self): def test_cache_used_when_avail(self):
"""Cache is pulled from if the object has already been accessed""" """Cache is pulled from if the object has already been accessed"""
domain, _ = Domain.objects.get_or_create(name="igorville.gov") domain, _ = Domain.objects.get_or_create(name="igorville.gov")
cr_date=domain.creation_date cr_date = domain.creation_date
#repeat the getter call # repeat the getter call
cr_date=domain.creation_date cr_date = domain.creation_date
#value should still be set correctly # value should still be set correctly
self.assertEqual(cr_date, self.mockDataInfoDomain.cr_date ) self.assertEqual(cr_date, self.mockDataInfoDomain.cr_date)
self.assertEqual(domain._cache["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 # send was only called once & not on the second getter call
self.mock_foo.assert_called_once() self.mock_foo.assert_called_once()
def test_cache_nested_elements(self): def test_cache_nested_elements(self):
"""Cache works correctly with the nested objects cache and hosts""" """Cache works correctly with the nested objects cache and hosts"""
domain, _ = Domain.objects.get_or_create(name="igorville.gov") domain, _ = Domain.objects.get_or_create(name="igorville.gov")
#the cached contacts and hosts should be dictionaries of what is passed to them # the cached contacts and hosts should be dictionaries of what is passed to them
expectedContactsDict={'id':self.mockDataInfoDomain.contacts[0], expectedContactsDict = {
'auth_info':self.mockDataInfoContact.auth_info, "id": self.mockDataInfoDomain.contacts[0],
'cr_date':self.mockDataInfoContact.cr_date "auth_info": self.mockDataInfoContact.auth_info,
"cr_date": self.mockDataInfoContact.cr_date,
}
expectedHostsDict = {
"name": self.mockDataInfoDomain.hosts[0],
"cr_date": self.mockDataInfoHosts.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 # this can be changed when the getter for contacts is implemented
domain._get_property("contacts") domain._get_property("contacts")
#check domain info is still correct and not overridden # check domain info is still correct and not overridden
self.assertEqual(domain._cache["auth_info"], self.mockDataInfoDomain.auth_info ) self.assertEqual(domain._cache["auth_info"], self.mockDataInfoDomain.auth_info)
self.assertEqual(domain._cache["cr_date"], self.mockDataInfoDomain.cr_date ) self.assertEqual(domain._cache["cr_date"], self.mockDataInfoDomain.cr_date)
#check contacts # check contacts
self.assertEqual(domain._cache["_contacts"], self.mockDataInfoDomain.contacts ) self.assertEqual(domain._cache["_contacts"], self.mockDataInfoDomain.contacts)
self.assertEqual(domain._cache["contacts"], [expectedContactsDict]) self.assertEqual(domain._cache["contacts"], [expectedContactsDict])
#get and check hosts is set correctly # get and check hosts is set correctly
domain._get_property("hosts") domain._get_property("hosts")
self.assertEqual(domain._cache["hosts"], [expectedHostsDict]) self.assertEqual(domain._cache["hosts"], [expectedHostsDict])

View file

@ -137,7 +137,6 @@ class DomainNameserversView(DomainPermissionView, FormMixin):
domain = self.get_object() domain = self.get_object()
return [{"server": name} for name, *ip in domain.nameservers] return [{"server": name} for name, *ip in domain.nameservers]
def get_success_url(self): def get_success_url(self):
"""Redirect to the nameservers page for the domain.""" """Redirect to the nameservers page for the domain."""
return reverse("domain-nameservers", kwargs={"pk": self.object.pk}) return reverse("domain-nameservers", kwargs={"pk": self.object.pk})