()
| 269 | } |
| 270 | |
| 271 | func (ts *UserTestSuite) TestUserUpdatePassword() { |
| 272 | u, err := models.FindUserByEmailAndAudience(ts.API.db, "test@example.com", ts.Config.JWT.Aud) |
| 273 | require.NoError(ts.T(), err) |
| 274 | |
| 275 | r, err := models.GrantAuthenticatedUser(ts.API.db, u, models.GrantParams{}) |
| 276 | require.NoError(ts.T(), err) |
| 277 | |
| 278 | r2, err := models.GrantAuthenticatedUser(ts.API.db, u, models.GrantParams{}) |
| 279 | require.NoError(ts.T(), err) |
| 280 | |
| 281 | // create a session and modify it's created_at time to simulate a session that is not recently logged in |
| 282 | notRecentlyLoggedIn, err := models.FindSessionByID(ts.API.db, *r2.SessionId, true) |
| 283 | require.NoError(ts.T(), err) |
| 284 | |
| 285 | // cannot use Update here because Update doesn't removes the created_at field |
| 286 | require.NoError(ts.T(), ts.API.db.RawQuery( |
| 287 | "update "+notRecentlyLoggedIn.TableName()+" set created_at = ? where id = ?", |
| 288 | time.Now().Add(-24*time.Hour), |
| 289 | notRecentlyLoggedIn.ID).Exec(), |
| 290 | ) |
| 291 | |
| 292 | type expected struct { |
| 293 | code int |
| 294 | isAuthenticated bool |
| 295 | } |
| 296 | |
| 297 | var cases = []struct { |
| 298 | desc string |
| 299 | newPassword string |
| 300 | currentPassword string |
| 301 | nonce string |
| 302 | requireReauthentication bool |
| 303 | requireCurrentPassword bool |
| 304 | sessionId *uuid.UUID |
| 305 | expected expected |
| 306 | }{ |
| 307 | { |
| 308 | desc: "Need reauthentication because outside of recently logged in window", |
| 309 | newPassword: "newpassword123", |
| 310 | nonce: "", |
| 311 | requireReauthentication: true, |
| 312 | sessionId: ¬RecentlyLoggedIn.ID, |
| 313 | expected: expected{code: http.StatusBadRequest, isAuthenticated: false}, |
| 314 | }, |
| 315 | { |
| 316 | desc: "No nonce provided", |
| 317 | newPassword: "newpassword123", |
| 318 | nonce: "", |
| 319 | sessionId: ¬RecentlyLoggedIn.ID, |
| 320 | requireReauthentication: true, |
| 321 | expected: expected{code: http.StatusBadRequest, isAuthenticated: false}, |
| 322 | }, |
| 323 | { |
| 324 | desc: "Invalid nonce", |
| 325 | newPassword: "newpassword1234", |
| 326 | nonce: "123456", |
| 327 | sessionId: ¬RecentlyLoggedIn.ID, |
| 328 | requireReauthentication: true, |
nothing calls this directly
no test coverage detected