Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed Oct 5, 2024
1 parent 007a0a2 commit a7b223f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
43 changes: 43 additions & 0 deletions internal/api/mail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,49 @@ func (ts *MailTestSuite) SetupTest() {
require.NoError(ts.T(), ts.API.db.Create(u), "Error saving new user")
}

func (ts *MailTestSuite) TestValidateEmail() {
cases := []struct {
desc string
email string
expectedEmail string
expectedError error
}{
{
desc: "valid email",
email: "test@example.com",
expectedEmail: "test@example.com",
expectedError: nil,
},
{
desc: "email should be normalized",
email: "TEST@EXAMPLE.COM",
expectedEmail: "test@example.com",
expectedError: nil,
},
{
desc: "empty email should return error",
email: "",
expectedEmail: "",
expectedError: badRequestError(ErrorCodeValidationFailed, "An email address is required"),
},
{
desc: "email length exceeds 255 characters",
// email has 256 characters
email: "testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest@example.com",
expectedEmail: "",
expectedError: badRequestError(ErrorCodeValidationFailed, "An email address is too long"),
},
}

for _, c := range cases {
ts.Run(c.desc, func() {
email, err := ts.API.validateEmail(c.email)
require.Equal(ts.T(), c.expectedError, err)
require.Equal(ts.T(), c.expectedEmail, email)
})
}
}

func (ts *MailTestSuite) TestGenerateLink() {
// create admin jwt
claims := &AccessTokenClaims{
Expand Down
14 changes: 14 additions & 0 deletions internal/api/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -900,6 +901,19 @@ func (ts *VerifyTestSuite) TestVerifyValidOtp() {
tokenHash: crypto.GenerateTokenHash(u.GetEmail(), "123456"),
},
},
{
desc: "Valid Email OTP (email casing shouldn't matter)",
sentTime: time.Now(),
body: map[string]interface{}{
"type": mail.EmailOTPVerification,
"token": "123456",
"email": strings.ToUpper(u.GetEmail()),
},
expected: expected{
code: http.StatusOK,
tokenHash: crypto.GenerateTokenHash(u.GetEmail(), "123456"),
},
},
{
desc: "Valid Email Change OTP",
sentTime: time.Now(),
Expand Down

0 comments on commit a7b223f

Please sign in to comment.