factuges-document-signing-s.../tests/api/test_authentication.py

40 lines
1.2 KiB
Python
Raw Permalink Normal View History

import asyncio
from types import SimpleNamespace
import pytest
from signing_service.api import authentication
from signing_service.application.errors import (
AuthenticationRequiredError,
InvalidCredentialsError,
)
class Credentials:
def __init__(self, value: str, scheme: str = "Bearer") -> None:
self.credentials = value
self.scheme = scheme
def test_missing_credential_is_rejected() -> None:
with pytest.raises(AuthenticationRequiredError):
asyncio.run(authentication.require_service_authentication(None))
def test_invalid_credential_is_rejected(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
authentication, "get_settings", lambda: SimpleNamespace(service_api_key="valid")
)
with pytest.raises(InvalidCredentialsError):
asyncio.run(authentication.require_service_authentication(Credentials("wrong")))
def test_valid_credential_is_accepted(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
authentication, "get_settings", lambda: SimpleNamespace(service_api_key="valid")
)
assert (
asyncio.run(authentication.require_service_authentication(Credentials("valid")))
is None
)