|
14 | 14 |
|
15 | 15 | import datetime |
16 | 16 | import urllib |
| 17 | +import logging |
| 18 | +from unittest import mock |
17 | 19 |
|
18 | 20 | import pytest # type: ignore |
19 | 21 |
|
20 | 22 | from google.auth import _helpers |
21 | 23 |
|
22 | 24 |
|
| 25 | +@pytest.fixture |
| 26 | +def logger(): |
| 27 | + """Provides a basic logger instance for testing.""" |
| 28 | + return logging.getLogger(__name__) |
| 29 | + |
| 30 | + |
23 | 31 | class SourceClass(object): |
24 | 32 | def func(self): # pragma: NO COVER |
25 | 33 | """example docstring""" |
@@ -263,3 +271,54 @@ def test_hash_value_different_hashing(): |
263 | 271 |
|
264 | 272 | def test_hash_value_none(): |
265 | 273 | assert _helpers._hash_value(None, "test") is None |
| 274 | + |
| 275 | + |
| 276 | +def test_is_logging_enabled_with_no_level_set(logger): |
| 277 | + |
| 278 | + with mock.patch("google.auth._helpers.CLIENT_LOGGING_SUPPORTED", True): |
| 279 | + assert _helpers.is_logging_enabled(logger) is False |
| 280 | + |
| 281 | + |
| 282 | +def test_is_logging_enabled_with_client_logging_not_supported(caplog, logger): |
| 283 | + |
| 284 | + with mock.patch("google.auth._helpers.CLIENT_LOGGING_SUPPORTED", False): |
| 285 | + caplog.set_level(logging.DEBUG, logger=__name__) |
| 286 | + assert _helpers.is_logging_enabled(logger) is False |
| 287 | + |
| 288 | + |
| 289 | +def test_is_logging_enabled_with_debug_disabled(caplog, logger): |
| 290 | + |
| 291 | + with mock.patch("google.auth._helpers.CLIENT_LOGGING_SUPPORTED", True): |
| 292 | + caplog.set_level(logging.INFO, logger=__name__) |
| 293 | + assert _helpers.is_logging_enabled(logger) is False |
| 294 | + |
| 295 | + |
| 296 | +def test_is_logging_enabled_with_debug_enabled(caplog, logger): |
| 297 | + with mock.patch("google.auth._helpers.CLIENT_LOGGING_SUPPORTED", True): |
| 298 | + caplog.set_level(logging.DEBUG, logger=__name__) |
| 299 | + assert _helpers.is_logging_enabled(logger) |
| 300 | + |
| 301 | + |
| 302 | +def test_request_log_debug_enabled(logger, caplog): |
| 303 | + logger.setLevel(logging.DEBUG) |
| 304 | + with mock.patch("google.auth._helpers.CLIENT_LOGGING_SUPPORTED", True): |
| 305 | + _helpers.request_log(logger, "GET", "http://example.com", {"key": "value"}, {"Authorization": "Bearer token"}) |
| 306 | + assert "Making request: GET http://example.com" in caplog.text |
| 307 | + |
| 308 | +def test_request_log_debug_disabled(logger, caplog): |
| 309 | + logger.setLevel(logging.INFO) |
| 310 | + with mock.patch("google.auth._helpers.CLIENT_LOGGING_SUPPORTED", True): |
| 311 | + _helpers.request_log(logger, "POST", "https://api.example.com", "data", {"Content-Type": "application/json"}) |
| 312 | + assert "Making request: POST https://api.example.com" not in caplog.text |
| 313 | + |
| 314 | +def test_response_log_debug_enabled(logger, caplog): |
| 315 | + logger.setLevel(logging.DEBUG) |
| 316 | + with mock.patch("google.auth._helpers.CLIENT_LOGGING_SUPPORTED", True): |
| 317 | + _helpers.response_log(logger, "response_object") |
| 318 | + assert "Response received..." in caplog.text |
| 319 | + |
| 320 | +def test_response_log_debug_disabled(logger, caplog): |
| 321 | + logger.setLevel(logging.INFO) |
| 322 | + with mock.patch("google.auth._helpers.CLIENT_LOGGING_SUPPORTED", True): |
| 323 | + _helpers.response_log(logger, "another_response") |
| 324 | + assert "Response received..." not in caplog.text |
0 commit comments