test is being mocked

This commit is contained in:
Alysia Broddrick 2023-06-12 08:19:09 -07:00
parent 09c9e1eed8
commit bb6c953b31
No known key found for this signature in database
GPG key ID: 03917052CD0F06B7
2 changed files with 78 additions and 20 deletions

View file

@ -377,7 +377,7 @@ class Domain(TimeStampedModel, DomainHelper):
while True:
try:
req = commands.InfoDomain(name=self.name)
return registry.send(req).res_data[0]
return registry.send(req, cleaned=True).res_data[0]
except RegistryError as e:
if already_tried_to_create:
raise e
@ -394,7 +394,7 @@ class Domain(TimeStampedModel, DomainHelper):
pw="2fooBAR123fooBaz"
), # not a password
)
registry.send(req)
registry.send(req, cleaned=True)
# no error, so go ahead and update state
self.state = Domain.State.CREATED
self.save()
@ -406,7 +406,7 @@ class Domain(TimeStampedModel, DomainHelper):
while True:
try:
req = commands.InfoContact(id=contact.registry_id)
return registry.send(req).res_data[0]
return registry.send(req, cleaned=True).res_data[0]
except RegistryError as e:
if e.code == ErrorCode.OBJECT_DOES_NOT_EXIST:
create = commands.CreateContact(
@ -457,9 +457,13 @@ 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", ...),
@ -472,9 +476,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 if v is not ...}
print("\ncleaned is "+str(cleaned)+"\n\n")
# get contact info, if there are any
if (
fetch_contacts
@ -488,7 +493,7 @@ class Domain(TimeStampedModel, DomainHelper):
# just asked the registry for still exists --
# if not, that's a problem
req = commands.InfoContact(id=id)
data = registry.send(req).res_data[0]
data = registry.send(req, cleaned=True).res_data[0]
# extract properties from response
# (Ellipsis is used to mean "null")
contact = {
@ -521,7 +526,7 @@ class Domain(TimeStampedModel, DomainHelper):
# just asked the registry for still exists --
# if not, that's a problem
req = commands.InfoHost(name=name)
data = registry.send(req).res_data[0]
data = registry.send(req, cleaned=True).res_data[0]
# extract properties from response
# (Ellipsis is used to mean "null")
host = {
@ -536,6 +541,7 @@ class Domain(TimeStampedModel, DomainHelper):
# replace the prior cache with new data
self._cache = cleaned
print("cache is "+str(self._cache))
except RegistryError as e:
logger.error(e)
@ -546,6 +552,7 @@ class Domain(TimeStampedModel, DomainHelper):
def _get_property(self, property):
"""Get some piece of info about a domain."""
if property not in self._cache:
print("get cache")
self._fetch_cache(
fetch_hosts=(property == "hosts"),
fetch_contacts=(property == "contacts"),

View file

@ -1,6 +1,7 @@
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,
@ -8,11 +9,42 @@ from registrar.models import (
PublicContact
)
from unittest import skip
try:
from epplib import InfoDomain
except ImportError:
# allow epplibwrapper to load without epplib, for testing and development
pass
##delete me
from django.core.cache import cache
class TestDomain(TestCase):
mockDataInfoDomain={"avail": False,
"auth_info": None,
"cr_date": 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'>}
# 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"):
@ -24,21 +56,40 @@ 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 mock_send(self, _request, cleaned):
print("*****IN MOCK******************")
print(_request)
print(_request.__class__)
return MagicMock(res_data=[self.mockDataInfoDomain] )
# InfoDomainResult(code=1000, msg='Command completed successfully',
# res_data=[InfoDomainResultData(
# roid='DF13128E9-GOV', statuses=[Status(state='serverTransferProhibited', description=None, lang='en'), Status(state='inactive', description=None, lang='en')]
# , cl_id='cloudflare', cr_id=None, cr_date=datetime.datetime(2023, 5, 25, 19, 45, 35, tzinfo=tzlocal()), up_id=None, up_date=datetime.datetime(2023, 5, 30, 19, 45, 35, tzinfo=tzlocal()), tr_date=None, name='ok.gov', registrant='JOSWENSON', admins=[], nsset=None, keyset=None, ex_date=datetime.date(2024, 5, 25), )], cl_tr_id='b7p4gy#2023-06-11T23:39:36.563158', sv_tr_id='4tE5uQFmQ8KDKsgvlqpFxQ==-71', extensions=[], msg_q=None)}
# @patch('epplibwrapper.CLIENT')
def test_cache(self):
# print(patch)
# domain, _= Domain.objects.get_or_create(name="igorville.gov")
# mockSend=MagicMock(return_val)
with patch ("registrar.models.domain.registry.send", new=self.mock_send):
# with patch("epplibwrapper.CLIENT") as registry_mock, \
# patch("epplibwrapper.CLIENT.send",side_effects=self.mock_send) as send_mock:
# with patch("epplibwrapper.CLIENT.send",side_effects=self.mock_send):
domain, _ = Domain.objects.get_or_create(name="igorville.gov")
sec=domain.security_contact
print(sec)
print("domain cache is as follows\n")
domain._get_property("auth_info")
print(domain._cache)
# 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)
# #would have expected the cache to contain the value
# print(domain._cache)
# print("\n")
# domain.registrant = 'abc123'
# r=domain.registrant
# print(domain._cache)