import pytest
from ri import (
RiAuthModule,
RiAuthConfig,
RiJWTManager,
RiJWTValidationOptions,
RiSessionManager,
RiSession,
RiPermissionManager,
RiPermission,
RiRole,
RiOAuthManager,
RiOAuthToken,
RiOAuthUserInfo,
RiOAuthProvider,
RiJWTRevocationList,
RiRevokedTokenInfo,
)
class TestRiAuthModule:
def test_auth_config_creation(self):
config = RiAuthConfig(
enabled=True,
jwt_secret="test-secret-key",
jwt_expiry_secs=3600,
session_timeout_secs=86400,
oauth_providers=[],
enable_api_keys=True,
enable_session_auth=True
)
assert config is not None
assert config.enabled is True
assert config.jwt_expiry_secs == 3600
def test_auth_module_creation(self):
config = RiAuthConfig(
enabled=True,
jwt_secret="test-secret-key",
jwt_expiry_secs=3600,
session_timeout_secs=86400
)
auth_module = RiAuthModule(config)
assert auth_module is not None
assert auth_module.is_enabled is True
class TestRiJWTManager:
def test_jwt_manager_creation(self):
jwt_manager = RiJWTManager("test-secret", 3600)
assert jwt_manager is not None
def test_token_generation(self):
jwt_manager = RiJWTManager("test-secret", 3600)
token = jwt_manager.py_generate_token("user123", ["user"], ["read:data"])
assert token is not None
assert len(token) > 0
def test_token_validation(self):
jwt_manager = RiJWTManager("test-secret", 3600)
token = jwt_manager.py_generate_token("user123", ["user"], ["read:data"])
claims = jwt_manager.py_validate_token(token)
assert claims is not None
class TestRiJWTValidationOptions:
def test_validation_options_creation(self):
options = RiJWTValidationOptions(
validate_exp=True,
validate_iat=True,
required_roles=["user"],
required_permissions=["read:data"]
)
assert options is not None
class TestRiSessionManager:
def test_session_manager_creation(self):
session_manager = RiSessionManager(86400)
assert session_manager is not None
class TestRiSession:
def test_session_creation(self):
session = RiSession(
id=None,
user_id="user123",
created_at=None,
last_accessed=None,
expires_at=None,
data=None,
ip_address=None,
user_agent=None
)
assert session.user_id == "user123"
class TestRiPermissionManager:
def test_permission_manager_creation(self):
perm_manager = RiPermissionManager()
assert perm_manager is not None
class TestRiPermission:
def test_permission_creation(self):
perm = RiPermission(
id=None,
name="read:users",
description="Can read user data",
resource="users",
action="read"
)
assert perm is not None
class TestRiRole:
def test_role_creation(self):
role = RiRole(
id=None,
name="admin",
description="Administrator role",
permissions=["read:users", "write:users"],
is_system=False
)
assert role.name == "admin"
class TestRiOAuthManager:
def test_oauth_manager_creation(self):
oauth_manager = RiOAuthManager()
assert oauth_manager is not None
class TestRiOAuthToken:
def test_oauth_token_creation(self):
token = RiOAuthToken(
access_token="access_123",
token_type="Bearer",
refresh_token="refresh_456",
scope="openid email",
expires_in=3600
)
assert token is not None
class TestRiOAuthUserInfo:
def test_oauth_user_info_creation(self):
user_info = RiOAuthUserInfo(
id="user123",
email="user@example.com",
name="John Doe",
avatar_url=None,
provider="google"
)
assert user_info.id == "user123"
assert user_info.email == "user@example.com"
class TestRiOAuthProvider:
def test_oauth_provider_creation(self):
provider = RiOAuthProvider(
id="google",
name="Google",
client_id="client_123",
client_secret="secret_456",
auth_url="https://accounts.google.com/o/oauth2/auth",
token_url="https://oauth2.googleapis.com/token",
user_info_url="https://openidconnect.googleapis.com/v1/userinfo",
redirect_uri="http://localhost/callback",
scopes=["openid", "email"],
enabled=True
)
assert provider.name == "Google"
assert provider.client_id == "client_123"
class TestRiJWTRevocationList:
def test_revocation_list_creation(self):
revocation_list = RiJWTRevocationList()
assert revocation_list is not None
def test_token_revocation(self):
revocation_list = RiJWTRevocationList()
revocation_list.revoke_token("token_123", "user123", "User logout", 3600)
is_revoked = revocation_list.is_revoked("token_123")
assert is_revoked is True
class TestRiRevokedTokenInfo:
def test_revoked_token_info_creation(self):
revoked_info = RiRevokedTokenInfo(
token_id="token_123",
user_id="user123",
revoked_at=0,
expires_at=3600,
reason="User logout"
)
assert revoked_info.token_id == "token_123"
assert revoked_info.reason == "User logout"
if __name__ == "__main__":
pytest.main([__file__, "-v"])