(t *testing.T)
| 749 | } |
| 750 | |
| 751 | func TestCreateResetToken(t *testing.T) { |
| 752 | t.Run("success", func(t *testing.T) { |
| 753 | db := testutils.InitMemoryDB(t) |
| 754 | |
| 755 | // Setup |
| 756 | a := app.NewTest() |
| 757 | a.Clock = clock.NewMock() |
| 758 | a.DB = db |
| 759 | server := MustNewServer(t, &a) |
| 760 | defer server.Close() |
| 761 | |
| 762 | u := testutils.SetupUserData(db, "alice@example.com", "somepassword") |
| 763 | |
| 764 | // Execute |
| 765 | dat := url.Values{} |
| 766 | dat.Set("email", "alice@example.com") |
| 767 | req := testutils.MakeFormReq(server.URL, "POST", "/reset-token", dat) |
| 768 | |
| 769 | res := testutils.HTTPDo(t, req) |
| 770 | |
| 771 | // Test |
| 772 | assert.StatusCodeEquals(t, res, http.StatusFound, "Status code mismtach") |
| 773 | |
| 774 | var tokenCount int64 |
| 775 | testutils.MustExec(t, db.Model(&database.Token{}).Count(&tokenCount), "counting tokens") |
| 776 | |
| 777 | var resetToken database.Token |
| 778 | testutils.MustExec(t, db.Where("user_id = ? AND type = ?", u.ID, database.TokenTypeResetPassword).First(&resetToken), "finding reset token") |
| 779 | |
| 780 | assert.Equal(t, tokenCount, int64(1), "reset_token count mismatch") |
| 781 | assert.NotEqual(t, resetToken.Value, nil, "reset_token value mismatch") |
| 782 | assert.Equal(t, resetToken.UsedAt, (*time.Time)(nil), "reset_token UsedAt mismatch") |
| 783 | }) |
| 784 | |
| 785 | t.Run("nonexistent email", func(t *testing.T) { |
| 786 | db := testutils.InitMemoryDB(t) |
| 787 | |
| 788 | // Setup |
| 789 | a := app.NewTest() |
| 790 | a.Clock = clock.NewMock() |
| 791 | a.DB = db |
| 792 | server := MustNewServer(t, &a) |
| 793 | defer server.Close() |
| 794 | |
| 795 | _ = testutils.SetupUserData(db, "alice@example.com", "somepassword") |
| 796 | |
| 797 | // Execute |
| 798 | dat := url.Values{} |
| 799 | dat.Set("email", "bob@example.com") |
| 800 | req := testutils.MakeFormReq(server.URL, "POST", "/reset-token", dat) |
| 801 | |
| 802 | res := testutils.HTTPDo(t, req) |
| 803 | |
| 804 | // Test |
| 805 | assert.StatusCodeEquals(t, res, http.StatusOK, "Status code mismtach") |
| 806 | |
| 807 | var tokenCount int64 |
| 808 | testutils.MustExec(t, db.Model(&database.Token{}).Count(&tokenCount), "counting tokens") |
nothing calls this directly
no test coverage detected