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