-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtest_cli_auth.py
More file actions
265 lines (211 loc) · 7.59 KB
/
Copy pathtest_cli_auth.py
File metadata and controls
265 lines (211 loc) · 7.59 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
"""
CLI authentication plumbing tests.
These are hermetic: no network, no model load. They cover two classes of bug:
1. Regression guard for the AttributeError crash fixed in PR #1 -- `paw info`
and `paw rename` used to read the removed module attributes
`paw.api_url`/`paw.api_key` and crashed when no flags were given.
2. `--api-url`/`--api-key` must actually take effect on `compile`, `run`, and
`login`. Before the 0.4.3 fix these were global flags that the high-level
`paw.compile()`/`paw.function()`/`paw.login()` paths silently ignored
(they only read env/config). The flag-plumbing tests below fail on that
older behavior and pass once the flags are wired through the environment.
"""
from types import SimpleNamespace
import pytest
import programasweights as paw
from programasweights import cli, config
import programasweights.client as paw_client
class FakeProgram:
id = "prog123"
slug = None
status = "ready"
error = None
timings = None
version = 1
version_action = "created"
def _install_fake_client(monkeypatch, sink):
"""Replace PAWClient with a fake that records the api_url/api_key it was
constructed with, and returns canned data instead of hitting the network."""
class FakeClient:
def __init__(self, api_url=None, api_key=None):
sink["api_url"] = api_url
sink["api_key"] = api_key
self._api_url = (api_url or "https://programasweights.com").rstrip("/")
self._api_key = api_key
def _headers(self):
return {"Content-Type": "application/json"}
def get_program_meta(self, program):
return {"id": program, "spec": "canned"}
def compile(self, *a, **k):
return FakeProgram()
monkeypatch.setattr(paw_client, "PAWClient", FakeClient)
return sink
@pytest.fixture(autouse=True)
def _hermetic_env(monkeypatch):
# Ignore any real ~/.config/programasweights/config.json and env so the
# only source of api_url/api_key is what the command threads through.
monkeypatch.setattr(config, "_load_config", lambda: {})
monkeypatch.delenv("PAW_API_KEY", raising=False)
monkeypatch.delenv("PAW_API_URL", raising=False)
# ── Regression guards: info/rename must not crash without flags ──
def test_cmd_info_no_flags_no_attributeerror(monkeypatch):
sink = _install_fake_client(monkeypatch, {})
args = SimpleNamespace(program="prog123", api_url=None, api_key=None, json=True)
rc = cli.cmd_info(args) # crashed with AttributeError before PR #1
assert rc == 0
assert sink["api_url"] is None and sink["api_key"] is None
def test_cmd_rename_no_flags_no_attributeerror(monkeypatch):
sink = _install_fake_client(monkeypatch, {})
import httpx
class FakeResp:
def raise_for_status(self):
return None
def json(self):
return {"slug": "new-slug"}
monkeypatch.setattr(httpx, "patch", lambda *a, **k: FakeResp())
args = SimpleNamespace(
program="prog123", new_slug="new-slug", api_url=None, api_key=None, json=True
)
rc = cli.cmd_rename(args) # crashed with AttributeError before PR #1
assert rc == 0
assert sink["api_url"] is None and sink["api_key"] is None
def test_cmd_info_honors_flags(monkeypatch):
sink = _install_fake_client(monkeypatch, {})
args = SimpleNamespace(
program="prog123",
api_url="http://test.local",
api_key="paw_sk_test",
json=True,
)
cli.cmd_info(args)
assert sink["api_url"] == "http://test.local"
assert sink["api_key"] == "paw_sk_test"
# ── Flag plumbing: compile/run/login must honor --api-url/--api-key ──
def test_apply_auth_overrides_sets_env(monkeypatch):
cli._apply_auth_overrides(
SimpleNamespace(api_url="http://test.local", api_key="paw_sk_test")
)
assert config.get_api_url() == "http://test.local"
assert config.get_api_key() == "paw_sk_test"
def test_apply_auth_overrides_ignores_absent(monkeypatch):
cli._apply_auth_overrides(SimpleNamespace(api_url=None, api_key=None))
# Falls back to default url, no key.
assert config.get_api_url() == "https://programasweights.com"
assert config.get_api_key() is None
def test_compile_flag_plumbing(monkeypatch):
sink = _install_fake_client(monkeypatch, {})
args = SimpleNamespace(
spec="Classify sentiment as positive or negative.",
compiler=None,
slug=None,
private=False,
api_url="http://test.local",
api_key="paw_sk_test",
json=True,
)
cli.cmd_compile(args)
# The flag must reach the client through get_api_url()/get_api_key().
assert sink["api_url"] == "http://test.local"
assert sink["api_key"] == "paw_sk_test"
def test_run_flag_plumbing(monkeypatch):
seen = {}
def fake_function(program_id, **kwargs):
seen["api_url"] = config.get_api_url()
seen["api_key"] = config.get_api_key()
return lambda *a, **k: "ok"
monkeypatch.setattr(paw, "function", fake_function)
args = SimpleNamespace(
program="prog123",
input="hello",
max_tokens=512,
temperature=0.0,
verbose=False,
api_url="http://test.local",
api_key="paw_sk_test",
json=True,
)
cli.cmd_run(args)
assert seen["api_url"] == "http://test.local"
assert seen["api_key"] == "paw_sk_test"
def test_login_flag_plumbing(monkeypatch):
seen = {}
def fake_login(key=None):
seen["api_url"] = config.get_api_url()
seen["key"] = key
monkeypatch.setattr(paw, "login", fake_login)
args = SimpleNamespace(
key="paw_sk_test", api_url="http://test.local", api_key=None
)
cli.cmd_login(args)
assert seen["api_url"] == "http://test.local"
assert seen["key"] == "paw_sk_test"
def test_run_base_json_and_offline_routing(monkeypatch, capsys):
seen = {}
class FakeFunction:
interpreter = "gpt2"
def __call__(self, input_text, **kwargs):
seen["call"] = (input_text, kwargs)
return "base output"
def fake_function(program_id, **kwargs):
seen["load"] = (program_id, kwargs)
return FakeFunction()
monkeypatch.setattr(paw, "function", fake_function)
args = SimpleNamespace(
program=None,
base=True,
interpreter="gpt2",
offline=True,
input="hello",
max_tokens=7,
temperature=0.25,
verbose=False,
api_url=None,
api_key=None,
json=True,
)
assert cli.cmd_run(args) == 0
assert seen["load"] == (
None,
{
"verbose": False,
"offline": True,
"interpreter": "gpt2",
},
)
payload = __import__("json").loads(capsys.readouterr().out)
assert payload == {
"mode": "base",
"program": None,
"interpreter": "gpt2",
"input": "hello",
"output": "base output",
}
@pytest.mark.parametrize(
"arguments",
[
["run", "--program", "", "--input", "x"],
["run", "--base", "--input", "x"],
[
"run",
"--program",
"program-id",
"--interpreter",
"gpt2",
"--input",
"x",
],
[
"run",
"--program",
"program-id",
"--base",
"--input",
"x",
],
],
)
def test_run_cli_rejects_invalid_mode_combinations(monkeypatch, arguments):
monkeypatch.setattr("sys.argv", ["paw", *arguments])
with pytest.raises(SystemExit) as exc_info:
cli.main()
assert exc_info.value.code == 2