Skip to content

Commit 0ff1c49

Browse files
committed
Fix test
1 parent e22b415 commit 0ff1c49

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

models/user/email_address.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,23 @@ import (
2525
var (
2626
// ErrEmailNotActivated e-mail address has not been activated error
2727
ErrEmailNotActivated = errors.New("e-mail address has not been activated")
28-
// ErrEmailCharIsNotSupported e-mail address contains unsupported character
29-
ErrEmailCharIsNotSupported = errors.New("e-mail address contains unsupported character")
3028
)
3129

30+
// ErrEmailCharIsNotSupported e-mail address contains unsupported character
31+
type ErrEmailCharIsNotSupported struct {
32+
Email string
33+
}
34+
35+
// IsErrEmailCharIsNotSupported checks if an error is an ErrEmailCharIsNotSupported
36+
func IsErrEmailCharIsNotSupported(err error) bool {
37+
_, ok := err.(ErrEmailCharIsNotSupported)
38+
return ok
39+
}
40+
41+
func (err ErrEmailCharIsNotSupported) Error() string {
42+
return fmt.Sprintf("e-mail address contains unsupported character [email: %s]", err.Email)
43+
}
44+
3245
// ErrEmailInvalid represents an error where the email address does not comply with RFC 5322
3346
type ErrEmailInvalid struct {
3447
Email string
@@ -120,7 +133,7 @@ func ValidateEmail(email string) error {
120133
}
121134

122135
if !emailRegexp.MatchString(email) {
123-
return ErrEmailCharIsNotSupported
136+
return ErrEmailCharIsNotSupported{email}
124137
}
125138

126139
if !(email[0] >= 'a' && email[0] <= 'z') &&

models/user/email_address_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func TestEmailAddressValidate(t *testing.T) {
278278
`first|[email protected]`: nil,
279279
`first}[email protected]`: nil,
280280
281-
`first;[email protected]`: ErrEmailCharIsNotSupported,
281+
`first;[email protected]`: ErrEmailCharIsNotSupported{`first;[email protected]`},
282282
"[email protected]": ErrEmailInvalid{"[email protected]"},
283283
"[email protected]": ErrEmailInvalid{"[email protected]"},
284284
"#[email protected]": ErrEmailInvalid{"#[email protected]"},
@@ -297,9 +297,9 @@ func TestEmailAddressValidate(t *testing.T) {
297297
"|[email protected]": ErrEmailInvalid{"|[email protected]"},
298298
"}[email protected]": ErrEmailInvalid{"}[email protected]"},
299299
"[email protected]": ErrEmailInvalid{"[email protected]"},
300-
";[email protected]": ErrEmailCharIsNotSupported,
301-
"Foo <[email protected]>": ErrEmailCharIsNotSupported,
302-
string([]byte{0xE2, 0x84, 0xAA}): ErrEmailCharIsNotSupported,
300+
";[email protected]": ErrEmailCharIsNotSupported{";[email protected]"},
301+
"Foo <[email protected]>": ErrEmailCharIsNotSupported{"Foo <[email protected]>"},
302+
string([]byte{0xE2, 0x84, 0xAA}): ErrEmailCharIsNotSupported{string([]byte{0xE2, 0x84, 0xAA})},
303303
}
304304
for kase, err := range kases {
305305
t.Run(kase, func(t *testing.T) {

models/user/user_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package user
66

77
import (
8+
"fmt"
89
"math/rand"
910
"strings"
1011
"testing"
@@ -232,7 +233,7 @@ func TestCreateUserInvalidEmail(t *testing.T) {
232233

233234
err := CreateUser(user)
234235
assert.Error(t, err)
235-
assert.True(t, IsErrEmailInvalid(err))
236+
assert.True(t, IsErrEmailCharIsNotSupported(err))
236237
}
237238

238239
func TestCreateUserEmailAlreadyUsed(t *testing.T) {

0 commit comments

Comments
 (0)