diff --git a/validator.go b/validator.go index 81a1bf8e..f85e1d40 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 00000000..3c223bd7 --- /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") + } +}