mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-20 01:36:00 +02:00
changed test to share patch function
This commit is contained in:
parent
7d8cdbcbf0
commit
f45095c82b
2 changed files with 88 additions and 93 deletions
|
@ -137,7 +137,6 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
|
||||
def __get__(self, obj, objtype=None):
|
||||
"""Called during get. Example: `r = domain.registrant`."""
|
||||
print("within the get")
|
||||
return super().__get__(obj, objtype)
|
||||
|
||||
def __set__(self, obj, value):
|
||||
|
@ -268,7 +267,6 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
#request = common.DomainContact(contact=id, type="tech")])
|
||||
#send request
|
||||
#registrant for billing? registrant tag in infordomainResult
|
||||
#update needs to be called cux registrant is set when domain is created
|
||||
#UpdateDomain(takes registrant)
|
||||
raise NotImplementedError()
|
||||
|
||||
|
@ -467,13 +465,9 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
"""Contact registry for info about a domain."""
|
||||
try:
|
||||
# get info from registry
|
||||
print("calling get or create\n")
|
||||
data = self._get_or_create_domain()
|
||||
print("after get or create\n")
|
||||
# extract properties from response
|
||||
# (Ellipsis is used to mean "null")
|
||||
print("data is \n")
|
||||
print(data)
|
||||
cache = {
|
||||
"auth_info": getattr(data, "auth_info", ...),
|
||||
"_contacts": getattr(data, "contacts", ...),
|
||||
|
@ -486,10 +480,10 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
"tr_date": getattr(data, "tr_date", ...),
|
||||
"up_date": getattr(data, "up_date", ...),
|
||||
}
|
||||
print("\nCACHE AT TOP\n\n"+str(cache)+"\n\n\n")
|
||||
|
||||
# remove null properties (to distinguish between "a value of None" and null)
|
||||
cleaned = {k: v for k, v in cache.items() if v is not ...}
|
||||
print("\ncleaned is "+str(cleaned)+"\n\n")
|
||||
|
||||
# get contact info, if there are any
|
||||
if (
|
||||
fetch_contacts
|
||||
|
@ -504,6 +498,7 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
# if not, that's a problem
|
||||
req = commands.InfoContact(id=id)
|
||||
data = registry.send(req, cleaned=True).res_data[0]
|
||||
|
||||
# extract properties from response
|
||||
# (Ellipsis is used to mean "null")
|
||||
contact = {
|
||||
|
@ -519,8 +514,9 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
"up_date": getattr(data, "up_date", ...),
|
||||
"voice": getattr(data, "voice", ...),
|
||||
}
|
||||
|
||||
cleaned["contacts"].append(
|
||||
{k: v for k, v in contact if v is not ...}
|
||||
{k: v for k, v in contact.items() if v is not ...}
|
||||
)
|
||||
|
||||
# get nameserver info, if there are any
|
||||
|
@ -547,11 +543,11 @@ 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 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
|
||||
print("cache is "+str(self._cache))
|
||||
|
||||
except RegistryError as e:
|
||||
logger.error(e)
|
||||
|
||||
|
@ -561,13 +557,12 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
|
||||
def _get_property(self, property):
|
||||
"""Get some piece of info about a domain."""
|
||||
print("I am called")
|
||||
if property not in self._cache:
|
||||
print("get cache")
|
||||
self._fetch_cache(
|
||||
fetch_hosts=(property == "hosts"),
|
||||
fetch_contacts=(property == "contacts"),
|
||||
)
|
||||
|
||||
if property in self._cache:
|
||||
return self._cache[property]
|
||||
else:
|
||||
|
|
|
@ -5,51 +5,41 @@ import datetime
|
|||
from registrar.models import (
|
||||
DomainApplication,
|
||||
User,
|
||||
Domain,
|
||||
PublicContact
|
||||
Domain
|
||||
)
|
||||
from unittest import skip
|
||||
from epplibwrapper import commands
|
||||
# try:
|
||||
# from epplib.commands import InfoDomain
|
||||
# except ImportError:
|
||||
# # allow epplibwrapper to load without epplib, for testing and development
|
||||
# print("epplib didn't load")
|
||||
# pass
|
||||
##delete me
|
||||
|
||||
class TestDomain(TestCase):
|
||||
class fakedEppObject(object):
|
||||
def __init__(self, auth_info=..., cr_date=..., _contacts=..., _hosts=...):
|
||||
""""""
|
||||
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
|
||||
self.contacts=contacts
|
||||
self.hosts=hosts
|
||||
|
||||
mockDataInfoDomain=fakedEppObject(False, None, datetime.datetime(2023, 5, 25, 19, 45, 35))
|
||||
# {'auth_info': <MagicMock name='send().res_data.__getitem__().auth_info' id='281473717645712'>, '_contacts': <MagicMock name='send().res_data.__getitem__().contacts' id='281473717644608'>, 'cr_date': <MagicMock name='send().res_data.__getitem__().cr_date' id='281473719330096'>, 'ex_date': <MagicMock name='send().res_data.__getitem__().ex_date' id='281473719328464'>, '_hosts': <MagicMock name='send().res_data.__getitem__().hosts' id='281473721505856'>, 'name': <MagicMock name='send().res_data.__getitem__().name' id='281473717398512'>, 'registrant': <MagicMock name='send().res_data.__getitem__().registrant' id='281473717289408'>, 'statuses': <MagicMock name='send().res_data.__getitem__().statuses' id='281473717293632'>, 'tr_date': <MagicMock name='send().res_data.__getitem__().tr_date' id='281473710170096'>, 'up_date': <MagicMock name='send().res_data.__getitem__().up_date' id='281473710170384'>}
|
||||
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):
|
||||
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.mock_foo = self.patcher.start()
|
||||
self.mock_foo.side_effect=self.mockSend
|
||||
|
||||
def tearDown(self):
|
||||
self.patcher.stop()
|
||||
|
||||
# mockDataContactInfo={
|
||||
# "id": id,
|
||||
# "auth_info": getattr(data, "auth_info", ...),
|
||||
# "cr_date": getattr(data, "cr_date", ...),
|
||||
# "disclose": getattr(data, "disclose", ...),
|
||||
# "email": getattr(data, "email", ...),
|
||||
# "fax": getattr(data, "fax", ...),
|
||||
# "postal_info": getattr(data, "postal_info", ...),
|
||||
# "statuses": getattr(data, "statuses", ...),
|
||||
# "tr_date": getattr(data, "tr_date", ...),
|
||||
# "up_date": getattr(data, "up_date", ...),
|
||||
# "voice": getattr(data, "voice", ...),
|
||||
# }
|
||||
# mockDataHosts={
|
||||
# "name": name,
|
||||
# "addrs": getattr(data, "addrs", ...),
|
||||
# "cr_date": getattr(data, "cr_date", ...),
|
||||
# "statuses": getattr(data, "statuses", ...),
|
||||
# "tr_date": getattr(data, "tr_date", ...),
|
||||
# "up_date": getattr(data, "up_date", ...),
|
||||
# }
|
||||
def test_empty_create_fails(self):
|
||||
"""Can't create a completely empty domain."""
|
||||
with self.assertRaisesRegex(IntegrityError, "name"):
|
||||
|
@ -61,57 +51,67 @@ class TestDomain(TestCase):
|
|||
# this assertion will not work -- for now, the fact that the
|
||||
# above command didn't error out is proof enough
|
||||
# self.assertEquals(domain.state, Domain.State.DRAFTED)
|
||||
def mockSend(self, _request, cleaned):
|
||||
print("*****IN MOCK******************")
|
||||
print(_request)
|
||||
print(commands.InfoDomain.__class__)
|
||||
|
||||
print(_request.__class__)
|
||||
if isinstance(_request,commands.InfoDomain):
|
||||
return MagicMock(res_data=[self.mockDataInfoDomain])
|
||||
elif isinstance(_request, commands.InfoContact):
|
||||
return
|
||||
return MagicMock(res_data=[self.mockDataInfoDomain])
|
||||
|
||||
def test_cache(self):
|
||||
# print(patch)
|
||||
# domain, _= Domain.objects.get_or_create(name="igorville.gov")
|
||||
# mockSend=MagicMock(return_val)
|
||||
# InfoDomain()
|
||||
|
||||
with patch ("registrar.models.domain.registry.send", new=self.mockSend) as mockEpp:
|
||||
# with patch("epplibwrapper.CLIENT") as registry_mock, \
|
||||
# patch("epplibwrapper.CLIENT.send",side_effects=self.mockSend) as send_mock:
|
||||
# with patch("epplibwrapper.CLIENT.send",side_effects=self.mockSend):
|
||||
domain, _ = Domain.objects.get_or_create(name="igorville.gov")
|
||||
print("setting val")
|
||||
# val=domain.nameservers
|
||||
_val=domain.creation_date
|
||||
# domain._get_property("auth_info")
|
||||
# domain._get_property("cr_date")
|
||||
print(domain._cache)
|
||||
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())
|
||||
domain.nameservers=[("","")]
|
||||
print("after setter")
|
||||
print(domain._cache)
|
||||
#check it a second time and make sure send is not called
|
||||
# sec=domain.security_contact
|
||||
# print(sec)
|
||||
# print("domain cache is as follows\n")
|
||||
|
||||
# #would have expected the cache to contain the value
|
||||
# print(domain._cache)
|
||||
# print("\n")
|
||||
# domain.registrant = 'abc123'
|
||||
# r=domain.registrant
|
||||
# print(domain._cache)
|
||||
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 )
|
||||
self.assertFalse("avail" in domain._cache.keys())
|
||||
|
||||
#using a setter should clear the cache
|
||||
domain.nameservers=[("","")]
|
||||
self.assertEquals(domain._cache, {})
|
||||
|
||||
#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
|
||||
|
||||
#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 )
|
||||
self.assertEqual(domain._cache["contacts"], [expectedContactsDict])
|
||||
|
||||
#get and check hosts is set correctly
|
||||
domain._get_property("hosts")
|
||||
self.assertEqual(domain._cache["hosts"], [expectedHostsDict])
|
||||
|
||||
@skip("cannot activate a domain without mock registry")
|
||||
def test_get_status(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue