2020-10-06 14:11:57 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-10-07 15:33:50 +00:00
|
|
|
"go.pinniped.dev/internal/oidc"
|
2020-10-06 14:11:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiscovery(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
|
2020-10-08 02:18:34 +00:00
|
|
|
issuer string
|
2020-10-06 14:11:57 +00:00
|
|
|
method string
|
2020-10-07 15:33:50 +00:00
|
|
|
path string
|
2020-10-06 14:11:57 +00:00
|
|
|
|
|
|
|
wantStatus int
|
|
|
|
wantContentType string
|
|
|
|
wantBody interface{}
|
|
|
|
}{
|
|
|
|
{
|
2020-10-08 02:18:34 +00:00
|
|
|
name: "happy path",
|
|
|
|
issuer: "https://some-issuer.com/some/path",
|
2020-10-07 14:53:05 +00:00
|
|
|
method: http.MethodGet,
|
2020-10-08 18:28:21 +00:00
|
|
|
path: "/some/path" + oidc.WellKnownEndpointPath,
|
2020-10-07 14:53:05 +00:00
|
|
|
wantStatus: http.StatusOK,
|
|
|
|
wantContentType: "application/json",
|
|
|
|
wantBody: &Metadata{
|
2020-10-07 15:33:50 +00:00
|
|
|
Issuer: "https://some-issuer.com/some/path",
|
2020-10-08 18:28:21 +00:00
|
|
|
AuthorizationEndpoint: "https://some-issuer.com/some/path/oauth2/authorize",
|
|
|
|
TokenEndpoint: "https://some-issuer.com/some/path/oauth2/token",
|
2020-10-07 15:33:50 +00:00
|
|
|
JWKSURI: "https://some-issuer.com/some/path/jwks.json",
|
|
|
|
ResponseTypesSupported: []string{"code"},
|
|
|
|
SubjectTypesSupported: []string{"public"},
|
|
|
|
IDTokenSigningAlgValuesSupported: []string{"RS256"},
|
|
|
|
TokenEndpointAuthMethodsSupported: []string{"client_secret_basic"},
|
|
|
|
TokenEndpointAuthSigningAlgoValuesSupported: []string{"RS256"},
|
|
|
|
ScopesSupported: []string{"openid", "offline"},
|
|
|
|
ClaimsSupported: []string{"groups"},
|
2020-10-07 14:53:05 +00:00
|
|
|
},
|
|
|
|
},
|
2020-10-06 14:11:57 +00:00
|
|
|
{
|
|
|
|
name: "bad method",
|
2020-10-08 02:18:34 +00:00
|
|
|
issuer: "https://some-issuer.com",
|
2020-10-06 14:11:57 +00:00
|
|
|
method: http.MethodPost,
|
2020-10-08 18:28:21 +00:00
|
|
|
path: oidc.WellKnownEndpointPath,
|
2020-10-06 14:11:57 +00:00
|
|
|
wantStatus: http.StatusMethodNotAllowed,
|
|
|
|
wantBody: map[string]string{
|
|
|
|
"error": "Method not allowed (try GET)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2020-10-08 02:18:34 +00:00
|
|
|
handler := New(test.issuer)
|
2020-10-07 15:33:50 +00:00
|
|
|
req := httptest.NewRequest(test.method, test.path, nil)
|
2020-10-06 14:11:57 +00:00
|
|
|
rsp := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rsp, req)
|
|
|
|
|
|
|
|
require.Equal(t, test.wantStatus, rsp.Code)
|
|
|
|
|
|
|
|
if test.wantContentType != "" {
|
|
|
|
require.Equal(t, test.wantContentType, rsp.Header().Get("Content-Type"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.wantBody != nil {
|
|
|
|
wantJSON, err := json.Marshal(test.wantBody)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.JSONEq(t, string(wantJSON), rsp.Body.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|