import flask import json import pytest from unittest.mock import patch, mock_open, call from werkzeug.exceptions import HTTPException from api import create_app from endpoints import send from conftest import get_auth_header @pytest.mark.usefixtures( "mock_clients", "mock_client_credentials", "mock_post_send", "mock_message_send" ) def test_post_send( mock_clients, mock_client_credentials, mock_post_send, mock_message_send ): app = create_app() with patch( "builtins.open", mock_open(read_data=json.dumps(mock_clients)) ) as mock_file_open, patch.object( send, "write_to_queue" ) as mock_write_to_queue, app.test_client() as app_test_client: auth_header = get_auth_header(app_test_client, mock_client_credentials) response = app_test_client.post( "/send", json=mock_post_send, headers=auth_header ) assert response.status_code == 200 mock_write_to_queue.assert_called_once_with( queue_name="client-1-outbox", msg=json.dumps(mock_message_send) ) @pytest.mark.usefixtures("mock_clients", "mock_post_send") def test_post_send_no_token(mock_clients, mock_post_send): app = create_app() with patch( "builtins.open", mock_open(read_data=json.dumps(mock_clients)) ) as mock_file_open, patch.object( send, "write_to_queue" ) as mock_write_to_queue, app.test_client() as app_test_client: response = app_test_client.post("/send", json=mock_post_send) assert response.status_code == 403 mock_write_to_queue.assert_not_called() @pytest.mark.usefixtures("mock_clients", "mock_post_send") def test_post_send_invalid_token(mock_clients, mock_post_send): app = create_app() with patch( "builtins.open", mock_open(read_data=json.dumps(mock_clients)) ) as mock_file_open, patch.object( send, "write_to_queue" ) as mock_write_to_queue, app.test_client() as app_test_client: auth_header = {"Authorization": "made-up-token"} response = app_test_client.post( "/send", json=mock_post_send, headers=auth_header ) assert response.status_code == 403 mock_write_to_queue.assert_not_called()