MCPcopy
hub / github.com/ory/hydra / TestGetConsentRequest

Function TestGetConsentRequest

consent/handler_test.go:164–227  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

162}
163
164func TestGetConsentRequest(t *testing.T) {
165 t.Parallel()
166
167 reg := testhelpers.NewRegistryMemory(t)
168
169 h := NewHandler(reg)
170 r := httprouterx.NewTestRouterAdminWithPrefix(t)
171 h.SetRoutes(r)
172 ts := httptest.NewServer(r)
173 defer ts.Close()
174
175 cl := &client.Client{
176 ID: "test client id",
177 Name: "test client name",
178 }
179
180 requestURL := "http://192.0.2.1"
181 consentRequestID := "test consent request id"
182
183 f := &flow.Flow{
184 Client: cl,
185 RequestURL: requestURL,
186 RequestedAt: time.Now(),
187 State: flow.FlowStateConsentUnused,
188 NID: reg.Persister().NetworkID(t.Context()),
189 ConsentRequestID: sqlxx.NullString(consentRequestID),
190 }
191
192 unhandledChallenge, err := f.ToConsentChallenge(t.Context(), reg)
193 require.NoError(t, err)
194
195 t.Run("unhandled flow", func(t *testing.T) {
196 resp, err := ts.Client().Get(ts.URL + "/admin" + ConsentPath + "?challenge=" + unhandledChallenge)
197 require.NoError(t, err)
198 require.EqualValues(t, http.StatusOK, resp.StatusCode)
199
200 var result flow.OAuth2ConsentRequest
201 require.NoError(t, json.NewDecoder(resp.Body).Decode(&result))
202 assert.Equal(t, unhandledChallenge, result.Challenge)
203 assert.Equal(t, requestURL, result.RequestURL)
204 assert.NotNil(t, result.Client)
205 })
206
207 t.Run("handled flow", func(t *testing.T) {
208 f.State = flow.FlowStateConsentUnused
209 require.NoError(t, f.InvalidateConsentRequest())
210 handledChallenge, err := f.ToConsentChallenge(t.Context(), reg)
211 require.NoError(t, err)
212
213 resp, err := ts.Client().Get(ts.URL + "/admin" + ConsentPath + "?challenge=" + handledChallenge)
214 require.NoError(t, err)
215 require.EqualValues(t, http.StatusGone, resp.StatusCode)
216
217 var result flow.OAuth2RedirectTo
218 require.NoError(t, json.NewDecoder(resp.Body).Decode(&result))
219 assert.Equal(t, requestURL, result.RedirectTo)
220 })
221

Callers

nothing calls this directly

Calls 12

SetRoutesMethod · 0.95
ToConsentChallengeMethod · 0.95
NewRegistryMemoryFunction · 0.92
NewHandlerFunction · 0.70
CloseMethod · 0.65
NowMethod · 0.65
NetworkIDMethod · 0.65
PersisterMethod · 0.65
GetMethod · 0.65
ClientMethod · 0.65
DecodeMethod · 0.65

Tested by

no test coverage detected