MCPcopy Index your code
hub / github.com/dnote/dnote / TestLogout

Function TestLogout

pkg/server/controllers/users_test.go:433–541  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

431}
432
433func TestLogout(t *testing.T) {
434 setupLogoutTest := func(t *testing.T, db *gorm.DB) (*httptest.Server, *database.Session, *database.Session) {
435 // Setup
436 a := app.NewTest()
437 a.Clock = clock.NewMock()
438 a.DB = db
439 server := MustNewServer(t, &a)
440
441 aliceUser := testutils.SetupUserData(db, "alice@example.com", "pass1234")
442 anotherUser := testutils.SetupUserData(db, "bob@example.com", "password123")
443
444 session1ExpiresAt := time.Now().Add(time.Hour * 24)
445 session1 := database.Session{
446 Key: "A9xgggqzTHETy++GDi1NpDNe0iyqosPm9bitdeNGkJU=",
447 UserID: aliceUser.ID,
448 ExpiresAt: session1ExpiresAt,
449 }
450 testutils.MustExec(t, db.Save(&session1), "preparing session1")
451 session2 := database.Session{
452 Key: "MDCpbvCRg7W2sH6S870wqLqZDZTObYeVd0PzOekfo/A=",
453 UserID: anotherUser.ID,
454 ExpiresAt: time.Now().Add(time.Hour * 24),
455 }
456 testutils.MustExec(t, db.Save(&session2), "preparing session2")
457
458 return server, &session1, &session2
459 }
460
461 testutils.RunForWebAndAPI(t, "authenticated", func(t *testing.T, target testutils.EndpointType) {
462 db := testutils.InitMemoryDB(t)
463
464 server, session1, _ := setupLogoutTest(t, db)
465 defer server.Close()
466
467 // Execute
468 var req *http.Request
469 if target == testutils.EndpointWeb {
470 dat := url.Values{}
471 req = testutils.MakeFormReq(server.URL, "POST", "/logout", dat)
472 req.AddCookie(&http.Cookie{Name: "id", Value: "A9xgggqzTHETy++GDi1NpDNe0iyqosPm9bitdeNGkJU=", Expires: session1.ExpiresAt, Path: "/", HttpOnly: true})
473 } else {
474 req = testutils.MakeReq(server.URL, "POST", "/api/v3/signout", "")
475 req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", session1.Key))
476 }
477
478 res := testutils.HTTPDo(t, req)
479
480 // Test
481 if target == testutils.EndpointWeb {
482 assert.StatusCodeEquals(t, res, http.StatusFound, "Status mismatch")
483 } else {
484 assert.StatusCodeEquals(t, res, http.StatusNoContent, "Status mismatch")
485 }
486
487 var sessionCount int64
488 var s2 database.Session
489 testutils.MustExec(t, db.Model(&database.Session{}).Count(&sessionCount), "counting session")
490 testutils.MustExec(t, db.Where("key = ?", "MDCpbvCRg7W2sH6S870wqLqZDZTObYeVd0PzOekfo/A=").First(&s2), "getting s2")

Callers

nothing calls this directly

Calls 15

NewTestFunction · 0.92
NewMockFunction · 0.92
SetupUserDataFunction · 0.92
MustExecFunction · 0.92
RunForWebAndAPIFunction · 0.92
InitMemoryDBFunction · 0.92
MakeFormReqFunction · 0.92
MakeReqFunction · 0.92
HTTPDoFunction · 0.92
StatusCodeEqualsFunction · 0.92
EqualFunction · 0.92
GetCookieByNameFunction · 0.92

Tested by

no test coverage detected