-
Notifications
You must be signed in to change notification settings - Fork 5
/
permission_test.go
87 lines (64 loc) · 1.75 KB
/
permission_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
package restrict
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type permissionSuite struct {
suite.Suite
testPresetName string
testAction string
}
func TestPermissionSuite(t *testing.T) {
suite.Run(t, new(permissionSuite))
}
func (s *permissionSuite) SetupSuite() {
s.testAction = "test-action"
s.testPresetName = "testPreset"
}
func (s *permissionSuite) TestMergePreset() {
// Do not extend conditions
testCondition := new(conditionMock)
testPreset := &Permission{
Action: s.testAction,
Conditions: Conditions{
testCondition,
testCondition,
},
}
testPermission := &Permission{}
assert.NotPanics(s.T(), func() {
testPermission.mergePreset(nil)
})
testPermission.mergePreset(testPreset)
assert.Equal(s.T(), testPreset.Action, testPermission.Action)
assert.Equal(s.T(), "", testPermission.Preset)
assert.Equal(s.T(), 2, len(testPermission.Conditions))
assert.Equal(s.T(), testCondition, testPermission.Conditions[0])
// Extend conditions
testPermission = &Permission{
Conditions: Conditions{
testCondition,
},
}
testPermission.mergePreset(testPreset)
assert.Equal(s.T(), 3, len(testPermission.Conditions))
// Should not override Permission's own action.
customAction := "customAction"
testPermission = &Permission{
Action: customAction,
}
testPermission.mergePreset(testPreset)
assert.Equal(s.T(), customAction, testPermission.Action)
}
func (s *permissionSuite) TestMergePreset_NilPresetConditions() {
testPreset := &Permission{
Action: s.testAction,
}
testPermission := &Permission{}
assert.NotPanics(s.T(), func() {
testPermission.mergePreset(nil)
})
testPermission.mergePreset(testPreset)
assert.Equal(s.T(), 0, len(testPermission.Conditions))
}