|
| 1 | +""" |
| 2 | +Tests for api_base override support (issue #6). |
| 3 | +
|
| 4 | +Covers: |
| 5 | +* shade.api_base module-level attribute reads and writes |
| 6 | +* Gateway(api_base=...) per-client override |
| 7 | +* Trailing slash normalisation |
| 8 | +* Precedence: explicit api_base > module-level shade.api_base > environment URL |
| 9 | +* Environment still controls Stellar network passphrase when api_base is set |
| 10 | +""" |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +import shade |
| 16 | +from shade import Gateway |
| 17 | +from shade import config as _config |
| 18 | +from shade.config import Environment |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(autouse=True) |
| 22 | +def _reset_api_base(): |
| 23 | + original = _config.api_base |
| 24 | + yield |
| 25 | + _config.api_base = original |
| 26 | + |
| 27 | + |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | +# Module-level shade.api_base |
| 30 | +# --------------------------------------------------------------------------- |
| 31 | + |
| 32 | +class TestModuleLevelApiBase: |
| 33 | + def test_defaults_to_none(self): |
| 34 | + assert shade.api_base is None |
| 35 | + |
| 36 | + def test_assignment_is_readable(self): |
| 37 | + shade.api_base = "https://staging.shadeprotocol.io" |
| 38 | + assert shade.api_base == "https://staging.shadeprotocol.io" |
| 39 | + |
| 40 | + def test_assignment_updates_config(self): |
| 41 | + shade.api_base = "https://staging.shadeprotocol.io" |
| 42 | + assert _config.api_base == "https://staging.shadeprotocol.io" |
| 43 | + |
| 44 | + def test_used_when_no_per_client_override(self): |
| 45 | + shade.api_base = "https://staging.shadeprotocol.io" |
| 46 | + gw = Gateway(api_key="test-key") |
| 47 | + assert gw._base_url == "https://staging.shadeprotocol.io" |
| 48 | + |
| 49 | + def test_trailing_slash_normalised(self): |
| 50 | + shade.api_base = "https://staging.shadeprotocol.io/" |
| 51 | + gw = Gateway(api_key="test-key") |
| 52 | + assert gw._base_url == "https://staging.shadeprotocol.io" |
| 53 | + |
| 54 | + def test_reset_to_none_restores_environment_url(self): |
| 55 | + shade.api_base = "https://staging.shadeprotocol.io" |
| 56 | + shade.api_base = None |
| 57 | + gw = Gateway(api_key="test-key") |
| 58 | + assert gw._base_url == Environment.MAINNET.base_url |
| 59 | + |
| 60 | + |
| 61 | +# --------------------------------------------------------------------------- |
| 62 | +# Per-client api_base |
| 63 | +# --------------------------------------------------------------------------- |
| 64 | + |
| 65 | +class TestPerClientApiBase: |
| 66 | + def test_overrides_environment_url(self): |
| 67 | + gw = Gateway(api_key="test-key", api_base="http://localhost:8000") |
| 68 | + assert gw._base_url == "http://localhost:8000" |
| 69 | + |
| 70 | + def test_trailing_slash_normalised(self): |
| 71 | + gw = Gateway(api_key="test-key", api_base="http://localhost:8000/") |
| 72 | + assert gw._base_url == "http://localhost:8000" |
| 73 | + |
| 74 | + def test_takes_precedence_over_module_level(self): |
| 75 | + shade.api_base = "https://staging.shadeprotocol.io" |
| 76 | + gw = Gateway(api_key="test-key", api_base="http://localhost:8000") |
| 77 | + assert gw._base_url == "http://localhost:8000" |
| 78 | + |
| 79 | + def test_http_client_uses_resolved_base_url(self): |
| 80 | + gw = Gateway(api_key="test-key", api_base="http://localhost:8000") |
| 81 | + assert gw._http.base_url == "http://localhost:8000" |
| 82 | + assert gw._async_http.base_url == "http://localhost:8000" |
| 83 | + |
| 84 | + |
| 85 | +# --------------------------------------------------------------------------- |
| 86 | +# Environment passphrase independence |
| 87 | +# --------------------------------------------------------------------------- |
| 88 | + |
| 89 | +class TestEnvironmentPassphrase: |
| 90 | + def test_mainnet_passphrase_unchanged_when_api_base_set(self): |
| 91 | + from stellar_sdk import Network |
| 92 | + gw = Gateway( |
| 93 | + api_key="test-key", |
| 94 | + api_base="http://localhost:8000", |
| 95 | + environment=Environment.MAINNET, |
| 96 | + ) |
| 97 | + assert gw.environment.network_passphrase == Network.PUBLIC_NETWORK_PASSPHRASE |
| 98 | + |
| 99 | + def test_testnet_passphrase_unchanged_when_api_base_set(self): |
| 100 | + from stellar_sdk import Network |
| 101 | + gw = Gateway( |
| 102 | + api_key="test-key", |
| 103 | + api_base="http://localhost:8000", |
| 104 | + environment=Environment.TESTNET, |
| 105 | + ) |
| 106 | + assert gw.environment.network_passphrase == Network.TESTNET_NETWORK_PASSPHRASE |
| 107 | + |
| 108 | + def test_api_base_overrides_url_not_passphrase(self): |
| 109 | + from stellar_sdk import Network |
| 110 | + gw = Gateway( |
| 111 | + api_key="test-key", |
| 112 | + api_base="http://localhost:8000", |
| 113 | + environment=Environment.MAINNET, |
| 114 | + ) |
| 115 | + assert gw._base_url == "http://localhost:8000" |
| 116 | + assert gw.environment.network_passphrase == Network.PUBLIC_NETWORK_PASSPHRASE |
| 117 | + |
| 118 | + |
| 119 | +# --------------------------------------------------------------------------- |
| 120 | +# URL resolution precedence |
| 121 | +# --------------------------------------------------------------------------- |
| 122 | + |
| 123 | +class TestUrlResolutionPrecedence: |
| 124 | + def test_environment_url_is_default(self): |
| 125 | + gw = Gateway(api_key="test-key", environment=Environment.MAINNET) |
| 126 | + assert gw._base_url == Environment.MAINNET.base_url |
| 127 | + |
| 128 | + def test_module_level_beats_environment(self): |
| 129 | + shade.api_base = "https://staging.shadeprotocol.io" |
| 130 | + gw = Gateway(api_key="test-key", environment=Environment.MAINNET) |
| 131 | + assert gw._base_url == "https://staging.shadeprotocol.io" |
| 132 | + |
| 133 | + def test_per_client_beats_module_level(self): |
| 134 | + shade.api_base = "https://staging.shadeprotocol.io" |
| 135 | + gw = Gateway(api_key="test-key", api_base="http://localhost:8000") |
| 136 | + assert gw._base_url == "http://localhost:8000" |
| 137 | + |
| 138 | + def test_testnet_environment_url_used_by_default(self): |
| 139 | + gw = Gateway(api_key="test-key", environment=Environment.TESTNET) |
| 140 | + assert gw._base_url == Environment.TESTNET.base_url |
| 141 | + |
| 142 | + |
| 143 | +# --------------------------------------------------------------------------- |
| 144 | +# Environment enum |
| 145 | +# --------------------------------------------------------------------------- |
| 146 | + |
| 147 | +class TestEnvironment: |
| 148 | + def test_mainnet_base_url(self): |
| 149 | + assert Environment.MAINNET.base_url == "https://api.shadeprotocol.io/v1" |
| 150 | + |
| 151 | + def test_testnet_base_url(self): |
| 152 | + assert Environment.TESTNET.base_url == "https://testnet.api.shadeprotocol.io/v1" |
| 153 | + |
| 154 | + def test_mainnet_network_passphrase(self): |
| 155 | + from stellar_sdk import Network |
| 156 | + assert Environment.MAINNET.network_passphrase == Network.PUBLIC_NETWORK_PASSPHRASE |
| 157 | + |
| 158 | + def test_testnet_network_passphrase(self): |
| 159 | + from stellar_sdk import Network |
| 160 | + assert Environment.TESTNET.network_passphrase == Network.TESTNET_NETWORK_PASSPHRASE |
| 161 | + |
| 162 | + |
| 163 | +# --------------------------------------------------------------------------- |
| 164 | +# ShadeClient alias |
| 165 | +# --------------------------------------------------------------------------- |
| 166 | + |
| 167 | +class TestShadeClientAlias: |
| 168 | + def test_shade_client_is_gateway(self): |
| 169 | + from shade import ShadeClient |
| 170 | + assert ShadeClient is Gateway |
| 171 | + |
| 172 | + def test_shade_client_accepts_api_base(self): |
| 173 | + from shade import ShadeClient |
| 174 | + client = ShadeClient(api_key="test-key", api_base="http://localhost:8000") |
| 175 | + assert client._base_url == "http://localhost:8000" |
0 commit comments