|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import typing as t |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pulp_glue.common.openapi import OpenAPI |
| 8 | + |
| 9 | +TEST_SCHEMA = json.dumps( |
| 10 | + { |
| 11 | + "openapi": "3.0.3", |
| 12 | + "paths": {"test/": {"get": {"operationId": "test_id", "responses": {200: {}}}}}, |
| 13 | + "components": {"schemas": {}}, |
| 14 | + } |
| 15 | +).encode() |
| 16 | + |
| 17 | + |
| 18 | +class MockRequest: |
| 19 | + headers: t.Dict[str, str] = {} |
| 20 | + body: t.Dict[str, t.Any] = {} |
| 21 | + |
| 22 | + |
| 23 | +class MockResponse: |
| 24 | + status_code = 200 |
| 25 | + headers: t.Dict[str, str] = {} |
| 26 | + text = "{}" |
| 27 | + content: t.Dict[str, t.Any] = {} |
| 28 | + |
| 29 | + def raise_for_status(self) -> None: |
| 30 | + pass |
| 31 | + |
| 32 | + |
| 33 | +class MockSession: |
| 34 | + def prepare_request(self, *args: t.Any, **kwargs: t.Any) -> MockRequest: |
| 35 | + return MockRequest() |
| 36 | + |
| 37 | + def send(self, request: MockRequest) -> MockResponse: |
| 38 | + return MockResponse() |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def openapi(monkeypatch: pytest.MonkeyPatch) -> OpenAPI: |
| 43 | + monkeypatch.setattr(OpenAPI, "load_api", lambda self, refresh_cache: TEST_SCHEMA) |
| 44 | + openapi = OpenAPI("base_url", "doc_path") |
| 45 | + openapi._parse_api(TEST_SCHEMA) |
| 46 | + monkeypatch.setattr(openapi, "_session", MockSession()) |
| 47 | + return openapi |
| 48 | + |
| 49 | + |
| 50 | +def test_openapi_logs_nothing_from_info(openapi: OpenAPI, caplog: pytest.LogCaptureFixture) -> None: |
| 51 | + caplog.set_level(logging.INFO) |
| 52 | + openapi.call("test_id") |
| 53 | + assert caplog.record_tuples == [] |
| 54 | + |
| 55 | + |
| 56 | +def test_openapi_logs_operation_info_to_debug( |
| 57 | + openapi: OpenAPI, caplog: pytest.LogCaptureFixture |
| 58 | +) -> None: |
| 59 | + caplog.set_level(logging.DEBUG) |
| 60 | + openapi.call("test_id") |
| 61 | + assert caplog.record_tuples == [ |
| 62 | + ("pulp_glue.openapi", logging.DEBUG + 3, "test_id : get test/"), |
| 63 | + ("pulp_glue.openapi", logging.DEBUG + 2, ""), |
| 64 | + ("pulp_glue.openapi", logging.DEBUG + 1, "{}"), |
| 65 | + ("pulp_glue.openapi", logging.DEBUG + 3, "Response: 200"), |
| 66 | + ("pulp_glue.openapi", logging.DEBUG + 1, "{}"), |
| 67 | + ] |
0 commit comments