Mocked sockets

This commit is contained in:
zandercymatics 2023-10-17 14:49:42 -06:00
parent d50de8516e
commit d943b69b3d
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 37 additions and 23 deletions

View file

@ -168,7 +168,7 @@ class EPPLibWrapper:
def _create_pool(self, client, login, options):
"""Creates and returns new pool instance"""
return EPPConnectionPool(
client, login, options
client, login, options
)
def start_connection_pool(
@ -190,8 +190,8 @@ class EPPLibWrapper:
# one socket, and if successful, then we know we can connect.
if (
not try_start_if_invalid
and settings.DEBUG
or not self._test_registry_connection_success()
and (settings.DEBUG
or not self._test_registry_connection_success())
):
logger.warning("Cannot contact the Registry")
self.pool_status.connection_success = False

View file

@ -4,6 +4,7 @@ from django.conf import settings
from django.test import TestCase
from epplibwrapper.client import EPPLibWrapper
from epplibwrapper.socket import Socket
from epplibwrapper.utility.pool import EPPConnectionPool
from registrar.models.domain import Domain
from registrar.models.domain import registry
@ -122,23 +123,36 @@ class TestConnectionPool(TestCase):
# Fake data for the _pool object
domain, _ = Domain.objects.get_or_create(name="freeman.gov")
def start_fake_connection(self):
registry.pool_status.pool_running = True
registry.pool_status.connection_success = True
registry._pool = registry.get_pool()
def fake_send(self):
return MagicMock(
code=1000,
msg="Command completed successfully",
res_data=None,
cl_tr_id="xkw1uo#2023-10-17T15:29:09.559376",
sv_tr_id="5CcH4gxISuGkq8eqvr1UyQ==-35a",
extensions=[],
msg_q=None,
)
# The connection pool will fail to start, start it manually
# so that our mocks can take over
with ExitStack() as stack:
stack.enter_context(patch.object(EPPLibWrapper, "get_pool", self.fake_pool))
stack.enter_context(patch.object(EPPLibWrapper, "start_connection_pool", start_fake_connection))
expected_contact = domain.security_contact
stack.enter_context(patch.object(Socket, "connect", None))
stack.enter_context(patch.object(Socket, "send", fake_send))
stack.enter_context(patch.object(Socket, "_create_socket", Socket()))
#stack.enter_context(patch.object(EPPLibWrapper, "get_pool", self.fake_pool))
pool = EPPLibWrapper(False)
# The connection pool will fail to start, start it manually
# so that our mocks can take over
pool.start_connection_pool(try_start_if_invalid=True)
print(f"this is pool {pool._pool.__dict__}")
# Pool should be running, and be the right size
self.assertEqual(pool.pool_status.pool_running, True)
self.assertEqual(pool.pool_status.connection_success, True)
pool.send(commands.InfoDomain(name="test.gov"), cleaned=True)
self.assertEqual(len(pool._pool.conn), self.pool_options["size"])
#pool.send()
# Trigger the getter - should succeed
#expected_contact = domain.security_contact
# Pretend that we've connected
registry.pool_status.pool_running = True
registry.pool_status.connection_success = True
# Trigger the getter - should succeed
self.assertEqual(registry.pool_status.pool_running, True)
self.assertEqual(registry.pool_status.connection_success, True)
self.assertEqual(len(registry.get_pool().conn), self.pool_options["size"])

View file

@ -1,8 +1,8 @@
from auditlog.registry import auditlog # type: ignore
from .contact import Contact
from .domain import Domain
from .domain_application import DomainApplication
from .domain_information import DomainInformation
from .domain import Domain
from .draft_domain import DraftDomain
from .host_ip import HostIP
from .host import Host
@ -17,9 +17,9 @@ from .transition_domain import TransitionDomain
__all__ = [
"Contact",
"Domain",
"DomainApplication",
"DomainInformation",
"Domain",
"DraftDomain",
"DomainInvitation",
"HostIP",