|
| 1 | +"""`Configuration` reads credentials and host from the environment. |
| 2 | +
|
| 3 | +The CLI already honours `HOTDATA_API_KEY`, `HOTDATA_WORKSPACE` and |
| 4 | +`HOTDATA_API_URL`; the SDK now does the same when the matching constructor |
| 5 | +argument is `None`. Explicit arguments always win, and an empty variable |
| 6 | +counts as unset. `Configuration` is generated from the in-repo mustache |
| 7 | +template, so the template is checked for the same wiring. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from hotdata import ApiClient, Configuration |
| 17 | +from hotdata.api.workspaces_api import WorkspacesApi |
| 18 | + |
| 19 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 20 | +TEMPLATE = REPO_ROOT / ".openapi-generator-templates" / "configuration.mustache" |
| 21 | + |
| 22 | +ENV_VARS = ("HOTDATA_API_KEY", "HOTDATA_WORKSPACE", "HOTDATA_API_URL") |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(autouse=True) |
| 26 | +def _clean_env(monkeypatch: pytest.MonkeyPatch) -> None: |
| 27 | + for name in ENV_VARS: |
| 28 | + monkeypatch.delenv(name, raising=False) |
| 29 | + |
| 30 | + |
| 31 | +def test_defaults_without_env() -> None: |
| 32 | + cfg = Configuration() |
| 33 | + assert cfg.api_key is None |
| 34 | + assert cfg.workspace_id is None |
| 35 | + assert cfg.host == "https://api.hotdata.dev" |
| 36 | + |
| 37 | + |
| 38 | +def test_env_fills_in_missing_args(monkeypatch: pytest.MonkeyPatch) -> None: |
| 39 | + monkeypatch.setenv("HOTDATA_API_KEY", "hd_env_key") |
| 40 | + monkeypatch.setenv("HOTDATA_WORKSPACE", "ws_env") |
| 41 | + monkeypatch.setenv("HOTDATA_API_URL", "https://api.example.test") |
| 42 | + cfg = Configuration() |
| 43 | + assert cfg.api_key == "hd_env_key" |
| 44 | + assert cfg.workspace_id == "ws_env" |
| 45 | + assert cfg.host == "https://api.example.test" |
| 46 | + # The env-provided values reach the wire exactly like explicit ones. |
| 47 | + auth = cfg.auth_settings() |
| 48 | + assert auth["BearerAuth"]["value"] == "Bearer hd_env_key" |
| 49 | + assert auth["WorkspaceId"]["value"] == "ws_env" |
| 50 | + |
| 51 | + |
| 52 | +def test_explicit_args_win_over_env(monkeypatch: pytest.MonkeyPatch) -> None: |
| 53 | + monkeypatch.setenv("HOTDATA_API_KEY", "hd_env_key") |
| 54 | + monkeypatch.setenv("HOTDATA_WORKSPACE", "ws_env") |
| 55 | + monkeypatch.setenv("HOTDATA_API_URL", "https://env.example.test") |
| 56 | + cfg = Configuration( |
| 57 | + api_key="hd_arg_key", workspace_id="ws_arg", host="https://arg.example.test" |
| 58 | + ) |
| 59 | + assert cfg.api_key == "hd_arg_key" |
| 60 | + assert cfg.workspace_id == "ws_arg" |
| 61 | + assert cfg.host == "https://arg.example.test" |
| 62 | + |
| 63 | + |
| 64 | +def test_empty_env_counts_as_unset(monkeypatch: pytest.MonkeyPatch) -> None: |
| 65 | + for name in ENV_VARS: |
| 66 | + monkeypatch.setenv(name, "") |
| 67 | + cfg = Configuration() |
| 68 | + assert cfg.api_key is None |
| 69 | + assert cfg.workspace_id is None |
| 70 | + assert cfg.host == "https://api.hotdata.dev" |
| 71 | + |
| 72 | + |
| 73 | +def test_env_host_is_used_for_requests(monkeypatch: pytest.MonkeyPatch) -> None: |
| 74 | + monkeypatch.setenv("HOTDATA_API_URL", "https://env.example.test") |
| 75 | + monkeypatch.setenv("HOTDATA_API_KEY", "hd_env_key") |
| 76 | + client = ApiClient(Configuration()) |
| 77 | + method, url, headers, _body, _post = WorkspacesApi(client)._list_workspaces_serialize( |
| 78 | + organization_public_id=None, |
| 79 | + _request_auth=None, |
| 80 | + _content_type=None, |
| 81 | + _headers=None, |
| 82 | + _host_index=0, |
| 83 | + ) |
| 84 | + assert method == "GET" |
| 85 | + assert url.startswith("https://env.example.test/v1/workspaces") |
| 86 | + assert headers["Authorization"] == "Bearer hd_env_key" |
| 87 | + |
| 88 | + |
| 89 | +def test_env_is_read_per_instance(monkeypatch: pytest.MonkeyPatch) -> None: |
| 90 | + # Resolved at construction, not at import: a later change to the |
| 91 | + # environment affects only configurations built after it. |
| 92 | + monkeypatch.setenv("HOTDATA_API_KEY", "first") |
| 93 | + first = Configuration() |
| 94 | + monkeypatch.setenv("HOTDATA_API_KEY", "second") |
| 95 | + second = Configuration() |
| 96 | + assert (first.api_key, second.api_key) == ("first", "second") |
| 97 | + |
| 98 | + |
| 99 | +def test_template_carries_the_same_env_defaults() -> None: |
| 100 | + # configuration.py is regenerated from this template; the env wiring must |
| 101 | + # live there too or the next regen silently drops it. |
| 102 | + src = TEMPLATE.read_text() |
| 103 | + for name in ENV_VARS: |
| 104 | + assert f'os.environ.get("{name}")' in src |
0 commit comments