MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestCallbackStateClientCookies

Function TestCallbackStateClientCookies

rest/oidc_api_test.go:2037–2107  ·  view source on GitHub ↗

Checks callback state cookie persistence across requests.

(t *testing.T)

Source from the content-addressed store, hash-verified

2035
2036// Checks callback state cookie persistence across requests.
2037func TestCallbackStateClientCookies(t *testing.T) {
2038 providers := auth.OIDCProviderMap{
2039 "foo": mockProviderWith("foo", mockProviderRegister{}, mockProviderUserPrefix{"foo"}),
2040 }
2041 authURL := "/db/_oidc?provider=foo&offline=true"
2042 defaultProvider := "foo"
2043 mockAuthServer, err := newMockAuthServer()
2044 require.NoError(t, err, "Error creating mock oauth2 server")
2045 mockAuthServer.Start()
2046 defer mockAuthServer.Shutdown()
2047 mockAuthServer.options.issuer = mockAuthServer.URL + "/" + defaultProvider
2048 refreshProviderConfig(providers, mockAuthServer.URL)
2049
2050 opts := auth.OIDCOptions{
2051 Providers: providers,
2052 DefaultProvider: &defaultProvider,
2053 }
2054 restTesterConfig := RestTesterConfig{
2055 DatabaseConfig: &DatabaseConfig{DbConfig: DbConfig{
2056 OIDCConfig: &opts,
2057 }},
2058 }
2059 restTester := NewRestTester(t, &restTesterConfig)
2060 defer restTester.Close()
2061
2062 mockSyncGateway := httptest.NewServer(restTester.TestPublicHandler())
2063 defer mockSyncGateway.Close()
2064 mockSyncGatewayURL := mockSyncGateway.URL
2065
2066 requestURL := mockSyncGatewayURL + authURL
2067 request, err := http.NewRequest(http.MethodGet, requestURL, nil)
2068 require.NoError(t, err, "Error creating new request")
2069
2070 t.Run("unsuccessful auth when callback state enabled with no cookies support from client", func(t *testing.T) {
2071 response, err := http.DefaultClient.Do(request)
2072 require.NoError(t, err, "Error sending request")
2073 defer func() { assert.NoError(t, response.Body.Close()) }()
2074 expectedAuthError := forceError{
2075 expectedErrorCode: http.StatusBadRequest,
2076 expectedErrorMessage: ErrNoStateCookie.Message,
2077 }
2078 assertHttpResponse(t, response, expectedAuthError)
2079 })
2080
2081 t.Run("successful auth when callback state enabled with cookies support from client", func(t *testing.T) {
2082 jar, err := cookiejar.New(nil)
2083 require.NoError(t, err, "Error creating new cookie jar")
2084 client := &http.Client{Jar: jar}
2085 response, err := client.Do(request)
2086 require.NoError(t, err, "Error sending request")
2087 defer func() { assert.NoError(t, response.Body.Close()) }()
2088 require.Equal(t, http.StatusOK, response.StatusCode)
2089 var authResponseActual OIDCTokenResponse
2090 require.NoError(t, err, json.NewDecoder(response.Body).Decode(&authResponseActual))
2091 assert.NotEmpty(t, authResponseActual.SessionID, "session_id doesn't exist")
2092 assert.Equal(t, "foo_noah", authResponseActual.Username, "name mismatch")
2093 })
2094

Callers

nothing calls this directly

Calls 14

CloseMethod · 0.95
TestPublicHandlerMethod · 0.95
mockProviderWithFunction · 0.85
newMockAuthServerFunction · 0.85
refreshProviderConfigFunction · 0.85
NewRestTesterFunction · 0.85
assertHttpResponseFunction · 0.85
ShutdownMethod · 0.80
GetDefaultProviderMethod · 0.80
StartMethod · 0.65
CloseMethod · 0.65
RunMethod · 0.65

Tested by

no test coverage detected