openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.108.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/test_utils/test_logging.py

100lines · modeblame

1ca831cfKrista Pratico1 years ago1import logging
2from typing import Any, Dict, cast
3
4import pytest
5
6from openai._utils import SensitiveHeadersFilter
7
8
9@pytest.fixture
10def logger_with_filter() -> logging.Logger:
11logger = logging.getLogger("test_logger")
12logger.setLevel(logging.DEBUG)
13logger.addFilter(SensitiveHeadersFilter())
14return logger
15
16
17def test_keys_redacted(logger_with_filter: logging.Logger, caplog: pytest.LogCaptureFixture) -> None:
18with caplog.at_level(logging.DEBUG):
19logger_with_filter.debug(
20"Request options: %s",
21{
22"method": "post",
23"url": "chat/completions",
24"headers": {"api-key": "12345", "Authorization": "Bearer token"},
25},
26)
27
28log_record = cast(Dict[str, Any], caplog.records[0].args)
29assert log_record["method"] == "post"
30assert log_record["url"] == "chat/completions"
31assert log_record["headers"]["api-key"] == "<redacted>"
32assert log_record["headers"]["Authorization"] == "<redacted>"
33assert (
34caplog.messages[0]
35== "Request options: {'method': 'post', 'url': 'chat/completions', 'headers': {'api-key': '<redacted>', 'Authorization': '<redacted>'}}"
36)
37
38
39def test_keys_redacted_case_insensitive(logger_with_filter: logging.Logger, caplog: pytest.LogCaptureFixture) -> None:
40with caplog.at_level(logging.DEBUG):
41logger_with_filter.debug(
42"Request options: %s",
43{
44"method": "post",
45"url": "chat/completions",
46"headers": {"Api-key": "12345", "authorization": "Bearer token"},
47},
48)
49
50log_record = cast(Dict[str, Any], caplog.records[0].args)
51assert log_record["method"] == "post"
52assert log_record["url"] == "chat/completions"
53assert log_record["headers"]["Api-key"] == "<redacted>"
54assert log_record["headers"]["authorization"] == "<redacted>"
55assert (
56caplog.messages[0]
57== "Request options: {'method': 'post', 'url': 'chat/completions', 'headers': {'Api-key': '<redacted>', 'authorization': '<redacted>'}}"
58)
59
60
61def test_no_headers(logger_with_filter: logging.Logger, caplog: pytest.LogCaptureFixture) -> None:
62with caplog.at_level(logging.DEBUG):
63logger_with_filter.debug(
64"Request options: %s",
65{"method": "post", "url": "chat/completions"},
66)
67
68log_record = cast(Dict[str, Any], caplog.records[0].args)
69assert log_record["method"] == "post"
70assert log_record["url"] == "chat/completions"
71assert "api-key" not in log_record
72assert "Authorization" not in log_record
73assert caplog.messages[0] == "Request options: {'method': 'post', 'url': 'chat/completions'}"
74
75
76def test_headers_without_sensitive_info(logger_with_filter: logging.Logger, caplog: pytest.LogCaptureFixture) -> None:
77with caplog.at_level(logging.DEBUG):
78logger_with_filter.debug(
79"Request options: %s",
80{
81"method": "post",
82"url": "chat/completions",
83"headers": {"custom": "value"},
84},
85)
86
87log_record = cast(Dict[str, Any], caplog.records[0].args)
88assert log_record["method"] == "post"
89assert log_record["url"] == "chat/completions"
90assert log_record["headers"] == {"custom": "value"}
91assert (
92caplog.messages[0]
93== "Request options: {'method': 'post', 'url': 'chat/completions', 'headers': {'custom': 'value'}}"
94)
95
96
97def test_standard_debug_msg(logger_with_filter: logging.Logger, caplog: pytest.LogCaptureFixture) -> None:
98with caplog.at_level(logging.DEBUG):
99logger_with_filter.debug("Sending HTTP Request: %s %s", "POST", "chat/completions")
100assert caplog.messages[0] == "Sending HTTP Request: POST chat/completions"