| package config |
| |
| import ( |
| "testing" |
| |
| "github.com/stretchr/testify/assert" |
| ) |
| |
| func TestDefaultOAuthConfig(t *testing.T) { |
| config := DefaultOAuthConfig() |
| |
| assert.NotNil(t, config) |
| |
| // Test default values |
| assert.Equal(t, "https://korap.ids-mannheim.de/api/v1.0/oauth2/authorize", config.AuthURL) |
| assert.Equal(t, "https://korap.ids-mannheim.de/api/v1.0/oauth2/token", config.TokenURL) |
| assert.Equal(t, "urn:ietf:wg:oauth:2.0:oob", config.RedirectURL) |
| assert.Equal(t, []string{"read"}, config.Scopes) |
| assert.False(t, config.Enabled) |
| |
| // Test that required fields are empty by default |
| assert.Empty(t, config.ClientID) |
| assert.Empty(t, config.ClientSecret) |
| |
| // Test ToOAuth2Config with disabled config |
| oauth2Config := config.ToOAuth2Config() |
| assert.Nil(t, oauth2Config, "ToOAuth2Config should return nil when disabled") |
| |
| // Test validation of default (disabled) config |
| err := config.Validate() |
| assert.NoError(t, err, "Default config should be valid (disabled)") |
| } |
| |
| func TestOAuthConfigValidate(t *testing.T) { |
| tests := []struct { |
| name string |
| config *OAuthConfig |
| wantError bool |
| errorMsg string |
| }{ |
| { |
| name: "valid enabled config", |
| config: &OAuthConfig{ |
| ClientID: "test-client", |
| ClientSecret: "test-secret", |
| AuthURL: "https://example.com/auth", |
| TokenURL: "https://example.com/token", |
| RedirectURL: "https://example.com/callback", |
| Scopes: []string{"read"}, |
| Enabled: true, |
| }, |
| wantError: false, |
| }, |
| { |
| name: "valid disabled config", |
| config: &OAuthConfig{ |
| Enabled: false, |
| }, |
| wantError: false, |
| }, |
| { |
| name: "missing client ID", |
| config: &OAuthConfig{ |
| ClientSecret: "test-secret", |
| AuthURL: "https://example.com/auth", |
| TokenURL: "https://example.com/token", |
| Enabled: true, |
| }, |
| wantError: true, |
| errorMsg: "client_id is required", |
| }, |
| { |
| name: "missing client secret", |
| config: &OAuthConfig{ |
| ClientID: "test-client", |
| AuthURL: "https://example.com/auth", |
| TokenURL: "https://example.com/token", |
| Enabled: true, |
| }, |
| wantError: true, |
| errorMsg: "client_secret is required", |
| }, |
| { |
| name: "missing token URL", |
| config: &OAuthConfig{ |
| ClientID: "test-client", |
| ClientSecret: "test-secret", |
| AuthURL: "https://example.com/auth", |
| Enabled: true, |
| }, |
| wantError: true, |
| errorMsg: "token_url is required", |
| }, |
| } |
| |
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| err := tt.config.Validate() |
| |
| if tt.wantError { |
| assert.Error(t, err) |
| assert.Contains(t, err.Error(), tt.errorMsg) |
| } else { |
| assert.NoError(t, err) |
| } |
| }) |
| } |
| } |
| |
| func TestOAuthConfigToOAuth2Config(t *testing.T) { |
| tests := []struct { |
| name string |
| config *OAuthConfig |
| wantNil bool |
| testFunc func(*testing.T, *OAuthConfig) |
| }{ |
| { |
| name: "disabled config returns nil", |
| config: &OAuthConfig{ |
| ClientID: "test-client", |
| ClientSecret: "test-secret", |
| Enabled: false, |
| }, |
| wantNil: true, |
| }, |
| { |
| name: "enabled config returns valid oauth2.Config", |
| config: &OAuthConfig{ |
| ClientID: "test-client", |
| ClientSecret: "test-secret", |
| AuthURL: "https://example.com/auth", |
| TokenURL: "https://example.com/token", |
| RedirectURL: "https://example.com/callback", |
| Scopes: []string{"read", "write"}, |
| Enabled: true, |
| }, |
| wantNil: false, |
| testFunc: func(t *testing.T, config *OAuthConfig) { |
| oauth2Config := config.ToOAuth2Config() |
| assert.NotNil(t, oauth2Config) |
| assert.Equal(t, config.ClientID, oauth2Config.ClientID) |
| assert.Equal(t, config.ClientSecret, oauth2Config.ClientSecret) |
| assert.Equal(t, config.AuthURL, oauth2Config.Endpoint.AuthURL) |
| assert.Equal(t, config.TokenURL, oauth2Config.Endpoint.TokenURL) |
| assert.Equal(t, config.RedirectURL, oauth2Config.RedirectURL) |
| assert.Equal(t, config.Scopes, oauth2Config.Scopes) |
| }, |
| }, |
| } |
| |
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| oauth2Config := tt.config.ToOAuth2Config() |
| |
| if tt.wantNil { |
| assert.Nil(t, oauth2Config) |
| } else { |
| assert.NotNil(t, oauth2Config) |
| if tt.testFunc != nil { |
| tt.testFunc(t, tt.config) |
| } |
| } |
| }) |
| } |
| } |
| |
| func TestOAuthConfigEdgeCases(t *testing.T) { |
| t.Run("empty scopes", func(t *testing.T) { |
| config := &OAuthConfig{ |
| ClientID: "test-client", |
| ClientSecret: "test-secret", |
| AuthURL: "https://example.com/auth", |
| TokenURL: "https://example.com/token", |
| Scopes: []string{}, |
| Enabled: true, |
| } |
| |
| err := config.Validate() |
| assert.NoError(t, err, "Empty scopes should be valid") |
| |
| oauth2Config := config.ToOAuth2Config() |
| assert.NotNil(t, oauth2Config) |
| assert.Empty(t, oauth2Config.Scopes) |
| }) |
| |
| t.Run("nil scopes", func(t *testing.T) { |
| config := &OAuthConfig{ |
| ClientID: "test-client", |
| ClientSecret: "test-secret", |
| AuthURL: "https://example.com/auth", |
| TokenURL: "https://example.com/token", |
| Scopes: nil, |
| Enabled: true, |
| } |
| |
| err := config.Validate() |
| assert.NoError(t, err, "Nil scopes should be valid") |
| |
| oauth2Config := config.ToOAuth2Config() |
| assert.NotNil(t, oauth2Config) |
| assert.Nil(t, oauth2Config.Scopes) |
| }) |
| } |
| |
| func TestOAuthConfigScopesHandling(t *testing.T) { |
| config := &OAuthConfig{ |
| ClientID: "test-client", |
| ClientSecret: "test-secret", |
| AuthURL: "https://example.com/auth", |
| TokenURL: "https://example.com/token", |
| RedirectURL: "https://example.com/callback", |
| Scopes: []string{"read", "write", "admin"}, |
| Enabled: true, |
| } |
| |
| oauth2Config := config.ToOAuth2Config() |
| assert.NotNil(t, oauth2Config) |
| assert.Len(t, oauth2Config.Scopes, 3) |
| assert.Contains(t, oauth2Config.Scopes, "read") |
| assert.Contains(t, oauth2Config.Scopes, "write") |
| assert.Contains(t, oauth2Config.Scopes, "admin") |
| } |
| |
| // Benchmark tests |
| func BenchmarkDefaultOAuthConfig(b *testing.B) { |
| for i := 0; i < b.N; i++ { |
| _ = DefaultOAuthConfig() |
| } |
| } |
| |
| func BenchmarkOAuthConfigValidate(b *testing.B) { |
| cfg := DefaultOAuthConfig() |
| cfg.Enabled = true |
| cfg.ClientID = "test-client" |
| cfg.ClientSecret = "test-secret" |
| cfg.TokenURL = "https://example.com/token" |
| |
| b.ResetTimer() |
| for i := 0; i < b.N; i++ { |
| _ = cfg.Validate() |
| } |
| } |
| |
| func BenchmarkOAuthConfigToOAuth2Config(b *testing.B) { |
| cfg := &OAuthConfig{ |
| Enabled: true, |
| ClientID: "test-client", |
| ClientSecret: "test-secret", |
| AuthURL: "https://example.com/auth", |
| TokenURL: "https://example.com/token", |
| RedirectURL: "https://example.com/callback", |
| Scopes: []string{"read", "write"}, |
| } |
| |
| b.ResetTimer() |
| for i := 0; i < b.N; i++ { |
| _ = cfg.ToOAuth2Config() |
| } |
| } |