forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_usermgr_test.go
206 lines (171 loc) · 4.64 KB
/
cluster_usermgr_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package gocb
import (
"testing"
)
func TestUserManagerGroupCrud(t *testing.T) {
if !globalCluster.SupportsFeature(UserGroupFeature) {
t.Skip("Skipping test as groups not supported.")
}
mgr, err := globalCluster.Users()
if err != nil {
t.Fatalf("Expected Groups to not error: %v", err)
}
err = mgr.UpsertGroup(Group{
Name: "test",
Description: "this is a test",
Roles: []Role{
{
Name: "replication_target",
Bucket: globalBucket.Name(),
},
{
Name: "security_admin",
},
},
LDAPGroupReference: "asda=price",
}, nil)
if err != nil {
t.Fatalf("Expected Upsert to not error: %v", err)
}
group, err := mgr.GetGroup("test", nil)
if err != nil {
t.Fatalf("Expected Get to not error: %v", err)
}
group.Description = "this is still a test"
group.Roles = append(group.Roles, Role{Name: "query_system_catalog"})
err = mgr.UpsertGroup(*group, nil)
if err != nil {
t.Fatalf("Expected Upsert to not error: %v", err)
}
group.Name = "test2"
err = mgr.UpsertGroup(*group, nil)
if err != nil {
t.Fatalf("Expected Upsert to not error: %v", err)
}
groups, err := mgr.GetAllGroups(nil)
if err != nil {
t.Fatalf("Expected GetAll to not error: %v", err)
}
if len(groups) < 2 {
t.Fatalf("Expected groups to contain at least 2 groups, was %v", groups)
}
err = mgr.DropGroup("test", nil)
if err != nil {
t.Fatalf("Expected Drop to not error: %v", err)
}
err = mgr.DropGroup("test2", nil)
if err != nil {
t.Fatalf("Expected Drop to not error: %v", err)
}
}
func TestUserManagerCrud(t *testing.T) {
if !globalCluster.SupportsFeature(UserManagerFeature) {
t.Skip("Skipping test as rbac not supported.")
}
mgr, err := globalCluster.Users()
if err != nil {
t.Fatalf("Expected Users to not error: %v", err)
}
err = mgr.UpsertGroup(Group{
Name: "test",
Description: "this is a test",
Roles: []Role{
{
Name: "replication_target",
Bucket: globalBucket.Name(),
},
{
Name: "security_admin",
},
},
}, nil)
if err != nil {
t.Fatalf("Expected UpsertGroup to not error: %v", err)
}
err = mgr.UpsertUser(User{
Username: "barry",
DisplayName: "sheen",
Password: "bangbang!",
Roles: []Role{
{
Name: "bucket_admin",
Bucket: globalBucket.Name(),
},
},
Groups: []string{"test"},
}, nil)
if err != nil {
t.Fatalf("Expected UpsertUser to not error: %v", err)
}
user, err := mgr.GetUser("barry", nil)
if err != nil {
t.Fatalf("Expected GetUser to not error: %v", err)
}
if user.User.Username != "barry" {
t.Fatalf("Expected user Username to be barry but was %s", user.User.Username)
}
if len(user.User.Groups) != 1 {
t.Fatalf("Expected user Groups to be length 1 but was %v", user.User.Groups)
}
if user.User.Groups[0] != "test" {
t.Fatalf("Expected user Groups 0 to be test but was %s", user.User.Groups[0])
}
if len(user.User.Roles) != 1 {
t.Fatalf("Expected user Roles to be length 1 but was %v", user.User.Roles)
}
if user.User.DisplayName != "sheen" {
t.Fatalf("Expected user DisplayName to be sheen but was %s", user.User.DisplayName)
}
if user.Domain != "local" {
t.Fatalf("Expected user Domain to be local but was %s", user.Domain)
}
if len(user.EffectiveRoles) != 3 {
t.Fatalf("Expected user EffectiveRoles to be length 3 but was %v", user.EffectiveRoles)
}
if len(user.EffectiveRolesAndOrigins) != 3 {
t.Fatalf("Expected user EffectiveRolesAndOrigins to be length 3 but was %v", user.EffectiveRolesAndOrigins)
}
user.User.DisplayName = "barries"
err = mgr.UpsertUser(user.User, nil)
if err != nil {
t.Fatalf("Expected UpsertUser to not error: %v", err)
}
users, err := mgr.GetAllUsers(nil)
if err != nil {
t.Fatalf("Expected GetAllUsers to not error: %v", err)
}
if len(users) == 0 {
t.Fatalf("Expected users length to be greater than 0")
}
err = mgr.DropUser("barry", nil)
if err != nil {
t.Fatalf("Expected DropUser to not error: %v", err)
}
err = mgr.DropGroup("test", nil)
if err != nil {
t.Fatalf("Expected DropGroup to not error: %v", err)
}
_, err = mgr.GetUser("barry", nil)
if err == nil {
t.Fatalf("Expected GetUser to error")
}
if !IsUserNotFoundError(err) {
t.Fatalf("Expected error to be user not found but was %v", err)
}
}
func TestUserManagerAvailableRoles(t *testing.T) {
if !globalCluster.SupportsFeature(UserManagerFeature) {
t.Skip("Skipping test as rbac not supported.")
}
mgr, err := globalCluster.Users()
if err != nil {
t.Fatalf("Expected Users to not error: %v", err)
}
roles, err := mgr.AvailableRoles(nil)
if err != nil {
t.Fatalf("Expected AvailableRoles to not error %v", err)
}
if len(roles) == 0 {
t.Fatalf("Expected roles to have entries")
}
}