Skip to content

Commit

Permalink
unit testing for verification code expiration flow
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Sep 23, 2024
1 parent 227973d commit 8559be4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
7 changes: 4 additions & 3 deletions api/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package api

import "time"

// VerificationCodeExpiration is the duration of the verification code
// before it is invalidated
var VerificationCodeExpiration = 2 * time.Minute

const (
// VerificationCodeExpiration is the duration of the verification code
// before it is invalidated
VerificationCodeExpiration = 2 * time.Minute
// VerificationCodeLength is the length of the verification code in bytes
VerificationCodeLength = 3
// VerificationCodeEmailSubject is the subject of the verification code email
Expand Down
29 changes: 28 additions & 1 deletion api/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"strings"
"testing"
"time"

qt "github.com/frankban/quicktest"
)
Expand Down Expand Up @@ -152,7 +153,8 @@ func TestVerifyAccountHandler(t *testing.T) {
c.Logf("error resetting test database: %v", err)
}
}()
// register a user
// register a user with short expiration time
VerificationCodeExpiration = 5 * time.Second
jsonUser := mustMarshal(&UserInfo{
Email: testEmail,
Password: testPass,
Expand All @@ -176,6 +178,7 @@ func TestVerifyAccountHandler(t *testing.T) {
c.Assert(err, qt.IsNil)
c.Assert(resp.StatusCode, qt.Equals, http.StatusUnauthorized)
c.Assert(resp.Body.Close(), qt.IsNil)
// try to verify the user (should fail)
// get the verification code from the email
mailBody, err := testMailService.FindEmail(context.Background(), testEmail)
c.Assert(err, qt.IsNil)
Expand All @@ -187,6 +190,30 @@ func TestVerifyAccountHandler(t *testing.T) {
})
req, err = http.NewRequest(http.MethodPost, testURL(verifyUserEndpoint), bytes.NewBuffer(verification))
c.Assert(err, qt.IsNil)
// wait to expire the verification code
time.Sleep(VerificationCodeExpiration)
resp, err = http.DefaultClient.Do(req)
c.Assert(err, qt.IsNil)
c.Assert(resp.StatusCode, qt.Equals, http.StatusUnauthorized)
c.Assert(resp.Body.Close(), qt.IsNil)
// resend the verification code and verify the user
req, err = http.NewRequest(http.MethodPost, testURL(verifyUserCodeEndpoint), bytes.NewBuffer(verification))
c.Assert(err, qt.IsNil)
resp, err = http.DefaultClient.Do(req)
c.Assert(err, qt.IsNil)
c.Assert(resp.StatusCode, qt.Equals, http.StatusOK)
c.Assert(resp.Body.Close(), qt.IsNil)
// get the verification code from the email
mailBody, err = testMailService.FindEmail(context.Background(), testEmail)
c.Assert(err, qt.IsNil)
mailCode = strings.TrimPrefix(mailBody, VerificationCodeTextBody)
// verify the user
verification = mustMarshal(&UserVerification{
Email: testEmail,
Code: mailCode,
})
req, err = http.NewRequest(http.MethodPost, testURL(verifyUserEndpoint), bytes.NewBuffer(verification))
c.Assert(err, qt.IsNil)
resp, err = http.DefaultClient.Do(req)
c.Assert(err, qt.IsNil)
c.Assert(resp.StatusCode, qt.Equals, http.StatusOK)
Expand Down

0 comments on commit 8559be4

Please sign in to comment.