git.iko.soy/typeable/oauth2-proxy
on v3.0.0 help sign in
typeable/oauth2-proxy/validator_test.go
162 lines 4.1 kB history raw
1package main
3import (
4 "io/ioutil"
5 "os"
6 "strings"
7 "testing"
8)
10type ValidatorTest struct {
11 authEmailFile *os.File
12 done chan bool
13 updateSeen bool
16func NewValidatorTest(t *testing.T) *ValidatorTest {
17 vt := &ValidatorTest{}
18 var err error
19 vt.authEmailFile, err = ioutil.TempFile("", "test_auth_emails_")
20 if err != nil {
21 t.Fatal("failed to create temp file: " + err.Error())
22 }
23 vt.done = make(chan bool, 1)
24 return vt
27func (vt *ValidatorTest) TearDown() {
28 vt.done <- true
29 os.Remove(vt.authEmailFile.Name())
32func (vt *ValidatorTest) NewValidator(domains []string,
33 updated chan<- bool) func(string) bool {
34 return newValidatorImpl(domains, vt.authEmailFile.Name(),
35 vt.done, func() {
36 if vt.updateSeen == false {
37 updated <- true
38 vt.updateSeen = true
39 }
40 })
43// This will close vt.authEmailFile.
44func (vt *ValidatorTest) WriteEmails(t *testing.T, emails []string) {
45 defer vt.authEmailFile.Close()
46 vt.authEmailFile.WriteString(strings.Join(emails, "\n"))
47 if err := vt.authEmailFile.Close(); err != nil {
48 t.Fatal("failed to close temp file " +
49 vt.authEmailFile.Name() + ": " + err.Error())
50 }
53func TestValidatorEmpty(t *testing.T) {
54 vt := NewValidatorTest(t)
55 defer vt.TearDown()
57 vt.WriteEmails(t, []string(nil))
58 domains := []string(nil)
59 validator := vt.NewValidator(domains, nil)
61 if validator("foo.bar@example.com") {
62 t.Error("nothing should validate when the email and " +
63 "domain lists are empty")
64 }
67func TestValidatorSingleEmail(t *testing.T) {
68 vt := NewValidatorTest(t)
69 defer vt.TearDown()
71 vt.WriteEmails(t, []string{"foo.bar@example.com"})
72 domains := []string(nil)
73 validator := vt.NewValidator(domains, nil)
75 if !validator("foo.bar@example.com") {
76 t.Error("email should validate")
77 }
78 if validator("baz.quux@example.com") {
79 t.Error("email from same domain but not in list " +
80 "should not validate when domain list is empty")
81 }
84func TestValidatorSingleDomain(t *testing.T) {
85 vt := NewValidatorTest(t)
86 defer vt.TearDown()
88 vt.WriteEmails(t, []string(nil))
89 domains := []string{"example.com"}
90 validator := vt.NewValidator(domains, nil)
92 if !validator("foo.bar@example.com") {
93 t.Error("email should validate")
94 }
95 if !validator("baz.quux@example.com") {
96 t.Error("email from same domain should validate")
97 }
100func TestValidatorMultipleEmailsMultipleDomains(t *testing.T) {
101 vt := NewValidatorTest(t)
102 defer vt.TearDown()
104 vt.WriteEmails(t, []string{
105 "xyzzy@example.com",
106 "plugh@example.com",
107 })
108 domains := []string{"example0.com", "example1.com"}
109 validator := vt.NewValidator(domains, nil)
111 if !validator("foo.bar@example0.com") {
112 t.Error("email from first domain should validate")
113 }
114 if !validator("baz.quux@example1.com") {
115 t.Error("email from second domain should validate")
116 }
117 if !validator("xyzzy@example.com") {
118 t.Error("first email in list should validate")
119 }
120 if !validator("plugh@example.com") {
121 t.Error("second email in list should validate")
122 }
123 if validator("xyzzy.plugh@example.com") {
124 t.Error("email not in list that matches no domains " +
125 "should not validate")
126 }
129func TestValidatorComparisonsAreCaseInsensitive(t *testing.T) {
130 vt := NewValidatorTest(t)
131 defer vt.TearDown()
133 vt.WriteEmails(t, []string{"Foo.Bar@Example.Com"})
134 domains := []string{"Frobozz.Com"}
135 validator := vt.NewValidator(domains, nil)
137 if !validator("foo.bar@example.com") {
138 t.Error("loaded email addresses are not lower-cased")
139 }
140 if !validator("Foo.Bar@Example.Com") {
141 t.Error("validated email addresses are not lower-cased")
142 }
143 if !validator("foo.bar@frobozz.com") {
144 t.Error("loaded domains are not lower-cased")
145 }
146 if !validator("foo.bar@Frobozz.Com") {
147 t.Error("validated domains are not lower-cased")
148 }
151func TestValidatorIgnoreSpacesInAuthEmails(t *testing.T) {
152 vt := NewValidatorTest(t)
153 defer vt.TearDown()
155 vt.WriteEmails(t, []string{" foo.bar@example.com "})
156 domains := []string(nil)
157 validator := vt.NewValidator(domains, nil)
159 if !validator("foo.bar@example.com") {
160 t.Error("email should validate")
161 }