-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_config.py
More file actions
359 lines (303 loc) · 12.5 KB
/
Copy pathtest_config.py
File metadata and controls
359 lines (303 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""Tests for Config loading with named profiles."""
from __future__ import annotations
import json
import os
from unittest.mock import patch
import pytest
from qbo_cli.config import Config
from qbo_cli.constants import DEFAULT_REDIRECT
def _clear_qbo_env():
"""Context manager helper: remove QBO_ env vars for clean Config tests."""
keys = ["QBO_CLIENT_ID", "QBO_CLIENT_SECRET", "QBO_REDIRECT_URI", "QBO_REALM_ID", "QBO_SANDBOX"]
old = {k: os.environ.pop(k, None) for k in keys}
return old
def _restore_env(old: dict):
for k, v in old.items():
if v is not None:
os.environ[k] = v
# ─── Profile loading ────────────────────────────────────────────────────────
class TestConfigProfileLoading:
def test_loads_prod_profile(self, tmp_path):
config_file = tmp_path / "config.json"
config_file.write_text(
json.dumps(
{
"prod": {"client_id": "prod-id", "client_secret": "prod-secret"},
"dev": {"client_id": "dev-id", "client_secret": "dev-secret"},
}
)
)
old = _clear_qbo_env()
try:
with patch("qbo_cli.config.CONFIG_PATH", config_file):
cfg = Config(profile="prod")
finally:
_restore_env(old)
assert cfg.client_id == "prod-id"
assert cfg.client_secret == "prod-secret"
def test_loads_dev_profile(self, tmp_path):
config_file = tmp_path / "config.json"
config_file.write_text(
json.dumps(
{
"prod": {"client_id": "prod-id", "client_secret": "prod-secret"},
"dev": {"client_id": "dev-id", "client_secret": "dev-secret"},
}
)
)
old = _clear_qbo_env()
try:
with patch("qbo_cli.config.CONFIG_PATH", config_file):
cfg = Config(profile="dev")
finally:
_restore_env(old)
assert cfg.client_id == "dev-id"
assert cfg.client_secret == "dev-secret"
def test_dev_sandbox_true(self, tmp_path):
config_file = tmp_path / "config.json"
config_file.write_text(
json.dumps(
{
"dev": {"client_id": "x", "client_secret": "y", "sandbox": True},
}
)
)
old = _clear_qbo_env()
try:
with patch("qbo_cli.config.CONFIG_PATH", config_file):
cfg = Config(profile="dev")
finally:
_restore_env(old)
assert cfg.sandbox is True
def test_sandbox_defaults_false(self, tmp_path):
config_file = tmp_path / "config.json"
config_file.write_text(
json.dumps(
{
"prod": {"client_id": "x", "client_secret": "y"},
}
)
)
old = _clear_qbo_env()
try:
with patch("qbo_cli.config.CONFIG_PATH", config_file):
cfg = Config(profile="prod")
finally:
_restore_env(old)
assert cfg.sandbox is False
def test_sandbox_string_coercion(self, tmp_path):
config_file = tmp_path / "config.json"
config_file.write_text(
json.dumps(
{
"dev": {"client_id": "x", "client_secret": "y", "sandbox": "true"},
}
)
)
old = _clear_qbo_env()
try:
with patch("qbo_cli.config.CONFIG_PATH", config_file):
cfg = Config(profile="dev")
finally:
_restore_env(old)
assert cfg.sandbox is True
# ─── Env var overrides ───────────────────────────────────────────────────────
class TestConfigEnvOverride:
def test_env_vars_override_profile(self, tmp_path):
config_file = tmp_path / "config.json"
config_file.write_text(
json.dumps(
{
"prod": {"client_id": "file-id", "client_secret": "file-secret", "realm_id": "file-realm"},
}
)
)
env = {
"QBO_CLIENT_ID": "env-id",
"QBO_CLIENT_SECRET": "env-secret",
"QBO_REALM_ID": "env-realm",
}
old = _clear_qbo_env()
try:
with patch("qbo_cli.config.CONFIG_PATH", config_file), patch.dict("os.environ", env, clear=False):
cfg = Config(profile="prod")
finally:
_restore_env(old)
assert cfg.client_id == "env-id"
assert cfg.client_secret == "env-secret"
assert cfg.realm_id == "env-realm"
def test_file_values_when_no_env(self, tmp_path):
config_file = tmp_path / "config.json"
config_file.write_text(
json.dumps(
{
"prod": {
"client_id": "file-id",
"client_secret": "file-secret",
"redirect_uri": "http://custom:9999/cb",
},
}
)
)
old = _clear_qbo_env()
try:
with patch("qbo_cli.config.CONFIG_PATH", config_file):
cfg = Config(profile="prod")
finally:
_restore_env(old)
assert cfg.client_id == "file-id"
assert cfg.client_secret == "file-secret"
assert cfg.redirect_uri == "http://custom:9999/cb"
# ─── Missing / invalid config ───────────────────────────────────────────────
class TestConfigMissingFile:
def test_graceful_fallback_missing_file(self, tmp_path):
config_file = tmp_path / "nonexistent.json"
old = _clear_qbo_env()
try:
with patch("qbo_cli.config.CONFIG_PATH", config_file):
cfg = Config(profile="prod")
finally:
_restore_env(old)
assert cfg.client_id == ""
assert cfg.client_secret == ""
assert cfg.redirect_uri == DEFAULT_REDIRECT
def test_graceful_fallback_invalid_json(self, tmp_path):
config_file = tmp_path / "config.json"
config_file.write_text("not json {{{")
old = _clear_qbo_env()
try:
with patch("qbo_cli.config.CONFIG_PATH", config_file):
cfg = Config(profile="prod")
finally:
_restore_env(old)
assert cfg.client_id == ""
# ─── Legacy flat format ──────────────────────────────────────────────────────
class TestConfigLegacyFlat:
def test_flat_config_warns_and_falls_back(self, tmp_path, capsys):
config_file = tmp_path / "config.json"
config_file.write_text(
json.dumps(
{
"client_id": "old-id",
"client_secret": "old-secret",
}
)
)
old = _clear_qbo_env()
try:
with patch("qbo_cli.config.CONFIG_PATH", config_file):
cfg = Config(profile="prod")
finally:
_restore_env(old)
assert cfg.client_id == "" # no profile section loaded
assert cfg.client_secret == ""
captured = capsys.readouterr().err
assert "legacy flat format" in captured
# ─── Missing profile in existing config ──────────────────────────────────────
class TestConfigMissingProfile:
def test_missing_profile_falls_back_to_empty(self, tmp_path):
"""Unknown profile gets empty config (env vars can still override)."""
config_file = tmp_path / "config.json"
config_file.write_text(
json.dumps(
{
"prod": {"client_id": "x"},
"dev": {"client_id": "y"},
}
)
)
old = _clear_qbo_env()
try:
with patch("qbo_cli.config.CONFIG_PATH", config_file):
cfg = Config(profile="staging")
finally:
_restore_env(old)
assert cfg.client_id == ""
assert cfg.client_secret == ""
def test_missing_profile_with_env_override(self, tmp_path):
"""Unknown profile still honors env var overrides."""
config_file = tmp_path / "config.json"
config_file.write_text(json.dumps({"prod": {"client_id": "x"}}))
old = _clear_qbo_env()
try:
with (
patch("qbo_cli.config.CONFIG_PATH", config_file),
patch.dict("os.environ", {"QBO_CLIENT_ID": "env-id", "QBO_CLIENT_SECRET": "env-secret"}),
):
cfg = Config(profile="staging")
finally:
_restore_env(old)
assert cfg.client_id == "env-id"
assert cfg.client_secret == "env-secret"
# ─── Validate ────────────────────────────────────────────────────────────────
class TestConfigValidate:
def test_validate_raises_when_no_client_id(self):
cfg = Config.__new__(Config)
cfg.profile = "prod"
cfg.client_id = ""
cfg.client_secret = "secret"
with pytest.raises(SystemExit):
cfg.validate()
def test_validate_raises_when_no_client_secret(self):
cfg = Config.__new__(Config)
cfg.profile = "prod"
cfg.client_id = "id-only"
cfg.client_secret = ""
with pytest.raises(SystemExit):
cfg.validate()
def test_validate_passes_when_both_set(self):
cfg = Config.__new__(Config)
cfg.profile = "prod"
cfg.client_id = "test-id"
cfg.client_secret = "test-secret"
cfg.validate() # should not raise
# ─── tokens_path ─────────────────────────────────────────────────────────────
class TestTokensPath:
def test_prod_tokens_path(self):
cfg = Config.__new__(Config)
cfg.profile = "prod"
assert cfg.tokens_path.name == "tokens.prod.json"
def test_dev_tokens_path(self):
cfg = Config.__new__(Config)
cfg.profile = "dev"
assert cfg.tokens_path.name == "tokens.dev.json"
# ─── Profile name validation ────────────────────────────────────────────────
class TestProfileValidation:
@pytest.mark.parametrize("name", ["../../evil", "", "a b", "a;b"])
def test_invalid_profile_name_dies(self, name):
with pytest.raises(SystemExit):
Config(profile=name)
def test_valid_profile_names(self, tmp_path):
config_file = tmp_path / "config.json"
config_file.write_text(json.dumps({"my-profile_1": {"client_id": "x"}}))
old = _clear_qbo_env()
try:
with patch("qbo_cli.config.CONFIG_PATH", config_file):
cfg = Config(profile="my-profile_1")
finally:
_restore_env(old)
assert cfg.profile == "my-profile_1"
# ─── QBO_SANDBOX env var rejection ──────────────────────────────────────────
class TestQboSandboxEnvRejected:
def test_qbo_sandbox_env_true_dies(self, tmp_path):
config_file = tmp_path / "config.json"
config_file.write_text(json.dumps({"prod": {}}))
with (
patch("qbo_cli.config.CONFIG_PATH", config_file),
patch.dict("os.environ", {"QBO_SANDBOX": "true"}, clear=False),
pytest.raises(SystemExit),
):
Config(profile="prod")
def test_qbo_sandbox_env_false_ignored(self, tmp_path):
config_file = tmp_path / "config.json"
config_file.write_text(json.dumps({"prod": {"client_id": "x"}}))
old = _clear_qbo_env()
try:
with (
patch("qbo_cli.config.CONFIG_PATH", config_file),
patch.dict("os.environ", {"QBO_SANDBOX": "false"}, clear=False),
):
cfg = Config(profile="prod")
finally:
_restore_env(old)
assert cfg.client_id == "x"