git.iko.soy/typeable/oauth2-proxy
on reference-generator help sign in
typeable/oauth2-proxy/validator_test.go
211 lines 5.3 kB history raw
1package main
3import (
4 "io/ioutil"
5 "os"
6 "strings"
7 "testing"
8)
10type ValidatorTest struct {
11 authEmailFileName string
12 done chan bool
13 updateSeen bool
16func NewValidatorTest(t *testing.T) *ValidatorTest {
17 vt := &ValidatorTest{}
18 var err error
19 f, err := ioutil.TempFile("", "test_auth_emails_")
20 if err != nil {
21 t.Fatalf("failed to create temp file: %v", err)
22 }
23 if err := f.Close(); err != nil {
24 t.Fatalf("failed to close temp file: %v", err)
25 }
26 vt.authEmailFileName = f.Name()
27 vt.done = make(chan bool, 1)
28 return vt
31func (vt *ValidatorTest) TearDown() {
32 vt.done <- true
33 os.Remove(vt.authEmailFileName)
36func (vt *ValidatorTest) NewValidator(domains []string,
37 updated chan<- bool) func(string) bool {
38 return newValidatorImpl(domains, vt.authEmailFileName,
39 vt.done, func() {
40 if vt.updateSeen == false {
41 updated <- true
42 vt.updateSeen = true
43 }
44 })
47func (vt *ValidatorTest) WriteEmails(t *testing.T, emails []string) {
48 f, err := os.OpenFile(vt.authEmailFileName, os.O_WRONLY, 0600)
49 if err != nil {
50 t.Fatalf("failed to open auth email file: %v", err)
51 }
53 if _, err := f.WriteString(strings.Join(emails, "\n")); err != nil {
54 t.Fatalf("failed to write emails to auth email file: %v", err)
55 }
57 if err := f.Close(); err != nil {
58 t.Fatalf("failed to close auth email file: %v", err)
59 }
62func TestValidatorEmpty(t *testing.T) {
63 vt := NewValidatorTest(t)
64 defer vt.TearDown()
66 vt.WriteEmails(t, []string(nil))
67 domains := []string(nil)
68 validator := vt.NewValidator(domains, nil)
70 if validator("foo.bar@example.com") {
71 t.Error("nothing should validate when the email and " +
72 "domain lists are empty")
73 }
76func TestValidatorSingleEmail(t *testing.T) {
77 vt := NewValidatorTest(t)
78 defer vt.TearDown()
80 vt.WriteEmails(t, []string{"foo.bar@example.com"})
81 domains := []string(nil)
82 validator := vt.NewValidator(domains, nil)
84 if !validator("foo.bar@example.com") {
85 t.Error("email should validate")
86 }
87 if validator("baz.quux@example.com") {
88 t.Error("email from same domain but not in list " +
89 "should not validate when domain list is empty")
90 }
93func TestValidatorSingleDomain(t *testing.T) {
94 vt := NewValidatorTest(t)
95 defer vt.TearDown()
97 vt.WriteEmails(t, []string(nil))
98 domains := []string{"example.com"}
99 validator := vt.NewValidator(domains, nil)
101 if !validator("foo.bar@example.com") {
102 t.Error("email should validate")
103 }
104 if !validator("baz.quux@example.com") {
105 t.Error("email from same domain should validate")
106 }
109func TestValidatorMultipleEmailsMultipleDomains(t *testing.T) {
110 vt := NewValidatorTest(t)
111 defer vt.TearDown()
113 vt.WriteEmails(t, []string{
114 "xyzzy@example.com",
115 "plugh@example.com",
116 })
117 domains := []string{"example0.com", "example1.com"}
118 validator := vt.NewValidator(domains, nil)
120 if !validator("foo.bar@example0.com") {
121 t.Error("email from first domain should validate")
122 }
123 if !validator("baz.quux@example1.com") {
124 t.Error("email from second domain should validate")
125 }
126 if !validator("xyzzy@example.com") {
127 t.Error("first email in list should validate")
128 }
129 if !validator("plugh@example.com") {
130 t.Error("second email in list should validate")
131 }
132 if validator("xyzzy.plugh@example.com") {
133 t.Error("email not in list that matches no domains " +
134 "should not validate")
135 }
138func TestValidatorComparisonsAreCaseInsensitive(t *testing.T) {
139 vt := NewValidatorTest(t)
140 defer vt.TearDown()
142 vt.WriteEmails(t, []string{"Foo.Bar@Example.Com"})
143 domains := []string{"Frobozz.Com"}
144 validator := vt.NewValidator(domains, nil)
146 if !validator("foo.bar@example.com") {
147 t.Error("loaded email addresses are not lower-cased")
148 }
149 if !validator("Foo.Bar@Example.Com") {
150 t.Error("validated email addresses are not lower-cased")
151 }
152 if !validator("foo.bar@frobozz.com") {
153 t.Error("loaded domains are not lower-cased")
154 }
155 if !validator("foo.bar@Frobozz.Com") {
156 t.Error("validated domains are not lower-cased")
157 }
160func TestValidatorIgnoreSpacesInAuthEmails(t *testing.T) {
161 vt := NewValidatorTest(t)
162 defer vt.TearDown()
164 vt.WriteEmails(t, []string{" foo.bar@example.com "})
165 domains := []string(nil)
166 validator := vt.NewValidator(domains, nil)
168 if !validator("foo.bar@example.com") {
169 t.Error("email should validate")
170 }
173func TestValidatorOverwriteEmailListDirectly(t *testing.T) {
174 vt := NewValidatorTest(t)
175 defer vt.TearDown()
177 vt.WriteEmails(t, []string{
178 "xyzzy@example.com",
179 "plugh@example.com",
180 })
181 domains := []string(nil)
182 updated := make(chan bool)
183 validator := vt.NewValidator(domains, updated)
185 if !validator("xyzzy@example.com") {
186 t.Error("first email in list should validate")
187 }
188 if !validator("plugh@example.com") {
189 t.Error("second email in list should validate")
190 }
191 if validator("xyzzy.plugh@example.com") {
192 t.Error("email not in list that matches no domains " +
193 "should not validate")
194 }
196 vt.WriteEmails(t, []string{
197 "xyzzy.plugh@example.com",
198 "plugh@example.com",
199 })
200 <-updated
202 if validator("xyzzy@example.com") {
203 t.Error("email removed from list should not validate")
204 }
205 if !validator("plugh@example.com") {
206 t.Error("email retained in list should validate")
207 }
208 if !validator("xyzzy.plugh@example.com") {
209 t.Error("email added to list should validate")
210 }