-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mux-authz_test.go
84 lines (67 loc) · 3.28 KB
/
mux-authz_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package authz
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
"net/http/httptest"
"testing"
)
func testRequest(t *testing.T, handler http.HandlerFunc, c *CasbinAuthorizer, user string, path string, method string, code int) {
req, err := http.NewRequest(method, path, nil)
if err != nil {
t.Fatal(err)
}
req.SetBasicAuth(user, "111")
r := mux.NewRouter()
r.HandleFunc("/{url:[A-Za-z0-9\\/]+}", handler)
r.Use(c.Middleware)
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
r.ServeHTTP(rr, req)
fmt.Println(req.URL.String())
// Check the status code is what we expect.
if rr.Code != code {
t.Errorf("%s, %s, %s: %d, supposed to be %d", user, path, method, rr.Code, code)
}
}
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
// A very simple health check.
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
}
func TestHealthCheckHandler(t *testing.T) {
c := &CasbinAuthorizer{}
_ = c.Load("authz_model.conf", "authz_policy.csv")
handler := http.HandlerFunc(HealthCheckHandler)
testRequest(t, handler, c, "alice", "/dataset1/resource1", "GET", 200)
testRequest(t, handler, c, "alice", "/dataset1/resource1", "POST", 200)
testRequest(t, handler, c, "alice", "/dataset1/resource2", "GET", 200)
testRequest(t, handler, c, "alice", "/dataset1/resource2", "POST", 403)
testRequest(t, handler, c, "bob", "/dataset2/resource1", "GET", 200)
testRequest(t, handler, c, "bob", "/dataset2/resource1", "POST", 200)
testRequest(t, handler, c, "bob", "/dataset2/resource1", "DELETE", 200)
testRequest(t, handler, c, "bob", "/dataset2/resource2", "GET", 200)
testRequest(t, handler, c, "bob", "/dataset2/resource2", "POST", 403)
testRequest(t, handler, c, "bob", "/dataset2/resource2", "DELETE", 403)
testRequest(t, handler, c, "bob", "/dataset2/folder1/item1", "GET", 403)
testRequest(t, handler, c, "bob", "/dataset2/folder1/item1", "POST", 200)
testRequest(t, handler, c, "bob", "/dataset2/folder1/item1", "DELETE", 403)
testRequest(t, handler, c, "bob", "/dataset2/folder1/item2", "GET", 403)
testRequest(t, handler, c, "bob", "/dataset2/folder1/item2", "POST", 200)
testRequest(t, handler, c, "bob", "/dataset2/folder1/item2", "DELETE", 403)
// cathy can access all /dataset1/* resources via all methods because it has the dataset1_admin role.
testRequest(t, handler, c, "cathy", "/dataset1/item", "GET", 200)
testRequest(t, handler, c, "cathy", "/dataset1/item", "POST", 200)
testRequest(t, handler, c, "cathy", "/dataset1/item", "DELETE", 200)
testRequest(t, handler, c, "cathy", "/dataset2/item", "GET", 403)
testRequest(t, handler, c, "cathy", "/dataset2/item", "POST", 403)
testRequest(t, handler, c, "cathy", "/dataset2/item", "DELETE", 403)
// delete all roles on user cathy, so cathy cannot access any resources now.
c.Enforcer.DeleteRolesForUser("cathy")
testRequest(t, handler, c, "cathy", "/dataset1/item", "GET", 403)
testRequest(t, handler, c, "cathy", "/dataset1/item", "POST", 403)
testRequest(t, handler, c, "cathy", "/dataset1/item", "DELETE", 403)
testRequest(t, handler, c, "cathy", "/dataset2/item", "GET", 403)
testRequest(t, handler, c, "cathy", "/dataset2/item", "POST", 403)
testRequest(t, handler, c, "cathy", "/dataset2/item", "DELETE", 403)
}