@@ -7,23 +7,117 @@ import (
77 "testing"
88)
99
10−func TestValidatorComparisonsAreCaseInsensitive(t *testing.T) {
11− auth_email_file, err := ioutil.TempFile("", "test_auth_emails_")
10+type ValidatorTest struct {
11+ auth_email_file *os.File
12+}
13+
14+func NewValidatorTest(t *testing.T) *ValidatorTest {
15+ vt := &ValidatorTest{}
16+ var err error
17+ vt.auth_email_file, err = ioutil.TempFile("", "test_auth_emails_")
1218 if err != nil {
1319 t.Fatal("failed to create temp file: " + err.Error())
1420 }
15− defer os.Remove(auth_email_file.Name())
21+ return vt
22+}
1623
17− auth_email_file.WriteString(
18− strings.Join([]string{"Foo.Bar@Example.Com"}, "\n"))
19− err = auth_email_file.Close()
20− if err != nil {
21− t.Fatal("failed to close temp file " + auth_email_file.Name() +
22− ": " + err.Error())
24+func (vt *ValidatorTest) TearDown() {
25+ os.Remove(vt.auth_email_file.Name())
26+}
27+
28+// This will close vt.auth_email_file.
29+func (vt *ValidatorTest) WriteEmails(t *testing.T, emails []string) {
30+ defer vt.auth_email_file.Close()
31+ vt.auth_email_file.WriteString(strings.Join(emails, "\n"))
32+ if err := vt.auth_email_file.Close(); err != nil {
33+ t.Fatal("failed to close temp file " +
34+ vt.auth_email_file.Name() + ": " + err.Error())
35+ }
36+}
37+
38+func TestValidatorEmpty(t *testing.T) {
39+ vt := NewValidatorTest(t)
40+ defer vt.TearDown()
41+
42+ vt.WriteEmails(t, []string(nil))
43+ domains := []string(nil)
44+ validator := NewValidator(domains, vt.auth_email_file.Name())
45+
46+ if validator("foo.bar@example.com") {
47+ t.Error("nothing should validate when the email and " +
48+ "domain lists are empty")
49+ }
50+}
51+
52+func TestValidatorSingleEmail(t *testing.T) {
53+ vt := NewValidatorTest(t)
54+ defer vt.TearDown()
55+
56+ vt.WriteEmails(t, []string{"foo.bar@example.com"})
57+ domains := []string(nil)
58+ validator := NewValidator(domains, vt.auth_email_file.Name())
59+
60+ if !validator("foo.bar@example.com") {
61+ t.Error("email should validate")
62+ }
63+ if validator("baz.quux@example.com") {
64+ t.Error("email from same domain but not in list " +
65+ "should not validate when domain list is empty")
2366 }
67+}
68+
69+func TestValidatorSingleDomain(t *testing.T) {
70+ vt := NewValidatorTest(t)
71+ defer vt.TearDown()
72+
73+ vt.WriteEmails(t, []string(nil))
74+ domains := []string{"example.com"}
75+ validator := NewValidator(domains, vt.auth_email_file.Name())
76+
77+ if !validator("foo.bar@example.com") {
78+ t.Error("email should validate")
79+ }
80+ if !validator("baz.quux@example.com") {
81+ t.Error("email from same domain should validate")
82+ }
83+}
84+
85+func TestValidatorMultipleEmailsMultipleDomains(t *testing.T) {
86+ vt := NewValidatorTest(t)
87+ defer vt.TearDown()
88+
89+ vt.WriteEmails(t, []string{
90+ "xyzzy@example.com",
91+ "plugh@example.com",
92+ })
93+ domains := []string{"example0.com", "example1.com"}
94+ validator := NewValidator(domains, vt.auth_email_file.Name())
95+
96+ if !validator("foo.bar@example0.com") {
97+ t.Error("email from first domain should validate")
98+ }
99+ if !validator("baz.quux@example1.com") {
100+ t.Error("email from second domain should validate")
101+ }
102+ if !validator("xyzzy@example.com") {
103+ t.Error("first email in list should validate")
104+ }
105+ if !validator("plugh@example.com") {
106+ t.Error("second email in list should validate")
107+ }
108+ if validator("xyzzy.plugh@example.com") {
109+ t.Error("email not in list that matches no domains " +
110+ "should not validate")
111+ }
112+}
113+
114+func TestValidatorComparisonsAreCaseInsensitive(t *testing.T) {
115+ vt := NewValidatorTest(t)
116+ defer vt.TearDown()
24117
118+ vt.WriteEmails(t, []string{"Foo.Bar@Example.Com"})
25119 domains := []string{"Frobozz.Com"}
26− validator := NewValidator(domains, auth_email_file.Name())
120+ validator := NewValidator(domains, vt.auth_email_file.Name())
27121
28122 if !validator("foo.bar@example.com") {
29123 t.Error("loaded email addresses are not lower-cased")