diff --git a/app/tests/test_download.py b/app/tests/test_download.py index f37d770a..07d286f9 100644 --- a/app/tests/test_download.py +++ b/app/tests/test_download.py @@ -76,8 +76,8 @@ def test_download_record_standard_user_with_citable_reference_with_file_extensio msg = json.dumps({"user_id": user_id, "file": key}) - assert caplog.records[0].levelname == "INFO" - assert caplog.records[0].message == msg + assert caplog.records[1].levelname == "INFO" + assert caplog.records[1].message == msg @mock_aws def test_download_record_standard_user_with_citable_reference_without_file_extension( @@ -119,8 +119,8 @@ def test_download_record_standard_user_with_citable_reference_without_file_exten msg = json.dumps({"user_id": user_id, "file": key}) - assert caplog.records[0].levelname == "INFO" - assert caplog.records[0].message == msg + assert caplog.records[1].levelname == "INFO" + assert caplog.records[1].message == msg @mock_aws def test_download_record_standard_user_without_citable_reference( @@ -162,8 +162,8 @@ def test_download_record_standard_user_without_citable_reference( msg = json.dumps({"user_id": user_id, "file": key}) - assert caplog.records[0].levelname == "INFO" - assert caplog.records[0].message == msg + assert caplog.records[1].levelname == "INFO" + assert caplog.records[1].message == msg @mock_aws def test_download_record_standard_user_get_file_errors( @@ -226,8 +226,8 @@ def test_download_record_standard_user_read_file_error( msg = "Error reading S3 file content: Read error" assert response.status_code == 500 - assert caplog.records[0].levelname == "ERROR" - assert caplog.records[0].message == msg + assert caplog.records[1].levelname == "ERROR" + assert caplog.records[1].message == msg @mock_aws def test_raises_404_for_standard_user_without_access_to_files_transferring_body( diff --git a/app/tests/test_middlewares.py b/app/tests/test_middlewares.py index e6fdc552..ea1f65f5 100644 --- a/app/tests/test_middlewares.py +++ b/app/tests/test_middlewares.py @@ -1,15 +1,12 @@ -import json -from unittest.mock import MagicMock - import pytest -from flask import current_app from flask.testing import FlaskClient +from moto import mock_aws from app.main.middlewares.log_page_view import log_page_view @pytest.mark.parametrize( - "route_path, method, session_object, route_function, expected_response, expected_log_data", + "route_path, method, session_object, route_function, expected_response, expected_log", [ ( "/test_route", @@ -17,12 +14,7 @@ {"user_id": "test_user"}, lambda: "Test Response", b"Test Response", - { - "event": "page_view", - "user_id": "test_user", - "route": "/test_route", - "method": "GET", - }, + '{"event": "page_view", "user_id": "test_user", "route": "/test_route", "method": "GET"}', ), ( "/anonymous_route", @@ -30,12 +22,7 @@ {}, lambda: "Anonymous Response", b"Anonymous Response", - { - "event": "page_view", - "user_id": "anonymous", - "route": "/anonymous_route", - "method": "GET", - }, + '{"event": "page_view", "user_id": "anonymous", "route": "/anonymous_route", "method": "GET"}', ), ( "/post_route", @@ -43,15 +30,11 @@ {"user_id": "test_user"}, lambda: "Post Response", b"Post Response", - { - "event": "page_view", - "user_id": "test_user", - "route": "/post_route", - "method": "POST", - }, + '{"event": "page_view", "user_id": "test_user", "route": "/post_route", "method": "POST"}', ), ], ) +@mock_aws def test_log_page_view( app, client: FlaskClient, @@ -60,18 +43,15 @@ def test_log_page_view( session_object, route_function, expected_response, - expected_log_data, + expected_log, + caplog, ): - mock_logger = MagicMock() @app.route(route_path, methods=[method]) @log_page_view def dynamic_route(): return route_function() - with app.app_context(): - current_app.audit_logger = mock_logger - with client.session_transaction() as session: session.update(session_object) @@ -80,4 +60,5 @@ def dynamic_route(): assert response.status_code == 200 assert response.data == expected_response - mock_logger.info.assert_called_once_with(json.dumps(expected_log_data)) + assert caplog.records[0].levelname == "INFO" + assert caplog.records[0].message == expected_log