-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_test.go
More file actions
231 lines (207 loc) · 5.05 KB
/
resource_test.go
File metadata and controls
231 lines (207 loc) · 5.05 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package auth
import (
"context"
"errors"
"github.com/bottledcode/durable-php/cli/appcontext"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestNewResourcePermissions(t *testing.T) {
owner := &User{UserId: "12345"}
noOwner := &User{UserId: ""}
tests := []struct {
name string
owner *User
mode Mode
expected *Resource
}{
{
name: "ResourceWithOwner",
owner: owner,
mode: "read",
expected: &Resource{
Owners: map[UserId]struct{}{"12345": {}},
Shares: []Share{},
Mode: "read",
Expires: time.Now(),
},
},
{
name: "ResourceWithNoOwner",
owner: noOwner,
mode: "write",
expected: &Resource{
Owners: map[UserId]struct{}{},
Shares: []Share{},
Mode: "write",
Expires: time.Now(),
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := NewResourcePermissions(test.owner, test.mode)
if !sameOwners(got.Owners, test.expected.Owners) {
t.Errorf("Owners was %v; want %v;", got.Owners, test.expected.Owners)
}
if len(got.Shares) != len(test.expected.Shares) {
t.Errorf("Shares length was %d; want %d;", len(got.Shares), len(test.expected.Shares))
}
if got.Mode != test.expected.Mode {
t.Errorf("Mode was \"%v\"; want \"%v\";", got.Mode, test.expected.Mode)
}
// Use a reasonable time closeness threshold because we cannot ensure that
// the generated and expected timestamps are identical
timeDiff := got.Expires.Sub(test.expected.Expires)
if timeDiff > time.Second || timeDiff < -time.Second {
t.Errorf("Expires was %v; want %v;", got.Expires, test.expected.Expires)
}
})
}
}
// sameOwners is a helper function to compare the owners maps.
// It allows for the order to be different.
func sameOwners(a, b map[UserId]struct{}) bool {
if len(a) != len(b) {
return false
}
for k := range a {
if _, ok := b[k]; !ok {
return false
}
}
return true
}
func Test_Resource_ShareOwnership(t *testing.T) {
tests := []struct {
name string
current *User
new UserId
keep bool
wantErr error
}{
{
name: "Share ownership with nil user",
new: "new-user",
wantErr: errors.New("cannot share with unknown user"),
},
{
name: "Share ownership and keep current",
current: &User{UserId: "current-user"},
new: "new-user",
keep: true,
},
{
name: "Share ownership and remove current",
current: &User{UserId: "current-user"},
new: "new-user",
keep: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &Resource{
Owners: make(map[UserId]struct{}),
}
if tt.current != nil {
r.Owners[tt.current.UserId] = struct{}{}
}
err := r.ShareOwnership(tt.new, tt.current, tt.keep)
assert.Equal(t, tt.wantErr, err)
if err == nil {
assert.Contains(t, r.Owners, tt.new)
if tt.keep && tt.current != nil {
assert.Contains(t, r.Owners, tt.current.UserId)
} else if tt.current != nil {
assert.NotContains(t, r.Owners, tt.current.UserId)
}
}
})
}
}
func TestResource_IsUserPermitted(t *testing.T) {
// Arrange
currentUser := &User{
UserId: UserId("some-user"),
Roles: []Role{"some-role"},
}
tests := []struct {
name string
mode Mode
perms CreatePermissions
want bool
withUser bool
}{
{
name: "PermissionShouldBeTrueForAnonymousMode",
mode: AnonymousMode,
perms: CreatePermissions{
Mode: AnonymousMode,
TimeToLive: 10,
},
want: true,
withUser: false,
},
{
name: "PermissionShouldBeTrueForAuthenticatedMode",
mode: AuthenticatedMode,
perms: CreatePermissions{
Mode: AuthenticatedMode,
TimeToLive: 10,
Users: []UserId{currentUser.UserId},
},
want: true,
withUser: true,
},
{
name: "PermissionShouldBeFalseForUnauthenticatedUserInAuthenticatedMode",
mode: AuthenticatedMode,
perms: CreatePermissions{
Mode: AuthenticatedMode,
TimeToLive: 10,
Users: []UserId{"some-other-user"},
},
want: false,
withUser: false,
},
{
name: "PermissionShouldBeTrueForExplicitMode",
mode: ExplicitMode,
perms: CreatePermissions{
Mode: ExplicitMode,
TimeToLive: 10,
Users: []UserId{currentUser.UserId},
},
want: true,
withUser: true,
},
{
name: "PermissionShouldBeFalseForUnincludedUserInExplicitMode",
mode: ExplicitMode,
perms: CreatePermissions{
Mode: ExplicitMode,
TimeToLive: 10,
Users: []UserId{"some-other-user"},
},
want: false,
withUser: true,
},
}
ctx := context.WithValue(context.Background(), appcontext.CurrentUserKey, currentUser)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &Resource{
Mode: tt.mode,
Expires: time.Now().Add(time.Duration(tt.perms.TimeToLive) * time.Nanosecond),
}
ctx2 := ctx
if !tt.withUser {
ctx2 = context.Background()
}
if got := r.isUserPermitted(tt.perms, ctx2); got != tt.want {
t.Errorf("isUserPermitted() = %v, want %v", got, tt.want)
}
})
}
}