|
1 | | -""" |
2 | | -Unit tests for EntraID app roles JWT claim extraction. |
3 | | -
|
4 | | -This module tests the get_app_roles_from_id_token method to ensure it correctly |
5 | | -extracts app roles from Microsoft EntraID JWT tokens and prevents regressions. |
6 | | -""" |
7 | | - |
8 | | -import pytest |
9 | 1 | import jwt |
10 | 2 |
|
11 | 3 | from litellm.proxy.management_endpoints.ui_sso import MicrosoftSSOHandler |
| 4 | +from litellm.proxy.management_endpoints.types import get_litellm_user_role |
| 5 | +from litellm.proxy._types import LitellmUserRoles |
| 6 | + |
| 7 | + |
| 8 | +def test_extracts_proxy_admin_role_from_jwt(): |
| 9 | + """Ensure supported app roles like 'proxy_admin' are extracted from the id_token.""" |
| 10 | + payload = { |
| 11 | + "sub": "user123", |
| 12 | + |
| 13 | + "app_roles": ["proxy_admin"], |
| 14 | + "aud": "litellm-app", |
| 15 | + "iss": "https://login.microsoftonline.com/tenant-id/v2.0", |
| 16 | + "exp": 9999999999, |
| 17 | + } |
| 18 | + |
| 19 | + token = jwt.encode(payload, "secret", algorithm="HS256") |
| 20 | + roles = MicrosoftSSOHandler.get_app_roles_from_id_token(token) |
| 21 | + |
| 22 | + assert roles == ["proxy_admin"] |
| 23 | + |
| 24 | + |
| 25 | +def test_maps_internal_user_role(): |
| 26 | + """Ensure internal_user role is correctly mapped to LitellmUserRoles.""" |
| 27 | + payload = { |
| 28 | + "sub": "user456", |
| 29 | + |
| 30 | + "app_roles": ["internal_user"], |
| 31 | + "aud": "litellm-app", |
| 32 | + "iss": "https://login.microsoftonline.com/tenant-id/v2.0", |
| 33 | + "exp": 9999999999, |
| 34 | + } |
| 35 | + |
| 36 | + token = jwt.encode(payload, "secret", algorithm="HS256") |
| 37 | + roles = MicrosoftSSOHandler.get_app_roles_from_id_token(token) |
| 38 | + |
| 39 | + # Map to LitellmUserRoles |
| 40 | + chosen = None |
| 41 | + for r in roles: |
| 42 | + mapped = get_litellm_user_role(r) |
| 43 | + if mapped is not None: |
| 44 | + chosen = mapped |
| 45 | + break |
| 46 | + |
| 47 | + assert chosen == LitellmUserRoles.INTERNAL_USER |
| 48 | + |
| 49 | + |
| 50 | +def test_maps_proxy_admin_viewer_role(): |
| 51 | + """Ensure proxy_admin_viewer role is correctly mapped.""" |
| 52 | + payload = { |
| 53 | + "sub": "user789", |
| 54 | + |
| 55 | + "app_roles": ["proxy_admin_viewer"], |
| 56 | + "aud": "litellm-app", |
| 57 | + "iss": "https://login.microsoftonline.com/tenant-id/v2.0", |
| 58 | + "exp": 9999999999, |
| 59 | + } |
| 60 | + |
| 61 | + token = jwt.encode(payload, "secret", algorithm="HS256") |
| 62 | + roles = MicrosoftSSOHandler.get_app_roles_from_id_token(token) |
| 63 | + |
| 64 | + chosen = None |
| 65 | + for r in roles: |
| 66 | + mapped = get_litellm_user_role(r) |
| 67 | + if mapped is not None: |
| 68 | + chosen = mapped |
| 69 | + break |
| 70 | + |
| 71 | + assert chosen == LitellmUserRoles.PROXY_ADMIN_VIEW_ONLY |
| 72 | + |
| 73 | + |
| 74 | +def test_defaults_to_internal_user_viewer_when_no_role(): |
| 75 | + """Ensure default role is internal_user_viewer when no app role is present.""" |
| 76 | + payload = { |
| 77 | + "sub": "user_no_role", |
| 78 | + |
| 79 | + "aud": "litellm-app", |
| 80 | + "iss": "https://login.microsoftonline.com/tenant-id/v2.0", |
| 81 | + "exp": 9999999999, |
| 82 | + } |
| 83 | + |
| 84 | + token = jwt.encode(payload, "secret", algorithm="HS256") |
| 85 | + roles = MicrosoftSSOHandler.get_app_roles_from_id_token(token) |
| 86 | + |
| 87 | + assert roles == [] |
12 | 88 |
|
| 89 | + # Default role would be internal_user_viewer |
| 90 | + default_role = LitellmUserRoles.INTERNAL_USER_VIEW_ONLY |
| 91 | + assert default_role.value == "internal_user_viewer" |
13 | 92 |
|
14 | | -class TestEntraIDAppRoles: |
15 | | - """Test EntraID app roles extraction from JWT tokens""" |
16 | | - |
17 | | - def test_get_app_roles_from_id_token_works_without_roles(self): |
18 | | - """Test that JWT token works fine without app_roles claim""" |
19 | | - # Arrange - Token without app_roles (normal user) |
20 | | - payload = { |
21 | | - "sub": "user123", |
22 | | - |
23 | | - "aud": "litellm-app", |
24 | | - "iss": "https://login.microsoftonline.com/tenant-id/v2.0", |
25 | | - "exp": 9999999999, |
26 | | - } |
27 | | - no_roles_token = jwt.encode(payload, "secret", algorithm="HS256") |
28 | | - |
29 | | - # Act |
30 | | - result = MicrosoftSSOHandler.get_app_roles_from_id_token(no_roles_token) |
31 | | - |
32 | | - # Assert - Should return empty list, not error |
33 | | - assert result == [] |
34 | | - assert len(result) == 0 |
35 | | - |
36 | | - def test_get_app_roles_from_id_token_assigns_roles_when_present(self): |
37 | | - """Test that valid app roles are properly assigned when present""" |
38 | | - # Arrange - Token with valid roles |
39 | | - payload = { |
40 | | - "sub": "user123", |
41 | | - |
42 | | - "app_roles": ["proxy_admin"], |
43 | | - "aud": "litellm-app", |
44 | | - "iss": "https://login.microsoftonline.com/tenant-id/v2.0", |
45 | | - "exp": 9999999999, |
46 | | - } |
47 | | - valid_roles_token = jwt.encode(payload, "secret", algorithm="HS256") |
48 | | - |
49 | | - # Act |
50 | | - result = MicrosoftSSOHandler.get_app_roles_from_id_token(valid_roles_token) |
51 | | - |
52 | | - # Assert - Should extract the role |
53 | | - assert result == ["proxy_admin"] |
54 | | - assert len(result) == 1 |
55 | | - assert "proxy_admin" in result |
|
0 commit comments