github.com/pusher/oauth2_proxy@v3.2.0+incompatible/validator_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  type ValidatorTest struct {
    11  	authEmailFile *os.File
    12  	done          chan bool
    13  	updateSeen    bool
    14  }
    15  
    16  func 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
    25  }
    26  
    27  func (vt *ValidatorTest) TearDown() {
    28  	vt.done <- true
    29  	os.Remove(vt.authEmailFile.Name())
    30  }
    31  
    32  func (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  		})
    41  }
    42  
    43  // This will close vt.authEmailFile.
    44  func (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  	}
    51  }
    52  
    53  func TestValidatorEmpty(t *testing.T) {
    54  	vt := NewValidatorTest(t)
    55  	defer vt.TearDown()
    56  
    57  	vt.WriteEmails(t, []string(nil))
    58  	domains := []string(nil)
    59  	validator := vt.NewValidator(domains, nil)
    60  
    61  	if validator("foo.bar@example.com") {
    62  		t.Error("nothing should validate when the email and " +
    63  			"domain lists are empty")
    64  	}
    65  }
    66  
    67  func TestValidatorSingleEmail(t *testing.T) {
    68  	vt := NewValidatorTest(t)
    69  	defer vt.TearDown()
    70  
    71  	vt.WriteEmails(t, []string{"foo.bar@example.com"})
    72  	domains := []string(nil)
    73  	validator := vt.NewValidator(domains, nil)
    74  
    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  	}
    82  }
    83  
    84  func TestValidatorSingleDomain(t *testing.T) {
    85  	vt := NewValidatorTest(t)
    86  	defer vt.TearDown()
    87  
    88  	vt.WriteEmails(t, []string(nil))
    89  	domains := []string{"example.com"}
    90  	validator := vt.NewValidator(domains, nil)
    91  
    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  	}
    98  }
    99  
   100  func TestValidatorMultipleEmailsMultipleDomains(t *testing.T) {
   101  	vt := NewValidatorTest(t)
   102  	defer vt.TearDown()
   103  
   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)
   110  
   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  	}
   127  }
   128  
   129  func TestValidatorComparisonsAreCaseInsensitive(t *testing.T) {
   130  	vt := NewValidatorTest(t)
   131  	defer vt.TearDown()
   132  
   133  	vt.WriteEmails(t, []string{"Foo.Bar@Example.Com"})
   134  	domains := []string{"Frobozz.Com"}
   135  	validator := vt.NewValidator(domains, nil)
   136  
   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  	}
   149  }
   150  
   151  func TestValidatorIgnoreSpacesInAuthEmails(t *testing.T) {
   152  	vt := NewValidatorTest(t)
   153  	defer vt.TearDown()
   154  
   155  	vt.WriteEmails(t, []string{"   foo.bar@example.com   "})
   156  	domains := []string(nil)
   157  	validator := vt.NewValidator(domains, nil)
   158  
   159  	if !validator("foo.bar@example.com") {
   160  		t.Error("email should validate")
   161  	}
   162  }