test_security.py 882 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import pytest
  2. from clean_python import PermissionDenied
  3. from clean_python.fastapi import RequiresScope
  4. from clean_python.oauth2 import Token
  5. @pytest.fixture
  6. def token() -> Token:
  7. return Token(claims={"sub": "abc123", "scope": "a b", "username": "foo"})
  8. @pytest.fixture
  9. def token_multitenant() -> Token:
  10. return Token(
  11. claims={
  12. "sub": "abc123",
  13. "scope": "a b",
  14. "username": "foo",
  15. "tenant": 1,
  16. "tenant_name": "bar",
  17. }
  18. )
  19. @pytest.mark.parametrize("scope", ["a", "b"])
  20. async def test_requires_scope(token, scope):
  21. await RequiresScope(scope)(token)
  22. async def test_requires_scope_err(token):
  23. with pytest.raises(PermissionDenied):
  24. await RequiresScope("c")(token)
  25. def test_requries_scope_no_spaces_allowed():
  26. with pytest.raises(AssertionError):
  27. RequiresScope("c ")