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
|
2020-10-17 00:51:40 +00:00
|
|
|
wantBodyJSON interface{}
|
|
|
|
wantBodyString string
|
2020-10-06 14:11:57 +00:00
|
|
|
}{
|
|
|
|
{
|
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",
|
2020-10-17 00:51:40 +00:00
|
|
|
wantBodyJSON: &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"},
|
2020-12-03 12:46:07 +00:00
|
|
|
IDTokenSigningAlgValuesSupported: []string{"ES256"},
|
2020-10-07 15:33:50 +00:00
|
|
|
TokenEndpointAuthMethodsSupported: []string{"client_secret_basic"},
|
2020-12-07 22:15:31 +00:00
|
|
|
ScopesSupported: []string{"openid", "offline"},
|
|
|
|
ClaimsSupported: []string{"groups"},
|
2020-10-07 14:53:05 +00:00
|
|
|
},
|
|
|
|
},
|
2020-10-06 14:11:57 +00:00
|
|
|
{
|
2020-10-17 00:51:40 +00:00
|
|
|
name: "bad method",
|
|
|
|
issuer: "https://some-issuer.com",
|
|
|
|
method: http.MethodPost,
|
|
|
|
path: oidc.WellKnownEndpointPath,
|
|
|
|
wantStatus: http.StatusMethodNotAllowed,
|
|
|
|
wantContentType: "text/plain; charset=utf-8",
|
|
|
|
wantBodyString: "Method not allowed (try GET)\n",
|
2020-10-06 14:11:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2020-10-17 00:51:40 +00:00
|
|
|
handler := NewHandler(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)
|
|
|
|
|
2020-10-17 00:51:40 +00:00
|
|
|
require.Equal(t, test.wantContentType, rsp.Header().Get("Content-Type"))
|
2020-10-06 14:11:57 +00:00
|
|
|
|
2020-10-17 00:51:40 +00:00
|
|
|
if test.wantBodyJSON != nil {
|
|
|
|
wantJSON, err := json.Marshal(test.wantBodyJSON)
|
2020-10-06 14:11:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.JSONEq(t, string(wantJSON), rsp.Body.String())
|
|
|
|
}
|
2020-10-17 00:51:40 +00:00
|
|
|
|
|
|
|
if test.wantBodyString != "" {
|
|
|
|
require.Equal(t, test.wantBodyString, rsp.Body.String())
|
|
|
|
}
|
2020-10-06 14:11:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|