diff --git a/aries_cloudcontroller/aries_controller_base.py b/aries_cloudcontroller/aries_controller_base.py index bbbae12e..524ffcaf 100644 --- a/aries_cloudcontroller/aries_controller_base.py +++ b/aries_cloudcontroller/aries_controller_base.py @@ -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 @@ -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: diff --git a/setup.py b/setup.py index 48e488b4..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.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", diff --git a/tests/test_aries_controller_base.py b/tests/test_aries_controller_base.py index cb12cfed..2ea3f310 100644 --- a/tests/test_aries_controller_base.py +++ b/tests/test_aries_controller_base.py @@ -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):