2021-10-08 07:06:44 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
import request from 'supertest';
|
|
|
|
|
|
|
|
import { User } from '../../src/users/user.entity';
|
2022-01-06 15:41:36 -05:00
|
|
|
import { TestSetup, TestSetupBuilder } from '../test-setup';
|
2021-10-08 07:06:44 -04:00
|
|
|
|
|
|
|
describe('Tokens', () => {
|
2021-10-14 14:17:28 -04:00
|
|
|
let testSetup: TestSetup;
|
2021-10-08 07:06:44 -04:00
|
|
|
let agent: request.SuperAgentTest;
|
2021-10-14 14:17:28 -04:00
|
|
|
|
|
|
|
let user: User;
|
2021-10-08 07:06:44 -04:00
|
|
|
let keyId: string;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-01-06 15:41:36 -05:00
|
|
|
testSetup = await TestSetupBuilder.create().build();
|
2021-10-14 14:17:28 -04:00
|
|
|
|
|
|
|
user = await testSetup.userService.createUser('hardcoded', 'Testy');
|
|
|
|
await testSetup.identityService.createLocalIdentity(user, 'test');
|
|
|
|
await testSetup.app.init();
|
|
|
|
|
|
|
|
agent = request.agent(testSetup.app.getHttpServer());
|
2021-10-08 07:06:44 -04:00
|
|
|
await agent
|
2021-10-15 10:44:43 -04:00
|
|
|
.post('/api/private/auth/local/login')
|
2021-10-08 07:06:44 -04:00
|
|
|
.send({ username: 'hardcoded', password: 'test' })
|
|
|
|
.expect(201);
|
|
|
|
});
|
|
|
|
|
2022-03-04 07:13:46 -05:00
|
|
|
afterAll(async () => {
|
|
|
|
await testSetup.cleanup();
|
|
|
|
});
|
|
|
|
|
2021-10-08 07:06:44 -04:00
|
|
|
it(`POST /tokens`, async () => {
|
|
|
|
const tokenName = 'testToken';
|
|
|
|
const response = await agent
|
2021-10-15 10:44:43 -04:00
|
|
|
.post('/api/private/tokens')
|
2021-10-08 07:06:44 -04:00
|
|
|
.send({
|
|
|
|
label: tokenName,
|
2022-03-04 12:01:45 -05:00
|
|
|
validUntil: 0,
|
2021-10-08 07:06:44 -04:00
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201);
|
|
|
|
keyId = response.body.keyId;
|
|
|
|
expect(response.body.label).toBe(tokenName);
|
2022-03-04 12:01:45 -05:00
|
|
|
expect(new Date(response.body.validUntil).getTime()).toBeGreaterThan(
|
|
|
|
Date.now(),
|
|
|
|
);
|
2022-01-16 15:52:15 -05:00
|
|
|
expect(response.body.lastUsedAt).toBe(null);
|
2021-12-09 17:04:00 -05:00
|
|
|
expect(response.body.secret.length).toBe(98);
|
2021-10-08 07:06:44 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`GET /tokens`, async () => {
|
|
|
|
const tokenName = 'testToken';
|
|
|
|
const response = await agent
|
2021-10-15 10:44:43 -04:00
|
|
|
.get('/api/private/tokens/')
|
2021-10-08 07:06:44 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body[0].label).toBe(tokenName);
|
2022-03-04 12:01:45 -05:00
|
|
|
expect(new Date(response.body[0].validUntil).getTime()).toBeGreaterThan(
|
|
|
|
Date.now(),
|
|
|
|
);
|
2022-01-16 15:52:15 -05:00
|
|
|
expect(response.body[0].lastUsedAt).toBe(null);
|
2021-10-08 07:06:44 -04:00
|
|
|
expect(response.body[0].secret).not.toBeDefined();
|
|
|
|
});
|
|
|
|
it(`DELETE /tokens/:keyid`, async () => {
|
2021-10-15 10:44:43 -04:00
|
|
|
const response = await agent
|
|
|
|
.delete('/api/private/tokens/' + keyId)
|
|
|
|
.expect(204);
|
2021-10-08 07:06:44 -04:00
|
|
|
expect(response.body).toStrictEqual({});
|
|
|
|
});
|
|
|
|
it(`GET /tokens 2`, async () => {
|
|
|
|
const response = await agent
|
2021-10-15 10:44:43 -04:00
|
|
|
.get('/api/private/tokens/')
|
2021-10-08 07:06:44 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body).toStrictEqual([]);
|
|
|
|
});
|
|
|
|
});
|