mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-29 13:49:57 +00:00
filter out logs posting http logs
This commit is contained in:
16
tests/ahriman/core/log/conftest.py
Normal file
16
tests/ahriman/core/log/conftest.py
Normal file
@ -0,0 +1,16 @@
|
||||
import logging
|
||||
import pytest
|
||||
|
||||
from ahriman.core.log.filtered_access_logger import FilteredAccessLogger
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def filtered_access_logger() -> FilteredAccessLogger:
|
||||
"""
|
||||
fixture for custom access logger
|
||||
|
||||
Returns:
|
||||
FilteredAccessLogger: custom access logger test instance
|
||||
"""
|
||||
logger = logging.getLogger()
|
||||
return FilteredAccessLogger(logger)
|
71
tests/ahriman/core/log/test_filtered_access_logger.py
Normal file
71
tests/ahriman/core/log/test_filtered_access_logger.py
Normal file
@ -0,0 +1,71 @@
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from ahriman.core.log.filtered_access_logger import FilteredAccessLogger
|
||||
|
||||
|
||||
def test_is_logs_post() -> None:
|
||||
"""
|
||||
must correctly define if request belongs to logs posting
|
||||
"""
|
||||
request = MagicMock()
|
||||
|
||||
request.method = "POST"
|
||||
request.path = "/api/v1/packages/ahriman/logs"
|
||||
assert FilteredAccessLogger.is_logs_post(request)
|
||||
|
||||
request.method = "POST"
|
||||
request.path = "/api/v1/packages/linux-headers/logs"
|
||||
assert FilteredAccessLogger.is_logs_post(request)
|
||||
|
||||
request.method = "POST"
|
||||
request.path = "/api/v1/packages/memtest86+/logs"
|
||||
assert FilteredAccessLogger.is_logs_post(request)
|
||||
|
||||
request.method = "POST"
|
||||
request.path = "/api/v1/packages/memtest86%2B/logs"
|
||||
assert FilteredAccessLogger.is_logs_post(request)
|
||||
|
||||
request.method = "POST"
|
||||
request.path = "/api/v1/packages/python2.7/logs"
|
||||
assert FilteredAccessLogger.is_logs_post(request)
|
||||
|
||||
request.method = "GET"
|
||||
request.path = "/api/v1/packages/ahriman/logs"
|
||||
assert not FilteredAccessLogger.is_logs_post(request)
|
||||
|
||||
request.method = "POST"
|
||||
request.path = "/api/v1/packages/ahriman"
|
||||
assert not FilteredAccessLogger.is_logs_post(request)
|
||||
|
||||
request.method = "POST"
|
||||
request.path = "/api/v1/packages/ahriman/logs/random/path/after"
|
||||
assert not FilteredAccessLogger.is_logs_post(request)
|
||||
|
||||
|
||||
def test_log(filtered_access_logger: FilteredAccessLogger, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must emit log record
|
||||
"""
|
||||
request_mock = MagicMock()
|
||||
response_mock = MagicMock()
|
||||
is_log_path_mock = mocker.patch("ahriman.core.log.filtered_access_logger.FilteredAccessLogger.is_logs_post",
|
||||
return_value=False)
|
||||
log_mock = mocker.patch("aiohttp.web_log.AccessLogger.log")
|
||||
|
||||
filtered_access_logger.log(request_mock, response_mock, 0.001)
|
||||
is_log_path_mock.assert_called_once_with(request_mock)
|
||||
log_mock.assert_called_once_with(filtered_access_logger, request_mock, response_mock, 0.001)
|
||||
|
||||
|
||||
def test_log_filter_logs(filtered_access_logger: FilteredAccessLogger, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must skip log record in case if it is from logs posting
|
||||
"""
|
||||
request_mock = MagicMock()
|
||||
response_mock = MagicMock()
|
||||
mocker.patch("ahriman.core.log.filtered_access_logger.FilteredAccessLogger.is_logs_post", return_value=True)
|
||||
log_mock = mocker.patch("aiohttp.web_log.AccessLogger.log")
|
||||
|
||||
filtered_access_logger.log(request_mock, response_mock, 0.001)
|
||||
log_mock.assert_not_called()
|
@ -4,6 +4,7 @@ from aiohttp import web
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.core.exceptions import InitializeError
|
||||
from ahriman.core.log.filtered_access_logger import FilteredAccessLogger
|
||||
from ahriman.core.status.watcher import Watcher
|
||||
from ahriman.web.web import on_shutdown, on_startup, run_server
|
||||
|
||||
@ -48,8 +49,10 @@ def test_run(application: web.Application, mocker: MockerFixture) -> None:
|
||||
run_application_mock = mocker.patch("aiohttp.web.run_app")
|
||||
|
||||
run_server(application)
|
||||
run_application_mock.assert_called_once_with(application, host="127.0.0.1", port=port,
|
||||
handle_signals=False, access_log=pytest.helpers.anyvar(int))
|
||||
run_application_mock.assert_called_once_with(
|
||||
application, host="127.0.0.1", port=port, handle_signals=False,
|
||||
access_log=pytest.helpers.anyvar(int), access_log_class=FilteredAccessLogger
|
||||
)
|
||||
|
||||
|
||||
def test_run_with_auth(application_with_auth: web.Application, mocker: MockerFixture) -> None:
|
||||
@ -61,8 +64,10 @@ def test_run_with_auth(application_with_auth: web.Application, mocker: MockerFix
|
||||
run_application_mock = mocker.patch("aiohttp.web.run_app")
|
||||
|
||||
run_server(application_with_auth)
|
||||
run_application_mock.assert_called_once_with(application_with_auth, host="127.0.0.1", port=port,
|
||||
handle_signals=False, access_log=pytest.helpers.anyvar(int))
|
||||
run_application_mock.assert_called_once_with(
|
||||
application_with_auth, host="127.0.0.1", port=port, handle_signals=False,
|
||||
access_log=pytest.helpers.anyvar(int), access_log_class=FilteredAccessLogger
|
||||
)
|
||||
|
||||
|
||||
def test_run_with_debug(application_with_debug: web.Application, mocker: MockerFixture) -> None:
|
||||
@ -74,5 +79,7 @@ def test_run_with_debug(application_with_debug: web.Application, mocker: MockerF
|
||||
run_application_mock = mocker.patch("aiohttp.web.run_app")
|
||||
|
||||
run_server(application_with_debug)
|
||||
run_application_mock.assert_called_once_with(application_with_debug, host="127.0.0.1", port=port,
|
||||
handle_signals=False, access_log=pytest.helpers.anyvar(int))
|
||||
run_application_mock.assert_called_once_with(
|
||||
application_with_debug, host="127.0.0.1", port=port, handle_signals=False,
|
||||
access_log=pytest.helpers.anyvar(int), access_log_class=FilteredAccessLogger
|
||||
)
|
||||
|
Reference in New Issue
Block a user