// Copyright 2021-2022 the Pinniped contributors. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 package formposthtml import ( "bytes" "fmt" "net/url" "testing" "github.com/ory/fosite" "github.com/stretchr/testify/require" "go.pinniped.dev/internal/here" ) var ( testRedirectURL = "http://127.0.0.1:12345/callback" testResponseParams = url.Values{ "code": []string{"test-S629KHsCCBYV0PQ6FDSrn6iEXtVImQRBh7NCAk.JezyUSdCiSslYjtUmv7V5VAgiCz3ZkES9mYldg9GhqU"}, "scope": []string{"openid offline_access pinniped:request-audience"}, "state": []string{"01234567890123456789012345678901"}, } testExpectedFormPostOutput = here.Doc(`
`) // It's okay if this changes in the future, but this gives us a chance to eyeball the formatting. // Our browser-based integration tests should find any incompatibilities. testExpectedCSP = `default-src 'none'; ` + `script-src 'sha256-uIWC0J7wd7tWtcXmugZCkKsQpqOsQzqBI/mfQMtUde0='; ` + `style-src 'sha256-kXh6OrB2z7wkx7v1N3ay9deQhV5edwuogARaUtvNYN4='; ` + `img-src data:; ` + `connect-src *; ` + `frame-ancestors 'none'` ) func TestTemplate(t *testing.T) { // Use the Fosite helper to render the form, ensuring that the parameters all have the same names + types. var buf bytes.Buffer fosite.WriteAuthorizeFormPostResponse(testRedirectURL, testResponseParams, Template(), &buf) // Render again so we can confirm that there is no error returned (Fosite ignores any error). var buf2 bytes.Buffer require.NoError(t, Template().Execute(&buf2, struct { RedirURL string Parameters url.Values }{ RedirURL: testRedirectURL, Parameters: testResponseParams, })) // t.Logf("actual value:\n%s", buf2.String()) // useful when updating minify library causes new output require.Equal(t, buf.String(), buf2.String()) require.Equal(t, testExpectedFormPostOutput, buf.String()) } func TestContentSecurityPolicyHashes(t *testing.T) { require.Equal(t, testExpectedCSP, ContentSecurityPolicy()) } func TestHelpers(t *testing.T) { require.Equal(t, "test", panicOnError("test", nil)) require.PanicsWithError(t, "some error", func() { panicOnError("", fmt.Errorf("some error")) }) }