-
Notifications
You must be signed in to change notification settings - Fork 2
/
acl_test.go
84 lines (70 loc) · 2.52 KB
/
acl_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
package acl
import (
"testing"
"github.com/stretchr/testify/assert"
)
const (
rightProfileEdit = "profile.edit"
rightNewsList = "news.list"
rightNewsCreate = "news.create"
rightNewsEdit = "news.edit"
rightNewsDelete = "news.delete"
)
func createDummyExaminer(returnValue bool) ExaminerFunc {
return func(payload interface{}) bool {
return returnValue
}
}
func TestEverything(t *testing.T) {
a := assert.New(t)
user := NewRole("user").Grant(rightProfileEdit)
editor := NewRole("editor").AcquireFrom(user).Grant(rightNewsList, rightNewsCreate, rightNewsEdit)
admin := NewRole("admin").AcquireFrom(editor).Grant(rightNewsDelete)
// register the roles defined above
manager, err := NewManager().Register(user, editor, admin)
a.Nil(err)
a.Equal(user, manager.Get(user.Id))
a.Equal(editor, manager.Get(editor.Id))
a.Equal(admin, manager.Get(admin.Id))
// user: granted rights
a.True(user.Has(rightProfileEdit))
a.True(user.HasAllOf(rightProfileEdit))
a.False(user.Has(rightNewsList))
a.False(user.Has(rightNewsCreate))
a.False(user.Has(rightNewsEdit))
a.False(user.Has(rightNewsDelete))
// editor: granted rights
a.True(editor.HasAllOf(rightProfileEdit, rightNewsList, rightNewsCreate, rightNewsEdit))
a.True(editor.Has(rightProfileEdit))
a.True(editor.Has(rightNewsList))
a.True(editor.Has(rightNewsCreate))
a.True(editor.Has(rightNewsEdit))
a.False(editor.Has(rightNewsDelete))
// admin: granted rights
a.True(admin.HasAllOf(rightProfileEdit, rightNewsList, rightNewsCreate, rightNewsEdit, rightNewsDelete))
a.True(admin.Has(rightProfileEdit))
a.True(admin.Has(rightNewsList))
a.True(admin.Has(rightNewsCreate))
a.True(admin.Has(rightNewsEdit))
a.True(admin.Has(rightNewsDelete))
a.False(admin.HasAllOf(rightProfileEdit, rightNewsList, rightNewsCreate, rightNewsEdit, rightNewsDelete, "test"))
// finally, test the examiner and the result set
user.SetExaminer(createDummyExaminer(true))
admin.SetExaminer(createDummyExaminer(true))
rs := manager.Examine(1337)
a.False(rs.HasRole("guest"))
a.True(rs.HasRole("user"))
a.True(rs.HasRole("admin"))
}
func TestWorkWithRevokedRights(t *testing.T) {
guest := NewRole("guest").Grant("register")
user := NewRole("user").AcquireFrom(guest).Grant("view.profile").Revoke("register")
admin := NewRole("admin").AcquireFrom(user).Grant("user.delete")
a := assert.New(t)
a.True(guest.Has("register"))
a.True(user.Has("view.profile"))
a.False(user.Has("register"))
a.True(admin.Has("user.delete"))
a.True(admin.Has("view.profile"))
a.False(admin.Has("register"))
}