(t *testing.T)
| 856 | } |
| 857 | |
| 858 | func 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) |
nothing calls this directly
no test coverage detected