Skip to content

Commit

Permalink
fixed unit tests and removed logger mock in favor of caplog
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrasca committed Nov 29, 2024
1 parent 336da12 commit 2e079f2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
16 changes: 8 additions & 8 deletions app/tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
39 changes: 10 additions & 29 deletions app/tests/test_middlewares.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,40 @@
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",
"GET",
{"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",
"GET",
{},
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",
"POST",
{"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,
Expand All @@ -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)

Expand All @@ -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

0 comments on commit 2e079f2

Please sign in to comment.