|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +from jwt import PyJWKClient, PyJWK |
| 5 | +from jwt.exceptions import PyJWKClientError, DecodeError |
| 6 | +from fastapi.security import SecurityScopes, HTTPAuthorizationCredentials |
| 7 | + |
| 8 | +from common import AppConfig |
| 9 | +from common.config import AuthConfig |
| 10 | +from http_app.routes.auth import ( |
| 11 | + MissingAuthorizationServerException, |
| 12 | + _jwks_client, |
| 13 | + UnauthenticatedException, |
| 14 | + decode_jwt, |
| 15 | + UnauthorizedException, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +def test_jwks_client_raises_without_jwks_url(): |
| 20 | + with pytest.raises(MissingAuthorizationServerException): |
| 21 | + _jwks_client(config=AppConfig(AUTH=AuthConfig(JWKS_URL=None))) |
| 22 | + |
| 23 | + |
| 24 | +def test_jwks_client_returns_a_client_with_jwks_url(): |
| 25 | + result = _jwks_client(config=AppConfig(AUTH=AuthConfig(JWKS_URL="http://test.com"))) |
| 26 | + assert isinstance(result, PyJWKClient) |
| 27 | + |
| 28 | + |
| 29 | +async def test_decode_jwt_raises_without_token(): |
| 30 | + with pytest.raises(UnauthenticatedException): |
| 31 | + await decode_jwt( |
| 32 | + security_scopes=SecurityScopes(), |
| 33 | + config=AppConfig(), |
| 34 | + jwks_client=MagicMock(), |
| 35 | + token=None, |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize("exception", (PyJWKClientError, DecodeError)) |
| 40 | +async def test_decode_jwt_raises_if_jwks_client_fails(exception): |
| 41 | + mock_jwks_client = MagicMock(spec=PyJWKClient) |
| 42 | + mock_jwks_client.get_signing_key_from_jwt = MagicMock(side_effect=exception) |
| 43 | + with pytest.raises(UnauthorizedException): |
| 44 | + await decode_jwt( |
| 45 | + security_scopes=SecurityScopes(), |
| 46 | + config=AppConfig(), |
| 47 | + jwks_client=mock_jwks_client, |
| 48 | + token=HTTPAuthorizationCredentials( |
| 49 | + scheme="bearer", credentials="some_token" |
| 50 | + ), |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +async def test_decode_jwt_raises_if_decode_fails(): |
| 55 | + returned_key = MagicMock(spec=PyJWK) |
| 56 | + returned_key.key = "some_key" |
| 57 | + mock_jwks_client = MagicMock(spec=PyJWKClient) |
| 58 | + mock_jwks_client.get_signing_key_from_jwt = MagicMock(return_value=returned_key) |
| 59 | + |
| 60 | + with pytest.raises(UnauthorizedException): |
| 61 | + await decode_jwt( |
| 62 | + security_scopes=SecurityScopes(), |
| 63 | + config=AppConfig(), |
| 64 | + jwks_client=mock_jwks_client, |
| 65 | + token=HTTPAuthorizationCredentials( |
| 66 | + # The token cannot be decrypted and will trigger the exception |
| 67 | + scheme="bearer", |
| 68 | + credentials="some_token", |
| 69 | + ), |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +async def test_decode_jwt_returns_the_decoded_jwt_payload(): |
| 74 | + returned_key = MagicMock(spec=PyJWK) |
| 75 | + returned_key.key = "some_key" |
| 76 | + mock_jwks_client = MagicMock(spec=PyJWKClient) |
| 77 | + mock_jwks_client.get_signing_key_from_jwt = MagicMock(return_value=returned_key) |
| 78 | + |
| 79 | + with patch("jwt.decode", return_value={"decoded": "token"}): |
| 80 | + result = await decode_jwt( |
| 81 | + security_scopes=SecurityScopes(), |
| 82 | + config=AppConfig(), |
| 83 | + jwks_client=mock_jwks_client, |
| 84 | + token=HTTPAuthorizationCredentials( |
| 85 | + # The token cannot be decrypted and will trigger the exception |
| 86 | + scheme="bearer", |
| 87 | + credentials="some_token", |
| 88 | + ), |
| 89 | + ) |
| 90 | + |
| 91 | + assert result == {"decoded": "token"} |
0 commit comments