Skip to content

Commit

Permalink
Merge pull request #39 from didx-xyz/bug/add_listener
Browse files Browse the repository at this point in the history
fixed listener bug and added tests 

version bump to 0.2.6
  • Loading branch information
morrieinmaas authored May 20, 2021
2 parents c2ee16e + 53d2322 commit 78cfc94
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 9 deletions.
18 changes: 17 additions & 1 deletion aries_cloudcontroller/aries_controller_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ def register_listeners(self, listeners, defaults=True):
except Exception as exc:
logger.warning(f"Register webhooks listeners failed! {exc!r} occurred.")

def is_subscribed(self, listener):
"""Check if listener is subscribed
Args:
----
listener : dict
A dictionary comprised of "handler": handler (fct) and
"topic":"topicname" key-value pairs
"""
try:
pub_topic_path = listener["topic"]
pub_hanlder = listener["handler"]
return pub.isSubscribed(pub_hanlder, pub_topic_path)
except Exception as exc:
logger.warning(f"Unable to check if listener subscribed {exc!r} occurred.")

def add_listener(self, listener):
"""Subscribe to a listeners for a topic
Expand All @@ -159,7 +175,7 @@ def add_listener(self, listener):
"""
try:
pub_topic_path = listener["topic"]
logger.INFO("Subscribing too: " + pub_topic_path)
logger.info("Subscribing too: " + pub_topic_path)
pub.subscribe(listener["handler"], pub_topic_path)
logger.debug("Lister added for topic : ", pub_topic_path)
except Exception as exc:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def parse_requirements(filename):
if __name__ == "__main__":
setup(
name=PACKAGE_NAME,
version="0.2.4",
version="0.2.6",
description="A simple python package for controlling an aries agent through the admin-api interface",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
89 changes: 82 additions & 7 deletions tests/test_aries_controller_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,98 @@ async def test_remove_api_key(self):

await ac.terminate()

# TODO create mock for pubsub listening webhooks
# Maybe this makes more sense in aries_controller
# TODO Maybe this makes more sense in aries_controller
@pytest.mark.asyncio
async def test_register_listners(self):
pass
async def test_register_listeners(self):
ac = AriesAgentControllerBase(admin_url="0.0.0.0")

listeners = []
# Receive connection messages
def connections_handler(payload):
LOGGER.info("Connections Handler")

connection_listener = {"handler": connections_handler, "topic": "connections"}

listeners.append(connection_listener)

def issuer_handler(payload):
LOGGER.info("Issuer handler")

issuer_listener = {"topic": "issue_credential", "handler": issuer_handler}

listeners.append(issuer_listener)

ac.register_listeners(listeners)

for listener in listeners:
assert ac.is_subscribed(listener)

await ac.terminate()

@pytest.mark.asyncio
async def test_add_listener(self):
pass
ac = AriesAgentControllerBase(admin_url="0.0.0.0")

# Receive connection messages
def connections_handler(payload):
print("Connections Handler")

connection_listener = {"handler": connections_handler, "topic": "connections"}

ac.add_listener(connection_listener)

assert ac.is_subscribed(connection_listener)
await ac.terminate()

@pytest.mark.asyncio
async def test_remove_listener(self):
pass
ac = AriesAgentControllerBase(admin_url="0.0.0.0")

# Receive connection messages
def connections_handler(payload):
print("Connections Handler")

connection_listener = {"handler": connections_handler, "topic": "connections"}

ac.add_listener(connection_listener)

ac.remove_listener(connection_listener)

assert not ac.is_subscribed(connection_listener)
await ac.terminate()

@pytest.mark.asyncio
async def test_remove_all_listeners(self):
pass
ac = AriesAgentControllerBase(admin_url="0.0.0.0")

listeners = []

# Receive connection messages
def connections_handler(payload):
print("Connections Handler")

connection_listener = {"handler": connections_handler, "topic": "connections"}

listeners.append(connection_listener)

def issuer_handler(payload):
print("Issuer handler")

issuer_listener = {"topic": "issue_credential", "handler": issuer_handler}

listeners.append(issuer_listener)

ac.register_listeners(listeners)

ac.remove_all_listeners("issue_credential")

assert not ac.is_subscribed(issuer_listener)
assert ac.is_subscribed(connection_listener)

ac.remove_all_listeners()

assert not ac.is_subscribed(connection_listener)
await ac.terminate()

@pytest.mark.asyncio
async def test_listen_webhooks(self):
Expand Down

0 comments on commit 78cfc94

Please sign in to comment.