@nymphjs/tilmeld 1.0.0-beta.120 → 1.0.0-beta.121

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,8 @@
1
1
  import { Entity, Nymph, TilmeldAccessLevels, TilmeldAccessRequest, } from '@nymphjs/nymph';
2
2
  import Tilmeld from './Tilmeld.js';
3
3
  import { AccessControlError } from './errors/index.js';
4
+ import { UserTest } from './User.testArtifacts.js';
5
+ import { GroupTest } from './Group.testArtifacts.js';
4
6
  /**
5
7
  * This class is a test class that extends the Entity class.
6
8
  */
@@ -27,7 +29,7 @@ export class TestModel extends Entity {
27
29
  }
28
30
  }
29
31
  const TestModelClass = TestModel;
30
- export function TilmeldTest(nymphBase, it) {
32
+ export function TilmeldTest(nymphBase, describe, it) {
31
33
  const tilmeld = new Tilmeld({
32
34
  appName: 'My App',
33
35
  appUrl: 'http://localhost:8080',
@@ -45,1120 +47,1127 @@ export function TilmeldTest(nymphBase, it) {
45
47
  },
46
48
  });
47
49
  const nymph = new Nymph(nymphBase.config, nymphBase.driver, tilmeld);
48
- const User = tilmeld.User;
49
- const Group = tilmeld.Group;
50
- const TestModel = nymph.addEntityClass(TestModelClass);
51
- async function deleteTestData() {
52
- let all = await nymph.getEntities({
53
- class: TestModel,
54
- });
55
- expect(Array.isArray(all)).toEqual(true);
56
- for (const cur of all) {
57
- expect(await cur.$deleteSkipAC()).toEqual(true);
58
- }
59
- all = await nymph.getEntities({ class: TestModel });
60
- expect(all.length).toEqual(0);
61
- all = await nymph.getEntities({ class: User });
62
- expect(Array.isArray(all)).toEqual(true);
63
- for (const cur of all) {
64
- expect(await cur.$deleteSkipAC()).toEqual(true);
65
- }
66
- all = await nymph.getEntities({ class: User });
67
- expect(all.length).toEqual(0);
68
- all = await nymph.getEntities({ class: Group });
69
- expect(Array.isArray(all)).toEqual(true);
70
- for (const cur of all) {
71
- expect(await cur.$deleteSkipAC()).toEqual(true);
72
- }
73
- all = await nymph.getEntities({ class: Group });
74
- expect(all.length).toEqual(0);
75
- }
76
- async function makeUsers() {
77
- const admin = await User.factoryUsername('admin');
78
- const bob = await User.factoryUsername('bob');
79
- const alice = await User.factoryUsername('alice');
80
- const abgroup = await Group.factoryGroupname('abgroup');
81
- if (admin.guid == null) {
82
- admin.username = 'admin';
83
- admin.email = 'admin@localhost';
84
- admin.nameFirst = 'Admin';
85
- admin.nameLast = 'User';
86
- admin.name = 'Admin User';
87
- admin.$password('password');
88
- admin.$grant('system/admin');
89
- await admin.$saveSkipAC();
90
- }
91
- if (abgroup.guid == null) {
92
- abgroup.groupname = 'abgroup';
93
- abgroup.email = 'abgroup@localhost';
94
- abgroup.name = 'AB Group';
95
- await abgroup.$saveSkipAC();
50
+ describe('Tilmeld', () => {
51
+ UserTest(nymph, tilmeld, describe, it);
52
+ GroupTest(nymph, tilmeld, describe, it);
53
+ const User = tilmeld.User;
54
+ const Group = tilmeld.Group;
55
+ const TestModel = nymph.addEntityClass(TestModelClass);
56
+ async function deleteTestData() {
57
+ let all = await nymph.getEntities({
58
+ class: TestModel,
59
+ });
60
+ expect(Array.isArray(all)).toEqual(true);
61
+ for (const cur of all) {
62
+ expect(await cur.$deleteSkipAC()).toEqual(true);
63
+ }
64
+ all = await nymph.getEntities({ class: TestModel });
65
+ expect(all.length).toEqual(0);
66
+ all = await nymph.getEntities({ class: User });
67
+ expect(Array.isArray(all)).toEqual(true);
68
+ for (const cur of all) {
69
+ expect(await cur.$deleteSkipAC()).toEqual(true);
70
+ }
71
+ all = await nymph.getEntities({ class: User });
72
+ expect(all.length).toEqual(0);
73
+ all = await nymph.getEntities({ class: Group });
74
+ expect(Array.isArray(all)).toEqual(true);
75
+ for (const cur of all) {
76
+ expect(await cur.$deleteSkipAC()).toEqual(true);
77
+ }
78
+ all = await nymph.getEntities({ class: Group });
79
+ expect(all.length).toEqual(0);
96
80
  }
97
- if (bob.guid == null) {
98
- bob.username = 'bob';
99
- bob.email = 'bob@localhost';
100
- bob.nameFirst = 'Bob';
101
- bob.nameLast = 'User';
102
- bob.name = 'Bob User';
103
- bob.$password('password');
104
- bob.$addGroup(abgroup);
105
- await bob.$saveSkipAC();
81
+ async function makeUsers() {
82
+ const admin = await User.factoryUsername('admin');
83
+ const bob = await User.factoryUsername('bob');
84
+ const alice = await User.factoryUsername('alice');
85
+ const abgroup = await Group.factoryGroupname('abgroup');
86
+ if (admin.guid == null) {
87
+ admin.username = 'admin';
88
+ admin.email = 'admin@localhost';
89
+ admin.nameFirst = 'Admin';
90
+ admin.nameLast = 'User';
91
+ admin.name = 'Admin User';
92
+ admin.$password('password');
93
+ admin.$grant('system/admin');
94
+ await admin.$saveSkipAC();
95
+ }
96
+ if (abgroup.guid == null) {
97
+ abgroup.groupname = 'abgroup';
98
+ abgroup.email = 'abgroup@localhost';
99
+ abgroup.name = 'AB Group';
100
+ await abgroup.$saveSkipAC();
101
+ }
102
+ if (bob.guid == null) {
103
+ bob.username = 'bob';
104
+ bob.email = 'bob@localhost';
105
+ bob.nameFirst = 'Bob';
106
+ bob.nameLast = 'User';
107
+ bob.name = 'Bob User';
108
+ bob.$password('password');
109
+ bob.$addGroup(abgroup);
110
+ await bob.$saveSkipAC();
111
+ }
112
+ if (alice.guid == null) {
113
+ alice.username = 'alice';
114
+ alice.email = 'alice@localhost';
115
+ alice.nameFirst = 'Alice';
116
+ alice.nameLast = 'User';
117
+ alice.name = 'Alice User';
118
+ alice.$password('password');
119
+ alice.$addGroup(abgroup);
120
+ await alice.$saveSkipAC();
121
+ }
122
+ return { admin, bob, alice, abgroup };
106
123
  }
107
- if (alice.guid == null) {
108
- alice.username = 'alice';
109
- alice.email = 'alice@localhost';
110
- alice.nameFirst = 'Alice';
111
- alice.nameLast = 'User';
112
- alice.name = 'Alice User';
113
- alice.$password('password');
114
- alice.$addGroup(abgroup);
115
- await alice.$saveSkipAC();
116
- }
117
- return { admin, bob, alice, abgroup };
118
- }
119
- it('delete old test data', async () => {
120
- await deleteTestData();
121
- });
122
- it('users can log in', async () => {
123
- const { admin, bob, alice } = await makeUsers();
124
- let result = await admin.$login({
125
- username: 'admin',
126
- password: 'password',
124
+ it('delete old test data', async () => {
125
+ await deleteTestData();
127
126
  });
128
- expect(result.result).toEqual(true);
129
- expect(admin.$is(User.current())).toEqual(true);
130
- result = await admin.$logout();
131
- expect(result.result).toEqual(true);
132
- expect(User.current()).toBeNull();
133
- result = await bob.$login({
134
- username: 'bob',
135
- password: 'password',
127
+ it('users can log in', async () => {
128
+ const { admin, bob, alice } = await makeUsers();
129
+ let result = await admin.$login({
130
+ username: 'admin',
131
+ password: 'password',
132
+ });
133
+ expect(result.result).toEqual(true);
134
+ expect(admin.$is(User.current())).toEqual(true);
135
+ result = await admin.$logout();
136
+ expect(result.result).toEqual(true);
137
+ expect(User.current()).toBeNull();
138
+ result = await bob.$login({
139
+ username: 'bob',
140
+ password: 'password',
141
+ });
142
+ expect(result.result).toEqual(true);
143
+ expect(bob.$is(User.current())).toEqual(true);
144
+ result = await bob.$logout();
145
+ expect(result.result).toEqual(true);
146
+ expect(User.current()).toBeNull();
147
+ result = await alice.$login({
148
+ username: 'alice',
149
+ password: 'password',
150
+ });
151
+ expect(result.result).toEqual(true);
152
+ expect(alice.$is(User.current())).toEqual(true);
153
+ result = await alice.$logout();
154
+ expect(result.result).toEqual(true);
155
+ expect(User.current()).toBeNull();
136
156
  });
137
- expect(result.result).toEqual(true);
138
- expect(bob.$is(User.current())).toEqual(true);
139
- result = await bob.$logout();
140
- expect(result.result).toEqual(true);
141
- expect(User.current()).toBeNull();
142
- result = await alice.$login({
143
- username: 'alice',
144
- password: 'password',
157
+ it('creates tables for test model', async () => {
158
+ const testEntity = await nymph.getEntity({ class: TestModel });
159
+ expect(testEntity).toBeNull();
145
160
  });
146
- expect(result.result).toEqual(true);
147
- expect(alice.$is(User.current())).toEqual(true);
148
- result = await alice.$logout();
149
- expect(result.result).toEqual(true);
150
- expect(User.current()).toBeNull();
151
- });
152
- it('creates tables for test model', async () => {
153
- const testEntity = await nymph.getEntity({ class: TestModel });
154
- expect(testEntity).toBeNull();
155
- });
156
- it('user ownership, group ownership, and access controls are added on entities', async () => {
157
- const { bob } = await makeUsers();
158
- await tilmeld.fillSession(bob);
159
- const bobsEntity = await TestModel.factory();
160
- bobsEntity.name = "Bob's Entity";
161
- expect(await bobsEntity.$save()).toEqual(true);
162
- expect(bobsEntity.guid).not.toBeNull();
163
- expect(bob.$is(bobsEntity.user)).toEqual(true);
164
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
165
- expect(testEntityBob).not.toBeNull();
166
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
167
- expect(testEntityBob?.user?.$is(bob)).toEqual(true);
168
- expect(testEntityBob?.group?.$is(bob.group)).toEqual(true);
169
- expect(testEntityBob?.acUser).toEqual(TilmeldAccessLevels.FULL_ACCESS);
170
- expect(testEntityBob?.acGroup).toEqual(TilmeldAccessLevels.READ_ACCESS);
171
- expect(testEntityBob?.acOther).toEqual(TilmeldAccessLevels.NO_ACCESS);
172
- expect(testEntityBob?.acRead).toEqual([]);
173
- expect(testEntityBob?.acWrite).toEqual([]);
174
- expect(testEntityBob?.acFull).toEqual([]);
175
- });
176
- it("alice can't access an entity owned by bob", async () => {
177
- const { bob, alice } = await makeUsers();
178
- await tilmeld.fillSession(bob);
179
- const bobsEntity = await TestModel.factory();
180
- bobsEntity.name = "Bob's Entity";
181
- expect(await bobsEntity.$save()).toEqual(true);
182
- expect(bobsEntity.guid).not.toBeNull();
183
- expect(bob.$is(bobsEntity.user)).toEqual(true);
184
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
185
- expect(testEntityBob).not.toBeNull();
186
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
187
- tilmeld.clearSession();
188
- await tilmeld.fillSession(alice);
189
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
190
- expect(testEntityAlice).toBeNull();
191
- });
192
- it("alice can't write to an entity she doesn't have write permission for", async () => {
193
- const { bob, alice } = await makeUsers();
194
- await tilmeld.fillSession(bob);
195
- const bobsEntity = await TestModel.factory();
196
- bobsEntity.name = "Bob's Entity";
197
- bobsEntity.acRead = [alice.guid ?? ''];
198
- expect(await bobsEntity.$save()).toEqual(true);
199
- expect(bobsEntity.guid).not.toBeNull();
200
- expect(bob.$is(bobsEntity.user)).toEqual(true);
201
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
202
- expect(testEntityBob).not.toBeNull();
203
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
204
- tilmeld.clearSession();
205
- await tilmeld.fillSession(alice);
206
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
207
- expect(testEntityAlice).not.toBeNull();
208
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
209
- if (testEntityAlice == null) {
210
- throw new Error();
211
- }
212
- testEntityAlice.name = "Haha! It's mine now!";
213
- try {
214
- await testEntityAlice.$save();
215
- throw new Error('It should have thrown AccessControlError.');
216
- }
217
- catch (e) {
218
- expect(e).toBeInstanceOf(AccessControlError);
219
- }
220
- expect(await testEntityAlice.$refresh()).toEqual(true);
221
- expect(testEntityAlice.name).toEqual("Bob's Entity");
222
- });
223
- it('alice can write to an entity she has write permission for', async () => {
224
- const { bob, alice } = await makeUsers();
225
- await tilmeld.fillSession(bob);
226
- const bobsEntity = await TestModel.factory();
227
- bobsEntity.name = "Bob's Entity";
228
- bobsEntity.acWrite = [alice.guid ?? ''];
229
- expect(await bobsEntity.$save()).toEqual(true);
230
- expect(bobsEntity.guid).not.toBeNull();
231
- expect(bob.$is(bobsEntity.user)).toEqual(true);
232
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
233
- expect(testEntityBob).not.toBeNull();
234
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
235
- tilmeld.clearSession();
236
- await tilmeld.fillSession(alice);
237
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
238
- expect(testEntityAlice).not.toBeNull();
239
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
240
- if (testEntityAlice == null) {
241
- throw new Error();
242
- }
243
- testEntityAlice.name = "Haha! It's mine now!";
244
- expect(await testEntityAlice.$save()).toEqual(true);
245
- expect(await testEntityAlice.$refresh()).toEqual(true);
246
- expect(testEntityAlice.name).toEqual("Haha! It's mine now!");
247
- });
248
- it("alice can't delete an entity she doesn't have full permission for", async () => {
249
- const { bob, alice } = await makeUsers();
250
- await tilmeld.fillSession(bob);
251
- const bobsEntity = await TestModel.factory();
252
- bobsEntity.name = "Bob's Entity";
253
- bobsEntity.acWrite = [alice.guid ?? ''];
254
- expect(await bobsEntity.$save()).toEqual(true);
255
- expect(bobsEntity.guid).not.toBeNull();
256
- expect(bob.$is(bobsEntity.user)).toEqual(true);
257
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
258
- expect(testEntityBob).not.toBeNull();
259
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
260
- tilmeld.clearSession();
261
- await tilmeld.fillSession(alice);
262
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
263
- expect(testEntityAlice).not.toBeNull();
264
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
265
- if (testEntityAlice == null) {
266
- throw new Error();
267
- }
268
- try {
269
- await testEntityAlice.$delete();
270
- throw new Error('It should have thrown AccessControlError.');
271
- }
272
- catch (e) {
273
- expect(e).toBeInstanceOf(AccessControlError);
274
- }
275
- expect(await testEntityAlice.$refresh()).toEqual(true);
276
- expect(testEntityAlice.guid).toEqual(bobsEntity.guid);
277
- });
278
- it('alice can delete an entity she has full permission for', async () => {
279
- const { bob, alice } = await makeUsers();
280
- await tilmeld.fillSession(bob);
281
- const bobsEntity = await TestModel.factory();
282
- bobsEntity.name = "Bob's Entity";
283
- bobsEntity.acFull = [alice.guid ?? ''];
284
- expect(await bobsEntity.$save()).toEqual(true);
285
- expect(bobsEntity.guid).not.toBeNull();
286
- expect(bob.$is(bobsEntity.user)).toEqual(true);
287
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
288
- expect(testEntityBob).not.toBeNull();
289
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
290
- tilmeld.clearSession();
291
- await tilmeld.fillSession(alice);
292
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
293
- expect(testEntityAlice).not.toBeNull();
294
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
295
- if (testEntityAlice == null) {
296
- throw new Error();
297
- }
298
- expect(await testEntityAlice.$delete()).toEqual(true);
299
- const verifyEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
300
- expect(verifyEntityAlice).toBeNull();
301
- });
302
- it('alice can access an entity her group has permission for', async () => {
303
- const { bob, alice, abgroup } = await makeUsers();
304
- await tilmeld.fillSession(bob);
305
- const bobsEntity = await TestModel.factory();
306
- bobsEntity.name = "Bob's Entity";
307
- bobsEntity.acRead = [abgroup.guid ?? ''];
308
- expect(await bobsEntity.$save()).toEqual(true);
309
- expect(bobsEntity.guid).not.toBeNull();
310
- expect(bob.$is(bobsEntity.user)).toEqual(true);
311
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
312
- expect(testEntityBob).not.toBeNull();
313
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
314
- tilmeld.clearSession();
315
- await tilmeld.fillSession(alice);
316
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
317
- expect(testEntityAlice).not.toBeNull();
318
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
319
- });
320
- it("alice can't write to an entity her group doesn't have write permission for", async () => {
321
- const { bob, alice, abgroup } = await makeUsers();
322
- await tilmeld.fillSession(bob);
323
- const bobsEntity = await TestModel.factory();
324
- bobsEntity.name = "Bob's Entity";
325
- bobsEntity.acRead = [abgroup.guid ?? ''];
326
- expect(await bobsEntity.$save()).toEqual(true);
327
- expect(bobsEntity.guid).not.toBeNull();
328
- expect(bob.$is(bobsEntity.user)).toEqual(true);
329
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
330
- expect(testEntityBob).not.toBeNull();
331
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
332
- tilmeld.clearSession();
333
- await tilmeld.fillSession(alice);
334
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
335
- expect(testEntityAlice).not.toBeNull();
336
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
337
- if (testEntityAlice == null) {
338
- throw new Error();
339
- }
340
- testEntityAlice.name = "Haha! It's mine now!";
341
- try {
342
- await testEntityAlice.$save();
343
- throw new Error('It should have thrown AccessControlError.');
344
- }
345
- catch (e) {
346
- expect(e).toBeInstanceOf(AccessControlError);
347
- }
348
- expect(await testEntityAlice.$refresh()).toEqual(true);
349
- expect(testEntityAlice.name).toEqual("Bob's Entity");
350
- });
351
- it('alice can write to an entity her group has write permission for', async () => {
352
- const { bob, alice, abgroup } = await makeUsers();
353
- await tilmeld.fillSession(bob);
354
- const bobsEntity = await TestModel.factory();
355
- bobsEntity.name = "Bob's Entity";
356
- bobsEntity.acWrite = [abgroup.guid ?? ''];
357
- expect(await bobsEntity.$save()).toEqual(true);
358
- expect(bobsEntity.guid).not.toBeNull();
359
- expect(bob.$is(bobsEntity.user)).toEqual(true);
360
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
361
- expect(testEntityBob).not.toBeNull();
362
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
363
- tilmeld.clearSession();
364
- await tilmeld.fillSession(alice);
365
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
366
- expect(testEntityAlice).not.toBeNull();
367
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
368
- if (testEntityAlice == null) {
369
- throw new Error();
370
- }
371
- testEntityAlice.name = "Haha! It's mine now!";
372
- expect(await testEntityAlice.$save()).toEqual(true);
373
- expect(await testEntityAlice.$refresh()).toEqual(true);
374
- expect(testEntityAlice.name).toEqual("Haha! It's mine now!");
375
- });
376
- it("alice can't delete an entity her group doesn't have full permission for", async () => {
377
- const { bob, alice, abgroup } = await makeUsers();
378
- await tilmeld.fillSession(bob);
379
- const bobsEntity = await TestModel.factory();
380
- bobsEntity.name = "Bob's Entity";
381
- bobsEntity.acWrite = [abgroup.guid ?? ''];
382
- expect(await bobsEntity.$save()).toEqual(true);
383
- expect(bobsEntity.guid).not.toBeNull();
384
- expect(bob.$is(bobsEntity.user)).toEqual(true);
385
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
386
- expect(testEntityBob).not.toBeNull();
387
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
388
- tilmeld.clearSession();
389
- await tilmeld.fillSession(alice);
390
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
391
- expect(testEntityAlice).not.toBeNull();
392
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
393
- if (testEntityAlice == null) {
394
- throw new Error();
395
- }
396
- try {
397
- await testEntityAlice.$delete();
398
- throw new Error('It should have thrown AccessControlError.');
399
- }
400
- catch (e) {
401
- expect(e).toBeInstanceOf(AccessControlError);
402
- }
403
- expect(await testEntityAlice.$refresh()).toEqual(true);
404
- expect(testEntityAlice.guid).toEqual(bobsEntity.guid);
405
- });
406
- it('alice can delete an entity her group has full permission for', async () => {
407
- const { bob, alice, abgroup } = await makeUsers();
408
- await tilmeld.fillSession(bob);
409
- const bobsEntity = await TestModel.factory();
410
- bobsEntity.name = "Bob's Entity";
411
- bobsEntity.acFull = [abgroup.guid ?? ''];
412
- expect(await bobsEntity.$save()).toEqual(true);
413
- expect(bobsEntity.guid).not.toBeNull();
414
- expect(bob.$is(bobsEntity.user)).toEqual(true);
415
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
416
- expect(testEntityBob).not.toBeNull();
417
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
418
- tilmeld.clearSession();
419
- await tilmeld.fillSession(alice);
420
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
421
- expect(testEntityAlice).not.toBeNull();
422
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
423
- if (testEntityAlice == null) {
424
- throw new Error();
425
- }
426
- expect(await testEntityAlice.$delete()).toEqual(true);
427
- const verifyEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
428
- expect(verifyEntityAlice).toBeNull();
429
- });
430
- it('alice can access an entity her primary group has permission for', async () => {
431
- const { bob, alice } = await makeUsers();
432
- await tilmeld.fillSession(bob);
433
- const bobsEntity = await TestModel.factory();
434
- bobsEntity.name = "Bob's Entity";
435
- bobsEntity.group = alice.group;
436
- expect(await bobsEntity.$save()).toEqual(true);
437
- expect(bobsEntity.guid).not.toBeNull();
438
- expect(bob.$is(bobsEntity.user)).toEqual(true);
439
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
440
- expect(testEntityBob).not.toBeNull();
441
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
442
- tilmeld.clearSession();
443
- await tilmeld.fillSession(alice);
444
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
445
- expect(testEntityAlice).not.toBeNull();
446
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
447
- });
448
- it("alice can't write to an entity her primary group doesn't have write permission for", async () => {
449
- const { bob, alice } = await makeUsers();
450
- await tilmeld.fillSession(bob);
451
- const bobsEntity = await TestModel.factory();
452
- bobsEntity.name = "Bob's Entity";
453
- bobsEntity.group = alice.group;
454
- expect(await bobsEntity.$save()).toEqual(true);
455
- expect(bobsEntity.guid).not.toBeNull();
456
- expect(bob.$is(bobsEntity.user)).toEqual(true);
457
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
458
- expect(testEntityBob).not.toBeNull();
459
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
460
- tilmeld.clearSession();
461
- await tilmeld.fillSession(alice);
462
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
463
- expect(testEntityAlice).not.toBeNull();
464
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
465
- if (testEntityAlice == null) {
466
- throw new Error();
467
- }
468
- testEntityAlice.name = "Haha! It's mine now!";
469
- try {
470
- await testEntityAlice.$save();
471
- throw new Error('It should have thrown AccessControlError.');
472
- }
473
- catch (e) {
474
- expect(e).toBeInstanceOf(AccessControlError);
475
- }
476
- expect(await testEntityAlice.$refresh()).toEqual(true);
477
- expect(testEntityAlice.name).toEqual("Bob's Entity");
478
- });
479
- it('alice can write to an entity her primary group has write permission for', async () => {
480
- const { bob, alice } = await makeUsers();
481
- await tilmeld.fillSession(bob);
482
- const bobsEntity = await TestModel.factory();
483
- bobsEntity.name = "Bob's Entity";
484
- bobsEntity.group = alice.group;
485
- bobsEntity.acGroup = TilmeldAccessLevels.WRITE_ACCESS;
486
- expect(await bobsEntity.$save()).toEqual(true);
487
- expect(bobsEntity.guid).not.toBeNull();
488
- expect(bob.$is(bobsEntity.user)).toEqual(true);
489
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
490
- expect(testEntityBob).not.toBeNull();
491
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
492
- tilmeld.clearSession();
493
- await tilmeld.fillSession(alice);
494
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
495
- expect(testEntityAlice).not.toBeNull();
496
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
497
- if (testEntityAlice == null) {
498
- throw new Error();
499
- }
500
- testEntityAlice.name = "Haha! It's mine now!";
501
- expect(await testEntityAlice.$save()).toEqual(true);
502
- expect(await testEntityAlice.$refresh()).toEqual(true);
503
- expect(testEntityAlice.name).toEqual("Haha! It's mine now!");
504
- });
505
- it("alice can't delete an entity her primary group doesn't have full permission for", async () => {
506
- const { bob, alice } = await makeUsers();
507
- await tilmeld.fillSession(bob);
508
- const bobsEntity = await TestModel.factory();
509
- bobsEntity.name = "Bob's Entity";
510
- bobsEntity.group = alice.group;
511
- bobsEntity.acGroup = TilmeldAccessLevels.WRITE_ACCESS;
512
- expect(await bobsEntity.$save()).toEqual(true);
513
- expect(bobsEntity.guid).not.toBeNull();
514
- expect(bob.$is(bobsEntity.user)).toEqual(true);
515
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
516
- expect(testEntityBob).not.toBeNull();
517
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
518
- tilmeld.clearSession();
519
- await tilmeld.fillSession(alice);
520
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
521
- expect(testEntityAlice).not.toBeNull();
522
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
523
- if (testEntityAlice == null) {
524
- throw new Error();
525
- }
526
- try {
527
- await testEntityAlice.$delete();
528
- throw new Error('It should have thrown AccessControlError.');
529
- }
530
- catch (e) {
531
- expect(e).toBeInstanceOf(AccessControlError);
532
- }
533
- expect(await testEntityAlice.$refresh()).toEqual(true);
534
- expect(testEntityAlice.guid).toEqual(bobsEntity.guid);
535
- });
536
- it('alice can delete an entity her primary group has full permission for', async () => {
537
- const { bob, alice } = await makeUsers();
538
- await tilmeld.fillSession(bob);
539
- const bobsEntity = await TestModel.factory();
540
- bobsEntity.name = "Bob's Entity";
541
- bobsEntity.group = alice.group;
542
- bobsEntity.acGroup = TilmeldAccessLevels.FULL_ACCESS;
543
- expect(await bobsEntity.$save()).toEqual(true);
544
- expect(bobsEntity.guid).not.toBeNull();
545
- expect(bob.$is(bobsEntity.user)).toEqual(true);
546
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
547
- expect(testEntityBob).not.toBeNull();
548
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
549
- tilmeld.clearSession();
550
- await tilmeld.fillSession(alice);
551
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
552
- expect(testEntityAlice).not.toBeNull();
553
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
554
- if (testEntityAlice == null) {
555
- throw new Error();
556
- }
557
- expect(await testEntityAlice.$delete()).toEqual(true);
558
- const verifyEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
559
- expect(verifyEntityAlice).toBeNull();
560
- });
561
- it('alice can access an entity everyone has permission for', async () => {
562
- const { bob, alice } = await makeUsers();
563
- await tilmeld.fillSession(bob);
564
- const bobsEntity = await TestModel.factory();
565
- bobsEntity.name = "Bob's Entity";
566
- bobsEntity.acOther = TilmeldAccessLevels.READ_ACCESS;
567
- expect(await bobsEntity.$save()).toEqual(true);
568
- expect(bobsEntity.guid).not.toBeNull();
569
- expect(bob.$is(bobsEntity.user)).toEqual(true);
570
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
571
- expect(testEntityBob).not.toBeNull();
572
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
573
- tilmeld.clearSession();
574
- await tilmeld.fillSession(alice);
575
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
576
- expect(testEntityAlice).not.toBeNull();
577
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
578
- });
579
- it("alice can't write to an entity everyone doesn't have write permission for", async () => {
580
- const { bob, alice } = await makeUsers();
581
- await tilmeld.fillSession(bob);
582
- const bobsEntity = await TestModel.factory();
583
- bobsEntity.name = "Bob's Entity";
584
- bobsEntity.acOther = TilmeldAccessLevels.READ_ACCESS;
585
- expect(await bobsEntity.$save()).toEqual(true);
586
- expect(bobsEntity.guid).not.toBeNull();
587
- expect(bob.$is(bobsEntity.user)).toEqual(true);
588
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
589
- expect(testEntityBob).not.toBeNull();
590
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
591
- tilmeld.clearSession();
592
- await tilmeld.fillSession(alice);
593
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
594
- expect(testEntityAlice).not.toBeNull();
595
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
596
- if (testEntityAlice == null) {
597
- throw new Error();
598
- }
599
- testEntityAlice.name = "Haha! It's mine now!";
600
- try {
601
- await testEntityAlice.$save();
602
- throw new Error('It should have thrown AccessControlError.');
603
- }
604
- catch (e) {
605
- expect(e).toBeInstanceOf(AccessControlError);
606
- }
607
- expect(await testEntityAlice.$refresh()).toEqual(true);
608
- expect(testEntityAlice.name).toEqual("Bob's Entity");
609
- });
610
- it('alice can write to an entity everyone has write permission for', async () => {
611
- const { bob, alice } = await makeUsers();
612
- await tilmeld.fillSession(bob);
613
- const bobsEntity = await TestModel.factory();
614
- bobsEntity.name = "Bob's Entity";
615
- bobsEntity.acOther = TilmeldAccessLevels.WRITE_ACCESS;
616
- expect(await bobsEntity.$save()).toEqual(true);
617
- expect(bobsEntity.guid).not.toBeNull();
618
- expect(bob.$is(bobsEntity.user)).toEqual(true);
619
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
620
- expect(testEntityBob).not.toBeNull();
621
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
622
- tilmeld.clearSession();
623
- await tilmeld.fillSession(alice);
624
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
625
- expect(testEntityAlice).not.toBeNull();
626
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
627
- if (testEntityAlice == null) {
628
- throw new Error();
629
- }
630
- testEntityAlice.name = "Haha! It's mine now!";
631
- expect(await testEntityAlice.$save()).toEqual(true);
632
- expect(await testEntityAlice.$refresh()).toEqual(true);
633
- expect(testEntityAlice.name).toEqual("Haha! It's mine now!");
634
- });
635
- it("alice can't delete an entity everyone doesn't have full permission for", async () => {
636
- const { bob, alice } = await makeUsers();
637
- await tilmeld.fillSession(bob);
638
- const bobsEntity = await TestModel.factory();
639
- bobsEntity.name = "Bob's Entity";
640
- bobsEntity.acOther = TilmeldAccessLevels.WRITE_ACCESS;
641
- expect(await bobsEntity.$save()).toEqual(true);
642
- expect(bobsEntity.guid).not.toBeNull();
643
- expect(bob.$is(bobsEntity.user)).toEqual(true);
644
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
645
- expect(testEntityBob).not.toBeNull();
646
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
647
- tilmeld.clearSession();
648
- await tilmeld.fillSession(alice);
649
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
650
- expect(testEntityAlice).not.toBeNull();
651
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
652
- if (testEntityAlice == null) {
653
- throw new Error();
654
- }
655
- try {
656
- await testEntityAlice.$delete();
657
- throw new Error('It should have thrown AccessControlError.');
658
- }
659
- catch (e) {
660
- expect(e).toBeInstanceOf(AccessControlError);
661
- }
662
- expect(await testEntityAlice.$refresh()).toEqual(true);
663
- expect(testEntityAlice.guid).toEqual(bobsEntity.guid);
664
- });
665
- it('alice can delete an entity everyone has full permission for', async () => {
666
- const { bob, alice } = await makeUsers();
667
- await tilmeld.fillSession(bob);
668
- const bobsEntity = await TestModel.factory();
669
- bobsEntity.name = "Bob's Entity";
670
- bobsEntity.acOther = TilmeldAccessLevels.FULL_ACCESS;
671
- expect(await bobsEntity.$save()).toEqual(true);
672
- expect(bobsEntity.guid).not.toBeNull();
673
- expect(bob.$is(bobsEntity.user)).toEqual(true);
674
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
675
- expect(testEntityBob).not.toBeNull();
676
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
677
- tilmeld.clearSession();
678
- await tilmeld.fillSession(alice);
679
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
680
- expect(testEntityAlice).not.toBeNull();
681
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
682
- if (testEntityAlice == null) {
683
- throw new Error();
684
- }
685
- expect(await testEntityAlice.$delete()).toEqual(true);
686
- const verifyEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
687
- expect(verifyEntityAlice).toBeNull();
688
- });
689
- it('a clear session can delete an entity everyone has full permission for', async () => {
690
- const { bob } = await makeUsers();
691
- await tilmeld.fillSession(bob);
692
- const bobsEntity = await TestModel.factory();
693
- bobsEntity.name = "Bob's Entity";
694
- bobsEntity.acOther = TilmeldAccessLevels.FULL_ACCESS;
695
- expect(await bobsEntity.$save()).toEqual(true);
696
- expect(bobsEntity.guid).not.toBeNull();
697
- expect(bob.$is(bobsEntity.user)).toEqual(true);
698
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
699
- expect(testEntityBob).not.toBeNull();
700
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
701
- tilmeld.clearSession();
702
- const testEntityNobody = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
703
- expect(testEntityNobody).not.toBeNull();
704
- expect(testEntityNobody?.guid).toEqual(bobsEntity.guid);
705
- if (testEntityNobody == null) {
706
- throw new Error();
707
- }
708
- expect(await testEntityNobody.$delete()).toEqual(true);
709
- const verifyEntityNobody = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
710
- expect(verifyEntityNobody).toBeNull();
711
- });
712
- it('allows skip ac deletions', async () => {
713
- const allEntities = [
714
- ...(await nymph.getEntities({ class: TestModel, skipAc: true })),
715
- ...(await nymph.getEntities({ class: User, skipAc: true })),
716
- ...(await nymph.getEntities({ class: Group, skipAc: true })),
717
- ];
718
- for (let entity of allEntities) {
719
- expect(await entity.$deleteSkipAC()).toEqual(true);
720
- }
721
- });
722
- it('entity can be retrieved by queries on ac properties', async () => {
723
- const { bob, alice } = await makeUsers();
724
- await tilmeld.fillSession(bob);
725
- const bobsEntity = await TestModel.factory();
726
- bobsEntity.name = "Bob's Entity";
727
- bobsEntity.acRead = [
728
- alice.guid ?? '',
729
- bob.guid ?? '',
730
- ...(bob.group ? [bob.group.guid ?? ''] : []),
731
- ];
732
- bobsEntity.acWrite = [alice.guid ?? '', bob.guid ?? ''];
733
- bobsEntity.acFull = [bob.guid ?? ''];
734
- expect(await bobsEntity.$save()).toEqual(true);
735
- expect(bobsEntity.guid).not.toBeNull();
736
- expect(bob.$is(bobsEntity.user)).toEqual(true);
737
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
738
- expect(testEntityBob).not.toBeNull();
739
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
740
- tilmeld.clearSession();
741
- await tilmeld.fillSession(alice);
742
- // Equals queries.
743
- let testEntityAlice = await nymph.getEntity({ class: TestModel }, {
744
- type: '&',
745
- guid: bobsEntity.guid || '',
746
- equal: ['user', bob.guid],
747
- });
748
- expect(testEntityAlice).not.toBeNull();
749
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
750
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
751
- type: '&',
752
- guid: bobsEntity.guid || '',
753
- equal: ['user', '000000000000000000000000'],
161
+ it('user ownership, group ownership, and access controls are added on entities', async () => {
162
+ const { bob } = await makeUsers();
163
+ await tilmeld.fillSession(bob);
164
+ const bobsEntity = await TestModel.factory();
165
+ bobsEntity.name = "Bob's Entity";
166
+ expect(await bobsEntity.$save()).toEqual(true);
167
+ expect(bobsEntity.guid).not.toBeNull();
168
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
169
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
170
+ expect(testEntityBob).not.toBeNull();
171
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
172
+ expect(testEntityBob?.user?.$is(bob)).toEqual(true);
173
+ expect(testEntityBob?.group?.$is(bob.group)).toEqual(true);
174
+ expect(testEntityBob?.acUser).toEqual(TilmeldAccessLevels.FULL_ACCESS);
175
+ expect(testEntityBob?.acGroup).toEqual(TilmeldAccessLevels.READ_ACCESS);
176
+ expect(testEntityBob?.acOther).toEqual(TilmeldAccessLevels.NO_ACCESS);
177
+ expect(testEntityBob?.acRead).toEqual([]);
178
+ expect(testEntityBob?.acWrite).toEqual([]);
179
+ expect(testEntityBob?.acFull).toEqual([]);
754
180
  });
755
- expect(testEntityAlice).toBeNull();
756
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
757
- type: '&',
758
- guid: bobsEntity.guid || '',
759
- equal: ['group', bob.group?.guid],
181
+ it("alice can't access an entity owned by bob", async () => {
182
+ const { bob, alice } = await makeUsers();
183
+ await tilmeld.fillSession(bob);
184
+ const bobsEntity = await TestModel.factory();
185
+ bobsEntity.name = "Bob's Entity";
186
+ expect(await bobsEntity.$save()).toEqual(true);
187
+ expect(bobsEntity.guid).not.toBeNull();
188
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
189
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
190
+ expect(testEntityBob).not.toBeNull();
191
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
192
+ tilmeld.clearSession();
193
+ await tilmeld.fillSession(alice);
194
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
195
+ expect(testEntityAlice).toBeNull();
760
196
  });
761
- expect(testEntityAlice).not.toBeNull();
762
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
763
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
764
- type: '&',
765
- guid: bobsEntity.guid || '',
766
- equal: ['group', '000000000000000000000000'],
197
+ it("alice can't write to an entity she doesn't have write permission for", async () => {
198
+ const { bob, alice } = await makeUsers();
199
+ await tilmeld.fillSession(bob);
200
+ const bobsEntity = await TestModel.factory();
201
+ bobsEntity.name = "Bob's Entity";
202
+ bobsEntity.acRead = [alice.guid ?? ''];
203
+ expect(await bobsEntity.$save()).toEqual(true);
204
+ expect(bobsEntity.guid).not.toBeNull();
205
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
206
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
207
+ expect(testEntityBob).not.toBeNull();
208
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
209
+ tilmeld.clearSession();
210
+ await tilmeld.fillSession(alice);
211
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
212
+ expect(testEntityAlice).not.toBeNull();
213
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
214
+ if (testEntityAlice == null) {
215
+ throw new Error();
216
+ }
217
+ testEntityAlice.name = "Haha! It's mine now!";
218
+ try {
219
+ await testEntityAlice.$save();
220
+ throw new Error('It should have thrown AccessControlError.');
221
+ }
222
+ catch (e) {
223
+ expect(e).toBeInstanceOf(AccessControlError);
224
+ }
225
+ expect(await testEntityAlice.$refresh()).toEqual(true);
226
+ expect(testEntityAlice.name).toEqual("Bob's Entity");
767
227
  });
768
- expect(testEntityAlice).toBeNull();
769
- // Contains queries.
770
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
771
- type: '&',
772
- guid: bobsEntity.guid || '',
773
- contain: ['user', bob.guid],
228
+ it('alice can write to an entity she has write permission for', async () => {
229
+ const { bob, alice } = await makeUsers();
230
+ await tilmeld.fillSession(bob);
231
+ const bobsEntity = await TestModel.factory();
232
+ bobsEntity.name = "Bob's Entity";
233
+ bobsEntity.acWrite = [alice.guid ?? ''];
234
+ expect(await bobsEntity.$save()).toEqual(true);
235
+ expect(bobsEntity.guid).not.toBeNull();
236
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
237
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
238
+ expect(testEntityBob).not.toBeNull();
239
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
240
+ tilmeld.clearSession();
241
+ await tilmeld.fillSession(alice);
242
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
243
+ expect(testEntityAlice).not.toBeNull();
244
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
245
+ if (testEntityAlice == null) {
246
+ throw new Error();
247
+ }
248
+ testEntityAlice.name = "Haha! It's mine now!";
249
+ expect(await testEntityAlice.$save()).toEqual(true);
250
+ expect(await testEntityAlice.$refresh()).toEqual(true);
251
+ expect(testEntityAlice.name).toEqual("Haha! It's mine now!");
774
252
  });
775
- expect(testEntityAlice).not.toBeNull();
776
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
777
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
778
- type: '&',
779
- guid: bobsEntity.guid || '',
780
- contain: ['user', '000000000000000000000000'],
253
+ it("alice can't delete an entity she doesn't have full permission for", async () => {
254
+ const { bob, alice } = await makeUsers();
255
+ await tilmeld.fillSession(bob);
256
+ const bobsEntity = await TestModel.factory();
257
+ bobsEntity.name = "Bob's Entity";
258
+ bobsEntity.acWrite = [alice.guid ?? ''];
259
+ expect(await bobsEntity.$save()).toEqual(true);
260
+ expect(bobsEntity.guid).not.toBeNull();
261
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
262
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
263
+ expect(testEntityBob).not.toBeNull();
264
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
265
+ tilmeld.clearSession();
266
+ await tilmeld.fillSession(alice);
267
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
268
+ expect(testEntityAlice).not.toBeNull();
269
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
270
+ if (testEntityAlice == null) {
271
+ throw new Error();
272
+ }
273
+ try {
274
+ await testEntityAlice.$delete();
275
+ throw new Error('It should have thrown AccessControlError.');
276
+ }
277
+ catch (e) {
278
+ expect(e).toBeInstanceOf(AccessControlError);
279
+ }
280
+ expect(await testEntityAlice.$refresh()).toEqual(true);
281
+ expect(testEntityAlice.guid).toEqual(bobsEntity.guid);
781
282
  });
782
- expect(testEntityAlice).toBeNull();
783
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
784
- type: '&',
785
- guid: bobsEntity.guid || '',
786
- contain: ['group', bob.group?.guid],
283
+ it('alice can delete an entity she has full permission for', async () => {
284
+ const { bob, alice } = await makeUsers();
285
+ await tilmeld.fillSession(bob);
286
+ const bobsEntity = await TestModel.factory();
287
+ bobsEntity.name = "Bob's Entity";
288
+ bobsEntity.acFull = [alice.guid ?? ''];
289
+ expect(await bobsEntity.$save()).toEqual(true);
290
+ expect(bobsEntity.guid).not.toBeNull();
291
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
292
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
293
+ expect(testEntityBob).not.toBeNull();
294
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
295
+ tilmeld.clearSession();
296
+ await tilmeld.fillSession(alice);
297
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
298
+ expect(testEntityAlice).not.toBeNull();
299
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
300
+ if (testEntityAlice == null) {
301
+ throw new Error();
302
+ }
303
+ expect(await testEntityAlice.$delete()).toEqual(true);
304
+ const verifyEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
305
+ expect(verifyEntityAlice).toBeNull();
787
306
  });
788
- expect(testEntityAlice).not.toBeNull();
789
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
790
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
791
- type: '&',
792
- guid: bobsEntity.guid || '',
793
- contain: ['group', '000000000000000000000000'],
307
+ it('alice can access an entity her group has permission for', async () => {
308
+ const { bob, alice, abgroup } = await makeUsers();
309
+ await tilmeld.fillSession(bob);
310
+ const bobsEntity = await TestModel.factory();
311
+ bobsEntity.name = "Bob's Entity";
312
+ bobsEntity.acRead = [abgroup.guid ?? ''];
313
+ expect(await bobsEntity.$save()).toEqual(true);
314
+ expect(bobsEntity.guid).not.toBeNull();
315
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
316
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
317
+ expect(testEntityBob).not.toBeNull();
318
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
319
+ tilmeld.clearSession();
320
+ await tilmeld.fillSession(alice);
321
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
322
+ expect(testEntityAlice).not.toBeNull();
323
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
794
324
  });
795
- expect(testEntityAlice).toBeNull();
796
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
797
- type: '&',
798
- guid: bobsEntity.guid || '',
799
- contain: ['acRead', alice.guid],
325
+ it("alice can't write to an entity her group doesn't have write permission for", async () => {
326
+ const { bob, alice, abgroup } = await makeUsers();
327
+ await tilmeld.fillSession(bob);
328
+ const bobsEntity = await TestModel.factory();
329
+ bobsEntity.name = "Bob's Entity";
330
+ bobsEntity.acRead = [abgroup.guid ?? ''];
331
+ expect(await bobsEntity.$save()).toEqual(true);
332
+ expect(bobsEntity.guid).not.toBeNull();
333
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
334
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
335
+ expect(testEntityBob).not.toBeNull();
336
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
337
+ tilmeld.clearSession();
338
+ await tilmeld.fillSession(alice);
339
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
340
+ expect(testEntityAlice).not.toBeNull();
341
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
342
+ if (testEntityAlice == null) {
343
+ throw new Error();
344
+ }
345
+ testEntityAlice.name = "Haha! It's mine now!";
346
+ try {
347
+ await testEntityAlice.$save();
348
+ throw new Error('It should have thrown AccessControlError.');
349
+ }
350
+ catch (e) {
351
+ expect(e).toBeInstanceOf(AccessControlError);
352
+ }
353
+ expect(await testEntityAlice.$refresh()).toEqual(true);
354
+ expect(testEntityAlice.name).toEqual("Bob's Entity");
800
355
  });
801
- expect(testEntityAlice).not.toBeNull();
802
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
803
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
804
- type: '&',
805
- guid: bobsEntity.guid || '',
806
- contain: ['acRead', '000000000000000000000000'],
356
+ it('alice can write to an entity her group has write permission for', async () => {
357
+ const { bob, alice, abgroup } = await makeUsers();
358
+ await tilmeld.fillSession(bob);
359
+ const bobsEntity = await TestModel.factory();
360
+ bobsEntity.name = "Bob's Entity";
361
+ bobsEntity.acWrite = [abgroup.guid ?? ''];
362
+ expect(await bobsEntity.$save()).toEqual(true);
363
+ expect(bobsEntity.guid).not.toBeNull();
364
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
365
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
366
+ expect(testEntityBob).not.toBeNull();
367
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
368
+ tilmeld.clearSession();
369
+ await tilmeld.fillSession(alice);
370
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
371
+ expect(testEntityAlice).not.toBeNull();
372
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
373
+ if (testEntityAlice == null) {
374
+ throw new Error();
375
+ }
376
+ testEntityAlice.name = "Haha! It's mine now!";
377
+ expect(await testEntityAlice.$save()).toEqual(true);
378
+ expect(await testEntityAlice.$refresh()).toEqual(true);
379
+ expect(testEntityAlice.name).toEqual("Haha! It's mine now!");
807
380
  });
808
- expect(testEntityAlice).toBeNull();
809
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
810
- type: '&',
811
- guid: bobsEntity.guid || '',
812
- contain: ['acWrite', bob.guid],
381
+ it("alice can't delete an entity her group doesn't have full permission for", async () => {
382
+ const { bob, alice, abgroup } = await makeUsers();
383
+ await tilmeld.fillSession(bob);
384
+ const bobsEntity = await TestModel.factory();
385
+ bobsEntity.name = "Bob's Entity";
386
+ bobsEntity.acWrite = [abgroup.guid ?? ''];
387
+ expect(await bobsEntity.$save()).toEqual(true);
388
+ expect(bobsEntity.guid).not.toBeNull();
389
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
390
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
391
+ expect(testEntityBob).not.toBeNull();
392
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
393
+ tilmeld.clearSession();
394
+ await tilmeld.fillSession(alice);
395
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
396
+ expect(testEntityAlice).not.toBeNull();
397
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
398
+ if (testEntityAlice == null) {
399
+ throw new Error();
400
+ }
401
+ try {
402
+ await testEntityAlice.$delete();
403
+ throw new Error('It should have thrown AccessControlError.');
404
+ }
405
+ catch (e) {
406
+ expect(e).toBeInstanceOf(AccessControlError);
407
+ }
408
+ expect(await testEntityAlice.$refresh()).toEqual(true);
409
+ expect(testEntityAlice.guid).toEqual(bobsEntity.guid);
813
410
  });
814
- expect(testEntityAlice).not.toBeNull();
815
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
816
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
817
- type: '&',
818
- guid: bobsEntity.guid || '',
819
- contain: ['acWrite', '000000000000000000000000'],
411
+ it('alice can delete an entity her group has full permission for', async () => {
412
+ const { bob, alice, abgroup } = await makeUsers();
413
+ await tilmeld.fillSession(bob);
414
+ const bobsEntity = await TestModel.factory();
415
+ bobsEntity.name = "Bob's Entity";
416
+ bobsEntity.acFull = [abgroup.guid ?? ''];
417
+ expect(await bobsEntity.$save()).toEqual(true);
418
+ expect(bobsEntity.guid).not.toBeNull();
419
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
420
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
421
+ expect(testEntityBob).not.toBeNull();
422
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
423
+ tilmeld.clearSession();
424
+ await tilmeld.fillSession(alice);
425
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
426
+ expect(testEntityAlice).not.toBeNull();
427
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
428
+ if (testEntityAlice == null) {
429
+ throw new Error();
430
+ }
431
+ expect(await testEntityAlice.$delete()).toEqual(true);
432
+ const verifyEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
433
+ expect(verifyEntityAlice).toBeNull();
820
434
  });
821
- expect(testEntityAlice).toBeNull();
822
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
823
- type: '&',
824
- guid: bobsEntity.guid || '',
825
- contain: ['acFull', bob.guid],
435
+ it('alice can access an entity her primary group has permission for', async () => {
436
+ const { bob, alice } = await makeUsers();
437
+ await tilmeld.fillSession(bob);
438
+ const bobsEntity = await TestModel.factory();
439
+ bobsEntity.name = "Bob's Entity";
440
+ bobsEntity.group = alice.group;
441
+ expect(await bobsEntity.$save()).toEqual(true);
442
+ expect(bobsEntity.guid).not.toBeNull();
443
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
444
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
445
+ expect(testEntityBob).not.toBeNull();
446
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
447
+ tilmeld.clearSession();
448
+ await tilmeld.fillSession(alice);
449
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
450
+ expect(testEntityAlice).not.toBeNull();
451
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
826
452
  });
827
- expect(testEntityAlice).not.toBeNull();
828
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
829
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
830
- type: '&',
831
- guid: bobsEntity.guid || '',
832
- contain: ['acFull', '000000000000000000000000'],
453
+ it("alice can't write to an entity her primary group doesn't have write permission for", async () => {
454
+ const { bob, alice } = await makeUsers();
455
+ await tilmeld.fillSession(bob);
456
+ const bobsEntity = await TestModel.factory();
457
+ bobsEntity.name = "Bob's Entity";
458
+ bobsEntity.group = alice.group;
459
+ expect(await bobsEntity.$save()).toEqual(true);
460
+ expect(bobsEntity.guid).not.toBeNull();
461
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
462
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
463
+ expect(testEntityBob).not.toBeNull();
464
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
465
+ tilmeld.clearSession();
466
+ await tilmeld.fillSession(alice);
467
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
468
+ expect(testEntityAlice).not.toBeNull();
469
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
470
+ if (testEntityAlice == null) {
471
+ throw new Error();
472
+ }
473
+ testEntityAlice.name = "Haha! It's mine now!";
474
+ try {
475
+ await testEntityAlice.$save();
476
+ throw new Error('It should have thrown AccessControlError.');
477
+ }
478
+ catch (e) {
479
+ expect(e).toBeInstanceOf(AccessControlError);
480
+ }
481
+ expect(await testEntityAlice.$refresh()).toEqual(true);
482
+ expect(testEntityAlice.name).toEqual("Bob's Entity");
833
483
  });
834
- expect(testEntityAlice).toBeNull();
835
- // Ref queries.
836
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
837
- type: '&',
838
- guid: bobsEntity.guid || '',
839
- ref: ['user', bob],
484
+ it('alice can write to an entity her primary group has write permission for', async () => {
485
+ const { bob, alice } = await makeUsers();
486
+ await tilmeld.fillSession(bob);
487
+ const bobsEntity = await TestModel.factory();
488
+ bobsEntity.name = "Bob's Entity";
489
+ bobsEntity.group = alice.group;
490
+ bobsEntity.acGroup = TilmeldAccessLevels.WRITE_ACCESS;
491
+ expect(await bobsEntity.$save()).toEqual(true);
492
+ expect(bobsEntity.guid).not.toBeNull();
493
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
494
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
495
+ expect(testEntityBob).not.toBeNull();
496
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
497
+ tilmeld.clearSession();
498
+ await tilmeld.fillSession(alice);
499
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
500
+ expect(testEntityAlice).not.toBeNull();
501
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
502
+ if (testEntityAlice == null) {
503
+ throw new Error();
504
+ }
505
+ testEntityAlice.name = "Haha! It's mine now!";
506
+ expect(await testEntityAlice.$save()).toEqual(true);
507
+ expect(await testEntityAlice.$refresh()).toEqual(true);
508
+ expect(testEntityAlice.name).toEqual("Haha! It's mine now!");
840
509
  });
841
- expect(testEntityAlice).not.toBeNull();
842
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
843
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
844
- type: '&',
845
- guid: bobsEntity.guid || '',
846
- ref: ['user', '000000000000000000000000'],
510
+ it("alice can't delete an entity her primary group doesn't have full permission for", async () => {
511
+ const { bob, alice } = await makeUsers();
512
+ await tilmeld.fillSession(bob);
513
+ const bobsEntity = await TestModel.factory();
514
+ bobsEntity.name = "Bob's Entity";
515
+ bobsEntity.group = alice.group;
516
+ bobsEntity.acGroup = TilmeldAccessLevels.WRITE_ACCESS;
517
+ expect(await bobsEntity.$save()).toEqual(true);
518
+ expect(bobsEntity.guid).not.toBeNull();
519
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
520
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
521
+ expect(testEntityBob).not.toBeNull();
522
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
523
+ tilmeld.clearSession();
524
+ await tilmeld.fillSession(alice);
525
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
526
+ expect(testEntityAlice).not.toBeNull();
527
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
528
+ if (testEntityAlice == null) {
529
+ throw new Error();
530
+ }
531
+ try {
532
+ await testEntityAlice.$delete();
533
+ throw new Error('It should have thrown AccessControlError.');
534
+ }
535
+ catch (e) {
536
+ expect(e).toBeInstanceOf(AccessControlError);
537
+ }
538
+ expect(await testEntityAlice.$refresh()).toEqual(true);
539
+ expect(testEntityAlice.guid).toEqual(bobsEntity.guid);
847
540
  });
848
- expect(testEntityAlice).toBeNull();
849
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
850
- type: '&',
851
- guid: bobsEntity.guid || '',
852
- ref: ['group', bob.group ?? ''],
541
+ it('alice can delete an entity her primary group has full permission for', async () => {
542
+ const { bob, alice } = await makeUsers();
543
+ await tilmeld.fillSession(bob);
544
+ const bobsEntity = await TestModel.factory();
545
+ bobsEntity.name = "Bob's Entity";
546
+ bobsEntity.group = alice.group;
547
+ bobsEntity.acGroup = TilmeldAccessLevels.FULL_ACCESS;
548
+ expect(await bobsEntity.$save()).toEqual(true);
549
+ expect(bobsEntity.guid).not.toBeNull();
550
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
551
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
552
+ expect(testEntityBob).not.toBeNull();
553
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
554
+ tilmeld.clearSession();
555
+ await tilmeld.fillSession(alice);
556
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
557
+ expect(testEntityAlice).not.toBeNull();
558
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
559
+ if (testEntityAlice == null) {
560
+ throw new Error();
561
+ }
562
+ expect(await testEntityAlice.$delete()).toEqual(true);
563
+ const verifyEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
564
+ expect(verifyEntityAlice).toBeNull();
853
565
  });
854
- expect(testEntityAlice).not.toBeNull();
855
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
856
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
857
- type: '&',
858
- guid: bobsEntity.guid || '',
859
- ref: ['group', '000000000000000000000000'],
566
+ it('alice can access an entity everyone has permission for', async () => {
567
+ const { bob, alice } = await makeUsers();
568
+ await tilmeld.fillSession(bob);
569
+ const bobsEntity = await TestModel.factory();
570
+ bobsEntity.name = "Bob's Entity";
571
+ bobsEntity.acOther = TilmeldAccessLevels.READ_ACCESS;
572
+ expect(await bobsEntity.$save()).toEqual(true);
573
+ expect(bobsEntity.guid).not.toBeNull();
574
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
575
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
576
+ expect(testEntityBob).not.toBeNull();
577
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
578
+ tilmeld.clearSession();
579
+ await tilmeld.fillSession(alice);
580
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
581
+ expect(testEntityAlice).not.toBeNull();
582
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
860
583
  });
861
- expect(testEntityAlice).toBeNull();
862
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
863
- type: '&',
864
- guid: bobsEntity.guid || '',
865
- ref: ['acRead', alice],
584
+ it("alice can't write to an entity everyone doesn't have write permission for", async () => {
585
+ const { bob, alice } = await makeUsers();
586
+ await tilmeld.fillSession(bob);
587
+ const bobsEntity = await TestModel.factory();
588
+ bobsEntity.name = "Bob's Entity";
589
+ bobsEntity.acOther = TilmeldAccessLevels.READ_ACCESS;
590
+ expect(await bobsEntity.$save()).toEqual(true);
591
+ expect(bobsEntity.guid).not.toBeNull();
592
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
593
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
594
+ expect(testEntityBob).not.toBeNull();
595
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
596
+ tilmeld.clearSession();
597
+ await tilmeld.fillSession(alice);
598
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
599
+ expect(testEntityAlice).not.toBeNull();
600
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
601
+ if (testEntityAlice == null) {
602
+ throw new Error();
603
+ }
604
+ testEntityAlice.name = "Haha! It's mine now!";
605
+ try {
606
+ await testEntityAlice.$save();
607
+ throw new Error('It should have thrown AccessControlError.');
608
+ }
609
+ catch (e) {
610
+ expect(e).toBeInstanceOf(AccessControlError);
611
+ }
612
+ expect(await testEntityAlice.$refresh()).toEqual(true);
613
+ expect(testEntityAlice.name).toEqual("Bob's Entity");
866
614
  });
867
- expect(testEntityAlice).not.toBeNull();
868
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
869
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
870
- type: '&',
871
- guid: bobsEntity.guid || '',
872
- ref: ['acRead', '000000000000000000000000'],
615
+ it('alice can write to an entity everyone has write permission for', async () => {
616
+ const { bob, alice } = await makeUsers();
617
+ await tilmeld.fillSession(bob);
618
+ const bobsEntity = await TestModel.factory();
619
+ bobsEntity.name = "Bob's Entity";
620
+ bobsEntity.acOther = TilmeldAccessLevels.WRITE_ACCESS;
621
+ expect(await bobsEntity.$save()).toEqual(true);
622
+ expect(bobsEntity.guid).not.toBeNull();
623
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
624
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
625
+ expect(testEntityBob).not.toBeNull();
626
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
627
+ tilmeld.clearSession();
628
+ await tilmeld.fillSession(alice);
629
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
630
+ expect(testEntityAlice).not.toBeNull();
631
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
632
+ if (testEntityAlice == null) {
633
+ throw new Error();
634
+ }
635
+ testEntityAlice.name = "Haha! It's mine now!";
636
+ expect(await testEntityAlice.$save()).toEqual(true);
637
+ expect(await testEntityAlice.$refresh()).toEqual(true);
638
+ expect(testEntityAlice.name).toEqual("Haha! It's mine now!");
873
639
  });
874
- expect(testEntityAlice).toBeNull();
875
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
876
- type: '&',
877
- guid: bobsEntity.guid || '',
878
- ref: ['acWrite', bob],
640
+ it("alice can't delete an entity everyone doesn't have full permission for", async () => {
641
+ const { bob, alice } = await makeUsers();
642
+ await tilmeld.fillSession(bob);
643
+ const bobsEntity = await TestModel.factory();
644
+ bobsEntity.name = "Bob's Entity";
645
+ bobsEntity.acOther = TilmeldAccessLevels.WRITE_ACCESS;
646
+ expect(await bobsEntity.$save()).toEqual(true);
647
+ expect(bobsEntity.guid).not.toBeNull();
648
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
649
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
650
+ expect(testEntityBob).not.toBeNull();
651
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
652
+ tilmeld.clearSession();
653
+ await tilmeld.fillSession(alice);
654
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
655
+ expect(testEntityAlice).not.toBeNull();
656
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
657
+ if (testEntityAlice == null) {
658
+ throw new Error();
659
+ }
660
+ try {
661
+ await testEntityAlice.$delete();
662
+ throw new Error('It should have thrown AccessControlError.');
663
+ }
664
+ catch (e) {
665
+ expect(e).toBeInstanceOf(AccessControlError);
666
+ }
667
+ expect(await testEntityAlice.$refresh()).toEqual(true);
668
+ expect(testEntityAlice.guid).toEqual(bobsEntity.guid);
879
669
  });
880
- expect(testEntityAlice).not.toBeNull();
881
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
882
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
883
- type: '&',
884
- guid: bobsEntity.guid || '',
885
- ref: ['acWrite', '000000000000000000000000'],
670
+ it('alice can delete an entity everyone has full permission for', async () => {
671
+ const { bob, alice } = await makeUsers();
672
+ await tilmeld.fillSession(bob);
673
+ const bobsEntity = await TestModel.factory();
674
+ bobsEntity.name = "Bob's Entity";
675
+ bobsEntity.acOther = TilmeldAccessLevels.FULL_ACCESS;
676
+ expect(await bobsEntity.$save()).toEqual(true);
677
+ expect(bobsEntity.guid).not.toBeNull();
678
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
679
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
680
+ expect(testEntityBob).not.toBeNull();
681
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
682
+ tilmeld.clearSession();
683
+ await tilmeld.fillSession(alice);
684
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
685
+ expect(testEntityAlice).not.toBeNull();
686
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
687
+ if (testEntityAlice == null) {
688
+ throw new Error();
689
+ }
690
+ expect(await testEntityAlice.$delete()).toEqual(true);
691
+ const verifyEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
692
+ expect(verifyEntityAlice).toBeNull();
886
693
  });
887
- expect(testEntityAlice).toBeNull();
888
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
889
- type: '&',
890
- guid: bobsEntity.guid || '',
891
- ref: ['acFull', bob],
694
+ it('a clear session can delete an entity everyone has full permission for', async () => {
695
+ const { bob } = await makeUsers();
696
+ await tilmeld.fillSession(bob);
697
+ const bobsEntity = await TestModel.factory();
698
+ bobsEntity.name = "Bob's Entity";
699
+ bobsEntity.acOther = TilmeldAccessLevels.FULL_ACCESS;
700
+ expect(await bobsEntity.$save()).toEqual(true);
701
+ expect(bobsEntity.guid).not.toBeNull();
702
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
703
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
704
+ expect(testEntityBob).not.toBeNull();
705
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
706
+ tilmeld.clearSession();
707
+ const testEntityNobody = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
708
+ expect(testEntityNobody).not.toBeNull();
709
+ expect(testEntityNobody?.guid).toEqual(bobsEntity.guid);
710
+ if (testEntityNobody == null) {
711
+ throw new Error();
712
+ }
713
+ expect(await testEntityNobody.$delete()).toEqual(true);
714
+ const verifyEntityNobody = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
715
+ expect(verifyEntityNobody).toBeNull();
892
716
  });
893
- expect(testEntityAlice).not.toBeNull();
894
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
895
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
896
- type: '&',
897
- guid: bobsEntity.guid || '',
898
- ref: ['acFull', '000000000000000000000000'],
717
+ it('allows skip ac deletions', async () => {
718
+ const allEntities = [
719
+ ...(await nymph.getEntities({ class: TestModel, skipAc: true })),
720
+ ...(await nymph.getEntities({ class: User, skipAc: true })),
721
+ ...(await nymph.getEntities({ class: Group, skipAc: true })),
722
+ ];
723
+ for (let entity of allEntities) {
724
+ expect(await entity.$deleteSkipAC()).toEqual(true);
725
+ }
899
726
  });
900
- expect(testEntityAlice).toBeNull();
901
- // QRef queries.
902
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
903
- type: '&',
904
- guid: bobsEntity.guid || '',
905
- qref: [
906
- 'user',
907
- [
908
- { class: User },
909
- { type: '&', equal: ['username', bob.username ?? ''] },
727
+ it('entity can be retrieved by queries on ac properties', async () => {
728
+ const { bob, alice } = await makeUsers();
729
+ await tilmeld.fillSession(bob);
730
+ const bobsEntity = await TestModel.factory();
731
+ bobsEntity.name = "Bob's Entity";
732
+ bobsEntity.acRead = [
733
+ alice.guid ?? '',
734
+ bob.guid ?? '',
735
+ ...(bob.group ? [bob.group.guid ?? ''] : []),
736
+ ];
737
+ bobsEntity.acWrite = [alice.guid ?? '', bob.guid ?? ''];
738
+ bobsEntity.acFull = [bob.guid ?? ''];
739
+ expect(await bobsEntity.$save()).toEqual(true);
740
+ expect(bobsEntity.guid).not.toBeNull();
741
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
742
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
743
+ expect(testEntityBob).not.toBeNull();
744
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
745
+ tilmeld.clearSession();
746
+ await tilmeld.fillSession(alice);
747
+ // Equals queries.
748
+ let testEntityAlice = await nymph.getEntity({ class: TestModel }, {
749
+ type: '&',
750
+ guid: bobsEntity.guid || '',
751
+ equal: ['user', bob.guid],
752
+ });
753
+ expect(testEntityAlice).not.toBeNull();
754
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
755
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
756
+ type: '&',
757
+ guid: bobsEntity.guid || '',
758
+ equal: ['user', '000000000000000000000000'],
759
+ });
760
+ expect(testEntityAlice).toBeNull();
761
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
762
+ type: '&',
763
+ guid: bobsEntity.guid || '',
764
+ equal: ['group', bob.group?.guid],
765
+ });
766
+ expect(testEntityAlice).not.toBeNull();
767
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
768
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
769
+ type: '&',
770
+ guid: bobsEntity.guid || '',
771
+ equal: ['group', '000000000000000000000000'],
772
+ });
773
+ expect(testEntityAlice).toBeNull();
774
+ // Contains queries.
775
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
776
+ type: '&',
777
+ guid: bobsEntity.guid || '',
778
+ contain: ['user', bob.guid],
779
+ });
780
+ expect(testEntityAlice).not.toBeNull();
781
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
782
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
783
+ type: '&',
784
+ guid: bobsEntity.guid || '',
785
+ contain: ['user', '000000000000000000000000'],
786
+ });
787
+ expect(testEntityAlice).toBeNull();
788
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
789
+ type: '&',
790
+ guid: bobsEntity.guid || '',
791
+ contain: ['group', bob.group?.guid],
792
+ });
793
+ expect(testEntityAlice).not.toBeNull();
794
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
795
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
796
+ type: '&',
797
+ guid: bobsEntity.guid || '',
798
+ contain: ['group', '000000000000000000000000'],
799
+ });
800
+ expect(testEntityAlice).toBeNull();
801
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
802
+ type: '&',
803
+ guid: bobsEntity.guid || '',
804
+ contain: ['acRead', alice.guid],
805
+ });
806
+ expect(testEntityAlice).not.toBeNull();
807
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
808
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
809
+ type: '&',
810
+ guid: bobsEntity.guid || '',
811
+ contain: ['acRead', '000000000000000000000000'],
812
+ });
813
+ expect(testEntityAlice).toBeNull();
814
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
815
+ type: '&',
816
+ guid: bobsEntity.guid || '',
817
+ contain: ['acWrite', bob.guid],
818
+ });
819
+ expect(testEntityAlice).not.toBeNull();
820
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
821
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
822
+ type: '&',
823
+ guid: bobsEntity.guid || '',
824
+ contain: ['acWrite', '000000000000000000000000'],
825
+ });
826
+ expect(testEntityAlice).toBeNull();
827
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
828
+ type: '&',
829
+ guid: bobsEntity.guid || '',
830
+ contain: ['acFull', bob.guid],
831
+ });
832
+ expect(testEntityAlice).not.toBeNull();
833
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
834
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
835
+ type: '&',
836
+ guid: bobsEntity.guid || '',
837
+ contain: ['acFull', '000000000000000000000000'],
838
+ });
839
+ expect(testEntityAlice).toBeNull();
840
+ // Ref queries.
841
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
842
+ type: '&',
843
+ guid: bobsEntity.guid || '',
844
+ ref: ['user', bob],
845
+ });
846
+ expect(testEntityAlice).not.toBeNull();
847
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
848
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
849
+ type: '&',
850
+ guid: bobsEntity.guid || '',
851
+ ref: ['user', '000000000000000000000000'],
852
+ });
853
+ expect(testEntityAlice).toBeNull();
854
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
855
+ type: '&',
856
+ guid: bobsEntity.guid || '',
857
+ ref: ['group', bob.group ?? ''],
858
+ });
859
+ expect(testEntityAlice).not.toBeNull();
860
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
861
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
862
+ type: '&',
863
+ guid: bobsEntity.guid || '',
864
+ ref: ['group', '000000000000000000000000'],
865
+ });
866
+ expect(testEntityAlice).toBeNull();
867
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
868
+ type: '&',
869
+ guid: bobsEntity.guid || '',
870
+ ref: ['acRead', alice],
871
+ });
872
+ expect(testEntityAlice).not.toBeNull();
873
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
874
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
875
+ type: '&',
876
+ guid: bobsEntity.guid || '',
877
+ ref: ['acRead', '000000000000000000000000'],
878
+ });
879
+ expect(testEntityAlice).toBeNull();
880
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
881
+ type: '&',
882
+ guid: bobsEntity.guid || '',
883
+ ref: ['acWrite', bob],
884
+ });
885
+ expect(testEntityAlice).not.toBeNull();
886
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
887
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
888
+ type: '&',
889
+ guid: bobsEntity.guid || '',
890
+ ref: ['acWrite', '000000000000000000000000'],
891
+ });
892
+ expect(testEntityAlice).toBeNull();
893
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
894
+ type: '&',
895
+ guid: bobsEntity.guid || '',
896
+ ref: ['acFull', bob],
897
+ });
898
+ expect(testEntityAlice).not.toBeNull();
899
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
900
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
901
+ type: '&',
902
+ guid: bobsEntity.guid || '',
903
+ ref: ['acFull', '000000000000000000000000'],
904
+ });
905
+ expect(testEntityAlice).toBeNull();
906
+ // QRef queries.
907
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
908
+ type: '&',
909
+ guid: bobsEntity.guid || '',
910
+ qref: [
911
+ 'user',
912
+ [
913
+ { class: User },
914
+ { type: '&', equal: ['username', bob.username ?? ''] },
915
+ ],
910
916
  ],
911
- ],
917
+ });
918
+ expect(testEntityAlice).not.toBeNull();
919
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
920
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
921
+ type: '&',
922
+ guid: bobsEntity.guid || '',
923
+ qref: [
924
+ 'user',
925
+ [{ class: User }, { type: '&', equal: ['username', 'pickle'] }],
926
+ ],
927
+ });
928
+ expect(testEntityAlice).toBeNull();
929
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
930
+ type: '&',
931
+ guid: bobsEntity.guid || '',
932
+ qref: [
933
+ 'acRead',
934
+ [
935
+ { class: User },
936
+ { type: '&', equal: ['username', alice.username ?? ''] },
937
+ ],
938
+ ],
939
+ });
940
+ expect(testEntityAlice).not.toBeNull();
941
+ expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
942
+ testEntityAlice = await nymph.getEntity({ class: TestModel }, {
943
+ type: '&',
944
+ guid: bobsEntity.guid || '',
945
+ qref: [
946
+ 'acRead',
947
+ [{ class: User }, { type: '&', equal: ['username', 'pickle'] }],
948
+ ],
949
+ });
950
+ expect(testEntityAlice).toBeNull();
912
951
  });
913
- expect(testEntityAlice).not.toBeNull();
914
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
915
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
916
- type: '&',
917
- guid: bobsEntity.guid || '',
918
- qref: [
919
- 'user',
920
- [{ class: User }, { type: '&', equal: ['username', 'pickle'] }],
921
- ],
952
+ it('alice can access an entity with an access request', async () => {
953
+ const { bob, alice } = await makeUsers();
954
+ await tilmeld.fillSession(alice);
955
+ const alicesEntity = await TestModel.factory();
956
+ alicesEntity.name = "Alice's Entity";
957
+ expect(await alicesEntity.$save()).toEqual(true);
958
+ expect(alicesEntity.guid).not.toBeNull();
959
+ expect(alice.$is(alicesEntity.user)).toEqual(true);
960
+ const testEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: alicesEntity.guid || '' });
961
+ expect(testEntityAlice).not.toBeNull();
962
+ expect(testEntityAlice?.guid).toEqual(alicesEntity.guid);
963
+ tilmeld.clearSession();
922
964
  });
923
- expect(testEntityAlice).toBeNull();
924
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
925
- type: '&',
926
- guid: bobsEntity.guid || '',
927
- qref: [
928
- 'acRead',
929
- [
930
- { class: User },
931
- { type: '&', equal: ['username', alice.username ?? ''] },
932
- ],
933
- ],
965
+ it("access request for user owned doesn't return group owned entity", async () => {
966
+ const { bob, alice } = await makeUsers();
967
+ await tilmeld.fillSession(bob);
968
+ const bobsEntity = await TestModel.factory();
969
+ bobsEntity.name = "Bob's Entity";
970
+ bobsEntity.group = alice.group;
971
+ expect(await bobsEntity.$save()).toEqual(true);
972
+ expect(bobsEntity.guid).not.toBeNull();
973
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
974
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
975
+ expect(testEntityBob).not.toBeNull();
976
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
977
+ tilmeld.clearSession();
978
+ await tilmeld.fillSession(alice);
979
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
980
+ expect(testEntityAlice).not.toBeNull();
981
+ const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: bobsEntity.guid || '' });
982
+ expect(testFailureEntityAlice).toBeNull();
983
+ const testSuccessEntityAlice = await nymph.getEntity({
984
+ class: TestModel,
985
+ acRequest: TilmeldAccessRequest.PRIMARY_GROUP_OWNED,
986
+ }, { type: '&', guid: bobsEntity.guid || '' });
987
+ expect(testSuccessEntityAlice).not.toBeNull();
934
988
  });
935
- expect(testEntityAlice).not.toBeNull();
936
- expect(testEntityAlice?.guid).toEqual(bobsEntity.guid);
937
- testEntityAlice = await nymph.getEntity({ class: TestModel }, {
938
- type: '&',
939
- guid: bobsEntity.guid || '',
940
- qref: [
941
- 'acRead',
942
- [{ class: User }, { type: '&', equal: ['username', 'pickle'] }],
943
- ],
989
+ it("access request for user owned doesn't return acOther accessible entity", async () => {
990
+ const { bob, alice } = await makeUsers();
991
+ await tilmeld.fillSession(bob);
992
+ const bobsEntity = await TestModel.factory();
993
+ bobsEntity.name = "Bob's Entity";
994
+ bobsEntity.acOther = TilmeldAccessLevels.READ_ACCESS;
995
+ expect(await bobsEntity.$save()).toEqual(true);
996
+ expect(bobsEntity.guid).not.toBeNull();
997
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
998
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
999
+ expect(testEntityBob).not.toBeNull();
1000
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
1001
+ tilmeld.clearSession();
1002
+ await tilmeld.fillSession(alice);
1003
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1004
+ expect(testEntityAlice).not.toBeNull();
1005
+ const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: bobsEntity.guid || '' });
1006
+ expect(testFailureEntityAlice).toBeNull();
1007
+ const testSuccessEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.OTHER_ACCESSIBLE }, { type: '&', guid: bobsEntity.guid || '' });
1008
+ expect(testSuccessEntityAlice).not.toBeNull();
1009
+ });
1010
+ it("access request for user owned doesn't return user acRead accessible entity", async () => {
1011
+ const { bob, alice } = await makeUsers();
1012
+ await tilmeld.fillSession(bob);
1013
+ const bobsEntity = await TestModel.factory();
1014
+ bobsEntity.name = "Bob's Entity";
1015
+ bobsEntity.acRead = [alice.guid ?? ''];
1016
+ expect(await bobsEntity.$save()).toEqual(true);
1017
+ expect(bobsEntity.guid).not.toBeNull();
1018
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
1019
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1020
+ expect(testEntityBob).not.toBeNull();
1021
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
1022
+ tilmeld.clearSession();
1023
+ await tilmeld.fillSession(alice);
1024
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1025
+ expect(testEntityAlice).not.toBeNull();
1026
+ const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: bobsEntity.guid || '' });
1027
+ expect(testFailureEntityAlice).toBeNull();
1028
+ const testSuccessEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_ACCESSIBLE }, { type: '&', guid: bobsEntity.guid || '' });
1029
+ expect(testSuccessEntityAlice).not.toBeNull();
1030
+ });
1031
+ it("access request for user owned doesn't return group acRead accessible entity", async () => {
1032
+ const { bob, alice } = await makeUsers();
1033
+ await tilmeld.fillSession(bob);
1034
+ const bobsEntity = await TestModel.factory();
1035
+ bobsEntity.name = "Bob's Entity";
1036
+ bobsEntity.acWrite = [alice.group?.guid ?? ''];
1037
+ expect(await bobsEntity.$save()).toEqual(true);
1038
+ expect(bobsEntity.guid).not.toBeNull();
1039
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
1040
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1041
+ expect(testEntityBob).not.toBeNull();
1042
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
1043
+ tilmeld.clearSession();
1044
+ await tilmeld.fillSession(alice);
1045
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1046
+ expect(testEntityAlice).not.toBeNull();
1047
+ const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: bobsEntity.guid || '' });
1048
+ expect(testFailureEntityAlice).toBeNull();
1049
+ const testSuccessEntityAlice = await nymph.getEntity({
1050
+ class: TestModel,
1051
+ acRequest: TilmeldAccessRequest.PRIMARY_GROUP_ACCESSIBLE,
1052
+ }, { type: '&', guid: bobsEntity.guid || '' });
1053
+ expect(testSuccessEntityAlice).not.toBeNull();
1054
+ });
1055
+ it("access request for user owned doesn't return secondary group owned entity", async () => {
1056
+ const { bob, alice, abgroup } = await makeUsers();
1057
+ await tilmeld.fillSession(bob);
1058
+ const bobsEntity = await TestModel.factory();
1059
+ bobsEntity.name = "Bob's Entity";
1060
+ bobsEntity.group = abgroup;
1061
+ expect(await bobsEntity.$save()).toEqual(true);
1062
+ expect(bobsEntity.guid).not.toBeNull();
1063
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
1064
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1065
+ expect(testEntityBob).not.toBeNull();
1066
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
1067
+ tilmeld.clearSession();
1068
+ await tilmeld.fillSession(alice);
1069
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1070
+ expect(testEntityAlice).not.toBeNull();
1071
+ const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: bobsEntity.guid || '' });
1072
+ expect(testFailureEntityAlice).toBeNull();
1073
+ const testSuccessEntityAlice = await nymph.getEntity({
1074
+ class: TestModel,
1075
+ acRequest: TilmeldAccessRequest.SECONDARY_GROUP_OWNED,
1076
+ }, { type: '&', guid: bobsEntity.guid || '' });
1077
+ expect(testSuccessEntityAlice).not.toBeNull();
1078
+ });
1079
+ it("access request for user owned doesn't return secondary group accessible entity", async () => {
1080
+ const { bob, alice, abgroup } = await makeUsers();
1081
+ await tilmeld.fillSession(bob);
1082
+ const bobsEntity = await TestModel.factory();
1083
+ bobsEntity.name = "Bob's Entity";
1084
+ bobsEntity.acFull = [abgroup.guid ?? ''];
1085
+ expect(await bobsEntity.$save()).toEqual(true);
1086
+ expect(bobsEntity.guid).not.toBeNull();
1087
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
1088
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1089
+ expect(testEntityBob).not.toBeNull();
1090
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
1091
+ tilmeld.clearSession();
1092
+ await tilmeld.fillSession(alice);
1093
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1094
+ expect(testEntityAlice).not.toBeNull();
1095
+ const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: bobsEntity.guid || '' });
1096
+ expect(testFailureEntityAlice).toBeNull();
1097
+ const testSuccessEntityAlice = await nymph.getEntity({
1098
+ class: TestModel,
1099
+ acRequest: TilmeldAccessRequest.SECONDARY_GROUP_ACCESSIBLE,
1100
+ }, { type: '&', guid: bobsEntity.guid || '' });
1101
+ expect(testSuccessEntityAlice).not.toBeNull();
1102
+ // Test or-ing two levels together.
1103
+ const testSuccessEntityAlice2 = await nymph.getEntity({
1104
+ class: TestModel,
1105
+ acRequest: TilmeldAccessRequest.SECONDARY_GROUP_ACCESSIBLE |
1106
+ TilmeldAccessRequest.PRIMARY_GROUP_ACCESSIBLE,
1107
+ }, { type: '&', guid: bobsEntity.guid || '' });
1108
+ expect(testSuccessEntityAlice2).not.toBeNull();
1109
+ });
1110
+ it('access request for entity still works when filtering user owned entities manually', async () => {
1111
+ const { bob, alice } = await makeUsers();
1112
+ await tilmeld.fillSession(bob);
1113
+ const bobsEntity = await TestModel.factory();
1114
+ bobsEntity.name = "Bob's Entity";
1115
+ bobsEntity.acFull = [alice.guid ?? ''];
1116
+ expect(await bobsEntity.$save()).toEqual(true);
1117
+ expect(bobsEntity.guid).not.toBeNull();
1118
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
1119
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1120
+ expect(testEntityBob).not.toBeNull();
1121
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
1122
+ tilmeld.clearSession();
1123
+ await tilmeld.fillSession(alice);
1124
+ const alicesEntity = await TestModel.factory();
1125
+ alicesEntity.name = "Alice's Entity";
1126
+ expect(await alicesEntity.$save()).toEqual(true);
1127
+ expect(alicesEntity.guid).not.toBeNull();
1128
+ expect(alice.$is(alicesEntity.user)).toEqual(true);
1129
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1130
+ expect(testEntityAlice).not.toBeNull();
1131
+ const testWithoutFiltering = await nymph.getEntities({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '|', guid: [alicesEntity.guid || '', bobsEntity.guid || ''] });
1132
+ expect(testWithoutFiltering.length).toEqual(1);
1133
+ expect(testWithoutFiltering[0].guid).toEqual(alicesEntity?.guid || '');
1134
+ const testWithFiltering = await nymph.getEntities({
1135
+ class: TestModel,
1136
+ acRequest: TilmeldAccessRequest.USER_OWNED,
1137
+ }, { type: '|', guid: [alicesEntity.guid || '', bobsEntity.guid || ''] }, { type: '!&', ref: ['user', alice] });
1138
+ expect(testWithFiltering.length).toEqual(0);
1139
+ const testInclusive = await nymph.getEntities({ class: TestModel });
1140
+ const testInclusiveWithFiltering = await nymph.getEntities({ class: TestModel }, { type: '!&', ref: ['user', alice] });
1141
+ expect(testInclusiveWithFiltering.length).toBeGreaterThanOrEqual(1);
1142
+ const guids = testInclusiveWithFiltering.map((entity) => entity.guid);
1143
+ expect(guids).not.toContain(alicesEntity?.guid || '');
1144
+ expect(guids).toContain(bobsEntity?.guid || '');
1145
+ const allDefaultGuids = testInclusive.map((entity) => entity.guid);
1146
+ for (let guid of guids) {
1147
+ expect(allDefaultGuids).toContain(guid);
1148
+ }
1149
+ });
1150
+ it("access request for invalid access control doesn't return any inaccessible entities", async () => {
1151
+ const { bob, alice, abgroup } = await makeUsers();
1152
+ await tilmeld.fillSession(bob);
1153
+ const bobsEntity = await TestModel.factory();
1154
+ bobsEntity.name = "Bob's Entity";
1155
+ expect(await bobsEntity.$save()).toEqual(true);
1156
+ expect(bobsEntity.guid).not.toBeNull();
1157
+ expect(bob.$is(bobsEntity.user)).toEqual(true);
1158
+ const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1159
+ expect(testEntityBob).not.toBeNull();
1160
+ expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
1161
+ tilmeld.clearSession();
1162
+ await tilmeld.fillSession(alice);
1163
+ const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1164
+ expect(testEntityAlice).toBeNull();
1165
+ const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.UNOWNED * 2 }, { type: '&', guid: bobsEntity.guid || '' });
1166
+ expect(testFailureEntityAlice).toBeNull();
1167
+ });
1168
+ it('delete test data again', async () => {
1169
+ await deleteTestData();
944
1170
  });
945
- expect(testEntityAlice).toBeNull();
946
- });
947
- it('alice can access an entity with an access request', async () => {
948
- const { bob, alice } = await makeUsers();
949
- await tilmeld.fillSession(alice);
950
- const alicesEntity = await TestModel.factory();
951
- alicesEntity.name = "Alice's Entity";
952
- expect(await alicesEntity.$save()).toEqual(true);
953
- expect(alicesEntity.guid).not.toBeNull();
954
- expect(alice.$is(alicesEntity.user)).toEqual(true);
955
- const testEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: alicesEntity.guid || '' });
956
- expect(testEntityAlice).not.toBeNull();
957
- expect(testEntityAlice?.guid).toEqual(alicesEntity.guid);
958
- tilmeld.clearSession();
959
- });
960
- it("access request for user owned doesn't return group owned entity", async () => {
961
- const { bob, alice } = await makeUsers();
962
- await tilmeld.fillSession(bob);
963
- const bobsEntity = await TestModel.factory();
964
- bobsEntity.name = "Bob's Entity";
965
- bobsEntity.group = alice.group;
966
- expect(await bobsEntity.$save()).toEqual(true);
967
- expect(bobsEntity.guid).not.toBeNull();
968
- expect(bob.$is(bobsEntity.user)).toEqual(true);
969
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
970
- expect(testEntityBob).not.toBeNull();
971
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
972
- tilmeld.clearSession();
973
- await tilmeld.fillSession(alice);
974
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
975
- expect(testEntityAlice).not.toBeNull();
976
- const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: bobsEntity.guid || '' });
977
- expect(testFailureEntityAlice).toBeNull();
978
- const testSuccessEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.PRIMARY_GROUP_OWNED }, { type: '&', guid: bobsEntity.guid || '' });
979
- expect(testSuccessEntityAlice).not.toBeNull();
980
- });
981
- it("access request for user owned doesn't return acOther accessible entity", async () => {
982
- const { bob, alice } = await makeUsers();
983
- await tilmeld.fillSession(bob);
984
- const bobsEntity = await TestModel.factory();
985
- bobsEntity.name = "Bob's Entity";
986
- bobsEntity.acOther = TilmeldAccessLevels.READ_ACCESS;
987
- expect(await bobsEntity.$save()).toEqual(true);
988
- expect(bobsEntity.guid).not.toBeNull();
989
- expect(bob.$is(bobsEntity.user)).toEqual(true);
990
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
991
- expect(testEntityBob).not.toBeNull();
992
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
993
- tilmeld.clearSession();
994
- await tilmeld.fillSession(alice);
995
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
996
- expect(testEntityAlice).not.toBeNull();
997
- const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: bobsEntity.guid || '' });
998
- expect(testFailureEntityAlice).toBeNull();
999
- const testSuccessEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.OTHER_ACCESSIBLE }, { type: '&', guid: bobsEntity.guid || '' });
1000
- expect(testSuccessEntityAlice).not.toBeNull();
1001
- });
1002
- it("access request for user owned doesn't return user acRead accessible entity", async () => {
1003
- const { bob, alice } = await makeUsers();
1004
- await tilmeld.fillSession(bob);
1005
- const bobsEntity = await TestModel.factory();
1006
- bobsEntity.name = "Bob's Entity";
1007
- bobsEntity.acRead = [alice.guid ?? ''];
1008
- expect(await bobsEntity.$save()).toEqual(true);
1009
- expect(bobsEntity.guid).not.toBeNull();
1010
- expect(bob.$is(bobsEntity.user)).toEqual(true);
1011
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1012
- expect(testEntityBob).not.toBeNull();
1013
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
1014
- tilmeld.clearSession();
1015
- await tilmeld.fillSession(alice);
1016
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1017
- expect(testEntityAlice).not.toBeNull();
1018
- const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: bobsEntity.guid || '' });
1019
- expect(testFailureEntityAlice).toBeNull();
1020
- const testSuccessEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_ACCESSIBLE }, { type: '&', guid: bobsEntity.guid || '' });
1021
- expect(testSuccessEntityAlice).not.toBeNull();
1022
- });
1023
- it("access request for user owned doesn't return group acRead accessible entity", async () => {
1024
- const { bob, alice } = await makeUsers();
1025
- await tilmeld.fillSession(bob);
1026
- const bobsEntity = await TestModel.factory();
1027
- bobsEntity.name = "Bob's Entity";
1028
- bobsEntity.acWrite = [alice.group?.guid ?? ''];
1029
- expect(await bobsEntity.$save()).toEqual(true);
1030
- expect(bobsEntity.guid).not.toBeNull();
1031
- expect(bob.$is(bobsEntity.user)).toEqual(true);
1032
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1033
- expect(testEntityBob).not.toBeNull();
1034
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
1035
- tilmeld.clearSession();
1036
- await tilmeld.fillSession(alice);
1037
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1038
- expect(testEntityAlice).not.toBeNull();
1039
- const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: bobsEntity.guid || '' });
1040
- expect(testFailureEntityAlice).toBeNull();
1041
- const testSuccessEntityAlice = await nymph.getEntity({
1042
- class: TestModel,
1043
- acRequest: TilmeldAccessRequest.PRIMARY_GROUP_ACCESSIBLE,
1044
- }, { type: '&', guid: bobsEntity.guid || '' });
1045
- expect(testSuccessEntityAlice).not.toBeNull();
1046
- });
1047
- it("access request for user owned doesn't return secondary group owned entity", async () => {
1048
- const { bob, alice, abgroup } = await makeUsers();
1049
- await tilmeld.fillSession(bob);
1050
- const bobsEntity = await TestModel.factory();
1051
- bobsEntity.name = "Bob's Entity";
1052
- bobsEntity.group = abgroup;
1053
- expect(await bobsEntity.$save()).toEqual(true);
1054
- expect(bobsEntity.guid).not.toBeNull();
1055
- expect(bob.$is(bobsEntity.user)).toEqual(true);
1056
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1057
- expect(testEntityBob).not.toBeNull();
1058
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
1059
- tilmeld.clearSession();
1060
- await tilmeld.fillSession(alice);
1061
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1062
- expect(testEntityAlice).not.toBeNull();
1063
- const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: bobsEntity.guid || '' });
1064
- expect(testFailureEntityAlice).toBeNull();
1065
- const testSuccessEntityAlice = await nymph.getEntity({
1066
- class: TestModel,
1067
- acRequest: TilmeldAccessRequest.SECONDARY_GROUP_OWNED,
1068
- }, { type: '&', guid: bobsEntity.guid || '' });
1069
- expect(testSuccessEntityAlice).not.toBeNull();
1070
- });
1071
- it("access request for user owned doesn't return secondary group accessible entity", async () => {
1072
- const { bob, alice, abgroup } = await makeUsers();
1073
- await tilmeld.fillSession(bob);
1074
- const bobsEntity = await TestModel.factory();
1075
- bobsEntity.name = "Bob's Entity";
1076
- bobsEntity.acFull = [abgroup.guid ?? ''];
1077
- expect(await bobsEntity.$save()).toEqual(true);
1078
- expect(bobsEntity.guid).not.toBeNull();
1079
- expect(bob.$is(bobsEntity.user)).toEqual(true);
1080
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1081
- expect(testEntityBob).not.toBeNull();
1082
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
1083
- tilmeld.clearSession();
1084
- await tilmeld.fillSession(alice);
1085
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1086
- expect(testEntityAlice).not.toBeNull();
1087
- const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '&', guid: bobsEntity.guid || '' });
1088
- expect(testFailureEntityAlice).toBeNull();
1089
- const testSuccessEntityAlice = await nymph.getEntity({
1090
- class: TestModel,
1091
- acRequest: TilmeldAccessRequest.SECONDARY_GROUP_ACCESSIBLE,
1092
- }, { type: '&', guid: bobsEntity.guid || '' });
1093
- expect(testSuccessEntityAlice).not.toBeNull();
1094
- // Test or-ing two levels together.
1095
- const testSuccessEntityAlice2 = await nymph.getEntity({
1096
- class: TestModel,
1097
- acRequest: TilmeldAccessRequest.SECONDARY_GROUP_ACCESSIBLE |
1098
- TilmeldAccessRequest.PRIMARY_GROUP_ACCESSIBLE,
1099
- }, { type: '&', guid: bobsEntity.guid || '' });
1100
- expect(testSuccessEntityAlice2).not.toBeNull();
1101
- });
1102
- it('access request for entity still works when filtering user owned entities manually', async () => {
1103
- const { bob, alice } = await makeUsers();
1104
- await tilmeld.fillSession(bob);
1105
- const bobsEntity = await TestModel.factory();
1106
- bobsEntity.name = "Bob's Entity";
1107
- bobsEntity.acFull = [alice.guid ?? ''];
1108
- expect(await bobsEntity.$save()).toEqual(true);
1109
- expect(bobsEntity.guid).not.toBeNull();
1110
- expect(bob.$is(bobsEntity.user)).toEqual(true);
1111
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1112
- expect(testEntityBob).not.toBeNull();
1113
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
1114
- tilmeld.clearSession();
1115
- await tilmeld.fillSession(alice);
1116
- const alicesEntity = await TestModel.factory();
1117
- alicesEntity.name = "Alice's Entity";
1118
- expect(await alicesEntity.$save()).toEqual(true);
1119
- expect(alicesEntity.guid).not.toBeNull();
1120
- expect(alice.$is(alicesEntity.user)).toEqual(true);
1121
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1122
- expect(testEntityAlice).not.toBeNull();
1123
- const testWithoutFiltering = await nymph.getEntities({ class: TestModel, acRequest: TilmeldAccessRequest.USER_OWNED }, { type: '|', guid: [alicesEntity.guid || '', bobsEntity.guid || ''] });
1124
- expect(testWithoutFiltering.length).toEqual(1);
1125
- expect(testWithoutFiltering[0].guid).toEqual(alicesEntity?.guid || '');
1126
- const testWithFiltering = await nymph.getEntities({
1127
- class: TestModel,
1128
- acRequest: TilmeldAccessRequest.USER_OWNED,
1129
- }, { type: '|', guid: [alicesEntity.guid || '', bobsEntity.guid || ''] }, { type: '!&', ref: ['user', alice] });
1130
- expect(testWithFiltering.length).toEqual(0);
1131
- const testInclusive = await nymph.getEntities({ class: TestModel });
1132
- const testInclusiveWithFiltering = await nymph.getEntities({ class: TestModel }, { type: '!&', ref: ['user', alice] });
1133
- expect(testInclusiveWithFiltering.length).toBeGreaterThanOrEqual(1);
1134
- const guids = testInclusiveWithFiltering.map((entity) => entity.guid);
1135
- expect(guids).not.toContain(alicesEntity?.guid || '');
1136
- expect(guids).toContain(bobsEntity?.guid || '');
1137
- const allDefaultGuids = testInclusive.map((entity) => entity.guid);
1138
- for (let guid of guids) {
1139
- expect(allDefaultGuids).toContain(guid);
1140
- }
1141
- });
1142
- it("access request for invalid access control doesn't return any inaccessible entities", async () => {
1143
- const { bob, alice, abgroup } = await makeUsers();
1144
- await tilmeld.fillSession(bob);
1145
- const bobsEntity = await TestModel.factory();
1146
- bobsEntity.name = "Bob's Entity";
1147
- expect(await bobsEntity.$save()).toEqual(true);
1148
- expect(bobsEntity.guid).not.toBeNull();
1149
- expect(bob.$is(bobsEntity.user)).toEqual(true);
1150
- const testEntityBob = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1151
- expect(testEntityBob).not.toBeNull();
1152
- expect(testEntityBob?.guid).toEqual(bobsEntity.guid);
1153
- tilmeld.clearSession();
1154
- await tilmeld.fillSession(alice);
1155
- const testEntityAlice = await nymph.getEntity({ class: TestModel }, { type: '&', guid: bobsEntity.guid || '' });
1156
- expect(testEntityAlice).toBeNull();
1157
- const testFailureEntityAlice = await nymph.getEntity({ class: TestModel, acRequest: TilmeldAccessRequest.UNOWNED * 2 }, { type: '&', guid: bobsEntity.guid || '' });
1158
- expect(testFailureEntityAlice).toBeNull();
1159
- });
1160
- it('delete test data again', async () => {
1161
- await deleteTestData();
1162
1171
  });
1163
1172
  }
1164
1173
  //# sourceMappingURL=testArtifacts.js.map