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 " 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"