2021-04-28 20:14:21 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-10-06 14:11:57 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-08-17 22:23:03 +00:00
|
|
|
"go.pinniped.dev/internal/here"
|
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
|
|
|
|
2021-05-11 17:31:33 +00:00
|
|
|
wantStatus int
|
|
|
|
wantContentType string
|
2021-08-17 22:23:03 +00:00
|
|
|
wantBodyJSON string
|
2021-05-11 17:31:33 +00:00
|
|
|
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",
|
2021-08-17 22:23:03 +00:00
|
|
|
wantBodyJSON: here.Doc(`
|
|
|
|
{
|
|
|
|
"issuer": "https://some-issuer.com/some/path",
|
|
|
|
"authorization_endpoint": "https://some-issuer.com/some/path/oauth2/authorize",
|
|
|
|
"token_endpoint": "https://some-issuer.com/some/path/oauth2/token",
|
|
|
|
"jwks_uri": "https://some-issuer.com/some/path/jwks.json",
|
|
|
|
"response_types_supported": ["code"],
|
|
|
|
"response_modes_supported": ["query", "form_post"],
|
|
|
|
"subject_types_supported": ["public"],
|
|
|
|
"id_token_signing_alg_values_supported": ["ES256"],
|
|
|
|
"token_endpoint_auth_methods_supported": ["client_secret_basic"],
|
|
|
|
"scopes_supported": ["openid", "offline"],
|
|
|
|
"claims_supported": ["groups"],
|
|
|
|
"discovery.supervisor.pinniped.dev/v1alpha1": {
|
|
|
|
"pinniped_identity_providers_endpoint": "https://some-issuer.com/some/path/v1alpha1/pinniped_identity_providers"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`),
|
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) {
|
2021-05-11 17:31:33 +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
|
|
|
|
2021-08-17 22:23:03 +00:00
|
|
|
if test.wantBodyJSON != "" {
|
|
|
|
require.JSONEq(t, test.wantBodyJSON, rsp.Body.String())
|
2020-10-06 14:11:57 +00:00
|
|
|
}
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|