|
| 1 | +package tokenprovider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestTokenProviderAuthenticator(t *testing.T) { |
| 14 | + t.Run("successful_authentication", func(t *testing.T) { |
| 15 | + provider := NewStaticTokenProvider("test-token-123") |
| 16 | + authenticator := NewAuthenticator(provider) |
| 17 | + |
| 18 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 19 | + err := authenticator.Authenticate(req) |
| 20 | + |
| 21 | + require.NoError(t, err) |
| 22 | + assert.Equal(t, "Bearer test-token-123", req.Header.Get("Authorization")) |
| 23 | + }) |
| 24 | + |
| 25 | + t.Run("authentication_with_custom_token_type", func(t *testing.T) { |
| 26 | + provider := NewStaticTokenProviderWithType("test-token", "MAC") |
| 27 | + authenticator := NewAuthenticator(provider) |
| 28 | + |
| 29 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 30 | + err := authenticator.Authenticate(req) |
| 31 | + |
| 32 | + require.NoError(t, err) |
| 33 | + assert.Equal(t, "MAC test-token", req.Header.Get("Authorization")) |
| 34 | + }) |
| 35 | + |
| 36 | + t.Run("authentication_error_propagation", func(t *testing.T) { |
| 37 | + provider := &mockProvider{ |
| 38 | + tokenFunc: func() (*Token, error) { |
| 39 | + return nil, errors.New("provider failed") |
| 40 | + }, |
| 41 | + } |
| 42 | + authenticator := NewAuthenticator(provider) |
| 43 | + |
| 44 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 45 | + err := authenticator.Authenticate(req) |
| 46 | + |
| 47 | + assert.Error(t, err) |
| 48 | + assert.Contains(t, err.Error(), "provider failed") |
| 49 | + assert.Empty(t, req.Header.Get("Authorization")) |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("empty_token_error", func(t *testing.T) { |
| 53 | + provider := &mockProvider{ |
| 54 | + tokenFunc: func() (*Token, error) { |
| 55 | + return &Token{ |
| 56 | + AccessToken: "", |
| 57 | + TokenType: "Bearer", |
| 58 | + }, nil |
| 59 | + }, |
| 60 | + } |
| 61 | + authenticator := NewAuthenticator(provider) |
| 62 | + |
| 63 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 64 | + err := authenticator.Authenticate(req) |
| 65 | + |
| 66 | + assert.Error(t, err) |
| 67 | + assert.Contains(t, err.Error(), "empty access token") |
| 68 | + assert.Empty(t, req.Header.Get("Authorization")) |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("uses_request_context", func(t *testing.T) { |
| 72 | + ctx, cancel := context.WithCancel(context.Background()) |
| 73 | + cancel() // Cancel immediately |
| 74 | + |
| 75 | + provider := &mockProvider{ |
| 76 | + tokenFunc: func() (*Token, error) { |
| 77 | + // This would normally check context cancellation |
| 78 | + return &Token{ |
| 79 | + AccessToken: "test-token", |
| 80 | + TokenType: "Bearer", |
| 81 | + }, nil |
| 82 | + }, |
| 83 | + } |
| 84 | + authenticator := NewAuthenticator(provider) |
| 85 | + |
| 86 | + req, _ := http.NewRequestWithContext(ctx, "GET", "http://example.com", nil) |
| 87 | + err := authenticator.Authenticate(req) |
| 88 | + |
| 89 | + // Even with cancelled context, this should work as our mock doesn't check it |
| 90 | + require.NoError(t, err) |
| 91 | + assert.Equal(t, "Bearer test-token", req.Header.Get("Authorization")) |
| 92 | + }) |
| 93 | + |
| 94 | + t.Run("external_token_integration", func(t *testing.T) { |
| 95 | + tokenFunc := func() (string, error) { |
| 96 | + return "external-token-456", nil |
| 97 | + } |
| 98 | + provider := NewExternalTokenProvider(tokenFunc) |
| 99 | + authenticator := NewAuthenticator(provider) |
| 100 | + |
| 101 | + req, _ := http.NewRequest("POST", "http://example.com/api", nil) |
| 102 | + err := authenticator.Authenticate(req) |
| 103 | + |
| 104 | + require.NoError(t, err) |
| 105 | + assert.Equal(t, "Bearer external-token-456", req.Header.Get("Authorization")) |
| 106 | + }) |
| 107 | +} |
0 commit comments