@@ -4,126 +4,102 @@ import (
4
4
"net/http"
5
5
"testing"
6
6
7
- "github.com/franela/goblin"
8
7
"github.com/go-kit/log"
9
- . "github.com/onsi/gomega"
10
8
"github.com/spacelift-io/spcontext"
9
+ "github.com/stretchr/testify/assert"
10
+ "github.com/stretchr/testify/require"
11
11
12
12
"github.com/spacelift-io/vcs-agent/privatevcs/validation/blocklist"
13
13
)
14
14
15
- func TestList (t * testing.T ) {
16
- g := goblin .Goblin (t )
17
- RegisterFailHandler (func (m string , _ ... int ) { g .Fail (m ) })
18
-
19
- g .Describe ("List" , func () {
20
- var sut * blocklist.List
21
-
22
- g .Describe ("Load" , func () {
23
- var err error
24
- var path string
25
-
26
- g .JustBeforeEach (func () { sut , err = blocklist .Load (path ) })
27
-
28
- g .Describe ("with an invalid path" , func () {
29
- g .BeforeEach (func () { path = "fixtures/not.there" })
30
-
31
- g .It ("should return an error" , func () {
32
- Expect (sut ).To (BeNil ())
33
- Expect (err ).To (MatchError (`couldn't read the blocklist file "fixtures/not.there": open fixtures/not.there: no such file or directory` ))
34
- })
35
- })
36
-
37
- g .Describe ("with a duplicate rule" , func () {
38
- g .BeforeEach (func () { path = "fixtures/duplicate.yaml" })
39
-
40
- g .It ("should return an error" , func () {
41
- Expect (sut ).To (BeNil ())
42
- Expect (err ).To (MatchError (`invalid blocklist file "fixtures/duplicate.yaml": duplicate rule name "Duplicate"` ))
43
- })
44
- })
45
-
46
- g .Describe ("with an invalid rule" , func () {
47
- g .BeforeEach (func () { path = "fixtures/invalid.yaml" })
48
-
49
- g .It ("should return an error" , func () {
50
- Expect (sut ).To (BeNil ())
51
- Expect (err ).To (MatchError ("invalid blocklist file \" fixtures/invalid.yaml\" : invalid rule 0: could not compile rule \" Invalid\" : invalid path matcher: error parsing regexp: missing closing ]: `[`" ))
52
- })
53
- })
54
-
55
- g .Describe ("with a valid blocklist" , func () {
56
- g .BeforeEach (func () { path = "fixtures/valid.yaml" })
57
-
58
- g .It ("should not return an error" , func () {
59
- Expect (err ).To (BeNil ())
60
- })
61
-
62
- g .It ("should have loaded the rule" , func () {
63
- Expect (sut .Rules ).To (HaveLen (2 ))
64
- })
65
- })
66
- })
67
-
68
- g .Describe ("Validate" , func () {
69
- var err error
70
- var ctx * spcontext.Context
71
- var req * http.Request
72
-
73
- g .BeforeEach (func () {
74
- ctx = spcontext .New (log .NewNopLogger ())
75
-
76
- req , err = http .NewRequest ("GET" , "https://example.com/foo" , nil )
77
- if err != nil {
78
- panic (err )
79
- }
80
-
81
- sut = new (blocklist.List )
82
- })
83
-
84
- g .JustBeforeEach (func () { _ , err = sut .Validate (ctx , "" , req ) })
85
-
86
- g .Describe ("with an empty list (default)" , func () {
87
- g .It ("should pass validation" , func () {
88
- Expect (err ).To (BeNil ())
89
- })
90
- })
91
-
92
- g .Describe ("with a non-matching rule" , func () {
93
- g .BeforeEach (func () {
94
- sut .Rules = []* blocklist.Rule {{
95
- Name : "NonMatching" ,
96
- Method : "POST" ,
97
- Path : ".*" ,
98
- }}
99
-
100
- if err := sut .Compile (); err != nil {
101
- panic (err )
102
- }
103
- })
104
-
105
- g .It ("should pass validation" , func () {
106
- Expect (err ).To (BeNil ())
107
- })
108
- })
109
-
110
- g .Describe ("with a matching rule" , func () {
111
- g .BeforeEach (func () {
112
- sut .Rules = []* blocklist.Rule {{
113
- Name : "Matching" ,
114
- Method : "GET" ,
115
- Path : "/foo" ,
116
- }}
117
-
118
- if err := sut .Compile (); err != nil {
119
- panic (err )
120
- }
121
- })
122
-
123
- g .It ("should fail validation" , func () {
124
- Expect (err ).To (MatchError (`request blocked by rule "Matching"` ))
125
- })
126
- })
127
- })
15
+ func TestListLoad (t * testing.T ) {
16
+ t .Run ("with an invalid path" , func (t * testing.T ) {
17
+ path := "fixtures/not.there"
18
+
19
+ sut , err := blocklist .Load (path )
20
+
21
+ assert .Nil (t , sut )
22
+ assert .EqualError (t , err , `couldn't read the blocklist file "fixtures/not.there": open fixtures/not.there: no such file or directory` )
23
+ })
24
+
25
+ t .Run ("with a duplicate rule" , func (t * testing.T ) {
26
+ path := "fixtures/duplicate.yaml"
27
+
28
+ sut , err := blocklist .Load (path )
29
+
30
+ assert .Nil (t , sut )
31
+ assert .EqualError (t , err , `invalid blocklist file "fixtures/duplicate.yaml": duplicate rule name "Duplicate"` )
32
+ })
33
+
34
+ t .Run ("with an invalid rule" , func (t * testing.T ) {
35
+ path := "fixtures/invalid.yaml"
36
+
37
+ sut , err := blocklist .Load (path )
38
+
39
+ assert .Nil (t , sut )
40
+ assert .EqualError (t , err , "invalid blocklist file \" fixtures/invalid.yaml\" : invalid rule 0: could not compile rule \" Invalid\" : invalid path matcher: error parsing regexp: missing closing ]: `[`" )
41
+ })
42
+
43
+ t .Run ("with a valid blocklist" , func (t * testing.T ) {
44
+ path := "fixtures/valid.yaml"
45
+
46
+ sut , err := blocklist .Load (path )
47
+
48
+ assert .NoError (t , err )
49
+ assert .Len (t , sut .Rules , 2 )
128
50
})
129
51
}
52
+
53
+ func TestListValidate (t * testing.T ) {
54
+ t .Run ("with an empty list (default)" , func (t * testing.T ) {
55
+ ctx := spcontext .New (log .NewNopLogger ())
56
+ req , err := http .NewRequest ("GET" , "https://example.com/foo" , nil )
57
+ require .NoError (t , err , "failed to create request" )
58
+
59
+ sut := new (blocklist.List )
60
+
61
+ _ , err = sut .Validate (ctx , "" , req )
62
+
63
+ assert .NoError (t , err )
64
+ })
65
+
66
+ t .Run ("with a non-matching rule" , func (t * testing.T ) {
67
+ ctx := spcontext .New (log .NewNopLogger ())
68
+ req , err := http .NewRequest ("GET" , "https://example.com/foo" , nil )
69
+ require .NoError (t , err , "failed to create request" )
70
+
71
+ sut := new (blocklist.List )
72
+ sut .Rules = []* blocklist.Rule {{
73
+ Name : "NonMatching" ,
74
+ Method : "POST" ,
75
+ Path : ".*" ,
76
+ }}
77
+
78
+ err = sut .Compile ()
79
+ require .NoError (t , err , "failed to compile rules" )
80
+
81
+ _ , err = sut .Validate (ctx , "" , req )
82
+
83
+ assert .NoError (t , err )
84
+ })
85
+
86
+ t .Run ("with a matching rule" , func (t * testing.T ) {
87
+ ctx := spcontext .New (log .NewNopLogger ())
88
+ req , err := http .NewRequest ("GET" , "https://example.com/foo" , nil )
89
+ require .NoError (t , err , "failed to create request" )
90
+
91
+ sut := new (blocklist.List )
92
+ sut .Rules = []* blocklist.Rule {{
93
+ Name : "Matching" ,
94
+ Method : "GET" ,
95
+ Path : "/foo" ,
96
+ }}
97
+
98
+ err = sut .Compile ()
99
+ require .NoError (t , err , "failed to compile rules" )
100
+
101
+ _ , err = sut .Validate (ctx , "" , req )
102
+
103
+ assert .EqualError (t , err , `request blocked by rule "Matching"` )
104
+ })
105
+ }
0 commit comments