From 55393c48754c64262fbd26a54c54a18760128ca7 Mon Sep 17 00:00:00 2001 From: Kang Ming Date: Wed, 8 May 2024 14:28:09 +0800 Subject: [PATCH] chore: fix verify tests --- internal/api/verify_test.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/internal/api/verify_test.go b/internal/api/verify_test.go index 9e194b9ec..5a177f569 100644 --- a/internal/api/verify_test.go +++ b/internal/api/verify_test.go @@ -696,16 +696,11 @@ func (ts *VerifyTestSuite) TestVerifySignupWithRedirectURLContainedPath() { func (ts *VerifyTestSuite) TestVerifyPKCEOTP() { u, err := models.FindUserByEmailAndAudience(ts.API.db, "test@example.com", ts.Config.JWT.Aud) require.NoError(ts.T(), err) - u.ConfirmationToken = "pkce_confirmation_token" - u.RecoveryToken = "pkce_recovery_token" t := time.Now() u.ConfirmationSentAt = &t u.RecoverySentAt = &t u.EmailChangeSentAt = &t - require.NoError(ts.T(), ts.API.db.Update(u)) - require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, u.ID, u.GetEmail(), u.ConfirmationToken, models.ConfirmationToken)) - require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, u.ID, u.GetEmail(), u.RecoveryToken, models.RecoveryToken)) cases := []struct { desc string @@ -713,10 +708,10 @@ func (ts *VerifyTestSuite) TestVerifyPKCEOTP() { authenticationMethod models.AuthenticationMethod }{ { - desc: "Verify banned user on signup", + desc: "Verify user on signup", payload: &VerifyParams{ Type: "signup", - Token: u.ConfirmationToken, + Token: "pkce_confirmation_token", }, authenticationMethod: models.EmailSignup, }, @@ -724,7 +719,7 @@ func (ts *VerifyTestSuite) TestVerifyPKCEOTP() { desc: "Verify magiclink", payload: &VerifyParams{ Type: "magiclink", - Token: u.RecoveryToken, + Token: "pkce_recovery_token", }, authenticationMethod: models.MagicLink, }, @@ -732,8 +727,16 @@ func (ts *VerifyTestSuite) TestVerifyPKCEOTP() { for _, c := range cases { ts.Run(c.desc, func() { var buffer bytes.Buffer + // since the test user is the same, the tokens are being cleared after each successful verification attempt + // so we create them on each run + if c.payload.Type == "signup" { + require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, u.ID, u.GetEmail(), c.payload.Token, models.ConfirmationToken)) + } else if c.payload.Type == "magiclink" { + require.NoError(ts.T(), models.CreateOneTimeToken(ts.API.db, u.ID, u.GetEmail(), c.payload.Token, models.RecoveryToken)) + } + require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(c.payload)) - codeChallenge := "codechallengecodechallengcodechallengcodechallengcodechallenge" + c.payload.Type + codeChallenge := "codechallengecodechallengcodechallengcodechallengcodechallenge" flowState := models.NewFlowState(c.authenticationMethod.String(), codeChallenge, models.SHA256, c.authenticationMethod, &u.ID) require.NoError(ts.T(), ts.API.db.Create(flowState))