@@ -1,149 +1,124 @@
11package validation
22
33import (
4− "testing"
5−
64 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
7− "github.com/stretchr/testify/assert"
5+ . "github.com/onsi/ginkgo"
6+ . "github.com/onsi/ginkgo/extensions/table"
7+ . "github.com/onsi/gomega"
88)
99
10−func Test_validateAllowlists(t *testing.T) {
11− opts := &options.Options{
12− SkipAuthRoutes: []string{
13− "POST=/foo/bar",
14− "PUT=^/foo/bar$",
15− },
16− SkipAuthRegex: []string{"/foo/baz"},
17− TrustedIPs: []string{
18− "10.32.0.1/32",
19− "43.36.201.0/24",
20− },
10+var _ = Describe("Allowlist", func() {
11+ type validateRoutesTableInput struct {
12+ routes []string
13+ errStrings []string
14+ }
15+
16+ type validateRegexesTableInput struct {
17+ regexes []string
18+ errStrings []string
19+ }
20+
21+ type validateTrustedIPsTableInput struct {
22+ trustedIPs []string
23+ errStrings []string
2124 }
22− assert.Equal(t, []string{}, validateAllowlists(opts))
23−}
2425
25−func Test_validateRoutes(t *testing.T) {
26− testCases := map[string]struct {
27− Regexes []string
28− Expected []string
29− }{
30− "Valid regex routes": {
31− Regexes: []string{
26+ DescribeTable("validateRoutes",
27+ func(r *validateRoutesTableInput) {
28+ opts := &options.Options{
29+ SkipAuthRoutes: r.routes,
30+ }
31+ Expect(validateRoutes(opts)).To(ConsistOf(r.errStrings))
32+ },
33+ Entry("Valid regex routes", &validateRoutesTableInput{
34+ routes: []string{
3235 "/foo",
3336 "POST=/foo/bar",
3437 "PUT=^/foo/bar$",
3538 "DELETE=/crazy/(?:regex)?/[^/]+/stuff$",
3639 },
37− Expected: []string{},
38− },
39− "Bad regexes do not compile": {
40− Regexes: []string{
40+ errStrings: []string{},
41+ }),
42+ Entry("Bad regexes do not compile", &validateRoutesTableInput{
43+ routes: []string{
4144 "POST=/(foo",
4245 "OPTIONS=/foo/bar)",
4346 "GET=^]/foo/bar[$",
4447 "GET=^]/foo/bar[$",
4548 },
46− Expected: []string{
49+ errStrings: []string{
4750 "error compiling regex //(foo/: error parsing regexp: missing closing ): `/(foo`",
4851 "error compiling regex //foo/bar)/: error parsing regexp: unexpected ): `/foo/bar)`",
4952 "error compiling regex /^]/foo/bar[$/: error parsing regexp: missing closing ]: `[$`",
5053 "error compiling regex /^]/foo/bar[$/: error parsing regexp: missing closing ]: `[$`",
5154 },
52− },
53− }
55+ }),
56+ )
5457
55− for testName, tc := range testCases {
56− t.Run(testName, func(t *testing.T) {
58+ DescribeTable("validateRegexes",
59+ func(r *validateRegexesTableInput) {
5760 opts := &options.Options{
58− SkipAuthRoutes: tc.Regexes,
61+ SkipAuthRegex: r.regexes,
5962 }
60− msgs := validateRoutes(opts)
61− assert.Equal(t, tc.Expected, msgs)
62− })
63− }
64−}
65−
66−func Test_validateRegexes(t *testing.T) {
67− testCases := map[string]struct {
68− Regexes []string
69− Expected []string
70− }{
71− "Valid regex routes": {
72− Regexes: []string{
63+ Expect(validateRegexes(opts)).To(ConsistOf(r.errStrings))
64+ },
65+ Entry("Valid regex routes", &validateRegexesTableInput{
66+ regexes: []string{
7367 "/foo",
7468 "/foo/bar",
7569 "^/foo/bar$",
7670 "/crazy/(?:regex)?/[^/]+/stuff$",
7771 },
78− Expected: []string{},
79− },
80− "Bad regexes do not compile": {
81− Regexes: []string{
72+ errStrings: []string{},
73+ }),
74+ Entry("Bad regexes do not compile", &validateRegexesTableInput{
75+ regexes: []string{
8276 "/(foo",
8377 "/foo/bar)",
8478 "^]/foo/bar[$",
8579 "^]/foo/bar[$",
8680 },
87− Expected: []string{
81+ errStrings: []string{
8882 "error compiling regex //(foo/: error parsing regexp: missing closing ): `/(foo`",
8983 "error compiling regex //foo/bar)/: error parsing regexp: unexpected ): `/foo/bar)`",
9084 "error compiling regex /^]/foo/bar[$/: error parsing regexp: missing closing ]: `[$`",
9185 "error compiling regex /^]/foo/bar[$/: error parsing regexp: missing closing ]: `[$`",
9286 },
93− },
94− }
87+ }),
88+ )
9589
96− for testName, tc := range testCases {
97− t.Run(testName, func(t *testing.T) {
90+ DescribeTable("validateTrustedIPs",
91+ func(t *validateTrustedIPsTableInput) {
9892 opts := &options.Options{
99− SkipAuthRegex: tc.Regexes,
93+ TrustedIPs: t.trustedIPs,
10094 }
101− msgs := validateRegexes(opts)
102− assert.Equal(t, tc.Expected, msgs)
103− })
104− }
105−}
106−
107−func Test_validateTrustedIPs(t *testing.T) {
108− testCases := map[string]struct {
109− TrustedIPs []string
110− Expected []string
111− }{
112− "Non-overlapping valid IPs": {
113− TrustedIPs: []string{
95+ Expect(validateTrustedIPs(opts)).To(ConsistOf(t.errStrings))
96+ },
97+ Entry("Non-overlapping valid IPs", &validateTrustedIPsTableInput{
98+ trustedIPs: []string{
11499 "127.0.0.1",
115100 "10.32.0.1/32",
116101 "43.36.201.0/24",
117102 "::1",
118103 "2a12:105:ee7:9234:0:0:0:0/64",
119104 },
120− Expected: []string{},
121− },
122− "Overlapping valid IPs": {
123− TrustedIPs: []string{
105+ errStrings: []string{},
106+ }),
107+ Entry("Overlapping valid IPs", &validateTrustedIPsTableInput{
108+ trustedIPs: []string{
124109 "135.180.78.199",
125110 "135.180.78.199/32",
126111 "d910:a5a1:16f8:ddf5:e5b9:5cef:a65e:41f4",
127112 "d910:a5a1:16f8:ddf5:e5b9:5cef:a65e:41f4/128",
128113 },
129− Expected: []string{},
130− },
131− "Invalid IPs": {
132− TrustedIPs: []string{"[::1]", "alkwlkbn/32"},
133− Expected: []string{
114+ errStrings: []string{},
115+ }),
116+ Entry("Invalid IPs", &validateTrustedIPsTableInput{
117+ trustedIPs: []string{"[::1]", "alkwlkbn/32"},
118+ errStrings: []string{
134119 "trusted_ips[0] ([::1]) could not be recognized",
135120 "trusted_ips[1] (alkwlkbn/32) could not be recognized",
136121 },
137− },
138− }
139−
140− for testName, tc := range testCases {
141− t.Run(testName, func(t *testing.T) {
142− opts := &options.Options{
143− TrustedIPs: tc.TrustedIPs,
144− }
145− msgs := validateTrustedIPs(opts)
146− assert.Equal(t, tc.Expected, msgs)
147− })
148− }
149−}
122+ }),
123+ )
124+})