mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2025-01-10 04:18:14 +02:00
Merge branch 'master' into Issue#259-RedirectToHTTPS
This commit is contained in:
commit
8d1a4c527f
@ -8,30 +8,34 @@ import (
|
||||
)
|
||||
|
||||
type ValidatorTest struct {
|
||||
authEmailFile *os.File
|
||||
done chan bool
|
||||
updateSeen bool
|
||||
authEmailFileName string
|
||||
done chan bool
|
||||
updateSeen bool
|
||||
}
|
||||
|
||||
func NewValidatorTest(t *testing.T) *ValidatorTest {
|
||||
vt := &ValidatorTest{}
|
||||
var err error
|
||||
vt.authEmailFile, err = ioutil.TempFile("", "test_auth_emails_")
|
||||
f, err := ioutil.TempFile("", "test_auth_emails_")
|
||||
if err != nil {
|
||||
t.Fatal("failed to create temp file: " + err.Error())
|
||||
t.Fatalf("failed to create temp file: %v", err)
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
t.Fatalf("failed to close temp file: %v", err)
|
||||
}
|
||||
vt.authEmailFileName = f.Name()
|
||||
vt.done = make(chan bool, 1)
|
||||
return vt
|
||||
}
|
||||
|
||||
func (vt *ValidatorTest) TearDown() {
|
||||
vt.done <- true
|
||||
os.Remove(vt.authEmailFile.Name())
|
||||
os.Remove(vt.authEmailFileName)
|
||||
}
|
||||
|
||||
func (vt *ValidatorTest) NewValidator(domains []string,
|
||||
updated chan<- bool) func(string) bool {
|
||||
return newValidatorImpl(domains, vt.authEmailFile.Name(),
|
||||
return newValidatorImpl(domains, vt.authEmailFileName,
|
||||
vt.done, func() {
|
||||
if vt.updateSeen == false {
|
||||
updated <- true
|
||||
@ -40,13 +44,18 @@ func (vt *ValidatorTest) NewValidator(domains []string,
|
||||
})
|
||||
}
|
||||
|
||||
// This will close vt.authEmailFile.
|
||||
func (vt *ValidatorTest) WriteEmails(t *testing.T, emails []string) {
|
||||
defer vt.authEmailFile.Close()
|
||||
vt.authEmailFile.WriteString(strings.Join(emails, "\n"))
|
||||
if err := vt.authEmailFile.Close(); err != nil {
|
||||
t.Fatal("failed to close temp file " +
|
||||
vt.authEmailFile.Name() + ": " + err.Error())
|
||||
f, err := os.OpenFile(vt.authEmailFileName, os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open auth email file: %v", err)
|
||||
}
|
||||
|
||||
if _, err := f.WriteString(strings.Join(emails, "\n")); err != nil {
|
||||
t.Fatalf("failed to write emails to auth email file: %v", err)
|
||||
}
|
||||
|
||||
if err := f.Close(); err != nil {
|
||||
t.Fatalf("failed to close auth email file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -160,3 +169,43 @@ func TestValidatorIgnoreSpacesInAuthEmails(t *testing.T) {
|
||||
t.Error("email should validate")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorOverwriteEmailListDirectly(t *testing.T) {
|
||||
vt := NewValidatorTest(t)
|
||||
defer vt.TearDown()
|
||||
|
||||
vt.WriteEmails(t, []string{
|
||||
"xyzzy@example.com",
|
||||
"plugh@example.com",
|
||||
})
|
||||
domains := []string(nil)
|
||||
updated := make(chan bool)
|
||||
validator := vt.NewValidator(domains, updated)
|
||||
|
||||
if !validator("xyzzy@example.com") {
|
||||
t.Error("first email in list should validate")
|
||||
}
|
||||
if !validator("plugh@example.com") {
|
||||
t.Error("second email in list should validate")
|
||||
}
|
||||
if validator("xyzzy.plugh@example.com") {
|
||||
t.Error("email not in list that matches no domains " +
|
||||
"should not validate")
|
||||
}
|
||||
|
||||
vt.WriteEmails(t, []string{
|
||||
"xyzzy.plugh@example.com",
|
||||
"plugh@example.com",
|
||||
})
|
||||
<-updated
|
||||
|
||||
if validator("xyzzy@example.com") {
|
||||
t.Error("email removed from list should not validate")
|
||||
}
|
||||
if !validator("plugh@example.com") {
|
||||
t.Error("email retained in list should validate")
|
||||
}
|
||||
if !validator("xyzzy.plugh@example.com") {
|
||||
t.Error("email added to list should validate")
|
||||
}
|
||||
}
|
||||
|
@ -1,48 +0,0 @@
|
||||
// +build go1.3,!plan9,!solaris,!windows
|
||||
|
||||
// Turns out you can't copy over an existing file on Windows.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func (vt *ValidatorTest) UpdateEmailFileViaCopyingOver(
|
||||
t *testing.T, emails []string) {
|
||||
origFile := vt.authEmailFile
|
||||
var err error
|
||||
vt.authEmailFile, err = ioutil.TempFile("", "test_auth_emails_")
|
||||
if err != nil {
|
||||
t.Fatal("failed to create temp file for copy: " + err.Error())
|
||||
}
|
||||
vt.WriteEmails(t, emails)
|
||||
err = os.Rename(vt.authEmailFile.Name(), origFile.Name())
|
||||
if err != nil {
|
||||
t.Fatal("failed to copy over temp file: " + err.Error())
|
||||
}
|
||||
vt.authEmailFile = origFile
|
||||
}
|
||||
|
||||
func TestValidatorOverwriteEmailListViaCopyingOver(t *testing.T) {
|
||||
vt := NewValidatorTest(t)
|
||||
defer vt.TearDown()
|
||||
|
||||
vt.WriteEmails(t, []string{"xyzzy@example.com"})
|
||||
domains := []string(nil)
|
||||
updated := make(chan bool)
|
||||
validator := vt.NewValidator(domains, updated)
|
||||
|
||||
if !validator("xyzzy@example.com") {
|
||||
t.Error("email in list should validate")
|
||||
}
|
||||
|
||||
vt.UpdateEmailFileViaCopyingOver(t, []string{"plugh@example.com"})
|
||||
<-updated
|
||||
|
||||
if validator("xyzzy@example.com") {
|
||||
t.Error("email removed from list should not validate")
|
||||
}
|
||||
}
|
@ -1,102 +0,0 @@
|
||||
// +build go1.3,!plan9,!solaris
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func (vt *ValidatorTest) UpdateEmailFile(t *testing.T, emails []string) {
|
||||
var err error
|
||||
vt.authEmailFile, err = os.OpenFile(
|
||||
vt.authEmailFile.Name(), os.O_WRONLY|os.O_CREATE, 0600)
|
||||
if err != nil {
|
||||
t.Fatal("failed to re-open temp file for updates")
|
||||
}
|
||||
vt.WriteEmails(t, emails)
|
||||
}
|
||||
|
||||
func (vt *ValidatorTest) UpdateEmailFileViaRenameAndReplace(
|
||||
t *testing.T, emails []string) {
|
||||
origFile := vt.authEmailFile
|
||||
var err error
|
||||
vt.authEmailFile, err = ioutil.TempFile("", "test_auth_emails_")
|
||||
if err != nil {
|
||||
t.Fatal("failed to create temp file for rename and replace: " +
|
||||
err.Error())
|
||||
}
|
||||
vt.WriteEmails(t, emails)
|
||||
|
||||
movedName := origFile.Name() + "-moved"
|
||||
err = os.Rename(origFile.Name(), movedName)
|
||||
err = os.Rename(vt.authEmailFile.Name(), origFile.Name())
|
||||
if err != nil {
|
||||
t.Fatal("failed to rename and replace temp file: " +
|
||||
err.Error())
|
||||
}
|
||||
vt.authEmailFile = origFile
|
||||
os.Remove(movedName)
|
||||
}
|
||||
|
||||
func TestValidatorOverwriteEmailListDirectly(t *testing.T) {
|
||||
vt := NewValidatorTest(t)
|
||||
defer vt.TearDown()
|
||||
|
||||
vt.WriteEmails(t, []string{
|
||||
"xyzzy@example.com",
|
||||
"plugh@example.com",
|
||||
})
|
||||
domains := []string(nil)
|
||||
updated := make(chan bool)
|
||||
validator := vt.NewValidator(domains, updated)
|
||||
|
||||
if !validator("xyzzy@example.com") {
|
||||
t.Error("first email in list should validate")
|
||||
}
|
||||
if !validator("plugh@example.com") {
|
||||
t.Error("second email in list should validate")
|
||||
}
|
||||
if validator("xyzzy.plugh@example.com") {
|
||||
t.Error("email not in list that matches no domains " +
|
||||
"should not validate")
|
||||
}
|
||||
|
||||
vt.UpdateEmailFile(t, []string{
|
||||
"xyzzy.plugh@example.com",
|
||||
"plugh@example.com",
|
||||
})
|
||||
<-updated
|
||||
|
||||
if validator("xyzzy@example.com") {
|
||||
t.Error("email removed from list should not validate")
|
||||
}
|
||||
if !validator("plugh@example.com") {
|
||||
t.Error("email retained in list should validate")
|
||||
}
|
||||
if !validator("xyzzy.plugh@example.com") {
|
||||
t.Error("email added to list should validate")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorOverwriteEmailListViaRenameAndReplace(t *testing.T) {
|
||||
vt := NewValidatorTest(t)
|
||||
defer vt.TearDown()
|
||||
|
||||
vt.WriteEmails(t, []string{"xyzzy@example.com"})
|
||||
domains := []string(nil)
|
||||
updated := make(chan bool, 1)
|
||||
validator := vt.NewValidator(domains, updated)
|
||||
|
||||
if !validator("xyzzy@example.com") {
|
||||
t.Error("email in list should validate")
|
||||
}
|
||||
|
||||
vt.UpdateEmailFileViaRenameAndReplace(t, []string{"plugh@example.com"})
|
||||
<-updated
|
||||
|
||||
if validator("xyzzy@example.com") {
|
||||
t.Error("email removed from list should not validate")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user