|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import json |
| 16 | +import logging |
| 17 | +from unittest import mock |
| 18 | + |
| 19 | +import pytest # type: ignore |
| 20 | + |
| 21 | +from google.auth.aio import _helpers |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def logger(): |
| 26 | + """Provides a basic logger instance for testing.""" |
| 27 | + return logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.asyncio |
| 31 | +async def test_response_log_debug_enabled(logger, caplog): |
| 32 | + logger.setLevel(logging.DEBUG) |
| 33 | + with mock.patch("google.auth._helpers.CLIENT_LOGGING_SUPPORTED", True): |
| 34 | + await _helpers.response_log_async(logger, {"payload": None}) |
| 35 | + assert len(caplog.records) == 1 |
| 36 | + record = caplog.records[0] |
| 37 | + assert record.message == "Response received..." |
| 38 | + assert record.httpResponse == "<class 'NoneType'>" |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.asyncio |
| 42 | +async def test_response_log_debug_disabled(logger, caplog): |
| 43 | + logger.setLevel(logging.INFO) |
| 44 | + with mock.patch("google.auth._helpers.CLIENT_LOGGING_SUPPORTED", True): |
| 45 | + await _helpers.response_log_async(logger, "another_response") |
| 46 | + assert "Response received..." not in caplog.text |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.asyncio |
| 50 | +async def test_response_log_debug_enabled_response_json(logger, caplog): |
| 51 | + class MockResponse: |
| 52 | + async def json(self): |
| 53 | + return {"key1": "value1", "key2": "value2", "key3": "value3"} |
| 54 | + |
| 55 | + response = MockResponse() |
| 56 | + logger.setLevel(logging.DEBUG) |
| 57 | + with mock.patch("google.auth._helpers.CLIENT_LOGGING_SUPPORTED", True): |
| 58 | + await _helpers.response_log_async(logger, response) |
| 59 | + assert len(caplog.records) == 1 |
| 60 | + record = caplog.records[0] |
| 61 | + assert record.message == "Response received..." |
| 62 | + assert record.httpResponse == {"key1": "value1", "key2": "value2", "key3": "value3"} |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.asyncio |
| 66 | +async def test_parse_response_async_json_valid(): |
| 67 | + class MockResponse: |
| 68 | + async def json(self): |
| 69 | + return {"data": "test"} |
| 70 | + |
| 71 | + response = MockResponse() |
| 72 | + expected = {"data": "test"} |
| 73 | + assert await _helpers._parse_response_async(response) == expected |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.asyncio |
| 77 | +async def test_parse_response_async_json_invalid(): |
| 78 | + class MockResponse: |
| 79 | + def json(self): |
| 80 | + raise json.JSONDecodeError("msg", "doc", 0) |
| 81 | + |
| 82 | + response = MockResponse() |
| 83 | + assert await _helpers._parse_response_async(response) is None |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.asyncio |
| 87 | +async def test_parse_response_async_no_json_method(): |
| 88 | + response = "plain text" |
| 89 | + assert await _helpers._parse_response_async(response) is None |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.asyncio |
| 93 | +async def test_parse_response_async_none(): |
| 94 | + assert await _helpers._parse_response_async(None) is None |
0 commit comments