From 781f34e8174952fb468f679fb7ff6c105e8f6a8d Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Tue, 7 Apr 2015 17:25:46 -0400 Subject: [PATCH] Make address/domain comparisons case-insensitive --- validator.go | 7 ++++++- validator_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 validator_test.go diff --git a/validator.go b/validator.go index 81a1bf8..f85e1d4 100644 --- a/validator.go +++ b/validator.go @@ -23,11 +23,16 @@ func NewValidator(domains []string, usersFile string) func(string) bool { csv_reader.TrimLeadingSpace = true records, err := csv_reader.ReadAll() for _, r := range records { - validUsers[r[0]] = true + validUsers[strings.ToLower(r[0])] = true } } + for i, domain := range domains { + domains[i] = strings.ToLower(domain) + } + validator := func(email string) bool { + email = strings.ToLower(email) valid := false for _, domain := range domains { emailSuffix := fmt.Sprintf("@%s", domain) diff --git a/validator_test.go b/validator_test.go new file mode 100644 index 0000000..3c223bd --- /dev/null +++ b/validator_test.go @@ -0,0 +1,40 @@ +package main + +import ( + "io/ioutil" + "os" + "strings" + "testing" +) + +func TestValidatorComparisonsAreCaseInsensitive(t *testing.T) { + auth_email_file, err := ioutil.TempFile("", "test_auth_emails_") + if err != nil { + t.Fatal("failed to create temp file: " + err.Error()) + } + defer os.Remove(auth_email_file.Name()) + + auth_email_file.WriteString( + strings.Join([]string{"Foo.Bar@Example.Com"}, "\n")) + err = auth_email_file.Close() + if err != nil { + t.Fatal("failed to close temp file " + auth_email_file.Name() + + ": " + err.Error()) + } + + domains := []string{"Frobozz.Com"} + validator := NewValidator(domains, auth_email_file.Name()) + + if !validator("foo.bar@example.com") { + t.Error("loaded email addresses are not lower-cased") + } + if !validator("Foo.Bar@Example.Com") { + t.Error("validated email addresses are not lower-cased") + } + if !validator("foo.bar@frobozz.com") { + t.Error("loaded domains are not lower-cased") + } + if !validator("foo.bar@Frobozz.Com") { + t.Error("validated domains are not lower-cased") + } +}