Skip to content

Commit 1630ca7

Browse files
authored
Internal: add test for POST /messages with sync (#464)
1 parent 4c50268 commit 1630ca7

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tests/api/test_p2p.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from configmanager import Config
66

77
P2P_PUB_URI = "/api/v0/p2p/pubsub/pub"
8+
POST_MESSAGES_URI = "/api/v0/messages"
89

910
MESSAGE_DICT = {
1011
"chain": "NULS2",
@@ -63,3 +64,37 @@ async def test_pubsub_pub_errors(ccn_api_client, mock_config: Config):
6364
P2P_PUB_URI, json={"topic": message_topic, "data": json.dumps(message_dict)}
6465
)
6566
assert response.status == 422, await response.text()
67+
68+
69+
@pytest.mark.asyncio
70+
async def test_post_message_sync(ccn_api_client, mocker):
71+
# Mock the functions used to create the RabbitMQ queue
72+
mocker.patch("aleph.web.controllers.p2p.get_mq_channel_from_request")
73+
mocked_queue = mocker.patch(
74+
"aleph.web.controllers.p2p.mq_make_aleph_message_topic_queue"
75+
)
76+
77+
# Create a mock MQ response object
78+
mock_mq_message = mocker.Mock()
79+
mock_mq_message.routing_key = f"processed.{MESSAGE_DICT['item_hash']}"
80+
mocker.patch(
81+
"aleph.web.controllers.p2p._mq_read_one_message", return_value=mock_mq_message
82+
)
83+
84+
response = await ccn_api_client.post(
85+
POST_MESSAGES_URI,
86+
json={
87+
"message": MESSAGE_DICT,
88+
"sync": True,
89+
},
90+
)
91+
92+
assert response.status == 200, await response.text()
93+
json_response = await response.json()
94+
pub_status = json_response["publication_status"]
95+
assert json_response["message_status"] == "processed"
96+
assert pub_status["status"] == "success"
97+
assert pub_status["failed"] == []
98+
99+
# Check that we cleaned up the queue
100+
assert mocked_queue.delete.called_once

0 commit comments

Comments
 (0)