-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PermissionUtils.test.ts
176 lines (129 loc) · 8.19 KB
/
PermissionUtils.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { PermissionUtils } from "./PermissionUtils";
describe('PermissionUtils', () => {
describe('#checkPermission', () => {
test('can test valid permission with single item', () => {
expect( PermissionUtils.checkPermission('FOO', ['FOO']) ).toBe(true);
});
test('can test valid permission with multiple items', () => {
expect( PermissionUtils.checkPermission('FOO', ['BAR', 'FOO']) ).toBe(true);
expect( PermissionUtils.checkPermission('FOO', ['BAR', 'FOO', 'HELLO']) ).toBe(true);
expect( PermissionUtils.checkPermission('FOO', ['WORLD', 'BAR', 'FOO']) ).toBe(true);
expect( PermissionUtils.checkPermission('FOO', ['FOO', 'HELLO', 'WORLD']) ).toBe(true);
});
test('can test invalid permission when permission list is empty', () => {
expect( PermissionUtils.checkPermission('FOO', []) ).toBe(false);
});
test('can test invalid permission when permission list has incorrect type', () => {
expect( PermissionUtils.checkPermission('FOO', ['BAR']) ).toBe(false);
});
test('can test invalid permission when permission list has multiple types', () => {
expect( PermissionUtils.checkPermission('FOO', ['BAR', 'HELLO', 'WORLD']) ).toBe(false);
});
});
describe('#checkPermissionList', () => {
test('can test invalid permission list when no permissions are accepted', () => {
expect( PermissionUtils.checkPermissionList([], ['FOO']) ).toStrictEqual({});
});
test('can test empty invalid permission list when no permissions are accepted', () => {
expect( PermissionUtils.checkPermissionList(
[], []) ).toStrictEqual({});
});
test('can test empty invalid permission list when one permission is accepted', () => {
expect( PermissionUtils.checkPermissionList(
['BAR'], []) ).toStrictEqual({BAR: false});
});
test('can test empty invalid permission list when some permissions are accepted', () => {
expect( PermissionUtils.checkPermissionList(
['BAR', 'FOO'], []) ).toStrictEqual({BAR: false, FOO: false});
});
test('can test valid permission list with single permission', () => {
expect( PermissionUtils.checkPermissionList(
['FOO', 'BAR'], ['FOO']) ).toStrictEqual({FOO: true, BAR: false});
});
test('can test valid permission list with two permissions', () => {
expect( PermissionUtils.checkPermissionList(
['FOO', 'BAR'], ['FOO', 'BAR']) ).toStrictEqual({FOO: true, BAR: true});
});
test('can test valid permission list with two permissions against single accepted permission', () => {
expect( PermissionUtils.checkPermissionList(
['FOO'], ['FOO', 'BAR']) ).toStrictEqual({FOO:true});
});
test('can test valid permission list with multiple permissions', () => {
expect( PermissionUtils.checkPermissionList(
['FOO', 'BAR', 'HELLO'], ['FOO', 'BAR', 'HELLO']) ).toStrictEqual({FOO: true, BAR: true, HELLO: true});
});
test('can test invalid permission list with single permission', () => {
expect( PermissionUtils.checkPermissionList(
['HELLO', 'WORLD'], ['FOO']) ).toStrictEqual({HELLO: false, WORLD: false});
});
test('can test invalid permission list with two permissions', () => {
expect( PermissionUtils.checkPermissionList(
['HELLO', 'WORLD'], ['FOO', 'BAR']) ).toStrictEqual({HELLO: false, WORLD: false});
});
test('can test invalid permission list with multiple permissions when only some match', () => {
expect( PermissionUtils.checkPermissionList(
['HELLO', 'WORLD', 'XXX'], ['FOO', 'BAR', 'HELLO']) ).toStrictEqual({HELLO: true, WORLD: false, XXX: false});
});
});
describe('#everyPermissionAccepted', () => {
test('can test that no permission matches on zero permission object', () => {
expect( PermissionUtils.everyPermissionAccepted({}) ).toStrictEqual(false);
});
test('can test that no permission matches on single false permission', () => {
expect( PermissionUtils.everyPermissionAccepted({FOO: false}) ).toStrictEqual(false);
});
test('can test that no permission matches on partially matched permissions', () => {
expect( PermissionUtils.everyPermissionAccepted({FOO: false, BAR: true}) ).toStrictEqual(false);
});
test('can test that no permission matches on partially matched with three permissions', () => {
expect( PermissionUtils.everyPermissionAccepted({FOO: true, BAR: false, HELLO: true}) ).toStrictEqual(false);
});
test('can test that every permission matches on on permission', () => {
expect( PermissionUtils.everyPermissionAccepted({FOO: true}) ).toStrictEqual(true);
});
test('can test that every permission matches on two permissions', () => {
expect( PermissionUtils.everyPermissionAccepted({FOO: true, BAR: true}) ).toStrictEqual(true);
});
test('can test that every permission matches on three permissions', () => {
expect( PermissionUtils.everyPermissionAccepted({FOO: true, BAR: true, HELLO: true}) ).toStrictEqual(true);
});
});
describe('#getAcceptedPermissionList', () => {
test('it returns accepted permissions', () => {
expect( PermissionUtils.getAcceptedPermissionList({})).toStrictEqual([]);
expect( PermissionUtils.getAcceptedPermissionList({FOO: false})).toStrictEqual([]);
expect( PermissionUtils.getAcceptedPermissionList({FOO: false, BAR: false})).toStrictEqual([]);
expect( PermissionUtils.getAcceptedPermissionList({FOO: false, BAR: false, WORLD: false})).toStrictEqual([]);
expect( PermissionUtils.getAcceptedPermissionList({FOO: true})).toStrictEqual(["FOO"]);
expect( PermissionUtils.getAcceptedPermissionList({FOO: true, BAR: false})).toStrictEqual(["FOO"]);
expect( PermissionUtils.getAcceptedPermissionList({FOO: true, BAR: true})).toStrictEqual(["FOO", "BAR"]);
expect( PermissionUtils.getAcceptedPermissionList({FOO: false, BAR: true})).toStrictEqual(["BAR"]);
expect( PermissionUtils.getAcceptedPermissionList({FOO: true, HELLO: false, BAR: true})).toStrictEqual(["FOO", "BAR"]);
expect( PermissionUtils.getAcceptedPermissionList({FOO: true, HELLO: true, BAR: true})).toStrictEqual(["FOO", "HELLO", "BAR"]);
});
});
describe('#somePermissionAccepted', () => {
test('can test that no permission matches on zero permission object', () => {
expect( PermissionUtils.somePermissionAccepted({}) ).toStrictEqual(false);
});
test('can test that no permission matches on single false permission', () => {
expect( PermissionUtils.somePermissionAccepted({FOO: false}) ).toStrictEqual(false);
});
test('can test that permission matches on partially matched permissions', () => {
expect( PermissionUtils.somePermissionAccepted({FOO: false, BAR: true}) ).toStrictEqual(true);
});
test('can test that permission matches on partially matched with three permissions', () => {
expect( PermissionUtils.somePermissionAccepted({FOO: true, BAR: false, HELLO: true}) ).toStrictEqual(true);
});
test('can test that permission matches on single permission', () => {
expect( PermissionUtils.somePermissionAccepted({FOO: true}) ).toStrictEqual(true);
});
test('can test that every permission matches on two permissions', () => {
expect( PermissionUtils.somePermissionAccepted({FOO: true, BAR: true}) ).toStrictEqual(true);
});
test('can test that every permission matches on three permissions', () => {
expect( PermissionUtils.somePermissionAccepted({FOO: true, BAR: true, HELLO: true}) ).toStrictEqual(true);
});
});
});