-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtest.js
219 lines (159 loc) Β· 5.31 KB
/
test.js
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
import test from 'ava';
import CanCan from '.';
class Model {
constructor(attrs = {}) {
this.attrs = attrs;
}
get(key) {
return this.attrs[key];
}
}
class User extends Model {}
class Product extends Model {}
test('allow one action', t => {
const cancan = new CanCan();
const {can, allow, cannot} = cancan;
allow(User, 'read', Product);
const user = new User();
const product = new Product();
t.true(can(user, 'read', product));
t.false(cannot(user, 'read', product));
t.false(can(user, 'create', product));
});
test('allow many actions', t => {
const cancan = new CanCan();
const {can, allow} = cancan;
allow(User, ['read', 'create', 'destroy'], Product);
const user = new User();
const product = new Product();
t.true(can(user, 'read', product));
t.true(can(user, 'create', product));
t.true(can(user, 'destroy', product));
});
test('allow all actions using "manage"', t => {
const cancan = new CanCan();
const {can, allow} = cancan;
allow(User, 'manage', Product);
const user = new User();
const product = new Product();
t.true(can(user, 'read', product));
t.true(can(user, 'create', product));
t.true(can(user, 'update', product));
t.true(can(user, 'destroy', product));
t.true(can(user, 'modify', product));
});
test('allow all actions and all objects', t => {
const cancan = new CanCan();
const {can, allow} = cancan;
allow(User, 'manage', 'all');
const user = new User();
const product = new Product();
t.true(can(user, 'read', user));
t.true(can(user, 'read', product));
});
test('allow only objects that satisfy given condition', t => {
const cancan = new CanCan();
const {can, allow} = cancan;
allow(User, 'read', Product, {published: true});
const user = new User();
const privateProduct = new Product();
const publicProduct = new Product({published: true});
t.false(can(user, 'read', privateProduct));
t.true(can(user, 'read', publicProduct));
});
test('allow only when performer passes a condition', t => {
const cancan = new CanCan();
const {can, allow} = cancan;
allow(User, 'read', Product, user => user.get('admin'));
const user = new User();
const adminUser = new User({admin: true});
const product = new Product();
t.false(can(user, 'read', product));
t.true(can(adminUser, 'read', product));
});
test('allow only when target passes a condition', t => {
const cancan = new CanCan();
const {can, allow} = cancan;
allow(User, 'read', Product, (user, product) => product.get('published'));
const user = new User();
const privateProduct = new Product();
const publicProduct = new Product({published: true});
t.false(can(user, 'read', privateProduct));
t.true(can(user, 'read', publicProduct));
});
test('throw when condition is not a function or an object', t => {
const cancan = new CanCan();
const {allow} = cancan;
t.notThrows(() => allow(User, 'read', Product, undefined));
t.throws(() => allow(User, 'read', Product, 'abc'), 'Expected condition to be object or function, got string');
});
test('allow permissions on classes', t => {
const cancan = new CanCan();
const {can, allow} = cancan;
allow(User, 'read', Product);
const user = new User();
t.true(can(user, 'read', Product));
});
test('throw if permission is not granted', t => {
const cancan = new CanCan();
const {allow, authorize} = cancan;
allow(User, 'read', Product, (user, product) => product.get('published'));
const user = new User();
const privateProduct = new Product();
const publicProduct = new Product({published: true});
authorize(user, 'read', publicProduct);
t.throws(() => authorize(user, 'read', privateProduct), 'Authorization error');
});
test('throw a custom error if permission is not granted', t => {
class AuthError {
constructor(message) {
this.message = message;
}
}
const cancan = new CanCan({
createError(performer, action) {
return new AuthError(`User couldn't ${action} product`);
}
});
const {allow, authorize} = cancan;
allow(User, 'read', Product, (user, product) => product.get('published'));
const user = new User();
const privateProduct = new Product();
const publicProduct = new Product({published: true});
authorize(user, 'read', publicProduct);
t.throws(() => authorize(user, 'read', privateProduct), AuthError, 'User couldn\'t read product');
});
test('override instanceOf', t => {
const cancan = new CanCan({
instanceOf(instance, model) {
return instance instanceof model.Instance;
}
});
const {allow, can, cannot} = cancan;
// Mimic Sequelize models
allow({Instance: User}, 'read', {Instance: Product});
const user = new User();
const product = new Product();
t.true(can(user, 'read', product));
t.false(cannot(user, 'read', product));
t.false(can(user, 'create', product));
});
test('pass options to the rule', t => {
const cancan = new CanCan();
const {can, allow} = cancan;
const admin = new User({role: 'administrator'});
const user = new User({role: 'user'});
allow(User, 'update', User, (user, target, options) => {
if (user.get('role') === 'administrator') {
return true;
}
// Don't let regular user update their role
if (user.get('role') === 'user' && options.fields.indexOf('role') >= 0) {
return false;
}
return true;
});
t.true(can(admin, 'update', user, {fields: ['role']}));
t.true(can(user, 'update', user, {fields: ['username']}));
t.false(can(user, 'update', user, {fields: ['role']}));
});