|
| 1 | +package authz |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/caddyserver/caddy/v2/caddytest" |
| 9 | +) |
| 10 | + |
| 11 | +var tester *caddytest.Tester |
| 12 | + |
| 13 | +func testRequest(t *testing.T, user string, path string, method string, code int) { |
| 14 | + req, err := http.NewRequest(method, fmt.Sprintf("http://localhost:9080%s", path), nil) |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("unable to create request %s", err) |
| 17 | + } |
| 18 | + req.Header.Set("Authorization", user) |
| 19 | + tester.AssertResponse(req, code, "") |
| 20 | +} |
| 21 | + |
| 22 | +func initTester(t *testing.T) { |
| 23 | + tester = caddytest.NewTester(t) |
| 24 | + tester.InitServer(` |
| 25 | + { |
| 26 | + http_port 9080 |
| 27 | + https_port 9443 |
| 28 | + } |
| 29 | + localhost:9080 { |
| 30 | + route /* { |
| 31 | + authz "authz_model.conf" "authz_policy.csv" |
| 32 | + respond "" |
| 33 | + } |
| 34 | + }`, "caddyfile") |
| 35 | +} |
| 36 | + |
| 37 | +func TestBasic(t *testing.T) { |
| 38 | + initTester(t) |
| 39 | + |
| 40 | + testRequest(t, "alice", "/dataset1/resource1", "GET", 200) |
| 41 | + testRequest(t, "alice", "/dataset1/resource1", "POST", 200) |
| 42 | + testRequest(t, "alice", "/dataset1/resource2", "GET", 200) |
| 43 | + testRequest(t, "alice", "/dataset1/resource2", "POST", 403) |
| 44 | +} |
| 45 | + |
| 46 | +func TestPathWildcard(t *testing.T) { |
| 47 | + initTester(t) |
| 48 | + |
| 49 | + testRequest(t, "bob", "/dataset2/resource1", "GET", 200) |
| 50 | + testRequest(t, "bob", "/dataset2/resource1", "POST", 200) |
| 51 | + testRequest(t, "bob", "/dataset2/resource1", "DELETE", 200) |
| 52 | + testRequest(t, "bob", "/dataset2/resource2", "GET", 200) |
| 53 | + testRequest(t, "bob", "/dataset2/resource2", "POST", 403) |
| 54 | + testRequest(t, "bob", "/dataset2/resource2", "DELETE", 403) |
| 55 | + |
| 56 | + testRequest(t, "bob", "/dataset2/folder1/item1", "GET", 403) |
| 57 | + testRequest(t, "bob", "/dataset2/folder1/item1", "POST", 200) |
| 58 | + testRequest(t, "bob", "/dataset2/folder1/item1", "DELETE", 403) |
| 59 | + testRequest(t, "bob", "/dataset2/folder1/item2", "GET", 403) |
| 60 | + testRequest(t, "bob", "/dataset2/folder1/item2", "POST", 200) |
| 61 | + testRequest(t, "bob", "/dataset2/folder1/item2", "DELETE", 403) |
| 62 | +} |
| 63 | + |
| 64 | +func TestRBAC(t *testing.T) { |
| 65 | + initTester(t) |
| 66 | + |
| 67 | + // cathy can access all /dataset1/* resources via all methods because it has the dataset1_admin role. |
| 68 | + testRequest(t, "cathy", "/dataset1/item", "GET", 200) |
| 69 | + testRequest(t, "cathy", "/dataset1/item", "POST", 200) |
| 70 | + testRequest(t, "cathy", "/dataset1/item", "DELETE", 200) |
| 71 | + testRequest(t, "cathy", "/dataset2/item", "GET", 403) |
| 72 | + testRequest(t, "cathy", "/dataset2/item", "POST", 403) |
| 73 | + testRequest(t, "cathy", "/dataset2/item", "DELETE", 403) |
| 74 | +} |
0 commit comments