Skip to content

Commit

Permalink
feat: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhuser committed Dec 5, 2024
1 parent e3666eb commit 91a3525
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
name: Test

on: pull_request

jobs:
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dev = [
"pydantic-settings>=2.6.1",
"pytest-asyncio>=0.24.0",
"pytest-cov>=6.0.0",
"pytest-dotenv>=0.5.2",
"pytest-mock>=3.14.0",
"pytest>=8.3.4",
"respx>=0.21.1",
Expand All @@ -63,3 +64,4 @@ dev = [
addopts = "-v -x --cov=src --cov-report=term-missing:skip-covered"
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"
env_files = ["tests/.env.test"]
2 changes: 1 addition & 1 deletion src/fastapi_zitadel_auth/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, List, Optional
from typing import Any, List
from pydantic import BaseModel, Field


Expand Down
3 changes: 3 additions & 0 deletions tests/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ZITADEL_HOST=https://test-zitadel-xs2hs.zitadel.cloud
ZITADEL_PROJECT_ID=123456789
OAUTH_CLIENT_ID=7777777777
92 changes: 92 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
Test the auth config module
"""

import pytest
from pydantic import ValidationError

from fastapi_zitadel_auth.config import AuthConfig


@pytest.fixture
def valid_config_data() -> dict:
"""
Valid configuration data for testing
"""
return {
"client_id": "test-client-123",
"project_id": "proj-123",
"base_url": "https://auth.example.com/",
}


class TestAuthConfig:
"""Test suite for AuthConfig class"""

def test_valid_config(self, valid_config_data):
"""Test creating config with valid data"""
config = AuthConfig(**valid_config_data)
assert config.client_id == "test-client-123"
assert config.project_id == "proj-123"
assert str(config.base_url) == "https://auth.example.com/"
assert config.algorithm == "RS256" # default value
assert config.scopes is None # default value

def test_computed_issuer(self, valid_config_data):
"""Test issuer computed field removes trailing slash"""
config = AuthConfig(**valid_config_data)
assert config.issuer == "https://auth.example.com"

# Test without trailing slash
valid_config_data["base_url"] = "https://auth.example.com"
config = AuthConfig(**valid_config_data)
assert config.issuer == "https://auth.example.com"

def test_computed_urls(self, valid_config_data):
"""Test computed URL fields"""
config = AuthConfig(**valid_config_data)

assert config.jwks_url == "https://auth.example.com/oauth/v2/keys"
assert config.authorization_url == "https://auth.example.com/oauth/v2/authorize"
assert config.token_url == "https://auth.example.com/oauth/v2/token"

def test_custom_scopes(self, valid_config_data):
"""Test configuration with custom scopes"""
valid_config_data["scopes"] = {
"openid": "OpenID scope",
"profile": "Profile information",
}
config = AuthConfig(**valid_config_data)
assert config.scopes == {
"openid": "OpenID scope",
"profile": "Profile information",
}

def test_custom_algorithm(self, valid_config_data):
"""Test configuration with custom algorithm"""
valid_config_data["algorithm"] = "ES256"
config = AuthConfig(**valid_config_data)
assert config.algorithm == "ES256"

def test_invalid_url(self, valid_config_data):
"""Test validation error for invalid URL"""
valid_config_data["base_url"] = "not-a-url"
with pytest.raises(ValidationError) as exc_info:
AuthConfig(**valid_config_data)
errors = exc_info.value.errors()
assert any(error["type"] == "url_parsing" for error in errors)

def test_missing_required_fields(self):
"""Test validation error for missing required fields"""
with pytest.raises(ValidationError) as exc_info:
AuthConfig()
errors = exc_info.value.errors()
required_fields = {"client_id", "project_id", "base_url"}
error_fields = {error["loc"][0] for error in errors}
assert required_fields.issubset(error_fields)

def test_config_immutability(self, valid_config_data):
"""Test that config is immutable after creation"""
config = AuthConfig(**valid_config_data)
with pytest.raises(Exception):
config.client_id = "new-client" # type: ignore
15 changes: 15 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 91a3525

Please sign in to comment.