|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/lestrrat-go/jwx/v2/jwk" |
| 8 | + "github.com/lestrrat-go/jwx/v2/jwt" |
| 9 | + "github.com/opentdf/platform/service/logger" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestContextWithAuthNInfo(t *testing.T) { |
| 14 | + // Create mock JWK, JWT, and raw token |
| 15 | + mockJWK, _ := jwk.FromRaw([]byte("mockKey")) |
| 16 | + mockJWT, _ := jwt.NewBuilder().Build() |
| 17 | + rawToken := "mockRawToken" |
| 18 | + |
| 19 | + // Initialize context |
| 20 | + ctx := context.Background() |
| 21 | + newCtx := ContextWithAuthNInfo(ctx, mockJWK, mockJWT, rawToken) |
| 22 | + |
| 23 | + // Assert that the context contains the correct values |
| 24 | + value := newCtx.Value(authnContextKey) |
| 25 | + testAuthContext, ok := value.(*authContext) |
| 26 | + assert.True(t, ok) |
| 27 | + assert.NotNil(t, testAuthContext) |
| 28 | + assert.Equal(t, mockJWK, testAuthContext.key, "JWK should match") |
| 29 | + assert.Equal(t, mockJWT, testAuthContext.accessToken, "JWT should match") |
| 30 | + assert.Equal(t, rawToken, testAuthContext.rawToken, "Raw token should match") |
| 31 | +} |
| 32 | + |
| 33 | +func TestGetJWKFromContext(t *testing.T) { |
| 34 | + // Create mock context with JWK |
| 35 | + mockJWK, _ := jwk.FromRaw([]byte("mockKey")) |
| 36 | + ctx := ContextWithAuthNInfo(context.Background(), mockJWK, nil, "") |
| 37 | + |
| 38 | + // Retrieve the JWK and assert |
| 39 | + retrievedJWK := GetJWKFromContext(ctx, logger.CreateTestLogger()) |
| 40 | + assert.NotNil(t, retrievedJWK, "JWK should not be nil") |
| 41 | + assert.Equal(t, mockJWK, retrievedJWK, "Retrieved JWK should match the mock JWK") |
| 42 | +} |
| 43 | + |
| 44 | +func TestGetAccessTokenFromContext(t *testing.T) { |
| 45 | + // Create mock context with JWT |
| 46 | + mockJWT, _ := jwt.NewBuilder().Build() |
| 47 | + ctx := ContextWithAuthNInfo(context.Background(), nil, mockJWT, "") |
| 48 | + |
| 49 | + // Retrieve the JWT and assert |
| 50 | + retrievedJWT := GetAccessTokenFromContext(ctx, logger.CreateTestLogger()) |
| 51 | + assert.NotNil(t, retrievedJWT, "Access token should not be nil") |
| 52 | + assert.Equal(t, mockJWT, retrievedJWT, "Retrieved JWT should match the mock JWT") |
| 53 | +} |
| 54 | + |
| 55 | +func TestGetRawAccessTokenFromContext(t *testing.T) { |
| 56 | + // Create mock context with raw token |
| 57 | + rawToken := "mockRawToken" |
| 58 | + ctx := ContextWithAuthNInfo(context.Background(), nil, nil, rawToken) |
| 59 | + |
| 60 | + // Retrieve the raw token and assert |
| 61 | + retrievedRawToken := GetRawAccessTokenFromContext(ctx, logger.CreateTestLogger()) |
| 62 | + assert.Equal(t, rawToken, retrievedRawToken, "Retrieved raw token should match the mock raw token") |
| 63 | +} |
| 64 | + |
| 65 | +func TestGetContextDetailsInvalidType(t *testing.T) { |
| 66 | + // Create a context with an invalid type |
| 67 | + ctx := context.WithValue(context.Background(), authnContextKey, "invalidType") |
| 68 | + |
| 69 | + // Assert that GetJWKFromContext handles the invalid type correctly |
| 70 | + retrievedJWK := GetJWKFromContext(ctx, logger.CreateTestLogger()) |
| 71 | + assert.Nil(t, retrievedJWK, "JWK should be nil when context value is invalid") |
| 72 | +} |
0 commit comments