generated from nationalarchives/da-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #674 from nationalarchives/AYR-1348/page-view-logs
AYR-1348 - Page view logs
- Loading branch information
Showing
5 changed files
with
105 additions
and
40 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import json | ||
from functools import wraps | ||
|
||
from flask import current_app, request, session | ||
|
||
|
||
def log_page_view(route_function): | ||
@wraps(route_function) | ||
def wrapper(*args, **kwargs): | ||
user_id = session.get("user_id", "anonymous") | ||
log_data = { | ||
"event": "api_request", | ||
"user_id": user_id, | ||
"route": request.path, | ||
"method": request.method, | ||
} | ||
current_app.audit_logger.info(json.dumps(log_data)) | ||
return route_function(*args, **kwargs) | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import pytest | ||
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", | ||
[ | ||
( | ||
"/test_route", | ||
"GET", | ||
{"user_id": "test_user"}, | ||
lambda: "Test Response", | ||
b"Test Response", | ||
'{"event": "api_request", "user_id": "test_user", "route": "/test_route", "method": "GET"}', | ||
), | ||
( | ||
"/anonymous_route", | ||
"GET", | ||
{}, | ||
lambda: "Anonymous Response", | ||
b"Anonymous Response", | ||
'{"event": "api_request", "user_id": "anonymous", "route": "/anonymous_route", "method": "GET"}', | ||
), | ||
( | ||
"/post_route", | ||
"POST", | ||
{"user_id": "test_user"}, | ||
lambda: "Post Response", | ||
b"Post Response", | ||
'{"event": "api_request", "user_id": "test_user", "route": "/post_route", "method": "POST"}', | ||
), | ||
], | ||
) | ||
@mock_aws | ||
def test_log_page_view( | ||
app, | ||
client: FlaskClient, | ||
route_path, | ||
method, | ||
session_object, | ||
route_function, | ||
expected_response, | ||
expected_log, | ||
caplog, | ||
): | ||
"""Test that the log_page_view middleware generates the correct log when an | ||
endpoint decorated with it is requested""" | ||
|
||
@app.route(route_path, methods=[method]) | ||
@log_page_view | ||
def dynamic_route(): | ||
return route_function() | ||
|
||
with client.session_transaction() as session: | ||
session.update(session_object) | ||
|
||
response = client.open(route_path, method=method) | ||
|
||
assert response.status_code == 200 | ||
assert response.data == expected_response | ||
|
||
assert caplog.records[0].levelname == "INFO" | ||
assert caplog.records[0].message == expected_log |