()
| 434 | } |
| 435 | |
| 436 | func (ts *UserTestSuite) TestAuthenticate() { |
| 437 | // every case uses "test" as the password |
| 438 | cases := []struct { |
| 439 | desc string |
| 440 | hash string |
| 441 | expectedHashCost int |
| 442 | }{ |
| 443 | { |
| 444 | desc: "Invalid bcrypt hash cost of 11", |
| 445 | hash: "$2y$11$4lH57PU7bGATpRcx93vIoObH3qDmft/pytbOzDG9/1WsyNmN5u4di", |
| 446 | expectedHashCost: bcrypt.MinCost, |
| 447 | }, |
| 448 | { |
| 449 | desc: "Valid bcrypt hash cost of 10", |
| 450 | hash: "$2y$10$va66S4MxFrH6G6L7BzYl0.QgcYgvSr/F92gc.3botlz7bG4p/g/1i", |
| 451 | expectedHashCost: bcrypt.DefaultCost, |
| 452 | }, |
| 453 | } |
| 454 | |
| 455 | for _, c := range cases { |
| 456 | ts.Run(c.desc, func() { |
| 457 | u, err := NewUserWithPasswordHash("", "", c.hash, "", nil) |
| 458 | require.NoError(ts.T(), err) |
| 459 | require.NoError(ts.T(), ts.db.Create(u)) |
| 460 | require.NotNil(ts.T(), u) |
| 461 | |
| 462 | isAuthenticated, _, err := u.Authenticate(context.Background(), ts.db, "test", nil, false, "") |
| 463 | require.NoError(ts.T(), err) |
| 464 | require.True(ts.T(), isAuthenticated) |
| 465 | |
| 466 | // check hash cost |
| 467 | hashCost, err := bcrypt.Cost([]byte(*u.EncryptedPassword)) |
| 468 | require.NoError(ts.T(), err) |
| 469 | require.Equal(ts.T(), c.expectedHashCost, hashCost) |
| 470 | }) |
| 471 | } |
| 472 | } |
nothing calls this directly
no test coverage detected