-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
85 lines (75 loc) · 1.89 KB
/
Copy pathmain_test.go
File metadata and controls
85 lines (75 loc) · 1.89 KB
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
85
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAuthz(t *testing.T) {
router := setupRouter()
type args struct {
sub, obj, act string
code int
}
var tests = []struct {
name string
args args
}{
// p, alice, /dataset1/*, GET
{
name: "alice GET dataset1/resource1",
args: args{"alice", "/dataset1/resource1", "GET", http.StatusOK},
},
{
name: "alice GET dataset1/resource2",
args: args{"alice", "/dataset1/resource2", "GET", http.StatusOK},
},
{
name: "bob GET dataset1/resource1",
args: args{"bob", "/dataset1/resource1", "GET", http.StatusForbidden},
},
// p, alice, /dataset1/resource1, POST
{
name: "alice POST dataset1/resource1",
args: args{"alice", "/dataset1/resource1", "POST", http.StatusOK},
},
{
name: "bob POST dataset1/resource1",
args: args{"bob", "/dataset1/resource1", "POST", http.StatusForbidden},
},
// p, bob, /dataset2/resource1, *
{
name: "bob GET dataset2/resource1",
args: args{"bob", "/dataset2/resource1", "GET", http.StatusOK},
},
{
name: "bob POST dataset2/resource1",
args: args{"bob", "/dataset2/resource1", "POST", http.StatusOK},
},
{
name: "bob PUT dataset2/resource1",
args: args{"bob", "/dataset2/resource1", "PUT", http.StatusOK},
},
// p, dataset1_admin, /dataset1/*, *
// g, cathy, dataset1_admin
{
name: "cathy GET dataset1/resource1",
args: args{"cathy", "/dataset1/resource1", "GET", http.StatusOK},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
a := tt.args
req, _ := http.NewRequest(a.act, a.obj, nil)
req.SetBasicAuth(a.sub, "")
router.ServeHTTP(w, req)
if w.Code == http.StatusForbidden {
t.Log(a.sub, a.act, a.obj, http.StatusText(w.Code))
} else {
t.Log(w.Body)
}
assert.Equal(t, a.code, w.Code)
})
}
}