MCPcopy
hub / github.com/ory/kratos / TestHandlerSelfServiceSessionManagement

Function TestHandlerSelfServiceSessionManagement

session/handler_test.go:858–1043  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

856}
857
858func TestHandlerSelfServiceSessionManagement(t *testing.T) {
859 t.Parallel()
860
861 _, reg := pkg.NewFastRegistryWithMocks(t,
862 configx.WithValues(testhelpers.DefaultIdentitySchemaConfig("file://./stub/identity.schema.json")),
863 )
864 ts, _, r, _ := testhelpers.NewKratosServerWithCSRFAndRouters(t, reg)
865
866 var setup func(t *testing.T) (*http.Client, *identity.Identity, *Session)
867 {
868 // we limit the scope of the channels, so you cannot accidentally mess up a test case
869 ident := make(chan *identity.Identity, 1)
870 sess := make(chan *Session, 1)
871 r.GET("/set", func(w http.ResponseWriter, r *http.Request) {
872 h, s := testhelpers.MockSessionCreateHandlerWithIdentity(t, reg, <-ident)
873 h(w, r)
874 sess <- s
875 })
876
877 setup = func(t *testing.T) (*http.Client, *identity.Identity, *Session) {
878 client := testhelpers.NewClientWithCookies(t)
879 i := identity.NewIdentity("") // the identity is created by the handler
880
881 ident <- i
882 testhelpers.MockHydrateCookieClient(t, client, ts.URL+"/set")
883 return client, i, <-sess
884 }
885 }
886
887 t.Run("case=list should return pagination headers", func(t *testing.T) {
888 client, i, _ := setup(t)
889
890 numSessions := 5
891 numSessionsActive := 2
892
893 sess := make([]Session, numSessions)
894 for j := range sess {
895 require.NoError(t, faker.FakeData(&sess[j]))
896 sess[j].Identity = i
897 if j < numSessionsActive {
898 sess[j].Active = true
899 } else {
900 sess[j].Active = false
901 }
902 require.NoError(t, reg.SessionPersister().UpsertSession(t.Context(), &sess[j]))
903 }
904
905 reqURL := ts.URL + "/sessions"
906 req, _ := http.NewRequest("GET", reqURL, nil)
907 res, err := client.Do(req)
908 require.NoError(t, err)
909 require.Equal(t, http.StatusOK, res.StatusCode)
910
911 require.NotEqual(t, "", res.Header.Get("Link"))
912 })
913
914 t.Run("case=should return 200 and number after invalidating all other sessions", func(t *testing.T) {
915 client, i, currSess := setup(t)

Callers

nothing calls this directly

Calls 15

NewFastRegistryWithMocksFunction · 0.92
NewClientWithCookiesFunction · 0.92
NewIdentityFunction · 0.92
MockHydrateCookieClientFunction · 0.92
setupFunction · 0.85
UTCMethod · 0.80
GETMethod · 0.65
RunMethod · 0.65
UpsertSessionMethod · 0.65

Tested by

no test coverage detected