mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-26 04:28:39 +02:00
test is being mocked
This commit is contained in:
parent
09c9e1eed8
commit
bb6c953b31
2 changed files with 78 additions and 20 deletions
|
@ -377,7 +377,7 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
req = commands.InfoDomain(name=self.name)
|
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:
|
except RegistryError as e:
|
||||||
if already_tried_to_create:
|
if already_tried_to_create:
|
||||||
raise e
|
raise e
|
||||||
|
@ -394,7 +394,7 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
pw="2fooBAR123fooBaz"
|
pw="2fooBAR123fooBaz"
|
||||||
), # not a password
|
), # not a password
|
||||||
)
|
)
|
||||||
registry.send(req)
|
registry.send(req, cleaned=True)
|
||||||
# no error, so go ahead and update state
|
# no error, so go ahead and update state
|
||||||
self.state = Domain.State.CREATED
|
self.state = Domain.State.CREATED
|
||||||
self.save()
|
self.save()
|
||||||
|
@ -406,7 +406,7 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
req = commands.InfoContact(id=contact.registry_id)
|
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:
|
except RegistryError as e:
|
||||||
if e.code == ErrorCode.OBJECT_DOES_NOT_EXIST:
|
if e.code == ErrorCode.OBJECT_DOES_NOT_EXIST:
|
||||||
create = commands.CreateContact(
|
create = commands.CreateContact(
|
||||||
|
@ -457,9 +457,13 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
"""Contact registry for info about a domain."""
|
"""Contact registry for info about a domain."""
|
||||||
try:
|
try:
|
||||||
# get info from registry
|
# get info from registry
|
||||||
|
print("calling get or create\n")
|
||||||
data = self._get_or_create_domain()
|
data = self._get_or_create_domain()
|
||||||
|
print("after get or create\n")
|
||||||
# extract properties from response
|
# extract properties from response
|
||||||
# (Ellipsis is used to mean "null")
|
# (Ellipsis is used to mean "null")
|
||||||
|
print("data is \n")
|
||||||
|
print(data)
|
||||||
cache = {
|
cache = {
|
||||||
"auth_info": getattr(data, "auth_info", ...),
|
"auth_info": getattr(data, "auth_info", ...),
|
||||||
"_contacts": getattr(data, "contacts", ...),
|
"_contacts": getattr(data, "contacts", ...),
|
||||||
|
@ -472,9 +476,10 @@ 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", ...),
|
||||||
}
|
}
|
||||||
|
print("\nCACHE AT TOP\n\n"+str(cache)+"\n\n\n")
|
||||||
# remove null properties (to distinguish between "a value of None" and null)
|
# remove null properties (to distinguish between "a value of None" and null)
|
||||||
cleaned = {k: v for k, v in cache if v is not ...}
|
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
|
# get contact info, if there are any
|
||||||
if (
|
if (
|
||||||
fetch_contacts
|
fetch_contacts
|
||||||
|
@ -488,7 +493,7 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
# just asked the registry for still exists --
|
# just asked the registry for still exists --
|
||||||
# if not, that's a problem
|
# if not, that's a problem
|
||||||
req = commands.InfoContact(id=id)
|
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
|
# extract properties from response
|
||||||
# (Ellipsis is used to mean "null")
|
# (Ellipsis is used to mean "null")
|
||||||
contact = {
|
contact = {
|
||||||
|
@ -521,7 +526,7 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
# just asked the registry for still exists --
|
# just asked the registry for still exists --
|
||||||
# if not, that's a problem
|
# if not, that's a problem
|
||||||
req = commands.InfoHost(name=name)
|
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
|
# extract properties from response
|
||||||
# (Ellipsis is used to mean "null")
|
# (Ellipsis is used to mean "null")
|
||||||
host = {
|
host = {
|
||||||
|
@ -536,6 +541,7 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
|
|
||||||
# replace the prior cache with new data
|
# replace the prior cache with new data
|
||||||
self._cache = cleaned
|
self._cache = cleaned
|
||||||
|
print("cache is "+str(self._cache))
|
||||||
except RegistryError as e:
|
except RegistryError as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
|
|
||||||
|
@ -546,6 +552,7 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
def _get_property(self, property):
|
def _get_property(self, property):
|
||||||
"""Get some piece of info about a domain."""
|
"""Get some piece of info about a domain."""
|
||||||
if property not in self._cache:
|
if property not in self._cache:
|
||||||
|
print("get cache")
|
||||||
self._fetch_cache(
|
self._fetch_cache(
|
||||||
fetch_hosts=(property == "hosts"),
|
fetch_hosts=(property == "hosts"),
|
||||||
fetch_contacts=(property == "contacts"),
|
fetch_contacts=(property == "contacts"),
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.db.utils import IntegrityError
|
from django.db.utils import IntegrityError
|
||||||
|
from unittest.mock import patch, MagicMock
|
||||||
|
import datetime
|
||||||
from registrar.models import (
|
from registrar.models import (
|
||||||
DomainApplication,
|
DomainApplication,
|
||||||
User,
|
User,
|
||||||
|
@ -8,11 +9,42 @@ from registrar.models import (
|
||||||
PublicContact
|
PublicContact
|
||||||
)
|
)
|
||||||
from unittest import skip
|
from unittest import skip
|
||||||
|
try:
|
||||||
|
from epplib import InfoDomain
|
||||||
|
except ImportError:
|
||||||
|
# allow epplibwrapper to load without epplib, for testing and development
|
||||||
|
pass
|
||||||
##delete me
|
##delete me
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
|
|
||||||
class TestDomain(TestCase):
|
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):
|
def test_empty_create_fails(self):
|
||||||
"""Can't create a completely empty domain."""
|
"""Can't create a completely empty domain."""
|
||||||
with self.assertRaisesRegex(IntegrityError, "name"):
|
with self.assertRaisesRegex(IntegrityError, "name"):
|
||||||
|
@ -24,22 +56,41 @@ class TestDomain(TestCase):
|
||||||
# this assertion will not work -- for now, the fact that the
|
# this assertion will not work -- for now, the fact that the
|
||||||
# above command didn't error out is proof enough
|
# above command didn't error out is proof enough
|
||||||
# self.assertEquals(domain.state, Domain.State.DRAFTED)
|
# 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):
|
def test_cache(self):
|
||||||
|
# print(patch)
|
||||||
# domain, _= Domain.objects.get_or_create(name="igorville.gov")
|
# 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")
|
||||||
|
domain._get_property("auth_info")
|
||||||
|
print(domain._cache)
|
||||||
|
# sec=domain.security_contact
|
||||||
|
# print(sec)
|
||||||
|
# print("domain cache is as follows\n")
|
||||||
|
|
||||||
domain, _ = Domain.objects.get_or_create(name="igorville.gov")
|
# #would have expected the cache to contain the value
|
||||||
sec=domain.security_contact
|
# print(domain._cache)
|
||||||
print(sec)
|
# print("\n")
|
||||||
print("domain cache is as follows\n")
|
# domain.registrant = 'abc123'
|
||||||
|
# r=domain.registrant
|
||||||
#would have expected the cache to contain the value
|
# print(domain._cache)
|
||||||
print(domain._cache)
|
|
||||||
print("\n")
|
|
||||||
domain.registrant = 'abc123'
|
|
||||||
r=domain.registrant
|
|
||||||
print(domain._cache)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue