MCPcopy Create free account
hub / github.com/dnote/dnote / TestLogin

Function TestLogin

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

Source from the content-addressed store, hash-verified

262}
263
264func TestLogin(t *testing.T) {
265 testutils.RunForWebAndAPI(t, "success", func(t *testing.T, target testutils.EndpointType) {
266 db := testutils.InitMemoryDB(t)
267
268 // Setup
269 a := app.NewTest()
270 a.Clock = clock.NewMock()
271 a.DB = db
272 server := MustNewServer(t, &a)
273
274 _ = testutils.SetupUserData(db, "alice@example.com", "pass1234")
275 defer server.Close()
276
277 // Execute
278 var req *http.Request
279 if target == testutils.EndpointWeb {
280 dat := url.Values{}
281 dat.Set("email", "alice@example.com")
282 dat.Set("password", "pass1234")
283 req = testutils.MakeFormReq(server.URL, "POST", "/login", dat)
284 } else {
285 dat := `{"email": "alice@example.com", "password": "pass1234"}`
286 req = testutils.MakeReq(server.URL, "POST", "/api/v3/signin", dat)
287 }
288
289 res := testutils.HTTPDo(t, req)
290
291 // Test
292 if target == testutils.EndpointWeb {
293 assert.StatusCodeEquals(t, res, http.StatusFound, "")
294 } else {
295 assert.StatusCodeEquals(t, res, http.StatusOK, "")
296 }
297
298 var user database.User
299 testutils.MustExec(t, db.Model(&database.User{}).First(&user), "finding user")
300 assert.NotEqual(t, user.LastLoginAt, nil, "LastLoginAt mismatch")
301
302 if target == testutils.EndpointWeb {
303 assertResponseSessionCookie(t, db, res)
304 } else {
305 // after register, should sign in user
306 var got SessionResponse
307 if err := json.NewDecoder(res.Body).Decode(&got); err != nil {
308 t.Fatal(errors.Wrap(err, "decoding payload"))
309 }
310
311 var sessionCount int64
312 var session database.Session
313 testutils.MustExec(t, db.Model(&database.Session{}).Count(&sessionCount), "counting session")
314 testutils.MustExec(t, db.First(&session), "getting session")
315
316 assert.Equal(t, sessionCount, int64(1), "sessionCount mismatch")
317 assert.Equal(t, got.Key, session.Key, "session Key mismatch")
318 assert.Equal(t, got.ExpiresAt, session.ExpiresAt.Unix(), "session ExpiresAt mismatch")
319
320 assertResponseSessionCookie(t, db, res)
321 }

Callers

nothing calls this directly

Calls 15

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

Tested by

no test coverage detected