From fe033e0012d0a323e86a0f5cd3efc5a21d16b0ab Mon Sep 17 00:00:00 2001 From: wip-abramson Date: Thu, 20 May 2021 17:51:32 +0100 Subject: [PATCH 1/3] fixed listener bug and added tests --- .../aries_controller_base.py | 21 +++- setup.py | 2 +- tests/test_aries_controller_base.py | 108 ++++++++++++++++-- 3 files changed, 122 insertions(+), 9 deletions(-) diff --git a/aries_cloudcontroller/aries_controller_base.py b/aries_cloudcontroller/aries_controller_base.py index bbbae12e..38098d1a 100644 --- a/aries_cloudcontroller/aries_controller_base.py +++ b/aries_cloudcontroller/aries_controller_base.py @@ -148,6 +148,25 @@ 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 @@ -159,7 +178,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: diff --git a/setup.py b/setup.py index 48e488b4..8915231d 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ def parse_requirements(filename): if __name__ == "__main__": setup( name=PACKAGE_NAME, - version="0.2.4", + version="0.2.5", 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", diff --git a/tests/test_aries_controller_base.py b/tests/test_aries_controller_base.py index cb12cfed..7b3e25c6 100644 --- a/tests/test_aries_controller_base.py +++ b/tests/test_aries_controller_base.py @@ -101,23 +101,117 @@ 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): + 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) + + 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): From 55bcdf457dbfa67d8a27de512ac8ad77baed75c4 Mon Sep 17 00:00:00 2001 From: wip-abramson Date: Thu, 20 May 2021 18:07:29 +0100 Subject: [PATCH 2/3] linting and minor change --- .../aries_controller_base.py | 3 -- tests/test_aries_controller_base.py | 35 +++++-------------- 2 files changed, 8 insertions(+), 30 deletions(-) diff --git a/aries_cloudcontroller/aries_controller_base.py b/aries_cloudcontroller/aries_controller_base.py index 38098d1a..524ffcaf 100644 --- a/aries_cloudcontroller/aries_controller_base.py +++ b/aries_cloudcontroller/aries_controller_base.py @@ -148,7 +148,6 @@ 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 @@ -165,8 +164,6 @@ def is_subscribed(self, listener): 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 diff --git a/tests/test_aries_controller_base.py b/tests/test_aries_controller_base.py index 7b3e25c6..2ea3f310 100644 --- a/tests/test_aries_controller_base.py +++ b/tests/test_aries_controller_base.py @@ -109,22 +109,16 @@ async def test_register_listeners(self): listeners = [] # Receive connection messages def connections_handler(payload): - print("Connections Handler") + LOGGER.info("Connections Handler") - connection_listener = { - "handler": connections_handler, - "topic": "connections" - } + connection_listener = {"handler": connections_handler, "topic": "connections"} listeners.append(connection_listener) def issuer_handler(payload): - print("Issuer handler") + LOGGER.info("Issuer handler") - issuer_listener = { - "topic": "issue_credential", - "handler": issuer_handler - } + issuer_listener = {"topic": "issue_credential", "handler": issuer_handler} listeners.append(issuer_listener) @@ -135,7 +129,6 @@ def issuer_handler(payload): await ac.terminate() - @pytest.mark.asyncio async def test_add_listener(self): ac = AriesAgentControllerBase(admin_url="0.0.0.0") @@ -144,10 +137,7 @@ async def test_add_listener(self): def connections_handler(payload): print("Connections Handler") - connection_listener = { - "handler": connections_handler, - "topic": "connections" - } + connection_listener = {"handler": connections_handler, "topic": "connections"} ac.add_listener(connection_listener) @@ -162,10 +152,7 @@ async def test_remove_listener(self): def connections_handler(payload): print("Connections Handler") - connection_listener = { - "handler": connections_handler, - "topic": "connections" - } + connection_listener = {"handler": connections_handler, "topic": "connections"} ac.add_listener(connection_listener) @@ -184,20 +171,14 @@ async def test_remove_all_listeners(self): def connections_handler(payload): print("Connections Handler") - connection_listener = { - "handler": connections_handler, - "topic": "connections" - } + 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 - } + issuer_listener = {"topic": "issue_credential", "handler": issuer_handler} listeners.append(issuer_listener) From 53d232290a1a8faac1b7f200f29052b95743895f Mon Sep 17 00:00:00 2001 From: r00tb0y Date: Thu, 20 May 2021 19:12:09 +0200 Subject: [PATCH 3/3] Version bump to 0.2.6 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 8915231d..3c49991e 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ def parse_requirements(filename): if __name__ == "__main__": setup( name=PACKAGE_NAME, - version="0.2.5", + 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",