2022-04-28 19:07:04 +00:00
|
|
|
// Copyright 2022 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2022-04-28 16:11:51 +00:00
|
|
|
package login
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"go.pinniped.dev/internal/oidc"
|
2022-05-05 20:12:06 +00:00
|
|
|
"go.pinniped.dev/internal/oidc/login/loginhtml"
|
|
|
|
"go.pinniped.dev/internal/testutil"
|
2022-04-28 16:11:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetLogin(t *testing.T) {
|
|
|
|
const (
|
2022-05-05 20:12:06 +00:00
|
|
|
testPath = "/some/path/login"
|
|
|
|
testUpstreamName = "some-ldap-idp"
|
|
|
|
testUpstreamType = "ldap"
|
|
|
|
testEncodedState = "fake-encoded-state-value"
|
2022-04-28 16:11:51 +00:00
|
|
|
)
|
2022-04-29 17:36:13 +00:00
|
|
|
|
2022-04-28 16:11:51 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
decodedState *oidc.UpstreamStateParamData
|
|
|
|
encodedState string
|
2022-04-29 17:36:13 +00:00
|
|
|
errParam string
|
2022-04-28 16:11:51 +00:00
|
|
|
idps oidc.UpstreamIdentityProvidersLister
|
|
|
|
wantStatus int
|
|
|
|
wantContentType string
|
|
|
|
wantBody string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Happy path ldap",
|
|
|
|
decodedState: &oidc.UpstreamStateParamData{
|
2022-05-05 20:12:06 +00:00
|
|
|
UpstreamName: testUpstreamName,
|
|
|
|
UpstreamType: testUpstreamType,
|
2022-04-28 16:11:51 +00:00
|
|
|
},
|
2022-05-05 20:12:06 +00:00
|
|
|
encodedState: testEncodedState, // the encoded and decoded state don't match, but that verification is handled one level up.
|
2022-04-28 16:11:51 +00:00
|
|
|
wantStatus: http.StatusOK,
|
|
|
|
wantContentType: htmlContentType,
|
2022-05-05 20:12:06 +00:00
|
|
|
wantBody: testutil.ExpectedLoginPageHTML(loginhtml.CSS(), testUpstreamName, testPath, testEncodedState, ""), // no alert message
|
2022-04-29 17:36:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "displays error banner when err=login_error param is sent",
|
|
|
|
decodedState: &oidc.UpstreamStateParamData{
|
2022-05-05 20:12:06 +00:00
|
|
|
UpstreamName: testUpstreamName,
|
|
|
|
UpstreamType: testUpstreamType,
|
2022-04-29 17:36:13 +00:00
|
|
|
},
|
2022-05-05 20:12:06 +00:00
|
|
|
encodedState: testEncodedState,
|
2022-04-29 17:36:13 +00:00
|
|
|
errParam: "login_error",
|
|
|
|
wantStatus: http.StatusOK,
|
|
|
|
wantContentType: htmlContentType,
|
2022-05-05 20:12:06 +00:00
|
|
|
wantBody: testutil.ExpectedLoginPageHTML(loginhtml.CSS(), testUpstreamName, testPath, testEncodedState,
|
|
|
|
"Incorrect username or password.",
|
|
|
|
),
|
2022-04-29 17:36:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "displays error banner when err=internal_error param is sent",
|
|
|
|
decodedState: &oidc.UpstreamStateParamData{
|
2022-05-05 20:12:06 +00:00
|
|
|
UpstreamName: testUpstreamName,
|
|
|
|
UpstreamType: testUpstreamType,
|
2022-04-29 17:36:13 +00:00
|
|
|
},
|
2022-05-05 20:12:06 +00:00
|
|
|
encodedState: testEncodedState,
|
2022-04-29 17:36:13 +00:00
|
|
|
errParam: "internal_error",
|
|
|
|
wantStatus: http.StatusOK,
|
|
|
|
wantContentType: htmlContentType,
|
2022-05-05 20:12:06 +00:00
|
|
|
wantBody: testutil.ExpectedLoginPageHTML(loginhtml.CSS(), testUpstreamName, testPath, testEncodedState,
|
|
|
|
"An internal error occurred. Please contact your administrator for help.",
|
|
|
|
),
|
2022-04-29 17:36:13 +00:00
|
|
|
},
|
|
|
|
{
|
2022-05-05 20:12:06 +00:00
|
|
|
// If we get an error that we don't recognize, that's also an error, so we
|
|
|
|
// should probably just tell you to contact your administrator...
|
2022-04-29 17:36:13 +00:00
|
|
|
name: "displays generic error banner when unrecognized err param is sent",
|
|
|
|
decodedState: &oidc.UpstreamStateParamData{
|
2022-05-05 20:12:06 +00:00
|
|
|
UpstreamName: testUpstreamName,
|
|
|
|
UpstreamType: testUpstreamType,
|
2022-04-29 17:36:13 +00:00
|
|
|
},
|
2022-05-05 20:12:06 +00:00
|
|
|
encodedState: testEncodedState,
|
2022-04-29 17:36:13 +00:00
|
|
|
errParam: "some_other_error",
|
|
|
|
wantStatus: http.StatusOK,
|
|
|
|
wantContentType: htmlContentType,
|
2022-05-05 20:12:06 +00:00
|
|
|
wantBody: testutil.ExpectedLoginPageHTML(loginhtml.CSS(), testUpstreamName, testPath, testEncodedState,
|
|
|
|
"An internal error occurred. Please contact your administrator for help.",
|
|
|
|
),
|
2022-04-28 16:11:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
tt := test
|
2022-05-03 23:46:09 +00:00
|
|
|
|
2022-04-28 16:11:51 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-05-03 23:46:09 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-05-05 20:12:06 +00:00
|
|
|
handler := NewGetHandler()
|
|
|
|
target := testPath + "?state=" + tt.encodedState
|
2022-04-29 17:36:13 +00:00
|
|
|
if tt.errParam != "" {
|
|
|
|
target += "&err=" + tt.errParam
|
|
|
|
}
|
|
|
|
req := httptest.NewRequest(http.MethodGet, target, nil)
|
2022-04-28 16:11:51 +00:00
|
|
|
rsp := httptest.NewRecorder()
|
|
|
|
err := handler(rsp, req, tt.encodedState, tt.decodedState)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-03 23:46:09 +00:00
|
|
|
require.Equal(t, tt.wantStatus, rsp.Code)
|
2022-04-28 16:11:51 +00:00
|
|
|
testutil.RequireEqualContentType(t, rsp.Header().Get("Content-Type"), tt.wantContentType)
|
|
|
|
body := rsp.Body.String()
|
2022-05-05 20:12:06 +00:00
|
|
|
// t.Log("actual body:", body) // useful when updating expected values
|
2022-04-28 16:11:51 +00:00
|
|
|
require.Equal(t, tt.wantBody, body)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|