Skip to content

Commit ddf7426

Browse files
committed
add invalid token test
1 parent 8252a30 commit ddf7426

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

tests/unit/server/test_auth.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# This source code is licensed under the terms described in the LICENSE file in
55
# the root directory of this source tree.
66

7+
import base64
78
from unittest.mock import AsyncMock, patch
89

910
import pytest
@@ -16,6 +17,7 @@
1617
AuthProviderConfig,
1718
AuthProviderType,
1819
TokenValidationResult,
20+
get_attributes_from_claims,
1921
)
2022

2123

@@ -435,7 +437,7 @@ async def mock_jwks_response(*args, **kwargs):
435437
"kty": "oct",
436438
"alg": "HS256",
437439
"use": "sig",
438-
"k": "MTIzNDU2Nzg5MA", # Base64-encoded "1234567890"
440+
"k": base64.b64encode(b"foobarbaz").decode(),
439441
}
440442
]
441443
},
@@ -446,15 +448,14 @@ async def mock_jwks_response(*args, **kwargs):
446448
def jwt_token_valid():
447449
from jose import jwt
448450

449-
# correctly signed jwt token with "kid" in header
450451
return jwt.encode(
451452
{
452453
"sub": "my-user",
453454
"groups": ["group1", "group2"],
454455
"scope": "foo bar",
455456
"aud": "llama-stack",
456457
},
457-
key="1234567890",
458+
key="foobarbaz",
458459
algorithm="HS256",
459460
headers={"kid": "1234567890"},
460461
)
@@ -467,4 +468,52 @@ def test_valid_oauth2_authentication(oauth2_client, jwt_token_valid):
467468
assert response.json() == {"message": "Authentication successful"}
468469

469470

471+
@patch("httpx.AsyncClient.get", new=mock_jwks_response)
472+
def test_invalid_oauth2_authentication(oauth2_client, invalid_token):
473+
response = oauth2_client.get("/test", headers={"Authorization": f"Bearer {invalid_token}"})
474+
assert response.status_code == 401
475+
assert "Invalid JWT token" in response.json()["error"]["message"]
476+
477+
478+
def test_get_attributes_from_claims():
479+
claims = {
480+
"sub": "my-user",
481+
"groups": ["group1", "group2"],
482+
"scope": "foo bar",
483+
"aud": "llama-stack",
484+
}
485+
attributes = get_attributes_from_claims(claims, {"sub": "roles", "groups": "teams"})
486+
assert attributes.roles == ["my-user"]
487+
assert attributes.teams == ["group1", "group2"]
488+
489+
claims = {
490+
"sub": "my-user",
491+
"tenant": "my-tenant",
492+
}
493+
attributes = get_attributes_from_claims(claims, {"sub": "roles", "tenant": "namespaces"})
494+
assert attributes.roles == ["my-user"]
495+
assert attributes.namespaces == ["my-tenant"]
496+
497+
claims = {
498+
"sub": "my-user",
499+
"username": "my-username",
500+
"tenant": "my-tenant",
501+
"groups": ["group1", "group2"],
502+
"team": "my-team",
503+
}
504+
attributes = get_attributes_from_claims(
505+
claims,
506+
{
507+
"sub": "roles",
508+
"tenant": "namespaces",
509+
"username": "roles",
510+
"team": "teams",
511+
"groups": "teams",
512+
},
513+
)
514+
assert set(attributes.roles) == {"my-user", "my-username"}
515+
assert set(attributes.teams) == {"my-team", "group1", "group2"}
516+
assert attributes.namespaces == ["my-tenant"]
517+
518+
470519
# TODO: add more tests for oauth2 token provider

0 commit comments

Comments
 (0)