diff --git a/api_notify_test.py b/api_notify_test.py new file mode 100644 index 0000000000000000000000000000000000000000..164ee0ae949b11164a07ccbcc08c955ebae31ee8 --- /dev/null +++ b/api_notify_test.py @@ -0,0 +1,59 @@ +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 notify +from conftest import get_auth_header + + +@pytest.mark.usefixtures( + "mock_clients", "mock_client_credentials", "mock_post_notify", "mock_message_notify" +) +def test_post_notify( + mock_clients, mock_client_credentials, mock_post_notify, mock_message_notify +): + app = create_app() + with patch( + "builtins.open", mock_open(read_data=json.dumps(mock_clients)) + ) as mock_file_open, patch.object( + notify, "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( + "/notify", json=mock_post_notify, headers=auth_header + ) + assert response.status_code == 200 + mock_write_to_queue.assert_called_once_with( + queue_name="client-1-broadcast", msg=json.dumps(mock_message_notify) + ) + + +@pytest.mark.usefixtures("mock_clients", "mock_post_notify") +def test_post_send_no_token(mock_clients, mock_post_notify): + app = create_app() + with patch( + "builtins.open", mock_open(read_data=json.dumps(mock_clients)) + ) as mock_file_open, patch.object( + notify, "write_to_queue" + ) as mock_write_to_queue, app.test_client() as app_test_client: + response = app_test_client.post("/notify", json=mock_post_notify) + assert response.status_code == 403 + mock_write_to_queue.assert_not_called() + + +@pytest.mark.usefixtures("mock_clients", "mock_post_notify") +def test_post_send_invalid_token(mock_clients, mock_post_notify): + app = create_app() + with patch( + "builtins.open", mock_open(read_data=json.dumps(mock_clients)) + ) as mock_file_open, patch.object( + notify, "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( + "/notify", json=mock_post_notify, headers=auth_header + ) + assert response.status_code == 403 + mock_write_to_queue.assert_not_called() diff --git a/api_receive_test.py b/api_receive_test.py new file mode 100644 index 0000000000000000000000000000000000000000..cee13f44a1dc3f7b6ac9c5a8237d760519adc887 --- /dev/null +++ b/api_receive_test.py @@ -0,0 +1,98 @@ +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 receive +from conftest import get_auth_header + + +@pytest.mark.usefixtures( + "mock_clients", "mock_client_credentials", "mock_read_from_queue_return" +) +def test_get_receive( + mock_clients, mock_client_credentials, mock_read_from_queue_return +): + app = create_app() + with patch( + "builtins.open", mock_open(read_data=json.dumps(mock_clients)) + ) as mock_file_open, patch.object( + receive, "read_from_queue" + ) as mock_read_from_queue, app.test_client() as app_test_client: + mock_read_from_queue.return_value = mock_read_from_queue_return + auth_header = get_auth_header(app_test_client, mock_client_credentials) + response = app_test_client.get( + "/receive", headers=auth_header + ) + assert response.status_code == 200 + mock_read_from_queue.assert_called_once_with( + queue_name="client-1-inbox", max_msgs=10 + ) + assert response.json == mock_read_from_queue_return + + +@pytest.mark.usefixtures( + "mock_clients", "mock_client_credentials", "mock_read_from_queue_return" +) +def test_get_receive_max_messages( + mock_clients, mock_client_credentials, mock_read_from_queue_return +): + app = create_app() + with patch( + "builtins.open", mock_open(read_data=json.dumps(mock_clients)) + ) as mock_file_open, patch.object( + receive, "read_from_queue" + ) as mock_read_from_queue, app.test_client() as app_test_client: + mock_read_from_queue.return_value = mock_read_from_queue_return + auth_header = get_auth_header(app_test_client, mock_client_credentials) + max_messages = 2 + response = app_test_client.get( + "/receive", query_string={ "max_messages": max_messages }, headers=auth_header + ) + assert response.status_code == 200 + mock_read_from_queue.assert_called_once_with( + queue_name="client-1-inbox", max_msgs=max_messages + ) + assert response.json == mock_read_from_queue_return + + +@pytest.mark.usefixtures( + "mock_clients", "mock_read_from_queue_return" +) +def test_get_receive_no_token( + mock_clients, mock_read_from_queue_return +): + app = create_app() + with patch( + "builtins.open", mock_open(read_data=json.dumps(mock_clients)) + ) as mock_file_open, patch.object( + receive, "read_from_queue" + ) as mock_read_from_queue, app.test_client() as app_test_client: + mock_read_from_queue.return_value = mock_read_from_queue_return + response = app_test_client.get( + "/receive" + ) + assert response.status_code == 403 + mock_read_from_queue.assert_not_called() + + +@pytest.mark.usefixtures( + "mock_clients", "mock_read_from_queue_return" +) +def test_get_receive_invalid_token( + mock_clients, mock_read_from_queue_return +): + app = create_app() + with patch( + "builtins.open", mock_open(read_data=json.dumps(mock_clients)) + ) as mock_file_open, patch.object( + receive, "read_from_queue" + ) as mock_read_from_queue, app.test_client() as app_test_client: + mock_read_from_queue.return_value = mock_read_from_queue_return + auth_header = {"Authorization": "made-up-token"} + response = app_test_client.get( + "/receive", headers=auth_header + ) + assert response.status_code == 403 + mock_read_from_queue.assert_not_called() diff --git a/conftest.py b/conftest.py index c6fcacdd262cb0ed24a75f24f7efd7deaabffebe..bf5c70d672a87141879e4ff2e2c4b450ce6bb34b 100644 --- a/conftest.py +++ b/conftest.py @@ -88,5 +88,15 @@ def mock_post_notify(): @pytest.fixture def mock_message_notify(): - post = posts()["send"] - return {"message": post["body"]} + post = posts()["notify"] + return {"topic": "broadcast", "message": post["body"]} + + +@pytest.fixture +def mock_read_from_queue_return(): + return [ + { + "topic": "soar.client-1.something", + "message": "this is a pub/sub message from client-1" + } + ] diff --git a/endpoints/receive.py b/endpoints/receive.py index bc299fa05c09c68b37e3104ef46596f390dede9c..8c892caefd9aabe6cd468c22eba0356b4b185c58 100644 --- a/endpoints/receive.py +++ b/endpoints/receive.py @@ -22,7 +22,8 @@ class Receive(AuthResource): if errors: abort(400, message=str(errors)) - max_messages = request.args.get("max_messages", 10) + # force query string parameter value into int + max_messages = int(request.args.get("max_messages", 10)) allow = self.auth(request) if allow: