-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtestUserManagement.go
369 lines (325 loc) · 9.96 KB
/
testUserManagement.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"fmt"
log "github.com/platinasystems/go-common/logs"
pcc "github.com/platinasystems/pcc-blackbox/lib"
"github.com/platinasystems/pcc-blackbox/models"
"github.com/platinasystems/pcc-models/security"
"testing"
"time"
)
////
// TEST
////
// Generic test for more endpoints
func genericTest(
t *testing.T,
item string,
addItem func(name string, description string) (*pcc.GenericModel, error),
getItem func(id uint64) (*pcc.GenericModel, error),
deleteItem func(id uint64) error,
listItems func() ([]pcc.GenericModel, error)) {
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
var (
items []pcc.GenericModel
added *pcc.GenericModel
get *pcc.GenericModel
err error
)
if addItem != nil {
name := fmt.Sprintf("%s-%d", "bb_test", time.Now().Unix())
log.AuctaLogger.Infof(fmt.Sprintf("adding the [%s] [%s]", item, name))
if added, err = addItem(name, "blackbox test"); err == nil { // add the item
log.AuctaLogger.Infof(fmt.Sprintf("added the [%s] [%v]", item, *added))
if getItem != nil { // get an item and check if it is the same with added
if get, err = getItem(added.GetId()); err == nil {
if get.GetDescription() == added.GetDescription() && get.GetName() == added.GetName() {
log.AuctaLogger.Infof(fmt.Sprintf("the fetched %s [%s] is ok", item, name))
} else {
msg := fmt.Sprintf("the %s %d is different than expected", item, added.GetId())
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
} else {
msg := fmt.Sprintf("error getting the %s with id %d", item, added.GetId())
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
if deleteItem != nil { // delete the added item
if err = deleteItem(added.GetId()); err == nil {
log.AuctaLogger.Infof(fmt.Sprintf("deleted the %s %d %s", item, added.GetId(), added.GetName()))
} else {
msg := fmt.Sprintf("error getting the %s with id %d", item, added.GetId())
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
// get all items
if listItems != nil {
if items, err = listItems(); err == nil {
log.AuctaLogger.Info(fmt.Sprintf("found %d %s", len(items), item))
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
}
// add, get and delete an entity. get all entities
func testUMEntity(t *testing.T) {
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
genericTest(t,
"entity",
func(name string, description string) (genericModel *pcc.GenericModel, err error) {
return Pcc.AddEntity(name, description)
},
func(id uint64) (genericModel *pcc.GenericModel, err error) {
return Pcc.GetEntity(id)
},
nil,
func() (models []pcc.GenericModel, err error) {
return Pcc.GetEntities()
},
)
}
// add, get and delete a role. get all roles
// test the deletion of a role still used
func testUMRole(t *testing.T) {
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
genericTest(t,
"role",
func(name string, description string) (genericModel *pcc.GenericModel, err error) {
return Pcc.AddRole(name, description)
},
func(id uint64) (genericModel *pcc.GenericModel, err error) {
return Pcc.GetSecurityRole(id)
},
func(id uint64) error {
return Pcc.DeleteRole(id)
}, func() (models []pcc.GenericModel, err error) {
return Pcc.GetSecurityRoles()
},
)
// test the deletion of a role still used
if user, err := addTestUser(); err == nil {
defer Pcc.DelUser(user.UserName)
if errOk := Pcc.DeleteRole(user.Role.Id); errOk == nil {
msg := fmt.Sprintf("the role [%d] has been deleted", user.Role.Id)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
} else {
log.AuctaLogger.Infof(fmt.Sprintf("the system does not allowed to delete the role [%d]. [%s]", user.Role.Id, errOk.Error()))
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
func testUMOperation(t *testing.T) {
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
// Convert operation to Generic Model
convert := func(f func() ([]pcc.Operation, error)) (models []pcc.GenericModel, err error) {
if operations, e := f(); e == nil {
for _, operation := range operations {
models = append(models, operation.GenericModel)
}
} else {
err = e
}
return
}
genericTest(t,
"operation",
nil, nil, nil,
func() (models []pcc.GenericModel, err error) {
return convert(Pcc.GetOperations)
},
)
genericTest(t,
"groupoperation",
nil, nil, nil,
func() (models []pcc.GenericModel, err error) {
return convert(Pcc.GetGroupOperations)
},
)
}
// add a test user
func addTestUser() (user *pcc.User, err error) {
name := fmt.Sprintf("%[email protected]", time.Now().Unix())
u := pcc.User{UserName: name, FirstName: "John", LastName: "Wick"}
var roles []pcc.GenericModel
if roles, err = Pcc.GetSecurityRoles(); err == nil {
u.RoleId = roles[0].Id
} else {
return
}
if user, err = Pcc.AddUser(u); err == nil {
fmt.Println(fmt.Sprintf("added the user [%d:%s] with role [%+v] and tenant [%+v]", user.Id, user.UserName, user.Role, user.Tenant))
}
return
}
// add and delete an user.
// add an existing user and check for the error.
// get all users.
func testUMUser(t *testing.T) {
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
if added, err := addTestUser(); err == nil {
user2 := *added
log.AuctaLogger.Info(fmt.Sprintf("trying add the same user %s", user2.UserName))
if _, errOk := Pcc.AddUser(user2); errOk == nil {
msg := "the duplicate user was added without issues"
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
} else {
log.AuctaLogger.Info(fmt.Sprintf("the system does not allowed to re-add the user [%s]. [%s]", added.UserName, errOk.Error()))
}
if err = Pcc.DelUser(added.UserName); err == nil {
log.AuctaLogger.Info(fmt.Sprintf("deleted the user %d %s", added.Id, added.UserName))
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
if users, err := Pcc.GetUsers(); err == nil {
log.AuctaLogger.Info(fmt.Sprintf("found %d users", len(users)))
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
// add, get and delete a tenant. get all tenants
// try to delete a tenant still used and check for the error
func testUMTenant(t *testing.T) {
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
var err error
name := fmt.Sprintf("%s-%d", "bb_test", time.Now().Unix())
tenant := &(security.Tenant{Name: name, Description: "blackbox test"})
log.AuctaLogger.Info(fmt.Sprintf("adding the tenant %s", name))
if tenant, err = Pcc.AddTenant(*tenant); err == nil {
log.AuctaLogger.Info(fmt.Sprintf("added the tenant %v", *tenant))
if tenant, err = Pcc.GetTenant(tenant.ID); err == nil {
if tenant.Name == name {
log.AuctaLogger.Info(fmt.Sprintf("the fetched tenant %s is ok", name))
} else {
msg := fmt.Sprintf("the tenant %d is different than expected", tenant.ID)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
if err = Pcc.DelTenant(tenant.ID); err == nil {
log.AuctaLogger.Info(fmt.Sprintf("deleted the tenant %d %s", tenant.ID, tenant.Name))
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
// get all tenants
if tenants, err := Pcc.GetTenants(); err == nil {
log.AuctaLogger.Info(fmt.Sprintf("found %d tenants", len(tenants)))
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
// test the deletion of a tenant still used
if user, err := addTestUser(); err == nil {
defer Pcc.DelUser(user.UserName)
if errOk := Pcc.DelTenant(user.Tenant.ID); errOk == nil {
msg := fmt.Sprintf("the tenant has been deleted [%d]", user.TenantId)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
} else {
log.AuctaLogger.Info(fmt.Sprintf("the system does not allowed to delete the tenant [%d]. [%s]", user.TenantId, errOk.Error()))
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
// add, get and delete an user space.
func testUMUserSpace(t *testing.T) {
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
scope := "bb-test"
content := "blackbox test"
if err := Pcc.SetUserSpace("bb-test", content); err == nil {
log.AuctaLogger.Info(fmt.Sprintf("added the user space [%s]", scope))
var get string
if get, err = Pcc.GetUserSpace(scope); err == nil {
if get == content {
log.AuctaLogger.Info(fmt.Sprintf("the fetched user space %s is ok", scope))
} else {
msg := fmt.Sprintf("the user space %s is different than expected %s", scope, get)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
if err = Pcc.DeleteUserSpace(scope); err == nil {
log.AuctaLogger.Info(fmt.Sprintf("deleted the user space %s", scope))
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}