@@ -26,12 +26,11 @@ func testRequest(t *testing.T, h echo.HandlerFunc, user string, path string, met
26
26
err := h (c )
27
27
28
28
if err != nil {
29
- if errObj , ok := err .(* echo.HTTPError ); ok {
29
+ var errObj * echo.HTTPError
30
+ if errors .As (err , & errObj ) {
30
31
if errObj .Code != code {
31
32
t .Errorf ("%s, %s, %s: %d, supposed to be %d" , user , path , method , errObj .Code , code )
32
33
}
33
- } else {
34
- t .Error (err )
35
34
}
36
35
} else {
37
36
if c .Response ().Status != code {
@@ -46,10 +45,10 @@ func TestAuth(t *testing.T) {
46
45
return c .String (http .StatusOK , "test" )
47
46
})
48
47
49
- testRequest (t , h , "alice" , "/dataset1/resource1" , echo .GET , 200 )
50
- testRequest (t , h , "alice" , "/dataset1/resource1" , echo .POST , 200 )
51
- testRequest (t , h , "alice" , "/dataset1/resource2" , echo .GET , 200 )
52
- testRequest (t , h , "alice" , "/dataset1/resource2" , echo .POST , 403 )
48
+ testRequest (t , h , "alice" , "/dataset1/resource1" , echo .GET , http . StatusOK )
49
+ testRequest (t , h , "alice" , "/dataset1/resource1" , echo .POST , http . StatusOK )
50
+ testRequest (t , h , "alice" , "/dataset1/resource2" , echo .GET , http . StatusOK )
51
+ testRequest (t , h , "alice" , "/dataset1/resource2" , echo .POST , http . StatusForbidden )
53
52
}
54
53
55
54
func TestPathWildcard (t * testing.T ) {
@@ -58,19 +57,19 @@ func TestPathWildcard(t *testing.T) {
58
57
return c .String (http .StatusOK , "test" )
59
58
})
60
59
61
- testRequest (t , h , "bob" , "/dataset2/resource1" , " GET" , 200 )
62
- testRequest (t , h , "bob" , "/dataset2/resource1" , " POST" , 200 )
63
- testRequest (t , h , "bob" , "/dataset2/resource1" , " DELETE" , 200 )
64
- testRequest (t , h , "bob" , "/dataset2/resource2" , " GET" , 200 )
65
- testRequest (t , h , "bob" , "/dataset2/resource2" , " POST" , 403 )
66
- testRequest (t , h , "bob" , "/dataset2/resource2" , " DELETE" , 403 )
67
-
68
- testRequest (t , h , "bob" , "/dataset2/folder1/item1" , " GET" , 403 )
69
- testRequest (t , h , "bob" , "/dataset2/folder1/item1" , " POST" , 200 )
70
- testRequest (t , h , "bob" , "/dataset2/folder1/item1" , " DELETE" , 403 )
71
- testRequest (t , h , "bob" , "/dataset2/folder1/item2" , " GET" , 403 )
72
- testRequest (t , h , "bob" , "/dataset2/folder1/item2" , " POST" , 200 )
73
- testRequest (t , h , "bob" , "/dataset2/folder1/item2" , " DELETE" , 403 )
60
+ testRequest (t , h , "bob" , "/dataset2/resource1" , echo . GET , http . StatusOK )
61
+ testRequest (t , h , "bob" , "/dataset2/resource1" , echo . POST , http . StatusOK )
62
+ testRequest (t , h , "bob" , "/dataset2/resource1" , echo . DELETE , http . StatusOK )
63
+ testRequest (t , h , "bob" , "/dataset2/resource2" , echo . GET , http . StatusOK )
64
+ testRequest (t , h , "bob" , "/dataset2/resource2" , echo . POST , http . StatusForbidden )
65
+ testRequest (t , h , "bob" , "/dataset2/resource2" , echo . DELETE , http . StatusForbidden )
66
+
67
+ testRequest (t , h , "bob" , "/dataset2/folder1/item1" , echo . GET , http . StatusForbidden )
68
+ testRequest (t , h , "bob" , "/dataset2/folder1/item1" , echo . POST , http . StatusOK )
69
+ testRequest (t , h , "bob" , "/dataset2/folder1/item1" , echo . DELETE , http . StatusForbidden )
70
+ testRequest (t , h , "bob" , "/dataset2/folder1/item2" , echo . GET , http . StatusForbidden )
71
+ testRequest (t , h , "bob" , "/dataset2/folder1/item2" , echo . POST , http . StatusOK )
72
+ testRequest (t , h , "bob" , "/dataset2/folder1/item2" , echo . DELETE , http . StatusForbidden )
74
73
}
75
74
76
75
func TestRBAC (t * testing.T ) {
@@ -80,22 +79,22 @@ func TestRBAC(t *testing.T) {
80
79
})
81
80
82
81
// cathy can access all /dataset1/* resources via all methods because it has the dataset1_admin role.
83
- testRequest (t , h , "cathy" , "/dataset1/item" , " GET" , 200 )
84
- testRequest (t , h , "cathy" , "/dataset1/item" , " POST" , 200 )
85
- testRequest (t , h , "cathy" , "/dataset1/item" , " DELETE" , 200 )
86
- testRequest (t , h , "cathy" , "/dataset2/item" , " GET" , 403 )
87
- testRequest (t , h , "cathy" , "/dataset2/item" , " POST" , 403 )
88
- testRequest (t , h , "cathy" , "/dataset2/item" , " DELETE" , 403 )
82
+ testRequest (t , h , "cathy" , "/dataset1/item" , echo . GET , http . StatusOK )
83
+ testRequest (t , h , "cathy" , "/dataset1/item" , echo . POST , http . StatusOK )
84
+ testRequest (t , h , "cathy" , "/dataset1/item" , echo . DELETE , http . StatusOK )
85
+ testRequest (t , h , "cathy" , "/dataset2/item" , echo . GET , http . StatusForbidden )
86
+ testRequest (t , h , "cathy" , "/dataset2/item" , echo . POST , http . StatusForbidden )
87
+ testRequest (t , h , "cathy" , "/dataset2/item" , echo . DELETE , http . StatusForbidden )
89
88
90
89
// delete all roles on user cathy, so cathy cannot access any resources now.
91
90
ce .DeleteRolesForUser ("cathy" )
92
91
93
- testRequest (t , h , "cathy" , "/dataset1/item" , " GET" , 403 )
94
- testRequest (t , h , "cathy" , "/dataset1/item" , " POST" , 403 )
95
- testRequest (t , h , "cathy" , "/dataset1/item" , " DELETE" , 403 )
96
- testRequest (t , h , "cathy" , "/dataset2/item" , " GET" , 403 )
97
- testRequest (t , h , "cathy" , "/dataset2/item" , " POST" , 403 )
98
- testRequest (t , h , "cathy" , "/dataset2/item" , " DELETE" , 403 )
92
+ testRequest (t , h , "cathy" , "/dataset1/item" , echo . GET , http . StatusForbidden )
93
+ testRequest (t , h , "cathy" , "/dataset1/item" , echo . POST , http . StatusForbidden )
94
+ testRequest (t , h , "cathy" , "/dataset1/item" , echo . DELETE , http . StatusForbidden )
95
+ testRequest (t , h , "cathy" , "/dataset2/item" , echo . GET , http . StatusForbidden )
96
+ testRequest (t , h , "cathy" , "/dataset2/item" , echo . POST , http . StatusForbidden )
97
+ testRequest (t , h , "cathy" , "/dataset2/item" , echo . DELETE , http . StatusForbidden )
99
98
}
100
99
101
100
func TestEnforceError (t * testing.T ) {
@@ -104,7 +103,7 @@ func TestEnforceError(t *testing.T) {
104
103
return c .String (http .StatusOK , "test" )
105
104
})
106
105
107
- testRequest (t , h , "cathy" , "/dataset1/item" , " GET" , 500 )
106
+ testRequest (t , h , "cathy" , "/dataset1/item" , echo . GET , http . StatusInternalServerError )
108
107
}
109
108
110
109
func TestCustomUserGetter (t * testing.T ) {
@@ -119,7 +118,7 @@ func TestCustomUserGetter(t *testing.T) {
119
118
h := MiddlewareWithConfig (cnf )(func (c echo.Context ) error {
120
119
return c .String (http .StatusOK , "test" )
121
120
})
122
- testRequest (t , h , "cathy" , "/dataset1/item" , " GET" , 403 )
121
+ testRequest (t , h , "cathy" , "/dataset1/item" , echo . GET , http . StatusForbidden )
123
122
}
124
123
125
124
func TestUserGetterError (t * testing.T ) {
@@ -134,7 +133,7 @@ func TestUserGetterError(t *testing.T) {
134
133
h := MiddlewareWithConfig (cnf )(func (c echo.Context ) error {
135
134
return c .String (http .StatusOK , "test" )
136
135
})
137
- testRequest (t , h , "cathy" , "/dataset1/item" , " GET" , 403 )
136
+ testRequest (t , h , "cathy" , "/dataset1/item" , echo . GET , http . StatusForbidden )
138
137
}
139
138
140
139
func TestCustomEnforceHandler (t * testing.T ) {
@@ -156,7 +155,22 @@ func TestCustomEnforceHandler(t *testing.T) {
156
155
h := MiddlewareWithConfig (cnf )(func (c echo.Context ) error {
157
156
return c .String (http .StatusOK , "test" )
158
157
})
159
- testRequest (t , h , "bob" , "/dataset2/resource1" , "GET" , http .StatusOK )
160
- testRequest (t , h , "bob" , "/user/alice" , "PATCH" , http .StatusForbidden )
161
- testRequest (t , h , "bob" , "/user/bob" , "PATCH" , http .StatusOK )
158
+ testRequest (t , h , "bob" , "/dataset2/resource1" , echo .GET , http .StatusOK )
159
+ testRequest (t , h , "bob" , "/user/alice" , echo .PATCH , http .StatusForbidden )
160
+ testRequest (t , h , "bob" , "/user/bob" , echo .PATCH , http .StatusOK )
161
+ }
162
+
163
+ func TestCustomSkipper (t * testing.T ) {
164
+ ce , _ := casbin .NewEnforcer ("auth_model.conf" , "auth_policy.csv" )
165
+ cnf := Config {
166
+ Skipper : func (c echo.Context ) bool {
167
+ return c .Request ().URL .Path == "/dataset1/resource1"
168
+ },
169
+ Enforcer : ce ,
170
+ }
171
+ h := MiddlewareWithConfig (cnf )(func (c echo.Context ) error {
172
+ return c .String (http .StatusOK , "test" )
173
+ })
174
+ testRequest (t , h , "alice" , "/dataset1/resource1" , echo .GET , http .StatusOK )
175
+ testRequest (t , h , "alice" , "/dataset1/resource2" , echo .POST , http .StatusForbidden )
162
176
}
0 commit comments