Add unit tests for spec parser and auth
This commit is contained in:
182
tests/unit/test_auth.py
Normal file
182
tests/unit/test_auth.py
Normal file
@@ -0,0 +1,182 @@
|
||||
"""Unit tests for the auth configuration module."""
|
||||
import pytest
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from api_testgen.core.auth import AuthConfig, AuthType
|
||||
from api_testgen.core.exceptions import AuthConfigError, MissingSecuritySchemeError
|
||||
|
||||
|
||||
class TestAuthConfig:
|
||||
"""Tests for AuthConfig class."""
|
||||
|
||||
def test_add_api_key(self):
|
||||
"""Test adding API key authentication."""
|
||||
auth = AuthConfig()
|
||||
auth.add_api_key("test_api_key", header_name="X-API-Key", api_key="test123")
|
||||
|
||||
method = auth.get_auth_method("test_api_key")
|
||||
|
||||
assert method is not None
|
||||
assert method["type"] == AuthType.API_KEY
|
||||
assert method["header_name"] == "X-API-Key"
|
||||
assert method["api_key"] == "test123"
|
||||
|
||||
def test_add_bearer_token(self):
|
||||
"""Test adding Bearer token authentication."""
|
||||
auth = AuthConfig()
|
||||
auth.add_bearer("test_bearer", token="abc123", token_prefix="Bearer")
|
||||
|
||||
method = auth.get_auth_method("test_bearer")
|
||||
|
||||
assert method is not None
|
||||
assert method["type"] == AuthType.BEARER
|
||||
assert method["token"] == "abc123"
|
||||
assert method["token_prefix"] == "Bearer"
|
||||
|
||||
def test_add_basic_auth(self):
|
||||
"""Test adding Basic authentication."""
|
||||
auth = AuthConfig()
|
||||
auth.add_basic("test_basic", username="user", password="pass")
|
||||
|
||||
method = auth.get_auth_method("test_basic")
|
||||
|
||||
assert method is not None
|
||||
assert method["type"] == AuthType.BASIC
|
||||
assert method["username"] == "user"
|
||||
assert method["password"] == "pass"
|
||||
|
||||
def test_method_chaining(self):
|
||||
"""Test that add methods return self for chaining."""
|
||||
auth = AuthConfig()
|
||||
|
||||
result = auth.add_api_key("key1")
|
||||
assert result is auth
|
||||
|
||||
result = auth.add_bearer("key2")
|
||||
assert result is auth
|
||||
|
||||
result = auth.add_basic("key3")
|
||||
assert result is auth
|
||||
|
||||
def test_get_all_methods(self):
|
||||
"""Test getting all configured auth methods."""
|
||||
auth = AuthConfig()
|
||||
auth.add_api_key("api_key")
|
||||
auth.add_bearer("bearer")
|
||||
|
||||
methods = auth.get_all_methods()
|
||||
|
||||
assert len(methods) == 2
|
||||
assert "api_key" in methods
|
||||
assert "bearer" in methods
|
||||
|
||||
def test_get_headers_api_key(self):
|
||||
"""Test getting headers for API key auth."""
|
||||
auth = AuthConfig()
|
||||
auth.add_api_key("test", header_name="X-Custom-Key", api_key="mykey")
|
||||
|
||||
headers = auth.get_headers("test")
|
||||
|
||||
assert headers["X-Custom-Key"] == "mykey"
|
||||
|
||||
def test_get_headers_bearer(self):
|
||||
"""Test getting headers for Bearer auth."""
|
||||
auth = AuthConfig()
|
||||
auth.add_bearer("test", token="mytoken", token_prefix="Bearer")
|
||||
|
||||
headers = auth.get_headers("test")
|
||||
|
||||
assert headers["Authorization"] == "Bearer mytoken"
|
||||
|
||||
def test_get_headers_basic(self):
|
||||
"""Test getting headers for Basic auth."""
|
||||
import base64
|
||||
|
||||
auth = AuthConfig()
|
||||
auth.add_basic("test", username="user", password="pass")
|
||||
|
||||
headers = auth.get_headers("test")
|
||||
|
||||
expected = base64.b64encode(b"user:pass").decode()
|
||||
assert headers["Authorization"] == f"Basic {expected}"
|
||||
|
||||
def test_get_headers_unconfigured_scheme_raises_error(self):
|
||||
"""Test that getting headers for unconfigured scheme raises error."""
|
||||
auth = AuthConfig()
|
||||
|
||||
with pytest.raises(AuthConfigError):
|
||||
auth.get_headers("nonexistent")
|
||||
|
||||
def test_build_from_spec(self):
|
||||
"""Test building auth config from OpenAPI security schemes."""
|
||||
auth = AuthConfig()
|
||||
|
||||
security_schemes = {{
|
||||
"ApiKeyAuth": {{"type": "apiKey", "name": "X-API-Key", "in": "header"}},
|
||||
"BearerAuth": {{"type": "http", "scheme": "bearer"}},
|
||||
"BasicAuth": {{"type": "http", "scheme": "basic"}},
|
||||
}}
|
||||
|
||||
security_requirements = [
|
||||
{{"ApiKeyAuth": []}},
|
||||
{{"BearerAuth": []}},
|
||||
]
|
||||
|
||||
auth.build_from_spec(security_schemes, security_requirements)
|
||||
|
||||
assert auth.get_auth_method("ApiKeyAuth") is not None
|
||||
assert auth.get_auth_method("BearerAuth") is not None
|
||||
assert auth.get_auth_method("BasicAuth") is None
|
||||
|
||||
def test_build_from_spec_missing_scheme_raises_error(self):
|
||||
"""Test that missing security scheme raises error."""
|
||||
auth = AuthConfig()
|
||||
|
||||
security_schemes = {{
|
||||
"ApiKeyAuth": {{"type": "apiKey", "name": "X-API-Key", "in": "header"}},
|
||||
}}
|
||||
|
||||
security_requirements = [
|
||||
{{"MissingScheme": []}},
|
||||
]
|
||||
|
||||
with pytest.raises(MissingSecuritySchemeError):
|
||||
auth.build_from_spec(security_schemes, security_requirements)
|
||||
|
||||
def test_generate_pytest_auth_code(self):
|
||||
"""Test generating pytest authentication code."""
|
||||
auth = AuthConfig()
|
||||
auth.add_api_key("test_key", header_name="X-Api-Key", api_key="key123")
|
||||
|
||||
code = auth.generate_auth_code("test_key", "pytest")
|
||||
|
||||
assert "X-Api-Key" in code
|
||||
assert "key123" in code
|
||||
|
||||
def test_generate_jest_auth_code(self):
|
||||
"""Test generating Jest authentication code."""
|
||||
auth = AuthConfig()
|
||||
auth.add_bearer("test_bearer", token="token123")
|
||||
|
||||
code = auth.generate_auth_code("test_bearer", "jest")
|
||||
|
||||
assert "Authorization" in code
|
||||
assert "token123" in code
|
||||
|
||||
def test_generate_go_auth_code(self):
|
||||
"""Test generating Go authentication code."""
|
||||
auth = AuthConfig()
|
||||
auth.add_api_key("test_key", header_name="X-Api-Key")
|
||||
|
||||
code = auth.generate_auth_code("test_key", "go")
|
||||
|
||||
assert "X-Api-Key" in code
|
||||
|
||||
def test_generate_auth_code_unconfigured_scheme(self):
|
||||
"""Test generating auth code for unconfigured scheme returns empty."""
|
||||
auth = AuthConfig()
|
||||
|
||||
code = auth.generate_auth_code("nonexistent", "pytest")
|
||||
|
||||
assert code == ""
|
||||
Reference in New Issue
Block a user