-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
authz_test.go
74 lines (62 loc) · 2.23 KB
/
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
package authz
import (
"fmt"
"net/http"
"testing"
"github.com/caddyserver/caddy/v2/caddytest"
)
var tester *caddytest.Tester
func testRequest(t *testing.T, user string, path string, method string, code int) {
req, err := http.NewRequest(method, fmt.Sprintf("http://localhost:9080%s", path), nil)
if err != nil {
t.Fatalf("unable to create request %s", err)
}
req.Header.Set("Authorization", user)
tester.AssertResponse(req, code, "")
}
func initTester(t *testing.T) {
tester = caddytest.NewTester(t)
tester.InitServer(`
{
http_port 9080
https_port 9443
}
localhost:9080 {
route /* {
authz "authz_model.conf" "authz_policy.csv"
respond ""
}
}`, "caddyfile")
}
func TestBasic(t *testing.T) {
initTester(t)
testRequest(t, "alice", "/dataset1/resource1", "GET", 200)
testRequest(t, "alice", "/dataset1/resource1", "POST", 200)
testRequest(t, "alice", "/dataset1/resource2", "GET", 200)
testRequest(t, "alice", "/dataset1/resource2", "POST", 403)
}
func TestPathWildcard(t *testing.T) {
initTester(t)
testRequest(t, "bob", "/dataset2/resource1", "GET", 200)
testRequest(t, "bob", "/dataset2/resource1", "POST", 200)
testRequest(t, "bob", "/dataset2/resource1", "DELETE", 200)
testRequest(t, "bob", "/dataset2/resource2", "GET", 200)
testRequest(t, "bob", "/dataset2/resource2", "POST", 403)
testRequest(t, "bob", "/dataset2/resource2", "DELETE", 403)
testRequest(t, "bob", "/dataset2/folder1/item1", "GET", 403)
testRequest(t, "bob", "/dataset2/folder1/item1", "POST", 200)
testRequest(t, "bob", "/dataset2/folder1/item1", "DELETE", 403)
testRequest(t, "bob", "/dataset2/folder1/item2", "GET", 403)
testRequest(t, "bob", "/dataset2/folder1/item2", "POST", 200)
testRequest(t, "bob", "/dataset2/folder1/item2", "DELETE", 403)
}
func TestRBAC(t *testing.T) {
initTester(t)
// cathy can access all /dataset1/* resources via all methods because it has the dataset1_admin role.
testRequest(t, "cathy", "/dataset1/item", "GET", 200)
testRequest(t, "cathy", "/dataset1/item", "POST", 200)
testRequest(t, "cathy", "/dataset1/item", "DELETE", 200)
testRequest(t, "cathy", "/dataset2/item", "GET", 403)
testRequest(t, "cathy", "/dataset2/item", "POST", 403)
testRequest(t, "cathy", "/dataset2/item", "DELETE", 403)
}