forked from mikespook/gorbac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permission_test.go
77 lines (72 loc) · 1.98 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
package gorbac
import (
"encoding/json"
"testing"
)
func TestStdPermission(t *testing.T) {
profile1 := NewStdPermission("profile")
profile2 := NewStdPermission("profile")
admin := NewStdPermission("admin")
if !profile1.Match(profile2) {
t.Fatalf("%s should have the permission", profile1.ID())
}
if !profile1.Match(profile1) {
t.Fatalf("%s should have the permission", profile1.ID())
}
if profile1.Match(admin) {
t.Fatalf("%s should not have the permission", profile1.ID())
}
text, err := json.Marshal(profile1)
if err != nil {
t.Fatal(err)
}
if string(text) == "\"profile\"" {
t.Fatalf("[\"profile\"] expected, but %s got", text)
}
var p StdPermission
if err := json.Unmarshal(text, &p); err != nil {
t.Fatal(err)
}
if p.ID() != "profile" {
t.Fatalf("[profile] expected, but %s got", p.ID())
}
}
func TestLayerPermission(t *testing.T) {
profile1 := NewLayerPermission("profile")
profile2 := NewLayerPermission("profile")
admin := NewLayerPermission("admin")
admindashboard := NewLayerPermission("admin:dashboard")
adminpassword := NewLayerPermission("admin:password")
if profile1.Match(NewStdPermission("std-permission")) {
t.Fatal("Type assertion issue")
}
if !profile1.Match(profile1) {
t.Fatalf("%s should have the permission", profile1.ID())
}
if !profile1.Match(profile2) {
t.Fatalf("%s should have the permission", profile1.ID())
}
if profile1.Match(admin) {
t.Fatalf("%s should not have the permission", profile1.ID())
}
text, err := json.Marshal(admin)
if err != nil {
t.Fatal(err)
}
var p LayerPermission
if err := json.Unmarshal(text, &p); err != nil {
t.Fatal(err)
}
if p.ID() != "admin" {
t.Fatalf("[admin] expected, but %s got", p.ID())
}
if !p.Match(admindashboard) {
t.Fatalf("%s should have the permission", p.ID())
}
if admindashboard.Match(&p) {
t.Fatalf("%s should not have the permission", admindashboard.ID())
}
if adminpassword.Match(admindashboard) {
t.Fatalf("%s should not have the permission", adminpassword.ID())
}
}