From 137330b2020f7d1b10d24c300b766505264074ce Mon Sep 17 00:00:00 2001 From: Eric Chiang Date: Tue, 1 Dec 2015 09:06:30 -0800 Subject: [PATCH] user: fix bug in ValidEmail helper mail.ParseAddress will stop parsing a string once it finds a valid email address. This means you could give ValidUser an email address followed by junk and it would mark it valid. This commit fixes this behavior and adds some more test cases. Fixes #189 --- user/user.go | 7 +++---- user/user_test.go | 3 +++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/user/user.go b/user/user.go index fd606d7c..a1b05de0 100644 --- a/user/user.go +++ b/user/user.go @@ -163,10 +163,9 @@ func ValidEmail(email string) bool { return false } - if address.Name != "" || address.Address == "" { - return false - } - return true + // Has mail.ParseAddress parsed the entire string and only found a single + // address without a name? + return address.Address == email } func ValidPassword(plaintext string) bool { diff --git a/user/user_test.go b/user/user_test.go index 31eeb082..eac81588 100644 --- a/user/user_test.go +++ b/user/user_test.go @@ -103,6 +103,9 @@ func TestValidEmail(t *testing.T) { {"r@r.com", true}, {"Barry Gibbs ", false}, {"", false}, + {"invalidemail", false}, + {"example@example.com example@example.com", false}, + {"example@example.com Hello, 世界", false}, } for i, tt := range tests {