-
Notifications
You must be signed in to change notification settings - Fork 176
/
permission_test.go
55 lines (48 loc) · 1.32 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
package gorbac
import (
"encoding/json"
"testing"
)
func TestBasicPermission(t *testing.T) {
profile1 := NewPermission("profile")
profile2 := NewPermission("profile")
admin := NewPermission("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[string]
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 TestBasicPermissionPointerReceiver(t *testing.T) {
P1 := StdPermission[string]{"testing"}
P1Pointer := NewPermission("testing")
if !P1.Match(P1Pointer) {
t.Fatalf("P1 %s should match P1Pointer %s", P1.ID(), P1Pointer.ID())
}
P2 := StdPermission[string]{"not-match"}
P2Pointer := NewPermission("not-match")
if P1.Match(P2) {
t.Fatalf("P1 %s should not match P2 %s", P1.ID(), P2.ID())
}
if P1.Match(P2Pointer) {
t.Fatalf("P1 %s should not match P2Pointer %s", P1.ID(), P2Pointer.ID())
}
}