|
| 1 | +// Copyright (c) 2025 A Bit of Help, Inc. |
| 2 | + |
| 3 | +package jwt_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + autherrors "github.com/abitofhelp/servicelib/auth/errors" |
| 11 | + "github.com/abitofhelp/servicelib/auth/jwt" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "go.uber.org/zap" |
| 15 | +) |
| 16 | + |
| 17 | +// TestRemoteValidationSuccess tests the case where remote validation succeeds |
| 18 | +func TestRemoteValidationSuccess(t *testing.T) { |
| 19 | + logger := zap.NewNop() |
| 20 | + config := jwt.Config{ |
| 21 | + SecretKey: "test-secret", |
| 22 | + TokenDuration: 1 * time.Hour, |
| 23 | + Issuer: "test-issuer", |
| 24 | + } |
| 25 | + service := jwt.NewService(config, logger) |
| 26 | + ctx := context.Background() |
| 27 | + |
| 28 | + // Create expected claims |
| 29 | + expectedClaims := &jwt.Claims{ |
| 30 | + UserID: "remote-user-123", |
| 31 | + Roles: []string{"admin", "remote-user"}, |
| 32 | + Scopes: []string{"read", "write"}, |
| 33 | + Resources: []string{"resource1", "resource2"}, |
| 34 | + } |
| 35 | + |
| 36 | + // Set up a mock validator that succeeds |
| 37 | + mockValidator := NewSuccessfulMockValidator(expectedClaims) |
| 38 | + |
| 39 | + // Set the mock validator as the remote validator |
| 40 | + service.WithRemoteValidator(jwt.RemoteConfig{}) |
| 41 | + // Access the private field using reflection |
| 42 | + setRemoteValidator(t, service, mockValidator) |
| 43 | + |
| 44 | + // Test with any token (the mock will ignore the actual token) |
| 45 | + claims, err := service.ValidateToken(ctx, "any-token") |
| 46 | + |
| 47 | + // Verify the mock was called |
| 48 | + assert.True(t, mockValidator.Called) |
| 49 | + |
| 50 | + // Verify the result |
| 51 | + assert.NoError(t, err) |
| 52 | + assert.NotNil(t, claims) |
| 53 | + assert.Equal(t, expectedClaims.UserID, claims.UserID) |
| 54 | + assert.Equal(t, expectedClaims.Roles, claims.Roles) |
| 55 | + assert.Equal(t, expectedClaims.Scopes, claims.Scopes) |
| 56 | + assert.Equal(t, expectedClaims.Resources, claims.Resources) |
| 57 | +} |
| 58 | + |
| 59 | +// TestRemoteValidationFailure tests the case where remote validation fails with a non-NotImplemented error |
| 60 | +func TestRemoteValidationFailure(t *testing.T) { |
| 61 | + logger := zap.NewNop() |
| 62 | + config := jwt.Config{ |
| 63 | + SecretKey: "test-secret", |
| 64 | + TokenDuration: 1 * time.Hour, |
| 65 | + Issuer: "test-issuer", |
| 66 | + } |
| 67 | + service := jwt.NewService(config, logger) |
| 68 | + ctx := context.Background() |
| 69 | + |
| 70 | + // Generate a valid token for local validation |
| 71 | + userID := "user123" |
| 72 | + roles := []string{"admin", "user"} |
| 73 | + token, err := service.GenerateToken(ctx, userID, roles, []string{}, []string{}) |
| 74 | + require.NoError(t, err) |
| 75 | + require.NotEmpty(t, token) |
| 76 | + |
| 77 | + // Set up a mock validator that fails with a custom error |
| 78 | + customError := autherrors.WithMessage(autherrors.ErrInvalidToken, "remote validation failed") |
| 79 | + mockValidator := NewFailingMockValidator(customError) |
| 80 | + |
| 81 | + // Set the mock validator as the remote validator |
| 82 | + service.WithRemoteValidator(jwt.RemoteConfig{}) |
| 83 | + // Access the private field using reflection |
| 84 | + setRemoteValidator(t, service, mockValidator) |
| 85 | + |
| 86 | + // Test validation - should fall back to local validation and succeed |
| 87 | + claims, err := service.ValidateToken(ctx, token) |
| 88 | + |
| 89 | + // Verify the mock was called |
| 90 | + assert.True(t, mockValidator.Called) |
| 91 | + |
| 92 | + // Verify the result - should succeed with local validation |
| 93 | + assert.NoError(t, err) |
| 94 | + assert.NotNil(t, claims) |
| 95 | + assert.Equal(t, userID, claims.UserID) |
| 96 | + assert.Equal(t, roles, claims.Roles) |
| 97 | +} |
| 98 | + |
| 99 | +// TestRemoteValidationNotImplemented tests the case where remote validation returns ErrNotImplemented |
| 100 | +func TestRemoteValidationNotImplemented(t *testing.T) { |
| 101 | + logger := zap.NewNop() |
| 102 | + config := jwt.Config{ |
| 103 | + SecretKey: "test-secret", |
| 104 | + TokenDuration: 1 * time.Hour, |
| 105 | + Issuer: "test-issuer", |
| 106 | + } |
| 107 | + service := jwt.NewService(config, logger) |
| 108 | + ctx := context.Background() |
| 109 | + |
| 110 | + // Generate a valid token for local validation |
| 111 | + userID := "user123" |
| 112 | + roles := []string{"admin", "user"} |
| 113 | + token, err := service.GenerateToken(ctx, userID, roles, []string{}, []string{}) |
| 114 | + require.NoError(t, err) |
| 115 | + require.NotEmpty(t, token) |
| 116 | + |
| 117 | + // Set up a mock validator that fails with ErrNotImplemented |
| 118 | + mockValidator := NewFailingMockValidator(autherrors.ErrNotImplemented) |
| 119 | + |
| 120 | + // Set the mock validator as the remote validator |
| 121 | + service.WithRemoteValidator(jwt.RemoteConfig{}) |
| 122 | + // Access the private field using reflection |
| 123 | + setRemoteValidator(t, service, mockValidator) |
| 124 | + |
| 125 | + // Test validation - should fall back to local validation and succeed |
| 126 | + claims, err := service.ValidateToken(ctx, token) |
| 127 | + |
| 128 | + // Verify the mock was called |
| 129 | + assert.True(t, mockValidator.Called) |
| 130 | + |
| 131 | + // Verify the result - should succeed with local validation |
| 132 | + assert.NoError(t, err) |
| 133 | + assert.NotNil(t, claims) |
| 134 | + assert.Equal(t, userID, claims.UserID) |
| 135 | + assert.Equal(t, roles, claims.Roles) |
| 136 | +} |
| 137 | + |
| 138 | +// Helper function to set the remote validator for testing |
| 139 | +func setRemoteValidator(t *testing.T, service *jwt.Service, validator jwt.TokenValidator) { |
| 140 | + // Use the exported method to set the remote validator for testing |
| 141 | + service.SetRemoteValidatorForTesting(validator) |
| 142 | +} |
0 commit comments