-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathemail_test.go
181 lines (164 loc) · 4.68 KB
/
email_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package types
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEmail_MarshalJSON_Validation(t *testing.T) {
type requiredEmail struct {
EmailField Email `json:"email"`
}
testCases := map[string]struct {
email Email
expectedJSON []byte
expectedError error
}{
"it should succeed marshalling a valid email and return valid JSON populated with the email": {
email: Email("[email protected]"),
expectedJSON: []byte(`{"email":"[email protected]"}`),
expectedError: nil,
},
"it should succeed marshalling a valid email and return valid JSON populated with the email with valid separators": {
email: Email("validemail+with_valid-separator{like}~these*[email protected]"),
expectedJSON: []byte(`{"email":"validemail+with_valid-separator{like}~these*[email protected]"}`),
expectedError: nil,
},
"it should fail marshalling an invalid email and return a validation error": {
email: Email("invalidemail"),
expectedJSON: nil,
expectedError: ErrValidationEmail,
},
"it should fail marshalling an empty email and return a validation error": {
email: Email(""),
expectedJSON: nil,
expectedError: ErrValidationEmail,
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
jsonBytes, err := json.Marshal(requiredEmail{EmailField: tc.email})
if tc.expectedError != nil {
assert.ErrorIs(t, err, tc.expectedError)
} else {
assert.JSONEq(t, string(tc.expectedJSON), string(jsonBytes))
}
})
}
}
func TestEmail_UnmarshalJSON_RequiredEmail_Validation(t *testing.T) {
type requiredEmail struct {
EmailField Email `json:"email"`
}
requiredEmailTestCases := map[string]struct {
jsonStr string
expectedEmail Email
expectedError error
}{
"it should succeed validating a valid email during the unmarshal process": {
jsonStr: `{"email":"[email protected]"}`,
expectedError: nil,
expectedEmail: func() Email {
e := Email("[email protected]")
return e
}(),
},
"it should fail validating an invalid email": {
jsonStr: `{"email":"not-an-email"}`,
expectedError: ErrValidationEmail,
expectedEmail: func() Email {
e := Email("not-an-email")
return e
}(),
},
"it should fail validating an empty email": {
jsonStr: `{"email":""}`,
expectedEmail: func() Email {
e := Email("")
return e
}(),
expectedError: ErrValidationEmail,
},
"it should fail validating a null email": {
jsonStr: `{"email":null}`,
expectedEmail: func() Email {
e := Email("")
return e
}(),
expectedError: ErrValidationEmail,
},
}
for name, tc := range requiredEmailTestCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
b := requiredEmail{}
err := json.Unmarshal([]byte(tc.jsonStr), &b)
assert.Equal(t, tc.expectedEmail, b.EmailField)
assert.ErrorIs(t, err, tc.expectedError)
})
}
}
func TestEmail_UnmarshalJSON_NullableEmail_Validation(t *testing.T) {
type nullableEmail struct {
EmailField *Email `json:"email,omitempty"`
}
nullableEmailTestCases := map[string]struct {
body nullableEmail
jsonStr string
expectedEmail *Email
expectedError error
}{
"it should succeed validating a valid email during the unmarshal process": {
body: nullableEmail{},
jsonStr: `{"email":"[email protected]"}`,
expectedError: nil,
expectedEmail: func() *Email {
e := Email("[email protected]")
return &e
}(),
},
"it should fail validating an invalid email": {
body: nullableEmail{},
jsonStr: `{"email":"not-an-email"}`,
expectedError: ErrValidationEmail,
expectedEmail: func() *Email {
e := Email("not-an-email")
return &e
}(),
},
"it should fail validating an empty email": {
body: nullableEmail{},
jsonStr: `{"email":""}`,
expectedError: ErrValidationEmail,
expectedEmail: func() *Email {
e := Email("")
return &e
}(),
},
"it should succeed validating a null email": {
body: nullableEmail{},
jsonStr: `{"email":null}`,
expectedEmail: nil,
expectedError: nil,
},
"it should succeed validating a missing email": {
body: nullableEmail{},
jsonStr: `{}`,
expectedEmail: nil,
expectedError: nil,
},
}
for name, tc := range nullableEmailTestCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
err := json.Unmarshal([]byte(tc.jsonStr), &tc.body)
assert.Equal(t, tc.expectedEmail, tc.body.EmailField)
if tc.expectedError != nil {
assert.ErrorIs(t, err, tc.expectedError)
}
})
}
}