|
| 1 | +"""Unit tests for the configurable API-key header (no network, no env).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from plane.api.base_resource import BaseResource |
| 8 | +from plane.api.v2._kernel.transport import V2Transport |
| 9 | +from plane.client import PlaneClient |
| 10 | +from plane.config import Configuration |
| 11 | +from plane.errors.errors import ConfigurationError |
| 12 | + |
| 13 | + |
| 14 | +def test_default_header_is_x_api_key() -> None: |
| 15 | + config = Configuration(base_path="https://api.plane.so", api_key="k") |
| 16 | + headers = BaseResource(config, "/workspaces/")._headers() |
| 17 | + assert headers["X-Api-Key"] == "k" |
| 18 | + |
| 19 | + |
| 20 | +def test_custom_header_replaces_x_api_key_in_v1_and_v2() -> None: |
| 21 | + config = Configuration( |
| 22 | + base_path="http://gateway/plane-api", api_key="k", api_key_header="X-Gravitee-Api-Key" |
| 23 | + ) |
| 24 | + for headers in (BaseResource(config, "/workspaces/")._headers(), V2Transport(config)._headers()): |
| 25 | + assert headers["X-Gravitee-Api-Key"] == "k" |
| 26 | + assert "X-Api-Key" not in headers |
| 27 | + |
| 28 | + |
| 29 | +def test_plane_client_passes_the_header_through() -> None: |
| 30 | + client = PlaneClient(base_url="http://gateway", api_key="k", api_key_header="X-Custom-Key") |
| 31 | + assert client.config.api_key_header == "X-Custom-Key" |
| 32 | + assert client.work_items._headers()["X-Custom-Key"] == "k" |
| 33 | + |
| 34 | + |
| 35 | +def test_blank_header_is_refused() -> None: |
| 36 | + with pytest.raises(ConfigurationError): |
| 37 | + Configuration(base_path="https://api.plane.so", api_key="k", api_key_header=" ") |
| 38 | + |
| 39 | + |
| 40 | +def test_access_token_ignores_the_header_setting() -> None: |
| 41 | + config = Configuration(base_path="https://api.plane.so", access_token="t", api_key_header="X-Other") |
| 42 | + headers = BaseResource(config, "/workspaces/")._headers() |
| 43 | + assert headers["Authorization"] == "Bearer t" |
| 44 | + assert "X-Other" not in headers |
0 commit comments