-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_test.go
179 lines (145 loc) · 4.22 KB
/
lib_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package rbacarango
import (
"context"
"crypto/rand"
"testing"
"time"
"github.com/arangodb/go-driver"
arangohttp "github.com/arangodb/go-driver/http"
"github.com/mikespook/gorbac/v2"
"github.com/oklog/ulid"
)
func databaseForTest(ctx context.Context) (driver.Database, error) {
con, err := arangohttp.NewConnection(arangohttp.ConnectionConfig{
Endpoints: []string{"http://127.0.0.1:8529"},
})
if err != nil {
return nil, err
}
client, err := driver.NewClient(driver.ClientConfig{
Connection: con,
})
if err != nil {
return nil, err
}
dbID := ulid.MustNew(ulid.Now(), rand.Reader)
dbName := "gotest_" + dbID.String()
return client.CreateDatabase(ctx, dbName, nil)
}
func TestSaveLoadRBAC(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
db, err := databaseForTest(ctx)
if err != nil {
t.Fatalf("arango test connection failed: %v", err)
}
err = CreateSchema(ctx, db)
if err != nil {
t.Fatalf("could not create schema: %v", err)
}
fooRole := gorbac.NewStdRole("foo")
fooRole.Assign(gorbac.NewStdPermission("bar"))
fooRole.Assign(gorbac.NewStdPermission("quux"))
gandalfRole := gorbac.NewStdRole("gandalf")
gandalfRole.Assign(gorbac.NewStdPermission("pass"))
gandalfRole.Assign(gorbac.NewStdPermission("quux"))
rbac := gorbac.New()
rbac.Add(fooRole)
rbac.Add(gandalfRole)
err = SaveRBAC(ctx, db, rbac)
if err != nil {
t.Fatalf("could not save rbac: %v", err)
}
loaded, err := LoadRBAC(ctx, db)
if err != nil {
t.Fatalf("could not load rbac: %v", err)
}
if !gorbac.AnyGranted(loaded, []string{"foo"}, gorbac.NewStdPermission("bar"), nil) {
t.Fatalf("foo role doesn't have bar permission")
}
if gorbac.AnyGranted(loaded, []string{"foo"}, gorbac.NewStdPermission("wtf"), nil) {
t.Fatalf("foo role has wtf permission, shouldn't have")
}
if !gorbac.AllGranted(loaded, []string{"foo", "gandalf"}, gorbac.NewStdPermission("quux"), nil) {
t.Fatalf("both foo and gandalf role don't have quux permission")
}
if gorbac.AllGranted(loaded, []string{"foo", "gandalf"}, gorbac.NewStdPermission("pass"), nil) {
t.Fatalf("only gandalf shall have pass permission, but foo has too")
}
}
const expectedCount = 1
func TestSaveTwiceAddedOnce(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
db, err := databaseForTest(ctx)
if err != nil {
t.Fatalf("arango test connection failed: %v", err)
}
err = CreateSchema(ctx, db)
if err != nil {
t.Fatalf("could not create schema: %v", err)
}
fooRole := gorbac.NewStdRole("foo")
fooRole.Assign(gorbac.NewStdPermission("bar"))
rbac := gorbac.New()
rbac.Add(fooRole)
err = SaveRBAC(ctx, db, rbac)
if err != nil {
t.Fatalf("could not save rbac: %v", err)
}
err = SaveRBAC(ctx, db, rbac)
if err != nil {
t.Fatalf("could not save rbac a second time: %v", err)
}
countedCollections := []string{
roleCollectionName,
permissionCollectionName,
rolePermEdgeCollectionName,
}
for _, collectionName := range countedCollections {
collection, err := db.Collection(ctx, collectionName)
if err != nil {
t.Fatalf("could not get collection %s: %v", collectionName, err)
}
count, err := collection.Count(ctx)
if err != nil {
t.Fatalf("could not count %s: %v", collectionName, err)
}
if count != expectedCount {
t.Fatalf("expected %s count %d, got %d", collectionName, expectedCount, count)
}
}
}
func TestLoadRoleWithoutPermissions(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
db, err := databaseForTest(ctx)
if err != nil {
t.Fatalf("arango test connection failed: %v", err)
}
err = CreateSchema(ctx, db)
if err != nil {
t.Fatalf("could not create schema: %v", err)
}
fooRole := gorbac.NewStdRole("quux")
rbac := gorbac.New()
rbac.Add(fooRole)
err = SaveRBAC(ctx, db, rbac)
if err != nil {
t.Fatalf("could not save rbac: %v", err)
}
loaded, err := LoadRBAC(ctx, db)
if err != nil {
t.Fatalf("could not load rbac: %v", err)
}
found := false
err = gorbac.Walk(loaded, func(r gorbac.Role, p []string) error {
if r.ID() == fooRole.ID() {
found = true
}
return nil
})
if !found {
t.Fatalf("Walking RBAC structure did not include %s role", fooRole.ID())
}
}