Skip to content

Commit

Permalink
Internal: add test for POST /messages with sync (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
odesenfans authored Aug 22, 2023
1 parent 4c50268 commit 1630ca7
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/api/test_p2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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

0 comments on commit 1630ca7

Please sign in to comment.