/
pat.e2e.test.ts
305 lines (258 loc) · 8.1 KB
/
pat.e2e.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { IUnleashTest, setupAppWithAuth } from '../../../helpers/test-helper';
import dbInit, { ITestDb } from '../../../helpers/database-init';
import getLogger from '../../../../fixtures/no-logger';
import { IPat } from '../../../../../lib/types/models/pat';
import { IPatStore } from '../../../../../lib/types/stores/pat-store';
import { PAT_LIMIT } from '../../../../../lib/util/constants';
let app: IUnleashTest;
let db: ITestDb;
let patStore: IPatStore;
let tomorrow = new Date();
let firstSecret;
let firstId;
tomorrow.setDate(tomorrow.getDate() + 1);
beforeAll(async () => {
db = await dbInit('user_pat', getLogger);
patStore = db.stores.patStore;
app = await setupAppWithAuth(db.stores);
await app.request
.post(`/auth/demo/login`)
.send({
email: 'user@getunleash.io',
})
.expect(200);
});
afterAll(async () => {
await app.destroy();
});
test('should create a PAT', async () => {
const description = 'expected description';
const { request } = app;
const { body } = await request
.post('/api/admin/user/tokens')
.send({
expiresAt: tomorrow,
description: description,
} as IPat)
.set('Content-Type', 'application/json')
.expect(201);
expect(new Date(body.expiresAt)).toEqual(tomorrow);
expect(body.description).toEqual(description);
firstSecret = body.secret;
firstId = body.id;
const response = await request
.get('/api/admin/user/tokens')
.expect('Content-Type', /json/)
.expect(200);
expect(response.body.pats).toHaveLength(1);
});
test('should delete the PAT', async () => {
const description = 'pat to be deleted';
const { request } = app;
const { body } = await request
.post('/api/admin/user/tokens')
.send({
description,
expiresAt: tomorrow,
} as IPat)
.set('Content-Type', 'application/json')
.expect(201);
const createdId = body.id;
await request.delete(`/api/admin/user/tokens/${createdId}`).expect(200);
});
test('should get all PATs', async () => {
const { request } = app;
const { body } = await request
.get('/api/admin/user/tokens')
.expect('Content-Type', /json/)
.expect(200);
expect(body.pats).toHaveLength(1);
expect(body.pats[0].secret).toBeUndefined();
expect(body.pats[0].id).toBeDefined();
});
test('should not allow deletion of other users PAT', async () => {
const { request } = app;
await app.request
.post(`/auth/demo/login`)
.send({
email: 'user-second@getunleash.io',
})
.expect(200);
await request.delete(`/api/admin/user/tokens/${firstId}`).expect(200);
await app.request
.post(`/auth/demo/login`)
.send({
email: 'user@getunleash.io',
})
.expect(200);
const { body } = await request
.get('/api/admin/user/tokens')
.expect('Content-Type', /json/)
.expect(200);
expect(body.pats).toHaveLength(1);
expect(body.pats[0].secret).toBeUndefined();
});
test('should get only current user PATs', async () => {
const { request } = app;
await app.request
.post(`/auth/demo/login`)
.send({
email: 'user-second@getunleash.io',
})
.expect(200);
await request
.post('/api/admin/user/tokens')
.send({
description: 'my pat',
expiresAt: tomorrow,
} as IPat)
.set('Content-Type', 'application/json')
.expect(201);
const { body } = await request
.get('/api/admin/user/tokens')
.expect('Content-Type', /json/)
.expect(200);
expect(body.pats).toHaveLength(1);
});
test('should fail creation of PAT with passed expiry', async () => {
const { request } = app;
let yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
await request
.post('/api/admin/user/tokens')
.send({
description: 'my expired pat',
expiresAt: yesterday,
} as IPat)
.set('Content-Type', 'application/json')
.expect(400);
});
test('should fail creation of PAT without a description', async () => {
await app.request
.post('/api/admin/user/tokens')
.send({
expiresAt: tomorrow,
} as IPat)
.set('Content-Type', 'application/json')
.expect(400);
});
test('should fail creation of PAT with a description that already exists for the current user', async () => {
const description = 'duplicate description';
await app.request
.post('/api/admin/user/tokens')
.send({
description,
expiresAt: tomorrow,
} as IPat)
.set('Content-Type', 'application/json')
.expect(201);
await app.request
.post('/api/admin/user/tokens')
.send({
description,
expiresAt: tomorrow,
} as IPat)
.set('Content-Type', 'application/json')
.expect(409);
});
test('should not fail creation of PAT when a description already exists for another user PAT', async () => {
const description = 'another duplicate description';
await app.request
.post('/api/admin/user/tokens')
.send({
description,
expiresAt: tomorrow,
} as IPat)
.set('Content-Type', 'application/json')
.expect(201);
await app.request
.post(`/auth/demo/login`)
.send({
email: 'user-other@getunleash.io',
})
.expect(200);
await app.request
.post('/api/admin/user/tokens')
.send({
description,
expiresAt: tomorrow,
} as IPat)
.set('Content-Type', 'application/json')
.expect(201);
});
test('should get user id 1', async () => {
await app.request.get('/logout').expect(302);
await app.request
.get('/api/admin/user')
.set('Authorization', firstSecret)
.expect(200)
.expect((res) => {
expect(res.body.user.email).toBe('user@getunleash.io');
expect(res.body.user.id).toBe(1);
});
});
test('should be able to get projects', async () => {
await app.request
.get('/api/admin/projects')
.set('Authorization', firstSecret)
.expect(200);
});
test('should be able to create a toggle', async () => {
await app.request
.post('/api/admin/projects/default/features')
.set('Authorization', firstSecret)
.send({
name: 'test-toggle',
type: 'release',
})
.expect(201);
});
test('should not get user with invalid token', async () => {
await app.request
.get('/api/admin/user')
.set('Authorization', 'randomtoken')
.expect(401);
});
test('should not get user with expired token', async () => {
const token = await patStore.create({
id: 1,
secret: 'user:expired-token',
description: 'expired-token',
userId: 1,
expiresAt: new Date('2020-01-01'),
});
await app.request
.get('/api/admin/user')
.set('Authorization', token.secret)
.expect(401);
});
test('should fail creation of PAT when PAT limit has been reached', async () => {
await app.request
.post(`/auth/demo/login`)
.send({
email: 'user-too-many-pats@getunleash.io',
})
.expect(200);
const tokenCreations = [];
for (let i = 0; i < PAT_LIMIT; i++) {
tokenCreations.push(
await app.request
.post('/api/admin/user/tokens')
.send({
description: `my pat ${i}`,
expiresAt: tomorrow,
} as IPat)
.set('Content-Type', 'application/json')
.expect(201),
);
}
await Promise.all(tokenCreations);
await app.request
.post('/api/admin/user/tokens')
.send({
description: `my pat ${PAT_LIMIT}`,
expiresAt: tomorrow,
} as IPat)
.set('Content-Type', 'application/json')
.expect(403);
});