Check whether the session is getting extended or refreshed if 10% or more of the current expiration time has elapsed.
(t *testing.T)
| 387 | // Check whether the session is getting extended or refreshed if 10% or more of the current |
| 388 | // expiration time has elapsed. |
| 389 | func TestSessionExtension(t *testing.T) { |
| 390 | rt := NewRestTester(t, &RestTesterConfig{GuestEnabled: true}) |
| 391 | defer rt.Close() |
| 392 | |
| 393 | id, err := base.GenerateRandomSecret() |
| 394 | require.NoError(t, err) |
| 395 | |
| 396 | const username = "Alice" |
| 397 | |
| 398 | authenticator := rt.GetDatabase().Authenticator(base.TestCtx(t)) |
| 399 | user, err := authenticator.NewUser(username, "Password", channels.BaseSetOf(t, "*")) |
| 400 | require.NoError(t, err) |
| 401 | require.NoError(t, authenticator.Save(user)) |
| 402 | |
| 403 | // Fake session with more than 10% of the 24 hours TTL has elapsed. It should cause a new |
| 404 | // cookie to be sent by the server with the same session ID and an extended expiration date. |
| 405 | fakeSession := auth.LoginSession{ |
| 406 | ID: id, |
| 407 | Username: username, |
| 408 | Expiration: time.Now().Add(4 * time.Hour), |
| 409 | Ttl: 24 * time.Hour, |
| 410 | SessionUUID: user.GetSessionUUID(), |
| 411 | } |
| 412 | |
| 413 | assert.NoError(t, rt.MetadataStore().Set(authenticator.DocIDForSession(fakeSession.ID), 0, nil, fakeSession)) |
| 414 | reqHeaders := map[string]string{ |
| 415 | "Cookie": auth.DefaultCookieName + "=" + fakeSession.ID, |
| 416 | } |
| 417 | |
| 418 | response := rt.SendRequestWithHeaders("PUT", "/{{.keyspace}}/doc1", `{"hi": "there"}`, reqHeaders) |
| 419 | log.Printf("PUT Request: Set-Cookie: %v", response.Header().Get("Set-Cookie")) |
| 420 | RequireStatus(t, response, http.StatusCreated) |
| 421 | assert.Contains(t, response.Header().Get("Set-Cookie"), auth.DefaultCookieName+"="+fakeSession.ID) |
| 422 | |
| 423 | response = rt.SendRequestWithHeaders("GET", "/{{.keyspace}}/doc1", "", reqHeaders) |
| 424 | log.Printf("GET Request: Set-Cookie: %v", response.Header().Get("Set-Cookie")) |
| 425 | RequireStatus(t, response, http.StatusOK) |
| 426 | assert.Equal(t, "", response.Header().Get("Set-Cookie")) |
| 427 | |
| 428 | // Explicitly delete the fake session doc from the bucket to simulate the test |
| 429 | // scenario for expired session. In reality, Sync Gateway rely on Couchbase |
| 430 | // Server to nuke the expired document based on TTL. Couchbase Server periodically |
| 431 | // removes all items with expiration times that have passed. |
| 432 | assert.NoError(t, rt.MetadataStore().Delete(authenticator.DocIDForSession(fakeSession.ID))) |
| 433 | |
| 434 | response = rt.SendRequestWithHeaders("GET", "/{{.keyspace}}/doc1", "", reqHeaders) |
| 435 | log.Printf("GET Request: Set-Cookie: %v", response.Header().Get("Set-Cookie")) |
| 436 | RequireStatus(t, response, http.StatusUnauthorized) |
| 437 | require.Contains(t, response.Body.String(), "Session Invalid") |
| 438 | } |
| 439 | |
| 440 | func TestSessionAPI(t *testing.T) { |
| 441 |
nothing calls this directly
no test coverage detected