9 . "github.com/onsi/gomega"
12type ValidatorTest struct {
13 authEmailFileName string
18func NewValidatorTest(t *testing.T) *ValidatorTest {
19 vt := &ValidatorTest{}
21 f, err := ioutil.TempFile("", "test_auth_emails_")
23 t.Fatalf("failed to create temp file: %v", err)
25 if err := f.Close(); err != nil {
26 t.Fatalf("failed to close temp file: %v", err)
28 vt.authEmailFileName = f.Name()
29 vt.done = make(chan bool, 1)
33func (vt *ValidatorTest) TearDown() {
35 os.Remove(vt.authEmailFileName)
38func (vt *ValidatorTest) NewValidator(domains []string,
39 updated chan<- bool) func(string) bool {
40 return newValidatorImpl(domains, vt.authEmailFileName,
42 if vt.updateSeen == false {
49func (vt *ValidatorTest) WriteEmails(t *testing.T, emails []string) {
50 f, err := os.OpenFile(vt.authEmailFileName, os.O_WRONLY, 0600)
52 t.Fatalf("failed to open auth email file: %v", err)
55 if _, err := f.WriteString(strings.Join(emails, "\n")); err != nil {
56 t.Fatalf("failed to write emails to auth email file: %v", err)
59 if err := f.Close(); err != nil {
60 t.Fatalf("failed to close auth email file: %v", err)
64func TestValidatorOverwriteEmailListDirectly(t *testing.T) {
65 testCasesPreUpdate := []struct {
71 name: "FirstEmailInList",
72 email: "xyzzy@example.com",
76 name: "SecondEmailInList",
77 email: "plugh@example.com",
81 name: "EmailNotInListThatMatchesNoDomains",
82 email: "xyzzy.plugh@example.com",
86 testCasesPostUpdate := []struct {
92 name: "email removed from list",
93 email: "xyzzy@example.com",
97 name: "email retained in list",
98 email: "plugh@example.com",
102 name: "email added to list",
103 email: "xyzzy.plugh@example.com",
108 vt := NewValidatorTest(t)
111 vt.WriteEmails(t, []string{
115 updated := make(chan bool)
116 validator := vt.NewValidator([]string(nil), updated)
118 for _, tc := range testCasesPreUpdate {
119 t.Run(tc.name, func(t *testing.T) {
121 authorized := validator(tc.email)
122 g.Expect(authorized).To(Equal(tc.expectedAuthZ))
126 vt.WriteEmails(t, []string{
127 "xyzzy.plugh@example.com",
132 for _, tc := range testCasesPostUpdate {
133 t.Run(tc.name, func(t *testing.T) {
135 authorized := validator(tc.email)
136 g.Expect(authorized).To(Equal(tc.expectedAuthZ))
141func TestValidatorCases(t *testing.T) {
142 testCases := []struct {
144 allowedEmails []string
145 allowedDomains []string
150 name: "EmailNotInCorrect1stSubDomainsNotInEmails",
151 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
152 allowedDomains: []string{".example0.com", ".example1.com"},
153 email: "foo.bar@example0.com",
154 expectedAuthZ: false,
157 name: "EmailInFirstDomain",
158 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
159 allowedDomains: []string{".example0.com", ".example1.com"},
160 email: "foo@bar.example0.com",
164 name: "EmailNotInCorrect2ndSubDomainsNotInEmails",
165 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
166 allowedDomains: []string{".example0.com", ".example1.com"},
167 email: "baz.quux@example1.com",
168 expectedAuthZ: false,
171 name: "EmailInSecondDomain",
172 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
173 allowedDomains: []string{".example0.com", ".example1.com"},
174 email: "baz@quux.example1.com",
178 name: "EmailInFirstEmailList",
179 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
180 allowedDomains: []string{".example0.com", ".example1.com"},
181 email: "xyzzy@example.com",
185 name: "EmailNotInDomainsNotInEmails",
186 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
187 allowedDomains: []string{".example0.com", ".example1.com"},
188 email: "xyzzy.plugh@example.com",
189 expectedAuthZ: false,
192 name: "EmailInLastEmailList",
193 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
194 allowedDomains: []string{".example0.com", ".example1.com"},
195 email: "plugh@example.com",
199 name: "EmailIn1stSubdomain",
201 allowedDomains: []string{"us.example.com", "de.example.com", "example.com"},
202 email: "xyzzy@us.example.com",
206 name: "EmailIn2ndSubdomain",
208 allowedDomains: []string{"us.example.com", "de.example.com", "example.com"},
209 email: "xyzzy@de.example.com",
213 name: "EmailNotInAnySubdomain",
215 allowedDomains: []string{"us.example.com", "de.example.com", "example.com"},
216 email: "global@au.example.com",
217 expectedAuthZ: false,
220 name: "EmailInLastSubdomain",
222 allowedDomains: []string{"us.example.com", "de.example.com", "example.com"},
223 email: "xyzzy@example.com",
227 name: "EmailDomainNotCompletelyMatch",
229 allowedDomains: []string{".example.com", ".example1.com"},
230 email: "something@fooexample.com",
231 expectedAuthZ: false,
234 name: "HackerExtraDomainPrefix1",
236 allowedDomains: []string{".mycompany.com"},
237 email: "something@evilhackmycompany.com",
238 expectedAuthZ: false,
241 name: "HackerExtraDomainPrefix2",
243 allowedDomains: []string{".mycompany.com"},
244 email: "something@ext.evilhackmycompany.com",
245 expectedAuthZ: false,
248 name: "EmptyDomainAndEmailList",
249 allowedEmails: []string(nil),
250 allowedDomains: []string(nil),
251 email: "foo.bar@example.com",
252 expectedAuthZ: false,
255 name: "EmailMatchWithAllowedEmails",
256 email: "foo.bar@example.com",
257 allowedEmails: []string{"foo.bar@example.com"},
258 allowedDomains: []string{"example.com"},
262 name: "EmailFromSameDomainButNotInList",
263 email: "baz.quux@example.com",
264 allowedEmails: []string{"foo.bar@example.com"},
265 allowedDomains: []string(nil),
266 expectedAuthZ: false,
269 name: "EmailMatchOnDomain",
270 email: "foo.bar@example.com",
271 allowedEmails: []string(nil),
272 allowedDomains: []string{"example.com"},
276 name: "EmailMatchOnDomain2",
277 email: "baz.quux@example.com",
278 allowedEmails: []string(nil),
279 allowedDomains: []string{"example.com"},
283 name: "EmailFromFirstDomainShouldValidate",
284 email: "foo.bar@example0.com",
285 allowedEmails: []string{"Foo.Bar@Example.Com"},
286 allowedDomains: []string{"example0.com", "example1.com"},
290 name: "EmailFromSecondDomainShouldValidate",
291 email: "baz.quux@example1.com",
292 allowedEmails: []string{"Foo.Bar@Example.Com"},
293 allowedDomains: []string{"example0.com", "example1.com"},
297 name: "FirstEmailInListShouldValidate",
298 email: "xyzzy@example.com",
299 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
300 allowedDomains: []string{"example0.com", "example1.com"},
304 name: "SecondEmailInListShouldValidate",
305 email: "plugh@example.com",
306 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
307 allowedDomains: []string{"example0.com", "example1.com"},
311 name: "EmailNotInListThatMatchesNoDomains ",
312 email: "xyzzy.plugh@example.com",
313 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
314 allowedDomains: []string{"example0.com", "example1.com"},
315 expectedAuthZ: false,
318 name: "LoadedEmailAddressesAreNotLowerCased",
319 email: "foo.bar@example.com",
320 allowedEmails: []string{"Foo.Bar@Example.Com"},
321 allowedDomains: []string{"Frobozz.Com"},
325 name: "ValidatedEmailAddressesAreNotLowerCased",
326 email: "Foo.Bar@Example.Com",
327 allowedEmails: []string{"Foo.Bar@Example.Com"},
328 allowedDomains: []string{"Frobozz.Com"},
332 name: "LoadedDomainsAreNotLowerCased",
333 email: "foo.bar@frobozz.com",
334 allowedEmails: []string{"Foo.Bar@Example.Com"},
335 allowedDomains: []string{"Frobozz.Com"},
339 name: "ValidatedDomainsAreNotLowerCased",
340 email: "foo.bar@Frobozz.Com",
341 allowedEmails: []string{"Foo.Bar@Example.Com"},
342 allowedDomains: []string{"Frobozz.Com"},
346 name: "IgnoreSpacesInAuthEmails",
347 email: "foo.bar@example.com",
348 allowedEmails: []string{" foo.bar@example.com "},
349 allowedDomains: []string(nil),
353 name: "IgnorePrefixSpacesInAuthEmails",
354 email: "foo.bar@example.com",
355 allowedEmails: []string{" foo.bar@example.com"},
356 allowedDomains: []string(nil),
360 name: "CheckForEqualityNotSuffix",
361 email: "foo@evilcompany.com",
362 allowedEmails: []string(nil),
363 allowedDomains: []string{".company.com"},
364 expectedAuthZ: false,
367 name: "CheckForEqualityNotSuffix2",
368 email: "foo@evilcompany.com",
369 allowedEmails: []string(nil),
370 allowedDomains: []string{"company.com"},
371 expectedAuthZ: false,
375 for _, tc := range testCases {
376 t.Run(tc.name, func(t *testing.T) {
377 vt := NewValidatorTest(t)
381 vt.WriteEmails(t, tc.allowedEmails)
382 validator := vt.NewValidator(tc.allowedDomains, nil)
383 authorized := validator(tc.email)
384 g.Expect(authorized).To(Equal(tc.expectedAuthZ))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list