git.iko.soy/typeable/oauth2-proxy
on v2.2 help sign in
typeable/oauth2-proxy/validator_watcher_test.go
102 lines 2.6 kB history raw
1// +build go1.3,!plan9,!solaris
3package main
5import (
6 "io/ioutil"
7 "os"
8 "testing"
9)
11func (vt *ValidatorTest) UpdateEmailFile(t *testing.T, emails []string) {
12 var err error
13 vt.auth_email_file, err = os.OpenFile(
14 vt.auth_email_file.Name(), os.O_WRONLY|os.O_CREATE, 0600)
15 if err != nil {
16 t.Fatal("failed to re-open temp file for updates")
17 }
18 vt.WriteEmails(t, emails)
21func (vt *ValidatorTest) UpdateEmailFileViaRenameAndReplace(
22 t *testing.T, emails []string) {
23 orig_file := vt.auth_email_file
24 var err error
25 vt.auth_email_file, err = ioutil.TempFile("", "test_auth_emails_")
26 if err != nil {
27 t.Fatal("failed to create temp file for rename and replace: " +
28 err.Error())
29 }
30 vt.WriteEmails(t, emails)
32 moved_name := orig_file.Name() + "-moved"
33 err = os.Rename(orig_file.Name(), moved_name)
34 err = os.Rename(vt.auth_email_file.Name(), orig_file.Name())
35 if err != nil {
36 t.Fatal("failed to rename and replace temp file: " +
37 err.Error())
38 }
39 vt.auth_email_file = orig_file
40 os.Remove(moved_name)
43func TestValidatorOverwriteEmailListDirectly(t *testing.T) {
44 vt := NewValidatorTest(t)
45 defer vt.TearDown()
47 vt.WriteEmails(t, []string{
48 "xyzzy@example.com",
49 "plugh@example.com",
50 })
51 domains := []string(nil)
52 updated := make(chan bool)
53 validator := vt.NewValidator(domains, updated)
55 if !validator("xyzzy@example.com") {
56 t.Error("first email in list should validate")
57 }
58 if !validator("plugh@example.com") {
59 t.Error("second email in list should validate")
60 }
61 if validator("xyzzy.plugh@example.com") {
62 t.Error("email not in list that matches no domains " +
63 "should not validate")
64 }
66 vt.UpdateEmailFile(t, []string{
67 "xyzzy.plugh@example.com",
68 "plugh@example.com",
69 })
70 <-updated
72 if validator("xyzzy@example.com") {
73 t.Error("email removed from list should not validate")
74 }
75 if !validator("plugh@example.com") {
76 t.Error("email retained in list should validate")
77 }
78 if !validator("xyzzy.plugh@example.com") {
79 t.Error("email added to list should validate")
80 }
83func TestValidatorOverwriteEmailListViaRenameAndReplace(t *testing.T) {
84 vt := NewValidatorTest(t)
85 defer vt.TearDown()
87 vt.WriteEmails(t, []string{"xyzzy@example.com"})
88 domains := []string(nil)
89 updated := make(chan bool)
90 validator := vt.NewValidator(domains, updated)
92 if !validator("xyzzy@example.com") {
93 t.Error("email in list should validate")
94 }
96 vt.UpdateEmailFileViaRenameAndReplace(t, []string{"plugh@example.com"})
97 <-updated
99 if validator("xyzzy@example.com") {
100 t.Error("email removed from list should not validate")
101 }