-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_test.go
137 lines (121 loc) · 2.7 KB
/
resource_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package leanauthz_test
import (
"testing"
authz "github.com/lazharichir/leanauthz"
)
func TestNewResource(t *testing.T) {
tests := []struct {
name string
str string
wantErr bool
}{
{
name: "valid resource",
str: "documents/doc_abcdef",
wantErr: false,
},
{
name: "empty resource",
str: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil && !tt.wantErr {
t.Errorf("NewResource() panicked unexpectedly: %v", r)
}
}()
got := authz.NewResource(tt.str)
if tt.wantErr {
t.Errorf("NewResource() did not return an error for an empty string")
}
if string(got) != tt.str {
t.Errorf("NewResource() = %v, want %v", got, tt.str)
}
})
}
}
func TestResource_SplitBy(t *testing.T) {
tests := []struct {
name string
r authz.Resource
sep string
want []string
}{
{
name: "valid resource",
r: authz.NewResource("documents/doc_abcdef"),
sep: "/",
want: []string{"documents", "doc_abcdef"},
},
{
name: "multiple pairs of parts",
r: authz.NewResource("documents/doc_abcdef/comments/cmt_123456"),
sep: "/",
want: []string{"documents", "doc_abcdef", "comments", "cmt_123456"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.r.SplitBy(tt.sep)
if len(got) != len(tt.want) {
t.Errorf("Resource.SplitBy() returned %d parts, want %d", len(got), len(tt.want))
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("Resource.SplitBy() = %v, want %v", got, tt.want)
break
}
}
})
}
}
func TestNewNamespacedResource(t *testing.T) {
tests := []struct {
name string
ns string
id string
want authz.Resource
wantErr bool
}{
{
name: "valid resource",
ns: "documents",
id: "doc_abcdef",
want: authz.NewResource("documents/doc_abcdef"),
wantErr: false,
},
{
name: "empty namespace",
ns: "",
id: "doc_abcdef",
want: authz.Resource(""),
wantErr: true,
},
{
name: "empty id",
ns: "documents",
id: "",
want: authz.Resource(""),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil && !tt.wantErr {
t.Errorf("NewNamespacedResource() panicked unexpectedly: %v", r)
}
}()
got := authz.NewNamespacedResource(tt.ns, tt.id)
if tt.wantErr {
t.Errorf("NewNamespacedResource() did not return an error for an empty string")
}
if string(got) != tt.want.String() {
t.Errorf("NewNamespacedResource() = %v, want %v", got, tt.want)
}
})
}
}