This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PermissionService.test.ts
89 lines (69 loc) · 3.95 KB
/
PermissionService.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { PermissionService } from "./PermissionService";
import { LogLevel } from "./types/LogLevel";
import { MockPermissionManager } from "./mocks/MockPermissionManager";
PermissionService.setLogLevel(LogLevel.WARN);
describe('PermissionService', () => {
const entityId = '123';
const anotherEntityId = '567';
const targetId = '1000';
describe('#constructor', () => {
test('can create service', () => {
const manager = new MockPermissionManager<string>(entityId, targetId, []);
const service = new PermissionService(manager);
expect( service ).toBeDefined();
});
});
describe('#getEntityPermissionList', () => {
test('can fetch entity permission list', async () => {
const manager = new MockPermissionManager<string>(entityId, targetId, ['FOO', 'BAR']);
const service = new PermissionService(manager);
const result = await service.getEntityPermissionList(entityId, targetId);
expect( result ).toStrictEqual(['FOO', 'BAR']);
});
test('can fetch entity permission list for wrong item', async () => {
const manager = new MockPermissionManager<string>(entityId, targetId, ['FOO', 'BAR']);
const service = new PermissionService(manager);
const result = await service.getEntityPermissionList(entityId, anotherEntityId);
expect( result ).toStrictEqual([]);
});
test('can fetch entity permission list for undefined target', async () => {
const manager = new MockPermissionManager<string>(entityId, undefined, ['FOO', 'BAR']);
const service = new PermissionService(manager);
const result = await service.getEntityPermissionList(entityId);
expect( result ).toStrictEqual(['FOO', 'BAR']);
});
});
describe('#checkEntityPermission', () => {
test('can check entity permission list', async () => {
const manager = new MockPermissionManager<string>(entityId, targetId, ['FOO', 'BAR']);
const service = new PermissionService(manager);
const result = await service.checkEntityPermission(['FOO'], entityId, targetId);
expect( result ).toStrictEqual({FOO: true});
});
test('can check invalid entity permission list', async () => {
const manager = new MockPermissionManager<string>(entityId, targetId, ['FOO', 'BAR']);
const service = new PermissionService(manager);
const result = await service.checkEntityPermission(['HELLO'], entityId, targetId);
expect( result ).toStrictEqual({HELLO: false});
});
test('can check partial entity permission list', async () => {
const manager = new MockPermissionManager<string>(entityId, targetId, ['FOO', 'BAR']);
const service = new PermissionService(manager);
const result = await service.checkEntityPermission(['HELLO', 'FOO'], entityId, targetId);
expect( result ).toStrictEqual({HELLO: false, FOO: true});
});
test('can check on empty entity permission list', async () => {
const manager = new MockPermissionManager<string>(entityId, targetId, []);
const service = new PermissionService(manager);
const result = await service.checkEntityPermission(['HELLO', 'FOO'], entityId, targetId);
expect( result ).toStrictEqual({HELLO: false, FOO: false});
});
test('can check wrong entity permission list', async () => {
const manager = new MockPermissionManager<string>(entityId, targetId, ['FOO', 'BAR']);
const service = new PermissionService(manager);
const result = await service.checkEntityPermission(['HELLO', 'FOO'], anotherEntityId, targetId);
expect( result ).toStrictEqual({HELLO: false, FOO: false});
});
});
});