Add tests for dynamic client registration validation in OAuth service

- Implemented tests to verify error handling for disallowed redirect URI hosts and schemes during dynamic client registration.
- Updated test clients to use localhost for redirect URIs, ensuring consistency in testing environment.
This commit is contained in:
Max 2025-07-18 14:58:29 +08:00
parent 944c3e1b8e
commit 9fd0d4713a
2 changed files with 42 additions and 3 deletions

View file

@ -367,6 +367,45 @@ func TestDynamicClientRegistration(t *testing.T) {
assert.Equal(t, types.ErrorInvalidRequest, oauthErr.Code)
assert.Contains(t, oauthErr.ErrorDescription, "At least one redirect URI is required")
})
t.Run("register with disallowed redirect URI host", func(t *testing.T) {
request := &types.DynamicClientRegistrationRequest{
ClientName: "Disallowed Host Client",
RedirectURIs: []string{"https://example.com/callback"}, // example.com is not in allowed hosts
}
response, err := service.DynamicClientRegistration(ctx, request)
assert.Error(t, err)
assert.Nil(t, response)
oauthErr, ok := err.(*types.ErrorResponse)
assert.True(t, ok)
assert.Equal(t, types.ErrorInvalidRequest, oauthErr.Code)
assert.Contains(t, oauthErr.ErrorDescription, "Redirect URI host 'example.com' is not allowed")
})
t.Run("register with disallowed redirect URI scheme", func(t *testing.T) {
// Temporarily restrict schemes to only HTTPS
originalSchemes := service.config.Client.AllowedRedirectURISchemes
service.config.Client.AllowedRedirectURISchemes = []string{"https"}
defer func() {
service.config.Client.AllowedRedirectURISchemes = originalSchemes
}()
request := &types.DynamicClientRegistrationRequest{
ClientName: "Disallowed Scheme Client",
RedirectURIs: []string{"http://localhost/callback"}, // HTTP is not allowed when only HTTPS is permitted
}
response, err := service.DynamicClientRegistration(ctx, request)
assert.Error(t, err)
assert.Nil(t, response)
oauthErr, ok := err.(*types.ErrorResponse)
assert.True(t, ok)
assert.Equal(t, types.ErrorInvalidRequest, oauthErr.Code)
assert.Contains(t, oauthErr.ErrorDescription, "Redirect URI scheme 'http' is not allowed")
})
}
// =============================================================================

View file

@ -83,7 +83,7 @@ var testClients = []*TestClient{
ClientSecret: "confidential-secret-12345",
ClientName: "Test Confidential Client",
ClientType: types.ClientTypeConfidential,
RedirectURIs: []string{"https://confidential.example.com/callback"},
RedirectURIs: []string{"https://localhost/callback"},
GrantTypes: []string{types.GrantTypeAuthorizationCode, types.GrantTypeRefreshToken},
ResponseTypes: []string{types.ResponseTypeCode},
Scope: "openid profile email",
@ -94,7 +94,7 @@ var testClients = []*TestClient{
ClientSecret: "", // Public clients don't have secrets
ClientName: "Test Public Client",
ClientType: types.ClientTypePublic,
RedirectURIs: []string{"https://public.example.com/callback"},
RedirectURIs: []string{"https://localhost/callback"},
GrantTypes: []string{types.GrantTypeAuthorizationCode},
ResponseTypes: []string{types.ResponseTypeCode},
Scope: "openid profile",
@ -105,7 +105,7 @@ var testClients = []*TestClient{
ClientSecret: "credentials-secret-67890",
ClientName: "Test Client Credentials Client",
ClientType: types.ClientTypeConfidential,
RedirectURIs: []string{"https://credentials.example.com/callback"},
RedirectURIs: []string{"https://localhost/callback"},
GrantTypes: []string{types.GrantTypeClientCredentials},
ResponseTypes: []string{types.ResponseTypeCode},
Scope: "api:read api:write",