diff --git a/rpc/integration/tests/__init__.py b/rpc/integration/tests/__init__.py index 6224ca13d..b32937cea 100644 --- a/rpc/integration/tests/__init__.py +++ b/rpc/integration/tests/__init__.py @@ -52,6 +52,12 @@ class Agent: def __init__(self, url: str): self.url = url + @unwrap_json_response + @fail_if_not_ok("Failed to get connections") + def get_connections(self, **kwargs): + """Get connections.""" + return get(self.url, "/connections", params=kwargs) + @unwrap_json_response @fail_if_not_ok("Create invitation failed") def create_invitation(self, **kwargs): @@ -75,6 +81,30 @@ def accept_invite(self, connection_id: str): f"/connections/{connection_id}/accept-invitation", ) + @unwrap_json_response + @fail_if_not_ok("Failed to send DRPC request") + def send_drpc_request(self, **kwargs): + """Send DRPC request.""" + return post(self.url, "/drpc/request", json=kwargs) + + @unwrap_json_response + @fail_if_not_ok("Failed to send DRPC response") + def send_drpc_response(self, **kwargs): + """Send DRPC response.""" + return post(self.url, "/drpc/response", json=kwargs) + + @unwrap_json_response + @fail_if_not_ok("Failed to get DRPC record list") + def get_drpc_records(self, **kwargs): + """Get DRPC record list.""" + return get(self.url, f"/drpc/records", params=kwargs) + + @unwrap_json_response + @fail_if_not_ok("Failed to get DRPC record") + def get_drpc_record(self, record_id: str): + """Get DRPC record.""" + return get(self.url, f"/drpc/records/{record_id}") + @unwrap_json_response @fail_if_not_ok("Failed to send basic message") def send_message(self, connection_id, content): @@ -98,7 +128,7 @@ def get(self, path: str, return_json: bool = True, fail_with: str = None, **kwar def post( self, path: str, return_json: bool = True, fail_with: str = None, **kwargs ): - """Do get to agent endpoint.""" + """Do post to agent endpoint.""" wrapped_post = post if fail_with: wrapped_post = fail_if_not_ok(fail_with)(wrapped_post) diff --git a/rpc/integration/tests/test_example.py b/rpc/integration/tests/test_example.py deleted file mode 100644 index b24186106..000000000 --- a/rpc/integration/tests/test_example.py +++ /dev/null @@ -1,31 +0,0 @@ -import pytest -import time - -from . import Agent, BOB, ALICE - - -@pytest.fixture(scope="session") -def bob(): - """bob agent fixture.""" - yield Agent(BOB) - - -@pytest.fixture(scope="session") -def alice(): - """resolver agent fixture.""" - yield Agent(ALICE) - - -@pytest.fixture(scope="session", autouse=True) -def established_connection(bob, alice): - """Established connection filter.""" - invite = bob.create_invitation(auto_accept="true")["invitation"] - resp = alice.receive_invite(invite, auto_accept="true") - yield resp["connection_id"] - - -def test_send_message(bob, alice, established_connection): - # make sure connection is active... - time.sleep(1) - - alice.send_message(established_connection, "hello bob") diff --git a/rpc/integration/tests/test_rpc.py b/rpc/integration/tests/test_rpc.py new file mode 100644 index 000000000..afbe178ac --- /dev/null +++ b/rpc/integration/tests/test_rpc.py @@ -0,0 +1,170 @@ +import pytest +import time + +from . import Agent, BOB, ALICE +from unittest.mock import patch + +rpc_request = {"jsonrpc": "2.0", "method": "add", "params": [1, 2], "id": 1} +rpc_response = {"jsonrpc": "2.0", "result": 3, "id": 1} +rpc_error = { + "jsonrpc": "2.0", + "error": {"code": -32601, "message": "Method not found"}, + "id": 1, +} + + +@pytest.fixture(scope="session") +def bob(): + """bob agent fixture.""" + yield Agent(BOB) + + +@pytest.fixture(scope="session") +def alice(): + """resolver agent fixture.""" + yield Agent(ALICE) + + +@pytest.fixture(scope="session", autouse=True) +def established_connection(bob, alice): + """Established connection filter.""" + invite = bob.create_invitation(auto_accept="true")["invitation"] + resp = alice.receive_invite(invite, auto_accept="true") + yield resp["connection_id"] + + +def test_drpc_request(bob, alice, established_connection): + # make sure connection is active... + time.sleep(1) + + alice_drpc_request = alice.send_drpc_request( + connection_id=established_connection, + request=rpc_request, + ) + assert True + + # make sure messages are exchanged... + time.sleep(1) + + bob_drpc_request_records = bob.get_drpc_records(thread_id=alice_drpc_request["@id"]) + bob_drpc_request_record = bob_drpc_request_records["results"][0] + tags = bob_drpc_request_record["tags"] + assert len(bob_drpc_request_records["results"]) == 1 + assert bob_drpc_request_record["state"] == "request-received" + assert tags["thread_id"] == alice_drpc_request["@id"] + assert "request" in bob_drpc_request_record + assert bob_drpc_request_record["request"] == rpc_request + + +def test_drpc_response(bob, alice, established_connection): + # make sure connection is active... + time.sleep(1) + + alice_drpc_request = alice.send_drpc_request( + connection_id=established_connection, + request=rpc_request, + ) + assert True + + # make sure messages are exchanged... + time.sleep(1) + + bob_connections = bob.get_connections(state="active") + bob_connection = bob_connections["results"][0] + bob_connection_id = bob_connection["connection_id"] + bob_drpc_response = bob.send_drpc_response( + connection_id=bob_connection_id, + thread_id=alice_drpc_request["@id"], + response=rpc_response, + ) + assert True + + # make sure messages are exchanged... + time.sleep(1) + + alice_drpc_response_records = alice.get_drpc_records( + connection_id=established_connection, thread_id=alice_drpc_request["@id"] + ) + alice_drpc_response_record = alice_drpc_response_records["results"][0] + alice_tags = alice_drpc_response_record["tags"] + alice_thread_id = alice_tags["thread_id"] + + bob_drpc_request_records = bob.get_drpc_records( + connection_id=bob_connection_id, thread_id=alice_drpc_request["@id"] + ) + bob_drpc_request_record = bob_drpc_request_records["results"][0] + bob_tags = bob_drpc_request_record["tags"] + bob_thread_id = bob_tags["thread_id"] + + assert len(alice_drpc_response_records["results"]) == 1 + assert alice_thread_id == bob_drpc_response["~thread"]["thid"] + assert alice_drpc_response_record["state"] == "completed" + assert "request" in alice_drpc_response_record + assert "response" in alice_drpc_response_record + assert alice_drpc_response_record["request"] == rpc_request + assert alice_drpc_response_record["response"] == rpc_response + + assert len(bob_drpc_request_records["results"]) == 1 + assert bob_thread_id == alice_drpc_request["@id"] + assert bob_drpc_request_record["state"] == "completed" + assert "request" in bob_drpc_request_record + assert "response" in bob_drpc_request_record + assert bob_drpc_request_record["request"] == rpc_request + assert bob_drpc_request_record["response"] == rpc_response + + +def test_drpc_response_error(bob, alice, established_connection): + # make sure connection is active... + time.sleep(1) + + alice_drpc_request = alice.send_drpc_request( + connection_id=established_connection, + request=rpc_request, + ) + assert True + + # make sure messages are exchanged... + time.sleep(1) + + bob_connections = bob.get_connections(state="active") + bob_connection = bob_connections["results"][0] + bob_connection_id = bob_connection["connection_id"] + bob_drpc_response = bob.send_drpc_response( + connection_id=bob_connection_id, + thread_id=alice_drpc_request["@id"], + response=rpc_error, + ) + assert True + + # make sure messages are exchanged... + time.sleep(1) + + alice_drpc_response_records = alice.get_drpc_records( + connection_id=established_connection, thread_id=alice_drpc_request["@id"] + ) + alice_drpc_response_record = alice_drpc_response_records["results"][0] + alice_tags = alice_drpc_response_record["tags"] + alice_thread_id = alice_tags["thread_id"] + + bob_drpc_request_records = bob.get_drpc_records( + connection_id=bob_connection_id, thread_id=alice_drpc_request["@id"] + ) + bob_drpc_request_record = bob_drpc_request_records["results"][0] + bob_tags = bob_drpc_request_record["tags"] + bob_thread_id = bob_tags["thread_id"] + + assert len(alice_drpc_response_records["results"]) == 1 + assert alice_thread_id == bob_drpc_response["~thread"]["thid"] + assert alice_drpc_response_record["state"] == "completed" + assert "request" in alice_drpc_response_record + assert "response" in alice_drpc_response_record + assert alice_drpc_response_record["request"] == rpc_request + assert alice_drpc_response_record["response"] == rpc_error + + assert len(bob_drpc_request_records["results"]) == 1 + assert bob_thread_id == alice_drpc_request["@id"] + assert bob_drpc_request_record["state"] == "completed" + assert "request" in bob_drpc_request_record + assert "response" in bob_drpc_request_record + assert bob_drpc_request_record["request"] == rpc_request + assert bob_drpc_request_record["response"] == rpc_error