testzeus-cli 0.0.2__py3-none-any.whl

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.
@@ -0,0 +1,5 @@
1
+ """
2
+ TestZeus CLI - Command-line interface for TestZeus testing platform
3
+ """
4
+
5
+ __version__ = "0.0.1"
testzeus_cli/auth.py ADDED
@@ -0,0 +1,133 @@
1
+ """
2
+ Authentication utilities for TestZeus CLI.
3
+ """
4
+
5
+ import asyncio
6
+ import click
7
+ from getpass import getpass
8
+ from rich.console import Console
9
+ from typing import Dict, Any, Optional
10
+
11
+ from testzeus_sdk import TestZeusClient
12
+ from testzeus_cli.config import update_config, get_client_config, clear_auth_data
13
+ from testzeus_cli.utils.auth import (
14
+ extract_tenant_from_token,
15
+ initialize_client_with_token,
16
+ )
17
+
18
+ console = Console()
19
+
20
+
21
+ @click.command(name="login")
22
+ @click.option("--email", help="Your TestZeus email")
23
+ @click.option("--password", help="Your TestZeus password", hide_input=True)
24
+ @click.option("--save", is_flag=True, default=True, help="Save credentials to config")
25
+ @click.pass_context
26
+ def login_command(
27
+ ctx: click.Context, email: Optional[str], password: Optional[str], save: bool
28
+ ) -> None:
29
+ """Log in to TestZeus"""
30
+ if not email:
31
+ email = click.prompt("Email")
32
+
33
+ if not password:
34
+ password = getpass("Password: ")
35
+
36
+ async def _login() -> Dict[str, Any]:
37
+ config, _ = get_client_config(
38
+ ctx.obj["PROFILE"], api_url=ctx.obj.get("API_URL")
39
+ )
40
+
41
+ # Create a clean config without email/password to avoid duplicates
42
+ base_config = {"base_url": config.get("base_url")}
43
+
44
+ # Create client with explicit email/password
45
+ client = TestZeusClient(email=email, password=password, **base_config)
46
+
47
+ async with client:
48
+ # Authenticate and get token
49
+ await client.ensure_authenticated()
50
+ token = client.token
51
+
52
+ # Get tenant ID from the authenticated client
53
+ tenant_id = client.get_tenant_id()
54
+
55
+ if save:
56
+ update_config(
57
+ ctx.obj["PROFILE"],
58
+ {
59
+ "api_url": client.base_url,
60
+ "email": email,
61
+ "token": token,
62
+ "tenant_id": tenant_id,
63
+ },
64
+ )
65
+
66
+ return {
67
+ "email": email,
68
+ "api_url": client.base_url,
69
+ "authenticated": client.is_authenticated(),
70
+ "tenant_id": tenant_id,
71
+ "user_id": client.get_user_id(),
72
+ }
73
+
74
+ try:
75
+ result = asyncio.run(_login())
76
+ console.print("[green]Successfully logged in![/green]")
77
+ console.print(f"Email: {result['email']}")
78
+ console.print(f"API URL: {result['api_url']}")
79
+ console.print(f"Tenant ID: {result['tenant_id']}")
80
+ except Exception as e:
81
+ console.print(f"[red]Login failed:[/red] {str(e)}")
82
+ exit(1)
83
+
84
+
85
+ @click.command(name="logout")
86
+ @click.pass_context
87
+ def logout_command(ctx: click.Context) -> None:
88
+ """Log out from TestZeus"""
89
+ profile = ctx.obj["PROFILE"]
90
+ clear_auth_data(profile)
91
+ console.print(f"[green]Successfully logged out from profile '{profile}'[/green]")
92
+
93
+
94
+ @click.command(name="whoami")
95
+ @click.pass_context
96
+ def whoami_command(ctx: click.Context) -> None:
97
+ """Show current user information"""
98
+
99
+ async def _whoami() -> Dict[str, Any]:
100
+ config, token = get_client_config(
101
+ ctx.obj["PROFILE"], api_url=ctx.obj.get("API_URL")
102
+ )
103
+ client = TestZeusClient(**config)
104
+
105
+ try:
106
+ async with client:
107
+ # Apply token from config if available
108
+ if token:
109
+ initialize_client_with_token(client, token)
110
+ else:
111
+ await client.ensure_authenticated()
112
+
113
+ return {
114
+ "email": client.email,
115
+ "api_url": client.base_url,
116
+ "tenant_id": client.get_tenant_id(),
117
+ "user_id": client.get_user_id(),
118
+ }
119
+ except Exception as e:
120
+ console.print(f"[red]Not authenticated:[/red] {str(e)}")
121
+ console.print("[yellow]Run 'testzeus login' to authenticate[/yellow]")
122
+ exit(1)
123
+
124
+ try:
125
+ user_info = asyncio.run(_whoami())
126
+ console.print("[green]Current User Information:[/green]")
127
+ console.print(f"Email: {user_info['email']}")
128
+ console.print(f"API URL: {user_info['api_url']}")
129
+ console.print(f"Tenant ID: {user_info['tenant_id']}")
130
+ console.print(f"User ID: {user_info['user_id']}")
131
+ except Exception as e:
132
+ console.print(f"[red]Failed to get user information:[/red] {str(e)}")
133
+ exit(1)
testzeus_cli/cli.py ADDED
@@ -0,0 +1,70 @@
1
+ """
2
+ TestZeus CLI - Command line interface for TestZeus testing platform.
3
+ """
4
+
5
+ import asyncio
6
+ import sys
7
+ import click
8
+ from rich.console import Console
9
+
10
+ from testzeus_cli import __version__
11
+ from testzeus_cli.auth import login_command, logout_command, whoami_command
12
+ from testzeus_cli.commands.tests import tests_group
13
+ from testzeus_cli.commands.test_runs import test_runs_group
14
+ from testzeus_cli.commands.test_data import test_data_group
15
+
16
+ # New command groups can be imported here when implemented
17
+
18
+ console = Console()
19
+
20
+
21
+ @click.group()
22
+ @click.version_option(version=__version__, prog_name="testzeus")
23
+ @click.option("--profile", default="default", help="Configuration profile to use")
24
+ @click.option("--api-url", help="TestZeus API URL")
25
+ @click.option("--verbose", is_flag=True, help="Enable verbose output")
26
+ @click.option(
27
+ "--format",
28
+ "output_format",
29
+ type=click.Choice(["json", "table", "yaml"]),
30
+ default="table",
31
+ help="Output format",
32
+ )
33
+ @click.pass_context
34
+ def cli(ctx, profile, api_url, verbose, output_format):
35
+ """TestZeus CLI - Command line interface for TestZeus testing platform"""
36
+ ctx.ensure_object(dict)
37
+ ctx.obj["PROFILE"] = profile
38
+ ctx.obj["API_URL"] = api_url
39
+ ctx.obj["VERBOSE"] = verbose
40
+ ctx.obj["FORMAT"] = output_format
41
+
42
+
43
+ # Add commands
44
+ cli.add_command(login_command)
45
+ cli.add_command(logout_command)
46
+ cli.add_command(whoami_command)
47
+ cli.add_command(tests_group)
48
+ cli.add_command(test_runs_group)
49
+ cli.add_command(test_data_group)
50
+ # New command groups can be added here when implemented
51
+
52
+
53
+ def main():
54
+ """Main entry point for the CLI"""
55
+ try:
56
+ cli(obj={})
57
+ except KeyboardInterrupt:
58
+ console.print("\n[yellow]Operation cancelled by user[/yellow]")
59
+ sys.exit(1)
60
+ except Exception as e:
61
+ console.print(f"[bold red]Error:[/bold red] {str(e)}")
62
+ if "--verbose" in sys.argv:
63
+ console.print_exception(show_locals=True)
64
+ else:
65
+ console.print("[yellow]Run with --verbose for more details[/yellow]")
66
+ sys.exit(1)
67
+
68
+
69
+ if __name__ == "__main__":
70
+ main()
@@ -0,0 +1,3 @@
1
+ """
2
+ Command modules for TestZeus CLI.
3
+ """
@@ -0,0 +1,295 @@
1
+ """
2
+ Commands for managing test data in TestZeus CLI.
3
+ """
4
+
5
+ import asyncio
6
+ import click
7
+ from typing import Dict, Any, Optional, List
8
+ from rich.console import Console
9
+ from pathlib import Path
10
+
11
+ from testzeus_sdk import TestZeusClient
12
+ from testzeus_cli.config import get_client_config
13
+ from testzeus_cli.utils.formatters import format_output
14
+ from testzeus_cli.utils.validators import validate_id, parse_key_value_pairs
15
+ from testzeus_cli.utils.auth import initialize_client_with_token
16
+ from testzeus_cli.commands.tests import clean_data_for_api
17
+
18
+ console = Console()
19
+
20
+
21
+ def _read_data_file(file_path: str) -> str:
22
+ """
23
+ Read data content from a file as plain text
24
+
25
+ Args:
26
+ file_path: Path to the data file
27
+
28
+ Returns:
29
+ The raw text content of the file
30
+ """
31
+ data_path = Path(file_path)
32
+ if not data_path.exists():
33
+ raise click.BadParameter(f"Data file not found: {file_path}")
34
+
35
+ try:
36
+ with open(data_path, "r", encoding="utf-8") as f:
37
+ content = f.read().strip()
38
+ except Exception as e:
39
+ raise click.BadParameter(f"Failed to read data file: {str(e)}")
40
+
41
+ # Always return the raw text content
42
+ return content
43
+
44
+
45
+ @click.group(name="test-data")
46
+ def test_data_group():
47
+ """Manage TestZeus test data"""
48
+ pass
49
+
50
+
51
+ @test_data_group.command(name="list")
52
+ @click.option(
53
+ "--filters", "-f", multiple=True, help="Filter results (format: key=value)"
54
+ )
55
+ @click.option("--sort", help="Sort by field")
56
+ @click.option("--expand", help="Expand related entities")
57
+ @click.pass_context
58
+ def list_test_data(ctx, filters, sort, expand):
59
+ """List test data with optional filters and sorting"""
60
+
61
+ async def _run():
62
+ config, token = get_client_config(
63
+ ctx.obj["PROFILE"], api_url=ctx.obj.get("API_URL")
64
+ )
65
+ client = TestZeusClient(**config)
66
+
67
+ # Parse filters
68
+ filter_dict = parse_key_value_pairs(filters) if filters else {}
69
+
70
+ async with client:
71
+ # Apply token from config if available
72
+ if token:
73
+ initialize_client_with_token(client, token)
74
+
75
+ test_data = await client.test_data.get_list(
76
+ expand=expand, sort=sort, filters=filter_dict
77
+ )
78
+ return test_data
79
+
80
+ result = asyncio.run(_run())
81
+ format_output(result, ctx.obj["FORMAT"])
82
+
83
+
84
+ @test_data_group.command(name="get")
85
+ @click.argument("test_data_id")
86
+ @click.option("--expand", help="Expand related entities")
87
+ @click.pass_context
88
+ def get_test_data(ctx, test_data_id, expand):
89
+ """Get a single test data by ID"""
90
+
91
+ async def _run():
92
+ config, token = get_client_config(
93
+ ctx.obj["PROFILE"], api_url=ctx.obj.get("API_URL")
94
+ )
95
+ client = TestZeusClient(**config)
96
+
97
+ test_data_id_validated = validate_id(test_data_id, "test data")
98
+
99
+ async with client:
100
+ # Apply token from config if available
101
+ if token:
102
+ initialize_client_with_token(client, token)
103
+
104
+ test_data = await client.test_data.get_one(
105
+ test_data_id_validated, expand=expand
106
+ )
107
+ return test_data.data
108
+
109
+ result = asyncio.run(_run())
110
+ format_output(result, ctx.obj["FORMAT"])
111
+
112
+
113
+ @test_data_group.command(name="create")
114
+ @click.option("--name", required=True, help="Test data name")
115
+ @click.option("--type", default="test", help="Test data type")
116
+ @click.option("--status", default="draft", help="Test data status")
117
+ @click.option("--input-data", "-d", help="Test data content as text")
118
+ @click.option(
119
+ "--file", "-f", type=click.Path(exists=True), help="Text file with test data"
120
+ )
121
+ @click.pass_context
122
+ def create_test_data(ctx, name, type, status, input_data, file):
123
+ """Create new test data"""
124
+
125
+ async def _run():
126
+ config, token = get_client_config(
127
+ ctx.obj["PROFILE"], api_url=ctx.obj.get("API_URL")
128
+ )
129
+ client = TestZeusClient(**config)
130
+
131
+ # Prepare test data
132
+ data = {"name": name, "type": type, "status": status}
133
+
134
+ # Add data content if provided directly
135
+ if input_data:
136
+ data["data_content"] = input_data
137
+
138
+ # Read from file if provided
139
+ if file:
140
+ try:
141
+ file_content = _read_data_file(file)
142
+ data["data_content"] = file_content
143
+ except Exception as e:
144
+ console.print(f"[red]Error reading file:[/red] {str(e)}")
145
+ exit(1)
146
+
147
+ async with client:
148
+ # Apply token from config if available
149
+ if token:
150
+ initialize_client_with_token(client, token)
151
+
152
+ new_test_data = await client.test_data.create(data)
153
+ return new_test_data.data
154
+
155
+ result = asyncio.run(_run())
156
+ console.print(
157
+ f"[green]Test data created successfully with ID: {result['id']}[/green]"
158
+ )
159
+ format_output(result, ctx.obj["FORMAT"])
160
+
161
+
162
+ @test_data_group.command(name="update")
163
+ @click.argument("test_data_id")
164
+ @click.option("--name", help="New test data name")
165
+ @click.option("--type", help="New test data type")
166
+ @click.option("--status", help="New test data status")
167
+ @click.option("--input-data", "-d", help="Test data content as text")
168
+ @click.option(
169
+ "--file",
170
+ "-f",
171
+ type=click.Path(exists=True),
172
+ help="Text file with test data content",
173
+ )
174
+ @click.pass_context
175
+ def update_test_data(ctx, test_data_id, name, type, status, input_data, file):
176
+ """Update existing test data"""
177
+
178
+ async def _run():
179
+ config, token = get_client_config(
180
+ ctx.obj["PROFILE"], api_url=ctx.obj.get("API_URL")
181
+ )
182
+ client = TestZeusClient(**config)
183
+
184
+ test_data_id_validated = validate_id(test_data_id, "test data")
185
+
186
+ async with client:
187
+ # Apply token from config if available
188
+ if token:
189
+ initialize_client_with_token(client, token)
190
+ else:
191
+ await client.ensure_authenticated()
192
+
193
+ # First, fetch the existing test data to get ALL fields
194
+ existing_test_data = await client.test_data.get_one(test_data_id_validated)
195
+
196
+ if ctx.obj.get("VERBOSE"):
197
+ console.print(
198
+ f"[blue]Existing test data: {existing_test_data.data}[/blue]"
199
+ )
200
+
201
+ # Start with all existing data to ensure we don't lose anything
202
+ updates = dict(existing_test_data.data)
203
+
204
+ # Track if we have any updates
205
+ has_updates = False
206
+
207
+ # Add only the fields the user wants to update
208
+ if name:
209
+ updates["name"] = name
210
+ has_updates = True
211
+
212
+ if type:
213
+ updates["type"] = type
214
+ has_updates = True
215
+
216
+ if status:
217
+ updates["status"] = status
218
+ has_updates = True
219
+
220
+ # Add data content if provided directly
221
+ if input_data:
222
+ updates["data_content"] = input_data
223
+ has_updates = True
224
+
225
+ # Read from file if provided
226
+ if file:
227
+ try:
228
+ file_content = _read_data_file(file)
229
+ updates["data_content"] = file_content
230
+ has_updates = True
231
+ except Exception as e:
232
+ console.print(f"[red]Error reading file:[/red] {str(e)}")
233
+ exit(1)
234
+
235
+ if not has_updates:
236
+ console.print("[yellow]No updates provided[/yellow]")
237
+ return None
238
+
239
+ if ctx.obj.get("VERBOSE"):
240
+ console.print(f"[blue]Updating test data with: {updates}[/blue]")
241
+
242
+ # Clean the data before sending to the API
243
+ clean_updates = clean_data_for_api(updates)
244
+
245
+ # The clean_data_for_api function now handles critical fields automatically
246
+
247
+ if ctx.obj.get("VERBOSE"):
248
+ console.print(f"[blue]Cleaned updates for API: {clean_updates}[/blue]")
249
+
250
+ updated_test_data = await client.test_data.update(
251
+ test_data_id_validated, clean_updates
252
+ )
253
+ return updated_test_data.data
254
+
255
+ result = asyncio.run(_run())
256
+ if result:
257
+ console.print(
258
+ f"[green]Test data updated successfully: {result['name']}[/green]"
259
+ )
260
+ format_output(result, ctx.obj["FORMAT"])
261
+
262
+
263
+ @test_data_group.command(name="delete")
264
+ @click.argument("test_data_id")
265
+ @click.confirmation_option(prompt="Are you sure you want to delete this test data?")
266
+ @click.pass_context
267
+ def delete_test_data(ctx, test_data_id):
268
+ """Delete test data"""
269
+
270
+ async def _run():
271
+ config, token = get_client_config(
272
+ ctx.obj["PROFILE"], api_url=ctx.obj.get("API_URL")
273
+ )
274
+ client = TestZeusClient(**config)
275
+
276
+ test_data_id_validated = validate_id(test_data_id, "test data")
277
+
278
+ async with client:
279
+ # Apply token from config if available
280
+ if token:
281
+ initialize_client_with_token(client, token)
282
+
283
+ # Optionally get the test data before deletion for confirmation
284
+ test_data = await client.test_data.get_one(test_data_id_validated)
285
+ await client.test_data.delete(test_data_id_validated)
286
+ return test_data.data
287
+
288
+ try:
289
+ result = asyncio.run(_run())
290
+ console.print(
291
+ f"[green]Test data '{result['name']}' deleted successfully[/green]"
292
+ )
293
+ except Exception as e:
294
+ console.print(f"[red]Failed to delete test data:[/red] {str(e)}")
295
+ exit(1)