forked from go-joe/joe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
285 lines (220 loc) · 8.53 KB
/
auth_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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package joe_test
import (
"errors"
"testing"
"github.com/go-joe/joe"
"github.com/go-joe/joe/joetest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
func TestAuth(t *testing.T) {
logger := zaptest.NewLogger(t)
store := joetest.NewStorage(t)
auth := joe.NewAuth(logger, store.Storage)
userID := "fgrosse"
// Initially the user should have no permissions whatsoever
err := auth.CheckPermission("test.foo", userID)
require.Equal(t, joe.ErrNotAllowed, err)
// Granting the empty scope is likely an error and thus should result in an error
_, err = auth.Grant("", userID)
require.EqualError(t, err, "scope cannot be empty")
err = auth.CheckPermission("", userID)
require.Equal(t, joe.ErrNotAllowed, err)
// Grant the test.foo scope
ok, err := auth.Grant("test.foo", userID)
require.NoError(t, err)
assert.True(t, ok)
// The user has exactly the test.foo scope and should be granted access.
err = auth.CheckPermission("test.foo", userID)
require.NoError(t, err)
// test.foo.bar is contained in the test.foo scope and the user should be granted access.
err = auth.CheckPermission("test.foo.bar", userID)
require.NoError(t, err)
// test is not contained in the test.foo scope so this should be denied.
err = auth.CheckPermission("test", userID)
require.Equal(t, joe.ErrNotAllowed, err)
// foo is also not contained in the test.foo scope so this should be denied.
err = auth.CheckPermission("foo", userID)
require.Equal(t, joe.ErrNotAllowed, err)
// Even though test.foo and test.bar share a common prefix this scope is not entirely
// contained in the granted scope so this should be denied.
err = auth.CheckPermission("test.bar", userID)
require.Equal(t, joe.ErrNotAllowed, err)
}
func TestAuth_GrantIsIdempotent(t *testing.T) {
logger := zaptest.NewLogger(t)
store := joetest.NewStorage(t)
auth := joe.NewAuth(logger, store.Storage)
// Lets assume dave already has permissions ot open the pod bay doors we want
// to make sure we will not append the same permissions multiple times.
existingPermissions := []string{"open_pod_bay_doors", "foo.bar"}
store.MustSet("joe.permissions.dave", existingPermissions)
ok, err := auth.Grant("open_pod_bay_doors", "dave")
require.NoError(t, err)
assert.False(t, ok)
store.AssertEquals("joe.permissions.dave", existingPermissions)
}
func TestAuth_GrantWiderScope(t *testing.T) {
logger := zaptest.NewLogger(t)
store := joetest.NewStorage(t)
auth := joe.NewAuth(logger, store.Storage)
store.MustSet("joe.permissions.fgrosse", []string{"foo.bar.baz", "test"})
ok, err := auth.Grant("foo", "fgrosse")
require.NoError(t, err)
assert.True(t, ok)
store.AssertEquals("joe.permissions.fgrosse", []string{"test", "foo"})
}
func TestAuth_GrantSmallerScope(t *testing.T) {
logger := zaptest.NewLogger(t)
store := joetest.NewStorage(t)
auth := joe.NewAuth(logger, store.Storage)
store.MustSet("joe.permissions.fgrosse", []string{"foo", "test"})
ok, err := auth.Grant("foo.bar.baz", "fgrosse")
require.NoError(t, err)
assert.False(t, ok)
store.AssertEquals("joe.permissions.fgrosse", []string{"foo", "test"})
}
func TestAuth_Revoke(t *testing.T) {
logger := zaptest.NewLogger(t)
store := joetest.NewStorage(t)
auth := joe.NewAuth(logger, store.Storage)
store.MustSet("joe.permissions.fgrosse", []string{"foo.bar", "test"})
ok, err := auth.Revoke("foo.bar", "fgrosse")
assert.NoError(t, err)
assert.True(t, ok)
store.AssertEquals("joe.permissions.fgrosse", []string{"test"})
}
func TestAuth_RevokeNonExistingScope(t *testing.T) {
logger := zaptest.NewLogger(t)
store := joetest.NewStorage(t)
auth := joe.NewAuth(logger, store.Storage)
store.MustSet("joe.permissions.fgrosse", []string{"test"})
ok, err := auth.Revoke("foo.bar", "fgrosse")
assert.NoError(t, err)
assert.False(t, ok)
store.AssertEquals("joe.permissions.fgrosse", []string{"test"})
}
func TestAuth_RevokeWiderScope(t *testing.T) {
logger := zaptest.NewLogger(t)
store := joetest.NewStorage(t)
auth := joe.NewAuth(logger, store.Storage)
store.MustSet("joe.permissions.fgrosse", []string{"foo"})
ok, err := auth.Revoke("foo.bar", "fgrosse")
assert.EqualError(t, err, `cannot revoke scope "foo.bar" because the user still has the more general scope "foo"`)
assert.False(t, ok)
}
func TestAuth_RevokeEmptyScope(t *testing.T) {
logger := zaptest.NewLogger(t)
store := joetest.NewStorage(t)
auth := joe.NewAuth(logger, store.Storage)
ok, err := auth.Revoke("", "fgrosse")
assert.EqualError(t, err, "scope cannot be empty")
assert.False(t, ok)
}
func TestAuth_RevokeLastScope(t *testing.T) {
logger := zaptest.NewLogger(t)
store := joetest.NewStorage(t)
auth := joe.NewAuth(logger, store.Storage)
store.MustSet("joe.permissions.fgrosse", []string{"test"})
ok, err := auth.Revoke("test", "fgrosse")
assert.NoError(t, err)
assert.True(t, ok)
ok, err = store.Get("joe.permissions.fgrosse", nil)
require.NoError(t, err)
assert.False(t, ok, "storage should no longer contain any permissions for this user")
}
func TestAuth_RevokeNoOldScopes(t *testing.T) {
logger := zaptest.NewLogger(t)
store := joetest.NewStorage(t)
auth := joe.NewAuth(logger, store.Storage)
ok, err := auth.Revoke("test", "fgrosse")
assert.NoError(t, err)
assert.False(t, ok)
ok, err = store.Get("joe.permissions.fgrosse", nil)
require.NoError(t, err)
assert.False(t, ok, "storage still not contain any permissions for this user")
}
func TestAuth_CheckPermission_Errors(t *testing.T) {
logger := zaptest.NewLogger(t)
mem := new(memoryMock)
store := joetest.NewStorage(t)
store.SetMemory(mem)
auth := joe.NewAuth(logger, store.Storage)
mem.On("Get", "joe.permissions.test").Return(nil, false, errors.New("that didn't work"))
err := auth.CheckPermission("xxx", "test")
assert.EqualError(t, err, "failed to load user permissions: that didn't work")
mem = new(memoryMock)
store.SetMemory(mem)
mem.On("Get", "joe.permissions.test").Return([]byte("nope!"), true, nil)
err = auth.CheckPermission("xxx", "test")
assert.EqualError(t, err, "failed to load user permissions: decode data: invalid character 'o' in literal null (expecting 'u')")
}
func TestAuth_Grant_Errors(t *testing.T) {
logger := zaptest.NewLogger(t)
mem := new(memoryMock)
store := joetest.NewStorage(t)
store.SetMemory(mem)
auth := joe.NewAuth(logger, store.Storage)
mem.On("Get", "joe.permissions.test").Return(nil, false, errors.New("that didn't work"))
_, err := auth.Grant("xxx", "test")
assert.EqualError(t, err, "failed to load user permissions: that didn't work")
mem = new(memoryMock)
store.SetMemory(mem)
mem.On("Get", "joe.permissions.test").Return(nil, false, nil)
mem.On("Set", "joe.permissions.test", []byte(`["xxx"]`)).Return(errors.New("not today"))
_, err = auth.Grant("xxx", "test")
assert.EqualError(t, err, "failed to update user permissions: not today")
}
func TestAuth_Revoke_Errors(t *testing.T) {
logger := zaptest.NewLogger(t)
mem := new(memoryMock)
store := joetest.NewStorage(t)
store.SetMemory(mem)
auth := joe.NewAuth(logger, store.Storage)
mem.On("Get", "joe.permissions.test").Return(nil, false, errors.New("that didn't work"))
_, err := auth.Revoke("xxx", "test")
assert.EqualError(t, err, "failed to load user permissions: that didn't work")
mem = new(memoryMock)
store.SetMemory(mem)
mem.On("Get", "joe.permissions.test").Return([]byte(`["foo", "bar"]`), true, nil)
mem.On("Set", "joe.permissions.test", []byte(`["bar"]`)).Return(errors.New("not today"))
_, err = auth.Revoke("foo", "test")
assert.EqualError(t, err, "failed to update user permissions: not today")
mem = new(memoryMock)
store.SetMemory(mem)
mem.On("Get", "joe.permissions.test").Return([]byte(`["foo"]`), true, nil)
mem.On("Delete", "joe.permissions.test").Return(false, errors.New("not today"))
_, err = auth.Revoke("foo", "test")
assert.EqualError(t, err, "failed to delete last user permission: not today")
}
type memoryMock struct {
mock.Mock
}
func (m *memoryMock) Set(key string, value []byte) error {
args := m.Called(key, value)
return args.Error(0)
}
func (m *memoryMock) Get(key string) (data []byte, ok bool, err error) {
args := m.Called(key)
if x := args.Get(0); x != nil {
data = x.([]byte)
}
return data, args.Bool(1), args.Error(2)
}
func (m *memoryMock) Delete(key string) (bool, error) {
args := m.Called(key)
return args.Bool(0), args.Error(1)
}
func (m *memoryMock) Keys() (keys []string, err error) {
args := m.Called()
if x := args.Get(0); x != nil {
keys = x.([]string)
}
return keys, args.Error(1)
}
func (m *memoryMock) Close() error {
args := m.Called()
return args.Error(0)
}