From 96bf1288c7a39e68c1d770ddfe38ea9cddcd78e2 Mon Sep 17 00:00:00 2001 From: Olivier Desenfans Date: Tue, 22 Aug 2023 11:42:34 +0200 Subject: [PATCH] Internal: add test for POST /messages with sync --- tests/api/test_p2p.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/api/test_p2p.py b/tests/api/test_p2p.py index 1ea4121bb..ee352319d 100644 --- a/tests/api/test_p2p.py +++ b/tests/api/test_p2p.py @@ -5,6 +5,7 @@ from configmanager import Config P2P_PUB_URI = "/api/v0/p2p/pubsub/pub" +POST_MESSAGES_URI = "/api/v0/messages" MESSAGE_DICT = { "chain": "NULS2", @@ -63,3 +64,37 @@ async def test_pubsub_pub_errors(ccn_api_client, mock_config: Config): P2P_PUB_URI, json={"topic": message_topic, "data": json.dumps(message_dict)} ) assert response.status == 422, await response.text() + + +@pytest.mark.asyncio +async def test_post_message_sync(ccn_api_client, mocker): + # Mock the functions used to create the RabbitMQ queue + mocker.patch("aleph.web.controllers.p2p.get_mq_channel_from_request") + mocked_queue = mocker.patch( + "aleph.web.controllers.p2p.mq_make_aleph_message_topic_queue" + ) + + # Create a mock MQ response object + mock_mq_message = mocker.Mock() + mock_mq_message.routing_key = f"processed.{MESSAGE_DICT['item_hash']}" + mocker.patch( + "aleph.web.controllers.p2p._mq_read_one_message", return_value=mock_mq_message + ) + + response = await ccn_api_client.post( + POST_MESSAGES_URI, + json={ + "message": MESSAGE_DICT, + "sync": True, + }, + ) + + assert response.status == 200, await response.text() + json_response = await response.json() + pub_status = json_response["publication_status"] + assert json_response["message_status"] == "processed" + assert pub_status["status"] == "success" + assert pub_status["failed"] == [] + + # Check that we cleaned up the queue + assert mocked_queue.delete.called_once