diff --git a/acapy_plugin_toolbox/connections.py b/acapy_plugin_toolbox/connections.py index 3e193a9f..a581c8a0 100644 --- a/acapy_plugin_toolbox/connections.py +++ b/acapy_plugin_toolbox/connections.py @@ -311,4 +311,5 @@ async def handle(self, context: RequestContext, responder: BaseResponder): mediation_id=context.message.mediation_id, ) connection_resp = Connection(**conn_record_to_message_repr(connection)) + connection_resp.assign_thread_from(context.message) await responder.send_reply(connection_resp) diff --git a/int/tests/test_basicmessage.py b/int/tests/test_basicmessage.py index 6d31621c..2cbe826c 100644 --- a/int/tests/test_basicmessage.py +++ b/int/tests/test_basicmessage.py @@ -1,12 +1,12 @@ """Basic Message Tests""" import asyncio import pytest - -from aries_staticagent import StaticConnection +from aries_staticagent import StaticConnection, utils @pytest.mark.asyncio async def test_send(connection: StaticConnection, connection_id: str): + """Test send message""" with connection.next() as future_recip_message: sent_message = await asyncio.wait_for( connection.send_and_await_reply_async( @@ -25,7 +25,7 @@ async def test_send(connection: StaticConnection, connection_id: str): == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/sent" ) assert recip_message["message"]["content"] == "Your hovercraft is full of eels." - # Delete messages to clear the state between tests + # TODO add proper backchannel for clearing messages await connection.send_and_await_reply_async( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/delete", @@ -34,40 +34,33 @@ async def test_send(connection: StaticConnection, connection_id: str): @pytest.mark.asyncio -async def test_delete(connection: StaticConnection, connection_id: str): - for i in range(6): - with connection.next() as future_recip_message: - sent_message = await asyncio.wait_for( - connection.send_and_await_reply_async( - { - "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/send", - "connection_id": connection_id, - "content": "Test Message #{}".format(i), - }, - return_route="all", - ), - timeout=60, - ) - recip_message = await asyncio.wait_for(future_recip_message, 60) - delete_message = await connection.send_and_await_reply_async( +async def test_new(connection: StaticConnection): + """Test new message notification""" + new_response = await connection.send_and_await_reply_async( { - "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/delete", - } + "@type": "https://didcomm.org/basicmessage/1.0/message", + "~l10n": {"locale": "en"}, + "sent_time": utils.timestamp(), + "content": "Your hovercraft is full of eels.", + }, + return_route="all", ) - get_messages = await connection.send_and_await_reply_async( + assert ( + new_response["@type"] + == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/new" + ) + assert new_response["message"]["content"] == "Your hovercraft is full of eels." + # Delete messages to clear the state between tests + await connection.send_and_await_reply_async( { - "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/get", + "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/delete", } ) - assert ( - delete_message["@type"] - == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/deleted" - ) - assert get_messages["count"] == 0 @pytest.mark.asyncio async def test_get(connection: StaticConnection, connection_id: str): + """Send multiple messages and verify that the proper count and content appears in messages list""" with connection.next() as future_recip_message: sent_message = await asyncio.wait_for( connection.send_and_await_reply_async( @@ -104,6 +97,10 @@ async def test_get(connection: StaticConnection, connection_id: str): == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/messages" ) assert get_messages["count"] == 2 + assert ( + get_messages["messages"][1]["content"] == "Are you suggesting coconuts migrate?" + ) + assert get_messages["messages"][0]["content"] == "'Tis but a flesh wound." # Delete messages to clear the state between tests await connection.send_and_await_reply_async( { @@ -114,6 +111,7 @@ async def test_get(connection: StaticConnection, connection_id: str): @pytest.mark.asyncio async def test_get_limit_offset(connection: StaticConnection, connection_id: str): + """Send multiple messages and verify that get returns the correct content according to the limit and offset""" for i in range(6): with connection.next() as future_recip_message: sent_message = await asyncio.wait_for( @@ -149,3 +147,37 @@ async def test_get_limit_offset(connection: StaticConnection, connection_id: str "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/delete", } ) + + +@pytest.mark.asyncio +async def test_delete(connection: StaticConnection, connection_id: str): + """Send multiple messages, delete them, and verify that the messages count is zero""" + for i in range(6): + with connection.next() as future_recip_message: + sent_message = await asyncio.wait_for( + connection.send_and_await_reply_async( + { + "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/send", + "connection_id": connection_id, + "content": "Test Message #{}".format(i), + }, + return_route="all", + ), + timeout=60, + ) + recip_message = await asyncio.wait_for(future_recip_message, 60) + delete_message = await connection.send_and_await_reply_async( + { + "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/delete", + } + ) + get_messages = await connection.send_and_await_reply_async( + { + "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/get", + } + ) + assert ( + delete_message["@type"] + == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/deleted" + ) + assert get_messages["count"] == 0 diff --git a/int/tests/test_connections.py b/int/tests/test_connections.py index 32a8d35a..37d988ca 100644 --- a/int/tests/test_connections.py +++ b/int/tests/test_connections.py @@ -1,51 +1,27 @@ -"""Connection Tests""" +"""Connections Tests""" import asyncio import pytest from acapy_backchannel import Client from acapy_backchannel.api.connection import delete_connection, get_connections -import time +from aries_staticagent import Message -@pytest.mark.asyncio -async def clear_connections(client: Client): - """Clear all connections, if any.""" - connections = await get_connections.asyncio(client=client) +@pytest.fixture(autouse=True) +async def clear_connection_state(backchannel: Client, connection_id: str): + """Clear connections after each test.""" + yield + connections = await get_connections.asyncio(client=backchannel) for connection in connections.results: - if connection.state == "connection": + if connection.connection_id != connection_id: await delete_connection.asyncio( - client=client, conn_id=connection.connection_id + client=backchannel, conn_id=connection.connection_id ) - # return(connections) - - -@pytest.mark.asyncio -@pytest.fixture(autouse=True) -async def clear_connection_state(backchannel: Client): - """Clear invitations after each test.""" - # yield - # await clear_connections(backchannel) - yield await clear_connections(backchannel) - - -time.sleep(3) - - -# Temporary Test: before connection -@pytest.mark.asyncio -async def test_get_list_before_connection(connection): - get_list_before_connection = await connection.send_and_await_reply_async( - { - "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" - } - ) - print("get_list before connection: ", get_list_before_connection["connections"]) - assert True # False @pytest.mark.asyncio async def test_create_connection(connection): """Send an invitation and receive it to create a new connection""" - invitation = await connection.send_and_await_reply_async( + msg_invitation = Message( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-invitations/0.1/create", "alias": "Invitation I sent to Alice", @@ -53,37 +29,34 @@ async def test_create_connection(connection): "group": "admin", "auto_accept": True, "multi_use": True, - }, + } + ) + invitation = await connection.send_and_await_reply_async( + msg_invitation, + condition=lambda reply: reply.thread["thid"] == msg_invitation.id, return_route="all", ) - received = await connection.send_and_await_reply_async( + msg_received = Message( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/receive-invitation", "invitation": invitation["invitation_url"], "auto_accept": True, } ) + received = await connection.send_and_await_reply_async( + msg_received, condition=lambda reply: reply.thread["thid"] == msg_received.id + ) assert ( received["@type"] == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/connection" ) - - -# Temporary Test: after connection -@pytest.mark.asyncio -async def test_get_list_after_connection(connection): - get_list_after_connection = await connection.send_and_await_reply_async( - { - "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" - } - ) - print("get_list after connection: ", get_list_after_connection["connections"]) - assert True # False + assert received["label"] == msg_invitation["label"] @pytest.mark.asyncio async def test_get_list(connection): - invitation = await connection.send_and_await_reply_async( + """Create two connections and verify that their connection_ids are in connections list""" + msg_invitation = Message( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-invitations/0.1/create", "alias": "Invitation I sent to Alice", @@ -91,19 +64,24 @@ async def test_get_list(connection): "group": "admin", "auto_accept": True, "multi_use": True, - }, + } + ) + invitation = await connection.send_and_await_reply_async( + msg_invitation, + condition=lambda reply: reply.thread["thid"] == msg_invitation.id, return_route="all", ) - received = await connection.send_and_await_reply_async( + msg_received = Message( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/receive-invitation", "invitation": invitation["invitation_url"], "auto_accept": True, } ) - print("Invitation: ", invitation) - print("Received: ", received) - invitation2 = await connection.send_and_await_reply_async( + received = await connection.send_and_await_reply_async( + msg_received, condition=lambda reply: reply.thread["thid"] == msg_received.id + ) + msg_invitation2 = Message( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-invitations/0.1/create", "alias": "Second invitation I sent to Alice", @@ -111,16 +89,23 @@ async def test_get_list(connection): "group": "admin", "auto_accept": True, "multi_use": True, - }, + } + ) + invitation2 = await connection.send_and_await_reply_async( + msg_invitation2, + condition=lambda reply: reply.thread["thid"] == msg_invitation2.id, return_route="all", ) - received2 = await connection.send_and_await_reply_async( + msg_received2 = Message( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/receive-invitation", "invitation": invitation2["invitation_url"], "auto_accept": True, } ) + received2 = await connection.send_and_await_reply_async( + msg_received2, condition=lambda reply: reply.thread["thid"] == msg_received2.id + ) get_list = await connection.send_and_await_reply_async( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" @@ -130,12 +115,18 @@ async def test_get_list(connection): get_list["@type"] == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/list" ) + assert received["connection_id"] in [ + connection_item["connection_id"] for connection_item in get_list["connections"] + ] + assert received2["connection_id"] in [ + connection_item["connection_id"] for connection_item in get_list["connections"] + ] @pytest.mark.asyncio async def test_update(connection): - """Update connection attribute""" - invitation = await connection.send_and_await_reply_async( + """Test update of connection attribute""" + msg_invitation = Message( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-invitations/0.1/create", "alias": "Invitation I sent to Alice", @@ -143,17 +134,25 @@ async def test_update(connection): "group": "admin", "auto_accept": True, "multi_use": True, - }, + } + ) + invitation = await connection.send_and_await_reply_async( + msg_invitation, + condition=lambda reply: reply.thread["thid"] == msg_invitation.id, return_route="all", ) - received = await connection.send_and_await_reply_async( + msg_received = Message( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/receive-invitation", "invitation": invitation["invitation_url"], "auto_accept": True, } ) - update = await connection.send_and_await_reply_async( + received = await connection.send_and_await_reply_async( + msg_received, + condition=lambda reply: reply.thread["thid"] == msg_received.id, + ) + msg_update = Message( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/update", "connection_id": received["connection_id"], @@ -161,12 +160,18 @@ async def test_update(connection): "role": "Updated role", } ) + update = await connection.send_and_await_reply_async( + msg_update, + condition=lambda reply: reply.thread["thid"] == msg_update.id, + ) assert update["label"] == "Updated label" @pytest.mark.asyncio async def test_delete(connection): - invitation = await connection.send_and_await_reply_async( + """Create an invitation, delete it, and verify that its label and connectio_id + is no longer in the connections list""" + invitation_msg = Message( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-invitations/0.1/create", "alias": "Invitation I sent to Alice", @@ -174,25 +179,23 @@ async def test_delete(connection): "group": "admin", "auto_accept": True, "multi_use": True, - }, + } + ) + invitation = await connection.send_and_await_reply_async( + invitation_msg, + condition=lambda reply: reply.thread["thid"] == invitation_msg.id, return_route="all", ) - received = await connection.send_and_await_reply_async( + msg_received = Message( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/receive-invitation", "invitation": invitation["invitation_url"], "auto_accept": True, } ) - get_list_beforedelete = await connection.send_and_await_reply_async( - { - "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" - } - ) - print("Connections before delete: ", get_list_beforedelete["connections"]) - assert ( - received["@type"] - == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/connection" + received = await connection.send_and_await_reply_async( + msg_received, + condition=lambda reply: reply.thread["thid"] == msg_received.id, ) delete_connection = await connection.send_and_await_reply_async( { @@ -200,16 +203,19 @@ async def test_delete(connection): "connection_id": received["connection_id"], } ) - get_list_afterdelete = await connection.send_and_await_reply_async( - { - "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" - } - ) - print("List after delete", get_list_afterdelete["connections"]) - # for i in get_list_beforedelete["connections"]: - # if i not in get_list_afterdelete["connections"]: - # print(i) assert ( delete_connection["@type"] == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/deleted" ) + assert delete_connection["connection_id"] == received["connection_id"] + get_list = await connection.send_and_await_reply_async( + { + "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" + } + ) + assert invitation_msg["label"] not in [ + connection_item["label"] for connection_item in get_list["connections"] + ] + assert received["connection_id"] not in [ + connection_item["connection_id"] for connection_item in get_list["connections"] + ] diff --git a/int/tests/test_invitations.py b/int/tests/test_invitations.py index 401474da..6a3adeb9 100644 --- a/int/tests/test_invitations.py +++ b/int/tests/test_invitations.py @@ -1,39 +1,24 @@ -"""Example tests.""" +"""Invitations tests""" import pytest - from acapy_backchannel import Client from acapy_backchannel.api.connection import delete_connection, get_connections -async def clear_invitations(client: Client): - """Clear all invitations, if any.""" - connections = await get_connections.asyncio(client=client) +@pytest.fixture(autouse=True) +async def clear_invitation_state(backchannel: Client, connection_id: str): + """Clear invitation after each test.""" + yield + connections = await get_connections.asyncio(client=backchannel) for connection in connections.results: if connection.state == "invitation": await delete_connection.asyncio( - client=client, conn_id=connection.connection_id + client=backchannel, conn_id=connection.connection_id ) -@pytest.fixture(autouse=True) -async def clear_invitation_state(backchannel: Client): - """Clear invitations after each test.""" - # We don't need to do any setup tasks for this fixture. - # Normally we would do some setup to create a value and then yield it for - # use in the test method. This fixture is special in that it doesn't require - # that setup and does not need to yield a value for use in a test method. - # Just need to clear state that may have been triggered by the test method. - - yield - - # Everything that follows the yield is executed after the test method and - # is where we perform tear down. - - await clear_invitations(backchannel) - - @pytest.mark.asyncio async def test_create_invitation(connection): + """Test create invitation protocol""" reply = await connection.send_and_await_reply_async( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-invitations/0.1/create", @@ -54,6 +39,7 @@ async def test_create_invitation(connection): @pytest.mark.asyncio async def test_get_list(connection): + """Test get list protocol""" reply = await connection.send_and_await_reply_async( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-invitations/0.1/get-list" @@ -68,9 +54,9 @@ async def test_get_list(connection): @pytest.mark.asyncio async def test_num_results(connection): + """Test that the create message protocol causes new item in results list""" # Input number of messages to add to the list - added_num = 1 - # Add new messages + added_num = 2 for i in range(added_num): await connection.send_and_await_reply_async( { @@ -83,7 +69,6 @@ async def test_num_results(connection): }, return_route="all", ) - # Retrieve results of invitations list to verify that create message causes new item in results list reply = await connection.send_and_await_reply_async( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-invitations/0.1/get-list" @@ -95,6 +80,7 @@ async def test_num_results(connection): @pytest.mark.asyncio async def test_empty_list(connection): + """Test that get-list returns no results if no create messages have been sent""" reply = await connection.send_and_await_reply_async( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-invitations/0.1/get-list"