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: "EmailNotInCorrect1stSubDomainsNotInEmailsWildcard",
158 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
159 allowedDomains: []string{"*.example0.com", "*.example1.com"},
160 email: "foo.bar@example0.com",
161 expectedAuthZ: false,
164 name: "EmailInFirstDomain",
165 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
166 allowedDomains: []string{".example0.com", ".example1.com"},
167 email: "foo@bar.example0.com",
171 name: "EmailInFirstDomainWildcard",
172 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
173 allowedDomains: []string{"*.example0.com", "*.example1.com"},
174 email: "foo@bar.example0.com",
178 name: "EmailNotInCorrect2ndSubDomainsNotInEmails",
179 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
180 allowedDomains: []string{".example0.com", ".example1.com"},
181 email: "baz.quux@example1.com",
182 expectedAuthZ: false,
185 name: "EmailInSecondDomain",
186 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
187 allowedDomains: []string{".example0.com", ".example1.com"},
188 email: "baz@quux.example1.com",
192 name: "EmailInSecondDomainWildcard",
193 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
194 allowedDomains: []string{"*.example0.com", "*.example1.com"},
195 email: "baz@quux.example1.com",
199 name: "EmailInFirstEmailList",
200 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
201 allowedDomains: []string{".example0.com", ".example1.com"},
202 email: "xyzzy@example.com",
206 name: "EmailInFirstEmailListWildcard",
207 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
208 allowedDomains: []string{"*.example0.com", "*.example1.com"},
209 email: "xyzzy@example.com",
213 name: "EmailNotInDomainsNotInEmails",
214 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
215 allowedDomains: []string{".example0.com", ".example1.com"},
216 email: "xyzzy.plugh@example.com",
217 expectedAuthZ: false,
220 name: "EmailInLastEmailList",
221 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
222 allowedDomains: []string{".example0.com", ".example1.com"},
223 email: "plugh@example.com",
227 name: "EmailIn1stSubdomain",
229 allowedDomains: []string{"us.example.com", "de.example.com", "example.com"},
230 email: "xyzzy@us.example.com",
234 name: "EmailIn2ndSubdomain",
236 allowedDomains: []string{"us.example.com", "de.example.com", "example.com"},
237 email: "xyzzy@de.example.com",
241 name: "EmailNotInAnySubdomain",
243 allowedDomains: []string{"us.example.com", "de.example.com", "example.com"},
244 email: "global@au.example.com",
245 expectedAuthZ: false,
248 name: "EmailInLastSubdomain",
250 allowedDomains: []string{"us.example.com", "de.example.com", "example.com"},
251 email: "xyzzy@example.com",
255 name: "EmailDomainNotCompletelyMatch",
257 allowedDomains: []string{".example.com", ".example1.com"},
258 email: "something@fooexample.com",
259 expectedAuthZ: false,
262 name: "HackerExtraDomainPrefix1",
264 allowedDomains: []string{".mycompany.com"},
265 email: "something@evilhackmycompany.com",
266 expectedAuthZ: false,
269 name: "HackerExtraDomainPrefix2",
271 allowedDomains: []string{".mycompany.com"},
272 email: "something@ext.evilhackmycompany.com",
273 expectedAuthZ: false,
276 name: "EmptyDomainAndEmailList",
277 allowedEmails: []string(nil),
278 allowedDomains: []string(nil),
279 email: "foo.bar@example.com",
280 expectedAuthZ: false,
283 name: "EmailMatchWithAllowedEmails",
284 email: "foo.bar@example.com",
285 allowedEmails: []string{"foo.bar@example.com"},
286 allowedDomains: []string{"example.com"},
290 name: "EmailFromSameDomainButNotInList",
291 email: "baz.quux@example.com",
292 allowedEmails: []string{"foo.bar@example.com"},
293 allowedDomains: []string(nil),
294 expectedAuthZ: false,
297 name: "EmailMatchOnDomain",
298 email: "foo.bar@example.com",
299 allowedEmails: []string(nil),
300 allowedDomains: []string{"example.com"},
304 name: "EmailMatchOnDomain2",
305 email: "baz.quux@example.com",
306 allowedEmails: []string(nil),
307 allowedDomains: []string{"example.com"},
311 name: "EmailFromFirstDomainShouldValidate",
312 email: "foo.bar@example0.com",
313 allowedEmails: []string{"Foo.Bar@Example.Com"},
314 allowedDomains: []string{"example0.com", "example1.com"},
318 name: "EmailFromSecondDomainShouldValidate",
319 email: "baz.quux@example1.com",
320 allowedEmails: []string{"Foo.Bar@Example.Com"},
321 allowedDomains: []string{"example0.com", "example1.com"},
325 name: "FirstEmailInListShouldValidate",
326 email: "xyzzy@example.com",
327 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
328 allowedDomains: []string{"example0.com", "example1.com"},
332 name: "SecondEmailInListShouldValidate",
333 email: "plugh@example.com",
334 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
335 allowedDomains: []string{"example0.com", "example1.com"},
339 name: "EmailNotInListThatMatchesNoDomains ",
340 email: "xyzzy.plugh@example.com",
341 allowedEmails: []string{"xyzzy@example.com", "plugh@example.com"},
342 allowedDomains: []string{"example0.com", "example1.com"},
343 expectedAuthZ: false,
346 name: "LoadedEmailAddressesAreNotLowerCased",
347 email: "foo.bar@example.com",
348 allowedEmails: []string{"Foo.Bar@Example.Com"},
349 allowedDomains: []string{"Frobozz.Com"},
353 name: "ValidatedEmailAddressesAreNotLowerCased",
354 email: "Foo.Bar@Example.Com",
355 allowedEmails: []string{"Foo.Bar@Example.Com"},
356 allowedDomains: []string{"Frobozz.Com"},
360 name: "LoadedDomainsAreNotLowerCased",
361 email: "foo.bar@frobozz.com",
362 allowedEmails: []string{"Foo.Bar@Example.Com"},
363 allowedDomains: []string{"Frobozz.Com"},
367 name: "ValidatedDomainsAreNotLowerCased",
368 email: "foo.bar@Frobozz.Com",
369 allowedEmails: []string{"Foo.Bar@Example.Com"},
370 allowedDomains: []string{"Frobozz.Com"},
374 name: "IgnoreSpacesInAuthEmails",
375 email: "foo.bar@example.com",
376 allowedEmails: []string{" foo.bar@example.com "},
377 allowedDomains: []string(nil),
381 name: "IgnorePrefixSpacesInAuthEmails",
382 email: "foo.bar@example.com",
383 allowedEmails: []string{" foo.bar@example.com"},
384 allowedDomains: []string(nil),
388 name: "CheckForEqualityNotSuffix",
389 email: "foo@evilcompany.com",
390 allowedEmails: []string(nil),
391 allowedDomains: []string{".company.com"},
392 expectedAuthZ: false,
395 name: "CheckForEqualityNotSuffix2",
396 email: "foo@evilcompany.com",
397 allowedEmails: []string(nil),
398 allowedDomains: []string{"company.com"},
399 expectedAuthZ: false,
402 name: "CheckForEqualityNotSuffixWildcard",
403 email: "foo@evilcompany.com",
404 allowedEmails: []string(nil),
405 allowedDomains: []string{"*.company.com"},
406 expectedAuthZ: false,
410 for _, tc := range testCases {
411 t.Run(tc.name, func(t *testing.T) {
412 vt := NewValidatorTest(t)
416 vt.WriteEmails(t, tc.allowedEmails)
417 validator := vt.NewValidator(tc.allowedDomains, nil)
418 authorized := validator(tc.email)
419 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