95 lines
3.2 KiB
Python
95 lines
3.2 KiB
Python
"""Functional tests using WebTest.
|
|
|
|
See: http://webtest.readthedocs.org/
|
|
"""
|
|
|
|
from app.common.logging import redact_headers
|
|
|
|
from .replay_base import ReplyBase
|
|
|
|
|
|
class TestModelInvalidJson(ReplyBase):
|
|
"""Test logging of context containing an invalid JSON tool call."""
|
|
|
|
recording = "context_tool_call_invalid_json"
|
|
|
|
def test(self, testapp, requests_mock, mocker):
|
|
"""Test logging of context containing an invalid JSON tool call."""
|
|
mocker.patch("rich.console.Console.print")
|
|
console_print = mocker.patch("rich.padding.Padding.__init__", return_value=None)
|
|
super().test(testapp, requests_mock)
|
|
console_print.assert_any_call(
|
|
"[red]Invalid JSON generated by the model:[/red]", (0, 4)
|
|
)
|
|
|
|
|
|
def test_redact_headers():
|
|
"""Test redacting headers."""
|
|
headers = {
|
|
"Authorization": "Bearer test-service-api-key",
|
|
"authorization": "Bearer test-service-api-key",
|
|
"non-sensitive": "test-value",
|
|
"api-key": "", # Empty value
|
|
"api_key": "short",
|
|
}
|
|
redacted_headers = redact_headers(headers)
|
|
assert redacted_headers == {
|
|
"Authorization": "Bear…-key",
|
|
"authorization": "Bear…-key",
|
|
"non-sensitive": "test-value",
|
|
"api-key": "",
|
|
"api_key": "...",
|
|
}
|
|
|
|
|
|
def test_should_not_refact(mocker):
|
|
"""Test that headers are not redacted if should_redact is False."""
|
|
mocker.patch("app.common.logging.should_redact", return_value=False)
|
|
headers = {
|
|
"api_key": "test",
|
|
}
|
|
redacted_headers = redact_headers(headers)
|
|
assert redacted_headers == headers
|
|
|
|
|
|
class TestLogContextEnabled(ReplyBase):
|
|
"""Test that log_request is called when LOG_CONTEXT=True."""
|
|
|
|
def modify_settings(self, app) -> None:
|
|
"""Ensure LOG_CONTEXT is enabled."""
|
|
app.config["LOG_CONTEXT"] = True
|
|
|
|
def test(self, testapp, requests_mock, mocker):
|
|
"""Test that log_request is called when LOG_CONTEXT is True."""
|
|
log_request_mock = mocker.patch("app.blueprint.log_request")
|
|
super().test(testapp, requests_mock)
|
|
log_request_mock.assert_called_once()
|
|
|
|
|
|
class TestLogContextDisabled(ReplyBase):
|
|
"""Test that log_request is NOT called when LOG_CONTEXT=False."""
|
|
|
|
def modify_settings(self, app) -> None:
|
|
"""Disable LOG_CONTEXT."""
|
|
app.config["LOG_CONTEXT"] = False
|
|
|
|
def test(self, testapp, requests_mock, mocker):
|
|
"""Test that log_request is NOT called when LOG_CONTEXT is False."""
|
|
log_request_mock = mocker.patch("app.blueprint.log_request")
|
|
super().test(testapp, requests_mock)
|
|
log_request_mock.assert_not_called()
|
|
|
|
|
|
class TestLogCompletionDisabled(ReplyBase):
|
|
"""Test that logging of create_message_panel is NOT called when LOG_COMPLETION=False."""
|
|
|
|
def modify_settings(self, app) -> None:
|
|
"""Disable LOG_COMPLETION."""
|
|
app.config["LOG_COMPLETION"] = False
|
|
|
|
def test(self, testapp, requests_mock, mocker):
|
|
"""Test that Rich.Live.update is NOT called when LOG_COMPLETION is False."""
|
|
live_update_mock = mocker.patch("rich.live.Live.update")
|
|
super().test(testapp, requests_mock)
|
|
live_update_mock.assert_not_called()
|