2022-09-28 15:53:56 +02:00
|
|
|
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';
|
2022-09-16 09:54:27 +02:00
|
|
|
|
|
|
|
let app: IUnleashTest;
|
|
|
|
let db: ITestDb;
|
|
|
|
|
|
|
|
let tomorrow = new Date();
|
2022-09-28 15:53:56 +02:00
|
|
|
let firstSecret;
|
2022-09-16 09:54:27 +02:00
|
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
db = await dbInit('user_pat', getLogger);
|
2022-09-28 15:53:56 +02:00
|
|
|
app = await setupAppWithAuth(db.stores, {
|
|
|
|
experimental: { flags: { personalAccessTokens: true } },
|
|
|
|
});
|
2022-09-16 09:54:27 +02:00
|
|
|
|
|
|
|
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 () => {
|
2022-09-26 14:42:39 +02:00
|
|
|
const description = 'expected description';
|
2022-09-16 09:54:27 +02:00
|
|
|
const { request } = app;
|
|
|
|
|
|
|
|
const { body } = await request
|
|
|
|
.post('/api/admin/user/tokens')
|
|
|
|
.send({
|
|
|
|
expiresAt: tomorrow,
|
2022-09-26 14:42:39 +02:00
|
|
|
description: description,
|
2022-09-16 09:54:27 +02:00
|
|
|
} as IPat)
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
expect(new Date(body.expiresAt)).toEqual(tomorrow);
|
2022-09-26 14:42:39 +02:00
|
|
|
expect(body.description).toEqual(description);
|
2022-09-28 15:53:56 +02:00
|
|
|
firstSecret = body.secret;
|
|
|
|
|
|
|
|
const response = await request
|
|
|
|
.get('/api/admin/user/tokens')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(response.body.pats).toHaveLength(1);
|
2022-09-16 09:54:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should delete the PAT', async () => {
|
|
|
|
const { request } = app;
|
|
|
|
|
2022-09-28 15:53:56 +02:00
|
|
|
const { body } = await request
|
2022-09-16 09:54:27 +02:00
|
|
|
.post('/api/admin/user/tokens')
|
|
|
|
.send({
|
|
|
|
expiresAt: tomorrow,
|
|
|
|
} as IPat)
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(201);
|
|
|
|
|
2022-09-28 15:53:56 +02:00
|
|
|
const createdSecret = body.secret;
|
2022-09-16 09:54:27 +02:00
|
|
|
|
|
|
|
await request.delete(`/api/admin/user/tokens/${createdSecret}`).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);
|
|
|
|
});
|
|
|
|
|
|
|
|
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({
|
|
|
|
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({
|
|
|
|
expiresAt: yesterday,
|
|
|
|
} as IPat)
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(500);
|
|
|
|
});
|
2022-09-28 15:53:56 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|