testzeus-cli 0.0.27__tar.gz → 0.0.29__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/PKG-INFO +2 -2
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/pyproject.toml +2 -2
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/agent_harness.py +126 -27
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/utils/auth.py +43 -18
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/LICENSE +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/README.md +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/__init__.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/auth.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/cli.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/__init__.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/connected_environments.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/daemon.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/device_pool.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/environments.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/extensions.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/hypermind_code_blocks.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/knowledge_bases.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/notification_channels.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/tags.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/test_data.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/test_report_runs.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/test_report_schedules.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/test_run_groups.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/test_runs.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/test_suite_node_runs.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/test_suite_runs.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/test_suite_schedules.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/test_suites.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/tests.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/tests_ai_generator.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/tests_authoring.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/commands/user_integrations.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/config.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/utils/__init__.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/utils/client.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/utils/files.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/utils/formatters.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/utils/links.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/utils/profile.py +0 -0
- {testzeus_cli-0.0.27 → testzeus_cli-0.0.29}/testzeus_cli/utils/validators.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: testzeus-cli
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.29
|
|
4
4
|
Summary: Command-line interface for TestZeus testing platform
|
|
5
5
|
License-File: LICENSE
|
|
6
6
|
Author: Shriyansh Agnihotri
|
|
@@ -22,7 +22,7 @@ Requires-Dist: pocketbase (>=0.15.0,<0.16.0)
|
|
|
22
22
|
Requires-Dist: pydantic (>=2.0.0,<3.0.0)
|
|
23
23
|
Requires-Dist: pyyaml (>=6.0,<7.0)
|
|
24
24
|
Requires-Dist: rich (>=13.3.1,<14.0.0)
|
|
25
|
-
Requires-Dist: testzeus-sdk (>=0.0.
|
|
25
|
+
Requires-Dist: testzeus-sdk (>=0.0.25,<0.1.0)
|
|
26
26
|
Requires-Dist: toml (>=0.10.2,<0.11.0)
|
|
27
27
|
Project-URL: Repository, https://github.com/test-zeus-ai/testzeus-cli
|
|
28
28
|
Description-Content-Type: text/markdown
|
|
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
|
|
|
4
4
|
|
|
5
5
|
[tool.poetry]
|
|
6
6
|
name = "testzeus-cli"
|
|
7
|
-
version = "0.0.
|
|
7
|
+
version = "0.0.29"
|
|
8
8
|
description = "Command-line interface for TestZeus testing platform"
|
|
9
9
|
authors = ["Shriyansh Agnihotri <shriyansh@testzeus.com>"]
|
|
10
10
|
readme = "README.md"
|
|
@@ -22,7 +22,7 @@ pydantic = "^2.0.0"
|
|
|
22
22
|
pyyaml = "^6.0"
|
|
23
23
|
keyring = "^24.0.0"
|
|
24
24
|
ipdb = "^0.13.13"
|
|
25
|
-
testzeus-sdk = ">=0.0.
|
|
25
|
+
testzeus-sdk = ">=0.0.25,<0.1.0"
|
|
26
26
|
pocketbase = "^0.15.0"
|
|
27
27
|
toml = "^0.10.2"
|
|
28
28
|
aiohttp = "^3.14.1"
|
|
@@ -6,7 +6,7 @@ generate adversarial pathways, run simulations, and monitor / cancel them.
|
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
8
|
import asyncio
|
|
9
|
-
from typing import Any, Dict
|
|
9
|
+
from typing import Any, Dict, Optional
|
|
10
10
|
|
|
11
11
|
import click
|
|
12
12
|
from rich.console import Console
|
|
@@ -25,32 +25,52 @@ def _pathway_row(data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
25
25
|
|
|
26
26
|
Dumping the raw record lets the generic 6-column table fill up with noise
|
|
27
27
|
(tenant/created_by/modified_by) and hide the useful fields, so project to
|
|
28
|
-
|
|
28
|
+
risk_level/objective/status explicitly. `name` is skipped: the generation
|
|
29
|
+
workflow only fills `objective`, so generated pathways all have name=null
|
|
30
|
+
and the column renders as a useless "None" for every row.
|
|
29
31
|
"""
|
|
30
32
|
objective = data.get("objective") or ""
|
|
31
33
|
if len(objective) > 80:
|
|
32
34
|
objective = objective[:77] + "..."
|
|
33
35
|
return {
|
|
34
36
|
"id": data.get("id"),
|
|
35
|
-
"name": data.get("name"),
|
|
36
37
|
"risk_level": data.get("risk_level"),
|
|
37
38
|
"objective": objective,
|
|
38
39
|
"status": data.get("status"),
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
|
|
43
|
+
def _simulation_row(data: Dict[str, Any]) -> Dict[str, Any]:
|
|
44
|
+
"""Trim a simulation record to the columns worth showing in a list.
|
|
45
|
+
|
|
46
|
+
Same idea as ``_pathway_row``: the generic 6-column table picker would bury
|
|
47
|
+
verdict/simulation_score under base fields, so project explicitly. Names are
|
|
48
|
+
workflow-generated and long, so truncate them.
|
|
49
|
+
"""
|
|
50
|
+
name = data.get("name") or ""
|
|
51
|
+
if len(name) > 40:
|
|
52
|
+
name = name[:37] + "..."
|
|
53
|
+
return {
|
|
54
|
+
"id": data.get("id"),
|
|
55
|
+
"name": name,
|
|
56
|
+
"status": data.get("status"),
|
|
57
|
+
"verdict": data.get("verdict"),
|
|
58
|
+
"simulation_score": data.get("simulation_score"),
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
42
62
|
def _client(ctx):
|
|
43
63
|
"""Build the config + client tuple used by every command."""
|
|
44
64
|
config, token, tenant_id, user_id = get_client_config(ctx.obj["PROFILE"], api_url=ctx.obj.get("API_URL"))
|
|
45
65
|
return TestZeusClient(**config), token, tenant_id, user_id
|
|
46
66
|
|
|
47
67
|
|
|
48
|
-
async def _auth(client, token, tenant_id=None):
|
|
68
|
+
async def _auth(client, token, tenant_id: Optional[str] = None, user_id: Optional[str] = None):
|
|
49
69
|
if token:
|
|
50
|
-
# Thread the profile tenant so
|
|
51
|
-
# has no tenant claim; otherwise create/run fails
|
|
52
|
-
# "No tenant ID available
|
|
53
|
-
initialize_client_with_token(client, token, profile_tenant_id=tenant_id)
|
|
70
|
+
# Thread the profile tenant and user id so owner-field injection works
|
|
71
|
+
# even when the token has no tenant/id claim; otherwise create/run fails
|
|
72
|
+
# deep in the SDK ("No tenant ID available" / blank created_by).
|
|
73
|
+
initialize_client_with_token(client, token, profile_tenant_id=tenant_id, profile_user_id=user_id)
|
|
54
74
|
else:
|
|
55
75
|
await client.ensure_authenticated()
|
|
56
76
|
if not hasattr(client, "agent_harness"):
|
|
@@ -73,9 +93,9 @@ def list_agents(ctx, page, per_page, status, search):
|
|
|
73
93
|
"""List available agents"""
|
|
74
94
|
|
|
75
95
|
async def _run():
|
|
76
|
-
client, token, tenant_id,
|
|
96
|
+
client, token, tenant_id, user_id = _client(ctx)
|
|
77
97
|
async with client:
|
|
78
|
-
await _auth(client, token, tenant_id)
|
|
98
|
+
await _auth(client, token, tenant_id, user_id)
|
|
79
99
|
return await client.agent_harness.list_agents(page=page, per_page=per_page, status=status, search=search)
|
|
80
100
|
|
|
81
101
|
try:
|
|
@@ -99,14 +119,19 @@ def list_pathways(ctx, agent_id, page, per_page):
|
|
|
99
119
|
# argument instead. The SDK builds the agent_id filter via the shared filter
|
|
100
120
|
# builder; add --filters/--sort here if the manager grows to support them.
|
|
101
121
|
async def _run():
|
|
102
|
-
client, token, tenant_id,
|
|
122
|
+
client, token, tenant_id, user_id = _client(ctx)
|
|
103
123
|
async with client:
|
|
104
|
-
await _auth(client, token, tenant_id)
|
|
124
|
+
await _auth(client, token, tenant_id, user_id)
|
|
105
125
|
agent = validate_id(agent_id, "agent")
|
|
106
126
|
res = await client.agent_harness.list_pathways(agent_id=agent, page=page, per_page=per_page)
|
|
107
|
-
# get_list returns AdversaryPathways models
|
|
108
|
-
# columns (
|
|
109
|
-
|
|
127
|
+
# get_list returns AdversaryPathways models. For the table, project to
|
|
128
|
+
# the useful columns (risk/objective/status) so they aren't crowded
|
|
129
|
+
# out; keep full records for json/yaml so machine-readable output
|
|
130
|
+
# stays lossless.
|
|
131
|
+
items = [p.data for p in res.get("items", [])]
|
|
132
|
+
if ctx.obj["FORMAT"] == "table":
|
|
133
|
+
items = [_pathway_row(d) for d in items]
|
|
134
|
+
res["items"] = items
|
|
110
135
|
return res
|
|
111
136
|
|
|
112
137
|
try:
|
|
@@ -129,9 +154,9 @@ def generate_pathways(ctx, agent_profile, prompt, name, agent_name, num, allow_d
|
|
|
129
154
|
"""Start an AI pathway-generation job for a Salesforce agent profile"""
|
|
130
155
|
|
|
131
156
|
async def _run():
|
|
132
|
-
client, token, tenant_id,
|
|
157
|
+
client, token, tenant_id, user_id = _client(ctx)
|
|
133
158
|
async with client:
|
|
134
|
-
await _auth(client, token, tenant_id)
|
|
159
|
+
await _auth(client, token, tenant_id, user_id)
|
|
135
160
|
# tenant/created_by/modified_by are auto-filled by the SDK manager.
|
|
136
161
|
return await client.agent_harness.generate_pathways(
|
|
137
162
|
agent_profile=agent_profile,
|
|
@@ -165,9 +190,9 @@ def run(ctx, agent_id, pathways, name, agent_name, run_as_profile, wait, timeout
|
|
|
165
190
|
"""Run adversarial simulations for the given pathways"""
|
|
166
191
|
|
|
167
192
|
async def _run():
|
|
168
|
-
client, token, tenant_id,
|
|
193
|
+
client, token, tenant_id, user_id = _client(ctx)
|
|
169
194
|
async with client:
|
|
170
|
-
await _auth(client, token, tenant_id)
|
|
195
|
+
await _auth(client, token, tenant_id, user_id)
|
|
171
196
|
agent = validate_id(agent_id, "agent")
|
|
172
197
|
# tenant/created_by/modified_by are auto-filled by the SDK manager.
|
|
173
198
|
if wait:
|
|
@@ -212,9 +237,9 @@ def status(ctx, group_id):
|
|
|
212
237
|
"""Get the status/results of a simulation run group"""
|
|
213
238
|
|
|
214
239
|
async def _run():
|
|
215
|
-
client, token, tenant_id,
|
|
240
|
+
client, token, tenant_id, user_id = _client(ctx)
|
|
216
241
|
async with client:
|
|
217
|
-
await _auth(client, token, tenant_id)
|
|
242
|
+
await _auth(client, token, tenant_id, user_id)
|
|
218
243
|
return await client.agent_harness.get_status(validate_id(group_id, "group"))
|
|
219
244
|
|
|
220
245
|
try:
|
|
@@ -231,9 +256,9 @@ def cancel(ctx, group_id):
|
|
|
231
256
|
"""Cancel a running simulation group"""
|
|
232
257
|
|
|
233
258
|
async def _run():
|
|
234
|
-
client, token, tenant_id,
|
|
259
|
+
client, token, tenant_id, user_id = _client(ctx)
|
|
235
260
|
async with client:
|
|
236
|
-
await _auth(client, token, tenant_id)
|
|
261
|
+
await _auth(client, token, tenant_id, user_id)
|
|
237
262
|
return await client.agent_harness.cancel(validate_id(group_id, "group"))
|
|
238
263
|
|
|
239
264
|
try:
|
|
@@ -251,9 +276,9 @@ def get_agent(ctx, agent_id):
|
|
|
251
276
|
"""Get full detail for a single agent"""
|
|
252
277
|
|
|
253
278
|
async def _run():
|
|
254
|
-
client, token, tenant_id,
|
|
279
|
+
client, token, tenant_id, user_id = _client(ctx)
|
|
255
280
|
async with client:
|
|
256
|
-
await _auth(client, token, tenant_id)
|
|
281
|
+
await _auth(client, token, tenant_id, user_id)
|
|
257
282
|
return await client.agent_harness.get_agent(validate_id(agent_id, "agent"))
|
|
258
283
|
|
|
259
284
|
try:
|
|
@@ -269,6 +294,80 @@ def get_agent(ctx, agent_id):
|
|
|
269
294
|
raise click.Abort()
|
|
270
295
|
|
|
271
296
|
|
|
297
|
+
@agent_harness_group.command(name="get-pathway")
|
|
298
|
+
@click.argument("pathway_id")
|
|
299
|
+
@click.pass_context
|
|
300
|
+
def get_pathway(ctx, pathway_id):
|
|
301
|
+
"""Get full detail for a single adversarial pathway"""
|
|
302
|
+
|
|
303
|
+
async def _run():
|
|
304
|
+
client, token, tenant_id, user_id = _client(ctx)
|
|
305
|
+
async with client:
|
|
306
|
+
await _auth(client, token, tenant_id, user_id)
|
|
307
|
+
# The facade has no get_pathway method; adversary_pathways is a plain
|
|
308
|
+
# PocketBase collection, so go through the sub-manager's get_one.
|
|
309
|
+
return await client.agent_harness.pathways.get_one(validate_id(pathway_id, "pathway"))
|
|
310
|
+
|
|
311
|
+
try:
|
|
312
|
+
print_platform_link(ctx, "agent-harness")
|
|
313
|
+
result = asyncio.run(_run())
|
|
314
|
+
format_output(result.data, ctx.obj["FORMAT"])
|
|
315
|
+
except Exception as e:
|
|
316
|
+
print_message(f"[red]Error getting pathway: {str(e)}[/red]", ctx.obj["FORMAT"])
|
|
317
|
+
raise click.Abort()
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
@agent_harness_group.command(name="list-simulations")
|
|
321
|
+
@click.argument("group_id")
|
|
322
|
+
@click.option("--page", default=1, help="Page number")
|
|
323
|
+
@click.option("--per-page", default=50, help="Results per page")
|
|
324
|
+
@click.pass_context
|
|
325
|
+
def list_simulations(ctx, group_id, page, per_page):
|
|
326
|
+
"""List the simulations of a run group"""
|
|
327
|
+
|
|
328
|
+
async def _run():
|
|
329
|
+
client, token, tenant_id, user_id = _client(ctx)
|
|
330
|
+
async with client:
|
|
331
|
+
await _auth(client, token, tenant_id, user_id)
|
|
332
|
+
group = validate_id(group_id, "group")
|
|
333
|
+
res = await client.agent_harness.list_simulations(group_id=group, page=page, per_page=per_page)
|
|
334
|
+
# Same format-aware projection as list-pathways: useful columns for
|
|
335
|
+
# the table, full records for json/yaml.
|
|
336
|
+
items = [s.data for s in res.get("items", [])]
|
|
337
|
+
if ctx.obj["FORMAT"] == "table":
|
|
338
|
+
items = [_simulation_row(d) for d in items]
|
|
339
|
+
res["items"] = items
|
|
340
|
+
return res
|
|
341
|
+
|
|
342
|
+
try:
|
|
343
|
+
print_platform_link(ctx, "agent-harness")
|
|
344
|
+
format_output(asyncio.run(_run()), ctx.obj["FORMAT"])
|
|
345
|
+
except Exception as e:
|
|
346
|
+
print_message(f"[red]Error listing simulations: {str(e)}[/red]", ctx.obj["FORMAT"])
|
|
347
|
+
raise click.Abort()
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
@agent_harness_group.command(name="get-simulation")
|
|
351
|
+
@click.argument("simulation_id")
|
|
352
|
+
@click.pass_context
|
|
353
|
+
def get_simulation(ctx, simulation_id):
|
|
354
|
+
"""Get full detail for a single simulation"""
|
|
355
|
+
|
|
356
|
+
async def _run():
|
|
357
|
+
client, token, tenant_id, user_id = _client(ctx)
|
|
358
|
+
async with client:
|
|
359
|
+
await _auth(client, token, tenant_id, user_id)
|
|
360
|
+
return await client.agent_harness.get_simulation(validate_id(simulation_id, "simulation"))
|
|
361
|
+
|
|
362
|
+
try:
|
|
363
|
+
print_platform_link(ctx, "agent-harness")
|
|
364
|
+
result = asyncio.run(_run())
|
|
365
|
+
format_output(result.data, ctx.obj["FORMAT"])
|
|
366
|
+
except Exception as e:
|
|
367
|
+
print_message(f"[red]Error getting simulation: {str(e)}[/red]", ctx.obj["FORMAT"])
|
|
368
|
+
raise click.Abort()
|
|
369
|
+
|
|
370
|
+
|
|
272
371
|
@agent_harness_group.command(name="delete-pathway")
|
|
273
372
|
@click.argument("pathway_id")
|
|
274
373
|
@click.pass_context
|
|
@@ -276,9 +375,9 @@ def delete_pathway(ctx, pathway_id):
|
|
|
276
375
|
"""Soft-delete an adversarial pathway"""
|
|
277
376
|
|
|
278
377
|
async def _run():
|
|
279
|
-
client, token, tenant_id,
|
|
378
|
+
client, token, tenant_id, user_id = _client(ctx)
|
|
280
379
|
async with client:
|
|
281
|
-
await _auth(client, token, tenant_id)
|
|
380
|
+
await _auth(client, token, tenant_id, user_id)
|
|
282
381
|
return await client.agent_harness.delete_pathway(validate_id(pathway_id, "pathway"))
|
|
283
382
|
|
|
284
383
|
try:
|
|
@@ -6,13 +6,27 @@ import base64
|
|
|
6
6
|
import json
|
|
7
7
|
import os
|
|
8
8
|
import logging
|
|
9
|
+
import time
|
|
9
10
|
from testzeus_sdk import TestZeusClient
|
|
10
11
|
from typing import Dict, Any, Optional, cast
|
|
11
12
|
|
|
12
|
-
|
|
13
13
|
logger = logging.getLogger("testzeus_cli.utils.auth")
|
|
14
14
|
|
|
15
15
|
|
|
16
|
+
class _MinimalAuthRecord:
|
|
17
|
+
"""Stand-in auth model for the token path (the SDK reads auth_store.model.id).
|
|
18
|
+
|
|
19
|
+
Any attribute other than ``id`` resolves to None instead of raising
|
|
20
|
+
AttributeError, so SDK/PocketBase code that probes e.g. ``.collectionId``
|
|
21
|
+
or ``.email`` degrades gracefully."""
|
|
22
|
+
|
|
23
|
+
def __init__(self, id: str):
|
|
24
|
+
self.id = id
|
|
25
|
+
|
|
26
|
+
def __getattr__(self, name: str) -> None:
|
|
27
|
+
return None
|
|
28
|
+
|
|
29
|
+
|
|
16
30
|
def _auth_debug_enabled() -> bool:
|
|
17
31
|
return str(os.environ.get("TESTZEUS_AUTH_DEBUG", "")).lower() in {"1", "true", "yes", "on"}
|
|
18
32
|
|
|
@@ -50,7 +64,7 @@ def decode_jwt_payload(token: str) -> Optional[Dict[str, Any]]:
|
|
|
50
64
|
return None
|
|
51
65
|
|
|
52
66
|
|
|
53
|
-
def initialize_client_with_token(client: TestZeusClient, token: str, profile_tenant_id: Optional[str] = None) -> None:
|
|
67
|
+
def initialize_client_with_token(client: TestZeusClient, token: str, profile_tenant_id: Optional[str] = None, profile_user_id: Optional[str] = None) -> None:
|
|
54
68
|
"""
|
|
55
69
|
Initialize a TestZeusClient with a token, properly setting up the auth store model.
|
|
56
70
|
|
|
@@ -58,16 +72,25 @@ def initialize_client_with_token(client: TestZeusClient, token: str, profile_ten
|
|
|
58
72
|
client: The TestZeusClient instance
|
|
59
73
|
token: JWT token
|
|
60
74
|
profile_tenant_id: Optional tenant ID from profile config, used as fallback when token has no tenant claim.
|
|
75
|
+
profile_user_id: Optional user ID from profile config, used as fallback when token has no id claim.
|
|
61
76
|
"""
|
|
62
|
-
# Set the token
|
|
63
|
-
client.token = token
|
|
64
|
-
client._authenticated = True
|
|
65
|
-
|
|
66
77
|
# Extract token data
|
|
67
78
|
claims = decode_jwt_payload(token)
|
|
68
79
|
tenant_id = None
|
|
69
80
|
user_id = None
|
|
70
81
|
|
|
82
|
+
# Fail fast on tokens that can't work, with an actionable message. Without
|
|
83
|
+
# this the user gets raw gateway 401s ("requires valid record authorization
|
|
84
|
+
# token") or, worse, silently empty collection lists.
|
|
85
|
+
if claims is None:
|
|
86
|
+
raise ValueError("Could not decode saved auth token - run 'testzeus login' to re-authenticate")
|
|
87
|
+
if isinstance(claims.get("exp"), (int, float)) and claims["exp"] < time.time():
|
|
88
|
+
raise ValueError("Saved auth token has expired - run 'testzeus login' to re-authenticate")
|
|
89
|
+
|
|
90
|
+
# Set the token
|
|
91
|
+
client.token = token
|
|
92
|
+
client._authenticated = True
|
|
93
|
+
|
|
71
94
|
if claims:
|
|
72
95
|
# Check for tenant ID in various possible locations within the token
|
|
73
96
|
if "tenant" in claims:
|
|
@@ -86,18 +109,24 @@ def initialize_client_with_token(client: TestZeusClient, token: str, profile_ten
|
|
|
86
109
|
elif "record" in claims and "id" in claims["record"]:
|
|
87
110
|
user_id = claims["record"]["id"]
|
|
88
111
|
|
|
112
|
+
# Fallback to the user ID saved in the profile config (mirrors the
|
|
113
|
+
# profile_tenant_id fallback) for tokens that carry no id claim.
|
|
114
|
+
if not user_id and profile_user_id:
|
|
115
|
+
user_id = profile_user_id
|
|
116
|
+
|
|
89
117
|
# Save the token to the auth store - this should properly populate auth_store.model
|
|
90
118
|
client.pb.auth_store.save(token, None)
|
|
91
119
|
|
|
92
|
-
#
|
|
93
|
-
|
|
120
|
+
# Attach the user id to the auth model. save(token, None) leaves model=None,
|
|
121
|
+
# so a minimal record stand-in is stored when needed - otherwise the SDK's
|
|
122
|
+
# get_user_id() (which reads auth_store.model.id) returns "" on the token
|
|
123
|
+
# path and every create fails with blank created_by/modified_by.
|
|
124
|
+
if user_id:
|
|
94
125
|
try:
|
|
95
|
-
|
|
96
|
-
if hasattr(client.pb.auth_store.model, "id"):
|
|
97
|
-
client.pb.auth_store.model.id = user_id
|
|
98
|
-
else:
|
|
99
|
-
# Add ID attribute to existing model
|
|
126
|
+
if client.pb.auth_store.model:
|
|
100
127
|
setattr(client.pb.auth_store.model, "id", user_id)
|
|
128
|
+
else:
|
|
129
|
+
client.pb.auth_store.save(token, _MinimalAuthRecord(id=user_id))
|
|
101
130
|
except Exception:
|
|
102
131
|
# If we can't set the model, that's okay - the SDK might handle it differently
|
|
103
132
|
pass
|
|
@@ -149,11 +178,7 @@ def extract_tenant_from_token(token: str) -> str:
|
|
|
149
178
|
elif "collectionId" in claims:
|
|
150
179
|
return str(claims["collectionId"])
|
|
151
180
|
# Look for tenant in other possible locations
|
|
152
|
-
elif (
|
|
153
|
-
"record" in claims
|
|
154
|
-
and claims["record"].get("collectionId")
|
|
155
|
-
and claims["record"]["collectionId"] != "_pb_users_auth_"
|
|
156
|
-
):
|
|
181
|
+
elif "record" in claims and claims["record"].get("collectionId") and claims["record"]["collectionId"] != "_pb_users_auth_":
|
|
157
182
|
return str(claims["record"]["collectionId"])
|
|
158
183
|
|
|
159
184
|
# No default value - let the API determine the appropriate tenant
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|