-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
241 lines (211 loc) Β· 6.05 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const { getAllPaths, hasAccess, getUserPaths } = require('./');
// Paths and access levels
const registry = [
{
path: '/',
parent: null,
level: 1,
},
{
path: '/rocket',
parent: '/secret',
level: 10,
},
{
path: '/secret',
parent: '/projects',
level: 999,
},
{
path: '/projects',
parent: '/',
level: 100,
},
{
path: '/photos',
parent: '/',
level: 200,
},
{
path: '/laboratory',
parent: '/projects',
level: 200,
},
{
path: '/john',
parent: '/projects',
level: 299,
},
// This path in unreachable from / - so it should not be returned in getAllPaths
{
path: '/dummy',
parent: '/path',
level: 666,
},
// we have to go deeper - shuffled on purpose
{
path: '/five',
parent: '/four',
level: 1,
},
{
path: '/one',
parent: '/',
level: 999, // ensure we propagate levels
},
{
path: '/four',
parent: '/three',
level: 1,
},
{
path: '/three',
parent: '/two',
level: 1,
},
{
path: '/two',
parent: '/one',
level: 1,
},
];
// Users with their access level
const users = [
{
name: 'superadmin',
level: 1000,
},
{
name: 'guest',
level: 201,
},
{
name: 'john',
level: 301,
},
];
describe('Falcode Algorithm Challenge', () => {
describe('Get all paths', () => {
const paths = getAllPaths(registry);
const availablePaths = [
'/',
'/projects',
'/photos',
'/projects/laboratory',
'/projects/john',
'/projects/secret',
'/projects/secret/rocket',
'/one',
'/one/two',
'/one/two/three',
'/one/two/three/four',
'/one/two/three/four/five',
];
it('should have correct length', () => {
expect(paths).toHaveLength(availablePaths.length);
});
availablePaths.forEach(path => {
it(`should return the path ${path}`, () => {
expect(paths.find(p => p.absolutePath === path)).toBeTruthy();
});
});
});
describe('Check user access should return true when we have the right level', () => {
const paths = getAllPaths(registry);
describe('superadmin', () => {
const superadmin = users[0];
const testCases = [
{ input: '/', expected: true },
{ input: '/photos', expected: true },
{ input: '/projects', expected: true },
{ input: '/projects/laboratory', expected: true },
{ input: '/projects/john', expected: true },
{ input: '/projects/secret', expected: true },
{ input: '/projects/secret/rocket', expected: true },
{ input: '/one', expected: true },
{ input: '/one/two', expected: true },
{ input: '/one/two/three', expected: true },
{ input: '/one/two/three/four', expected: true },
{ input: '/one/two/three/four/five', expected: true },
];
testCases.forEach(({ input, expected }) => {
it(`hasAccess of "${input}" should be ${expected}`, () => {
expect(hasAccess(superadmin, input, paths)).toBe(expected);
});
});
});
describe('guest', () => {
const testCases = [
{ input: '/', expected: true },
{ input: '/photos', expected: true },
{ input: '/projects', expected: true },
{ input: '/projects/laboratory', expected: true },
{ input: '/projects/john', expected: false },
{ input: '/projects/secret', expected: false },
{ input: '/projects/secret/rocket', expected: false },
{ input: '/one', expected: false },
{ input: '/one/two', expected: false },
{ input: '/one/two/three', expected: false },
{ input: '/one/two/three/four', expected: false },
{ input: '/one/two/three/four/five', expected: false },
];
const guest = users[1];
testCases.forEach(({ input, expected }) => {
it(`hasAccess of "${input}" should be ${expected}`, () => {
expect(hasAccess(guest, input, paths)).toBe(expected);
});
});
});
describe('john', () => {
const testCases = [
{ input: '/', expected: true },
{ input: '/photos', expected: true },
{ input: '/projects', expected: true },
{ input: '/projects/laboratory', expected: true },
{ input: '/projects/john', expected: true },
{ input: '/projects/secret', expected: false },
{ input: '/projects/secret/rocket', expected: false },
{ input: '/one', expected: false },
{ input: '/one/two', expected: false },
{ input: '/one/two/three', expected: false },
{ input: '/one/two/three/four', expected: false },
{ input: '/one/two/three/four/five', expected: false },
];
const john = users[2];
testCases.forEach(({ input, expected }) => {
it(`hasAccess of "${input}" should be ${expected}`, () => {
expect(hasAccess(john, input, paths)).toBe(expected);
});
});
});
});
describe('Get all the user possibilities', () => {
it('should return all paths for: superadmin', () => {
const paths = getAllPaths(registry);
const superadmin = users[0];
const availablePaths = getUserPaths(superadmin, paths);
expect(availablePaths).toHaveLength(12);
availablePaths.forEach(path => {
expect(hasAccess(superadmin, path.absolutePath, paths)).toBe(true);
});
});
it('should return all paths for: guest', () => {
const paths = getAllPaths(registry);
const guest = users[1];
const availablePaths = getUserPaths(guest, paths);
expect(availablePaths).toHaveLength(4);
availablePaths.forEach(path => {
expect(hasAccess(guest, path.absolutePath, paths)).toBe(true);
});
});
it('should return all paths for: john', () => {
const paths = getAllPaths(registry);
const john = users[2];
const availablePaths = getUserPaths(john, paths);
expect(availablePaths).toHaveLength(5);
availablePaths.forEach(path => {
expect(hasAccess(john, path.absolutePath, paths)).toBe(true);
});
});
});
});