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

Function TestHandlerRefreshSessionBySessionID

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

Source from the content-addressed store, hash-verified

1043}
1044
1045func TestHandlerRefreshSessionBySessionID(t *testing.T) {
1046 t.Parallel()
1047
1048 _, reg := pkg.NewFastRegistryWithMocks(t,
1049 configx.WithValues(testhelpers.DefaultIdentitySchemaConfig("file://./stub/identity.schema.json")),
1050 )
1051 publicServer, adminServer, _, _ := testhelpers.NewKratosServerWithCSRFAndRouters(t, reg)
1052
1053 i := identity.NewIdentity("")
1054 require.NoError(t, reg.IdentityManager().Create(context.Background(), i))
1055 s := &Session{Identity: i, ExpiresAt: time.Now().Add(5 * time.Minute)}
1056 require.NoError(t, reg.SessionPersister().UpsertSession(context.Background(), s))
1057
1058 t.Run("case=should return 200 after refreshing one session", func(t *testing.T) {
1059 client := testhelpers.NewClientWithCookies(t)
1060
1061 req, _ := http.NewRequest("PATCH", adminServer.URL+"/admin/sessions/"+s.ID.String()+"/extend", nil)
1062 res, err := client.Do(req)
1063 require.NoError(t, err)
1064 require.Equal(t, http.StatusOK, res.StatusCode)
1065
1066 updatedSession, err := reg.SessionPersister().GetSession(context.Background(), s.ID, ExpandNothing)
1067 require.Nil(t, err)
1068 require.True(t, s.ExpiresAt.Before(updatedSession.ExpiresAt))
1069 })
1070
1071 t.Run("case=should return 400 when bad UUID is sent", func(t *testing.T) {
1072 client := testhelpers.NewClientWithCookies(t)
1073 req, _ := http.NewRequest("PATCH", adminServer.URL+"/admin/sessions/BADUUID/extend", nil)
1074 res, err := client.Do(req)
1075 require.NoError(t, err)
1076 require.Equal(t, http.StatusBadRequest, res.StatusCode)
1077 })
1078
1079 t.Run("case=should return 404 when calling with missing UUID", func(t *testing.T) {
1080 client := testhelpers.NewClientWithCookies(t)
1081 someID, _ := uuid.NewV4()
1082 req, _ := http.NewRequest("PATCH", adminServer.URL+"/admin/sessions/"+someID.String()+"/extend", nil)
1083 res, err := client.Do(req)
1084 require.NoError(t, err)
1085 require.Equal(t, http.StatusNotFound, res.StatusCode)
1086 })
1087
1088 t.Run("case=should return 404 when calling puplic server", func(t *testing.T) {
1089 req := testhelpers.NewTestHTTPRequest(t, "PATCH", publicServer.URL+"/sessions/"+s.ID.String()+"/extend", nil)
1090
1091 res, err := publicServer.Client().Do(req)
1092 require.NoError(t, err)
1093 assert.Equal(t, http.StatusNotFound, res.StatusCode)
1094 body := ioutilx.MustReadAll(res.Body)
1095 assert.NotEqual(t, gjson.GetBytes(body, "error.id").String(), "security_csrf_violation")
1096 })
1097}
1098
1099type byCreatedAt []Session
1100

Callers

nothing calls this directly

Calls 15

NewFastRegistryWithMocksFunction · 0.92
NewIdentityFunction · 0.92
NewClientWithCookiesFunction · 0.92
NewTestHTTPRequestFunction · 0.92
IdentityManagerMethod · 0.65
NowMethod · 0.65
UpsertSessionMethod · 0.65
SessionPersisterMethod · 0.65
RunMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected