-
Notifications
You must be signed in to change notification settings - Fork 8
/
group_test.go
88 lines (84 loc) · 4.12 KB
/
group_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
// Copyright 2017 NDP Systèmes. All Rights Reserved.
// See LICENSE file for full licensing details.
package base
import (
"testing"
"github.com/hexya-erp/hexya/src/models"
"github.com/hexya-erp/hexya/src/models/security"
"github.com/hexya-erp/pool/h"
"github.com/hexya-erp/pool/m"
"github.com/hexya-erp/pool/q"
. "github.com/smartystreets/goconvey/convey"
)
func TestGroupLoading(t *testing.T) {
models.SimulateInNewEnvironment(security.SuperUserID, func(env models.Environment) {
var (
adminUser, user m.UserSet
adminGrp, someGroup, everyoneGroup m.GroupSet
)
Convey("Testing Group Loading", t, func() {
h.Group().NewSet(env).ReloadGroups()
groups := h.Group().NewSet(env).SearchAll()
So(groups.Len(), ShouldEqual, len(security.Registry.AllGroups()))
adminUser = h.User().Search(env, q.User().ID().Equals(security.SuperUserID))
adminGrp = h.Group().Search(env, q.Group().GroupID().Equals(security.GroupAdminID))
everyoneGroup = h.Group().Search(env, q.Group().GroupID().Equals(security.GroupEveryoneID))
So(adminUser.Groups().Ids(), ShouldHaveLength, 2)
So(adminUser.Groups().Ids(), ShouldContain, adminGrp.ID())
So(adminUser.Groups().Ids(), ShouldContain, everyoneGroup.ID())
})
Convey("Testing Group ReLoading with a new group", t, func() {
security.Registry.NewGroup("some_group", "Some Group")
h.Group().NewSet(env).ReloadGroups()
groups := h.Group().NewSet(env).SearchAll()
So(groups.Len(), ShouldEqual, len(security.Registry.AllGroups()))
})
Convey("Creating a new user with a new group", t, func() {
adminUser = h.User().Search(env, q.User().ID().Equals(security.SuperUserID))
adminGrp = h.Group().Search(env, q.Group().GroupID().Equals(security.GroupAdminID))
everyoneGroup = h.Group().Search(env, q.Group().GroupID().Equals(security.GroupEveryoneID))
someGroup = h.Group().Search(env, q.Group().GroupID().Equals("some_group"))
user = h.User().Create(env, h.User().NewData().
SetName("Test User").
SetLogin("test_user").
SetGroups(someGroup))
So(user.Groups().Ids(), ShouldHaveLength, 1)
So(user.Groups().Ids(), ShouldContain, someGroup.ID())
So(adminUser.Groups().Ids(), ShouldHaveLength, 2)
So(adminUser.Groups().Ids(), ShouldContain, adminGrp.ID())
So(adminUser.Groups().Ids(), ShouldContain, everyoneGroup.ID())
})
Convey("Giving the new user admin rights", t, func() {
groups := someGroup.Union(adminGrp)
user.SetGroups(groups)
h.Group().NewSet(env).ReloadGroups()
adminGrp = h.Group().Search(env, q.Group().GroupID().Equals(security.GroupAdminID))
everyoneGroup = h.Group().Search(env, q.Group().GroupID().Equals(security.GroupEveryoneID))
someGroup = h.Group().Search(env, q.Group().GroupID().Equals("some_group"))
So(user.Groups().Ids(), ShouldHaveLength, 3)
So(user.Groups().Ids(), ShouldContain, someGroup.ID())
So(user.Groups().Ids(), ShouldContain, adminGrp.ID())
So(user.Groups().Ids(), ShouldContain, everyoneGroup.ID())
So(adminUser.Groups().Ids(), ShouldHaveLength, 2)
So(adminUser.Groups().Ids(), ShouldContain, adminGrp.ID())
So(adminUser.Groups().Ids(), ShouldContain, everyoneGroup.ID())
})
Convey("Removing rights and checking that after reload, we get admin right", t, func() {
user = h.User().Search(env, q.User().Login().Equals("test_user"))
user.SetGroups(h.Group().NewSet(env))
adminUser = h.User().Search(env, q.User().ID().Equals(security.SuperUserID))
adminUser.SetGroups(h.Group().NewSet(env))
So(user.Groups().Ids(), ShouldBeEmpty)
So(adminUser.Groups().Ids(), ShouldBeEmpty)
h.Group().NewSet(env).ReloadGroups()
// We need to reload admin and everyone groups because they changed id
adminGrp = h.Group().Search(env, q.Group().GroupID().Equals(security.GroupAdminID))
everyoneGroup = h.Group().Search(env, q.Group().GroupID().Equals(security.GroupEveryoneID))
So(user.Groups().Ids(), ShouldHaveLength, 1)
So(user.Groups().Ids(), ShouldContain, everyoneGroup.ID())
So(adminUser.Groups().Ids(), ShouldHaveLength, 2)
So(adminUser.Groups().Ids(), ShouldContain, adminGrp.ID())
So(adminUser.Groups().Ids(), ShouldContain, everyoneGroup.ID())
})
})
}