Download api-contract-validator-cli/tests/test_outputs.py from slfs1991slfs/tb_osworld_test: direct link, hf CLI and curl.
- Browser
- Download file 15.7 kB
-
https://huggingface.co/datasets/slfs1991slfs/tb_osworld_test/resolve/main/api-contract-validator-cli/tests/test_outputs.py
- Command line
-
hf download hf://datasets/slfs1991slfs/tb_osworld_test/api-contract-validator-cli/tests/test_outputs.py
-
curl -L -o test_outputs.py https://huggingface.co/datasets/slfs1991slfs/tb_osworld_test/resolve/main/api-contract-validator-cli/tests/test_outputs.py
15.7 kB
| from pathlib import Path | |
| import subprocess | |
| import pytest | |
| import json | |
| import tempfile | |
| APP_DIR = Path("/app") | |
| APIVALIDATOR = APP_DIR / "apivalidator" | |
| def run_command(cmd, cwd=None, check=True, input_data=None): | |
| """Run a shell command and return the result.""" | |
| result = subprocess.run( | |
| cmd, | |
| shell=True, | |
| capture_output=True, | |
| text=True, | |
| timeout=300, | |
| cwd=cwd or "/app", | |
| input=input_data | |
| ) | |
| if check and result.returncode != 0: | |
| pytest.fail(f"Command failed: {cmd}\nstdout: {result.stdout}\nstderr: {result.stderr}") | |
| return result | |
| def test_apivalidator_exists(): | |
| """Test that apivalidator script exists.""" | |
| assert APIVALIDATOR.exists(), "apivalidator script must exist" | |
| def test_apivalidator_is_executable(): | |
| """Test that apivalidator is executable.""" | |
| assert APIVALIDATOR.is_file(), "apivalidator must be a file" | |
| import os | |
| if hasattr(os, 'access'): | |
| assert os.access(APIVALIDATOR, os.X_OK), "apivalidator must be executable" | |
| def test_apivalidator_help(): | |
| """Test that apivalidator shows help message.""" | |
| result = run_command("python3 apivalidator --help", check=False) | |
| assert result.returncode == 0, "apivalidator --help should exit with code 0" | |
| assert "spec" in result.stdout.lower() or "--spec" in result.stdout, \ | |
| "Help message should mention --spec" | |
| def test_apivalidator_requires_spec(): | |
| """Test that apivalidator requires --spec argument.""" | |
| result = run_command("python3 apivalidator --base-url http://example.com --output test.json", check=False) | |
| assert result.returncode != 0, "apivalidator should fail without --spec" | |
| def test_apivalidator_requires_base_url(): | |
| """Test that apivalidator requires --base-url argument.""" | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: | |
| f.write('{"openapi": "3.0.0", "paths": {}}') | |
| spec_file = f.name | |
| result = run_command(f"python3 apivalidator --spec {spec_file} --output test.json", check=False) | |
| assert result.returncode != 0, "apivalidator should fail without --base-url" | |
| def test_apivalidator_requires_output(): | |
| """Test that apivalidator requires --output argument.""" | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: | |
| f.write('{"openapi": "3.0.0", "paths": {}}') | |
| spec_file = f.name | |
| result = run_command(f"python3 apivalidator --spec {spec_file} --base-url http://example.com", check=False) | |
| assert result.returncode != 0, "apivalidator should fail without --output" | |
| def test_apivalidator_parses_openapi_spec(): | |
| """Test that apivalidator can parse OpenAPI specification.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spec_file = Path(tmpdir) / "spec.json" | |
| spec_file.write_text(json.dumps({ | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": { | |
| "/users": { | |
| "get": { | |
| "responses": { | |
| "200": { | |
| "description": "Success", | |
| "content": { | |
| "application/json": { | |
| "schema": { | |
| "type": "array", | |
| "items": {"type": "object"} | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| })) | |
| output_file = Path(tmpdir) / "output.json" | |
| result = run_command( | |
| f"python3 apivalidator --spec {spec_file} --base-url http://example.com --output {output_file} --format json", | |
| check=False | |
| ) | |
| # Should parse the spec (may fail on actual HTTP request, but should parse) | |
| assert result.returncode in [0, 1], "Should parse spec even if HTTP request fails" | |
| def test_apivalidator_parses_yaml_spec(): | |
| """Test that apivalidator can parse YAML OpenAPI specification.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spec_file = Path(tmpdir) / "spec.yaml" | |
| spec_file.write_text(""" | |
| openapi: 3.0.0 | |
| info: | |
| title: Test API | |
| version: 1.0.0 | |
| paths: | |
| /users: | |
| get: | |
| responses: | |
| '200': | |
| description: Success | |
| """) | |
| output_file = Path(tmpdir) / "output.json" | |
| result = run_command( | |
| f"python3 apivalidator --spec {spec_file} --base-url http://example.com --output {output_file} --format json", | |
| check=False | |
| ) | |
| # Should parse YAML spec | |
| assert result.returncode in [0, 1], "Should parse YAML spec even if HTTP request fails" | |
| def test_apivalidator_generates_json_output(): | |
| """Test that apivalidator generates JSON output.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spec_file = Path(tmpdir) / "spec.json" | |
| spec_file.write_text(json.dumps({ | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": {} | |
| })) | |
| output_file = Path(tmpdir) / "output.json" | |
| run_command( | |
| f"python3 apivalidator --spec {spec_file} --base-url http://example.com --output {output_file} --format json", | |
| check=False | |
| ) | |
| if output_file.exists(): | |
| data = json.loads(output_file.read_text()) | |
| assert "summary" in data, "JSON output should contain summary" | |
| def test_apivalidator_generates_markdown_output(): | |
| """Test that apivalidator generates Markdown output.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spec_file = Path(tmpdir) / "spec.json" | |
| spec_file.write_text(json.dumps({ | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": {} | |
| })) | |
| output_file = Path(tmpdir) / "output.md" | |
| run_command( | |
| f"python3 apivalidator --spec {spec_file} --base-url http://example.com --output {output_file} --format markdown", | |
| check=False | |
| ) | |
| if output_file.exists(): | |
| content = output_file.read_text() | |
| assert "#" in content or "##" in content, "Markdown should contain headers" | |
| def test_apivalidator_generates_html_output(): | |
| """Test that apivalidator generates HTML output.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spec_file = Path(tmpdir) / "spec.json" | |
| spec_file.write_text(json.dumps({ | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": {} | |
| })) | |
| output_file = Path(tmpdir) / "output.html" | |
| run_command( | |
| f"python3 apivalidator --spec {spec_file} --base-url http://example.com --output {output_file} --format html", | |
| check=False | |
| ) | |
| if output_file.exists(): | |
| content = output_file.read_text() | |
| assert "<html" in content.lower() or "<!doctype" in content.lower(), \ | |
| "HTML should contain HTML structure" | |
| def test_apivalidator_handles_invalid_spec(): | |
| """Test that apivalidator handles invalid specification file.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spec_file = Path(tmpdir) / "invalid.json" | |
| spec_file.write_text("invalid json content {") | |
| output_file = Path(tmpdir) / "output.json" | |
| result = run_command( | |
| f"python3 apivalidator --spec {spec_file} --base-url http://example.com --output {output_file}", | |
| check=False | |
| ) | |
| assert result.returncode != 0, "Should exit with error for invalid spec" | |
| def test_apivalidator_handles_invalid_format(): | |
| """Test that apivalidator handles invalid output format.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spec_file = Path(tmpdir) / "spec.json" | |
| spec_file.write_text(json.dumps({ | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": {} | |
| })) | |
| output_file = Path(tmpdir) / "output.txt" | |
| result = run_command( | |
| f"python3 apivalidator --spec {spec_file} --base-url http://example.com --output {output_file} --format invalid", | |
| check=False | |
| ) | |
| assert result.returncode != 0, "Should exit with error for invalid format" | |
| def test_apivalidator_supports_timeout(): | |
| """Test that apivalidator supports timeout parameter.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spec_file = Path(tmpdir) / "spec.json" | |
| spec_file.write_text(json.dumps({ | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": { | |
| "/test": { | |
| "get": { | |
| "responses": {"200": {"description": "Success"}} | |
| } | |
| } | |
| } | |
| })) | |
| output_file = Path(tmpdir) / "output.json" | |
| result = run_command( | |
| f"python3 apivalidator --spec {spec_file} --base-url http://example.com --output {output_file} --timeout 5", | |
| check=False | |
| ) | |
| # Should accept timeout parameter (may fail on HTTP, but should not error on parameter) | |
| assert result.returncode in [0, 1], "Should accept timeout parameter" | |
| def test_apivalidator_supports_auth(): | |
| """Test that apivalidator supports authentication token.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spec_file = Path(tmpdir) / "spec.json" | |
| spec_file.write_text(json.dumps({ | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": { | |
| "/test": { | |
| "get": { | |
| "responses": {"200": {"description": "Success"}} | |
| } | |
| } | |
| } | |
| })) | |
| output_file = Path(tmpdir) / "output.json" | |
| result = run_command( | |
| f"python3 apivalidator --spec {spec_file} --base-url http://example.com --output {output_file} --auth 'Bearer token123'", | |
| check=False | |
| ) | |
| # Should accept auth parameter | |
| assert result.returncode in [0, 1], "Should accept auth parameter" | |
| def test_apivalidator_extracts_endpoints(): | |
| """Test that apivalidator extracts endpoints from specification.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spec_file = Path(tmpdir) / "spec.json" | |
| spec_file.write_text(json.dumps({ | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": { | |
| "/users": { | |
| "get": {"responses": {"200": {"description": "Success"}}} | |
| }, | |
| "/posts": { | |
| "get": {"responses": {"200": {"description": "Success"}}} | |
| } | |
| } | |
| })) | |
| output_file = Path(tmpdir) / "output.json" | |
| run_command( | |
| f"python3 apivalidator --spec {spec_file} --base-url http://example.com --output {output_file} --format json", | |
| check=False | |
| ) | |
| if output_file.exists(): | |
| data = json.loads(output_file.read_text()) | |
| # Should have information about endpoints | |
| assert "summary" in data or "endpoints" in data, \ | |
| "Output should contain endpoint information" | |
| def test_apivalidator_validates_response_schema(): | |
| """Test that apivalidator validates response schemas.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spec_file = Path(tmpdir) / "spec.json" | |
| spec_file.write_text(json.dumps({ | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": { | |
| "/users": { | |
| "get": { | |
| "responses": { | |
| "200": { | |
| "description": "Success", | |
| "content": { | |
| "application/json": { | |
| "schema": { | |
| "type": "object", | |
| "required": ["id", "name"], | |
| "properties": { | |
| "id": {"type": "integer"}, | |
| "name": {"type": "string"} | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| })) | |
| output_file = Path(tmpdir) / "output.json" | |
| run_command( | |
| f"python3 apivalidator --spec {spec_file} --base-url http://example.com --output {output_file} --format json --validate-response", | |
| check=False | |
| ) | |
| # Should attempt to validate response schema | |
| assert output_file.exists() or True, "Should process validation flag" | |
| def test_apivalidator_generates_test_cases(): | |
| """Test that apivalidator can generate test cases.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spec_file = Path(tmpdir) / "spec.json" | |
| spec_file.write_text(json.dumps({ | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": { | |
| "/users": { | |
| "get": { | |
| "responses": {"200": {"description": "Success"}} | |
| } | |
| } | |
| } | |
| })) | |
| output_file = Path(tmpdir) / "output.json" | |
| run_command( | |
| f"python3 apivalidator --spec {spec_file} --base-url http://example.com --output {output_file} --format json --generate-tests", | |
| check=False | |
| ) | |
| if output_file.exists(): | |
| data = json.loads(output_file.read_text()) | |
| # Should have test cases if generation is enabled | |
| assert "test_cases" in data or "tests" in data or len(data.get("endpoints", [])) > 0, \ | |
| "Output should contain test case information when --generate-tests is used" | |
| def test_apivalidator_filters_endpoints(): | |
| """Test that apivalidator can filter endpoints.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spec_file = Path(tmpdir) / "spec.json" | |
| spec_file.write_text(json.dumps({ | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": { | |
| "/users": { | |
| "get": {"responses": {"200": {"description": "Success"}}} | |
| }, | |
| "/posts": { | |
| "get": {"responses": {"200": {"description": "Success"}}} | |
| } | |
| } | |
| })) | |
| output_file = Path(tmpdir) / "output.json" | |
| result = run_command( | |
| f"python3 apivalidator --spec {spec_file} --base-url http://example.com --output {output_file} --format json --endpoints /users", | |
| check=False | |
| ) | |
| # Should accept endpoint filter | |
| assert result.returncode in [0, 1], "Should accept endpoint filter parameter" | |