MCPcopy
hub / github.com/revel/revel / TestEmail

Function TestEmail

validators_test.go:226–291  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

224}
225
226func TestEmail(t *testing.T) {
227 // unicode char included
228 validStartingCharacters := strings.Split("!#$%^&*_+1234567890abcdefghijklmnopqrstuvwxyzñ", "")
229 invalidCharacters := strings.Split(" ()", "")
230
231 definiteInvalidDomains := []string{
232 "", // any empty string (x@)
233 ".com", // only the TLD (x@.com)
234 ".", // only the . (x@.)
235 ".*", // TLD containing symbol (x@.*)
236 "asdf", // no TLD
237 "a!@#$%^&*()+_.com", // characters which are not ASCII/0-9/dash(-) in a domain
238 "-a.com", // host starting with any symbol
239 "a-.com", // host ending with any symbol
240 "aå.com", // domain containing unicode (however, unicode domains do exist in the state of xn--<POINT>.com e.g. å.com = xn--5ca.com)
241 }
242
243 // Email pattern is not exposed
244 emailPattern := regexp.MustCompile("^[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[a-zA-Z0-9](?:[\\w-]*[\\w])?$")
245 for _, email := range []revel.Email{{revel.Match{emailPattern}}, revel.ValidEmail()} {
246 var currentEmail string
247
248 // test invalid starting chars
249 for _, startingChar := range validStartingCharacters {
250 currentEmail = fmt.Sprintf("%sñbc+123@do-main.com", startingChar)
251 if email.IsSatisfied(currentEmail) {
252 t.Errorf(noErrorsMessage, "starting characters", fmt.Sprintf("email = %s", currentEmail))
253 }
254
255 // validation should fail because of multiple @ symbols
256 currentEmail = fmt.Sprintf("%s@ñbc+123@do-main.com", startingChar)
257 if email.IsSatisfied(currentEmail) {
258 t.Errorf(errorsMessage, "starting characters with multiple @ symbols", fmt.Sprintf("email = %s", currentEmail))
259 }
260
261 // should fail simply because of the invalid char
262 for _, invalidChar := range invalidCharacters {
263 currentEmail = fmt.Sprintf("%sñbc%s+123@do-main.com", startingChar, invalidChar)
264 if email.IsSatisfied(currentEmail) {
265 t.Errorf(errorsMessage, "invalid starting characters", fmt.Sprintf("email = %s", currentEmail))
266 }
267 }
268 }
269
270 // test invalid domains
271 for _, invalidDomain := range definiteInvalidDomains {
272 currentEmail = fmt.Sprintf("a@%s", invalidDomain)
273 if email.IsSatisfied(currentEmail) {
274 t.Errorf(errorsMessage, "invalid domain", fmt.Sprintf("email = %s", currentEmail))
275 }
276 }
277
278 // should always be satisfied
279 if !email.IsSatisfied("t0.est+email123@1abc0-def.com") {
280 t.Errorf(noErrorsMessage, "guaranteed valid email", fmt.Sprintf("email = %s", "t0.est+email123@1abc0-def.com"))
281 }
282
283 // should never be satisfied (this is redundant given the loops above)

Callers

nothing calls this directly

Calls 3

ValidEmailFunction · 0.92
IsSatisfiedMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected