Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Make address/domain comparisons case-insensitive #83

Merged
merged 1 commit into from
Apr 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}