Skip to content

Commit

Permalink
Make address/domain comparisons case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Bland committed Apr 7, 2015
1 parent 9534808 commit 781f34e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
7 changes: 6 additions & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 781f34e

Please sign in to comment.