Make address/domain comparisons case-insensitive

This commit is contained in:
Mike Bland 2015-04-07 17:25:46 -04:00
parent 9534808a0d
commit 781f34e817
2 changed files with 46 additions and 1 deletions

View File

@ -23,11 +23,16 @@ func NewValidator(domains []string, usersFile string) func(string) bool {
csv_reader.TrimLeadingSpace = true csv_reader.TrimLeadingSpace = true
records, err := csv_reader.ReadAll() records, err := csv_reader.ReadAll()
for _, r := range records { 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 { validator := func(email string) bool {
email = strings.ToLower(email)
valid := false valid := false
for _, domain := range domains { for _, domain := range domains {
emailSuffix := fmt.Sprintf("@%s", domain) emailSuffix := fmt.Sprintf("@%s", domain)

40
validator_test.go Normal file
View File

@ -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")
}
}