2021-10-08 11:06:44 +00:00
|
|
|
/*
|
2022-09-25 00:05:30 +00:00
|
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
2021-10-08 11:06:44 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
import request from 'supertest';
|
|
|
|
|
2022-12-30 11:48:24 +00:00
|
|
|
import {
|
|
|
|
password1,
|
|
|
|
password2,
|
|
|
|
TestSetup,
|
|
|
|
TestSetupBuilder,
|
|
|
|
username1,
|
|
|
|
username2,
|
|
|
|
} from '../test-setup';
|
2021-10-08 11:06:44 +00:00
|
|
|
|
|
|
|
describe('Tokens', () => {
|
2021-10-14 18:17:28 +00:00
|
|
|
let testSetup: TestSetup;
|
2021-10-08 11:06:44 +00:00
|
|
|
let agent: request.SuperAgentTest;
|
2021-10-14 18:17:28 +00:00
|
|
|
|
2021-10-08 11:06:44 +00:00
|
|
|
let keyId: string;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-06-26 21:26:21 +00:00
|
|
|
testSetup = await TestSetupBuilder.create().withUsers().build();
|
2021-10-14 18:17:28 +00:00
|
|
|
await testSetup.app.init();
|
|
|
|
|
|
|
|
agent = request.agent(testSetup.app.getHttpServer());
|
2021-10-08 11:06:44 +00:00
|
|
|
await agent
|
2021-10-15 14:44:43 +00:00
|
|
|
.post('/api/private/auth/local/login')
|
2022-12-30 11:48:24 +00:00
|
|
|
.send({ username: username1, password: password1 })
|
2021-10-08 11:06:44 +00:00
|
|
|
.expect(201);
|
|
|
|
});
|
|
|
|
|
2022-03-04 12:13:46 +00:00
|
|
|
afterAll(async () => {
|
|
|
|
await testSetup.cleanup();
|
|
|
|
});
|
|
|
|
|
2021-10-08 11:06:44 +00:00
|
|
|
it(`POST /tokens`, async () => {
|
|
|
|
const tokenName = 'testToken';
|
|
|
|
const response = await agent
|
2021-10-15 14:44:43 +00:00
|
|
|
.post('/api/private/tokens')
|
2021-10-08 11:06:44 +00:00
|
|
|
.send({
|
|
|
|
label: tokenName,
|
2022-03-04 17:01:45 +00:00
|
|
|
validUntil: 0,
|
2021-10-08 11:06:44 +00:00
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201);
|
|
|
|
keyId = response.body.keyId;
|
|
|
|
expect(response.body.label).toBe(tokenName);
|
2022-03-04 17:01:45 +00:00
|
|
|
expect(new Date(response.body.validUntil).getTime()).toBeGreaterThan(
|
|
|
|
Date.now(),
|
|
|
|
);
|
2022-01-16 20:52:15 +00:00
|
|
|
expect(response.body.lastUsedAt).toBe(null);
|
2024-03-23 00:14:41 +00:00
|
|
|
expect(response.body.secret.length).toBe(102);
|
2021-10-08 11:06:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`GET /tokens`, async () => {
|
2022-06-26 21:26:21 +00:00
|
|
|
const tokenName = 'test';
|
2021-10-08 11:06:44 +00:00
|
|
|
const response = await agent
|
2021-10-15 14:44:43 +00:00
|
|
|
.get('/api/private/tokens/')
|
2021-10-08 11:06:44 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body[0].label).toBe(tokenName);
|
2022-03-04 17:01:45 +00:00
|
|
|
expect(new Date(response.body[0].validUntil).getTime()).toBeGreaterThan(
|
|
|
|
Date.now(),
|
|
|
|
);
|
2022-01-16 20:52:15 +00:00
|
|
|
expect(response.body[0].lastUsedAt).toBe(null);
|
2021-10-08 11:06:44 +00:00
|
|
|
expect(response.body[0].secret).not.toBeDefined();
|
|
|
|
});
|
|
|
|
it(`DELETE /tokens/:keyid`, async () => {
|
2022-06-26 21:26:21 +00:00
|
|
|
// try to delete token with wrong user
|
|
|
|
const agent2 = request.agent(testSetup.app.getHttpServer());
|
|
|
|
await agent2
|
|
|
|
.post('/api/private/auth/local/login')
|
2022-12-30 11:48:24 +00:00
|
|
|
.send({ username: username2, password: password2 })
|
2022-06-26 21:26:21 +00:00
|
|
|
.expect(201);
|
|
|
|
let response = await agent2
|
2021-10-15 14:44:43 +00:00
|
|
|
.delete('/api/private/tokens/' + keyId)
|
2022-06-26 21:26:21 +00:00
|
|
|
.expect(401);
|
|
|
|
expect(response.body.statusCode).toEqual(401);
|
|
|
|
|
|
|
|
// delete token with correct user
|
|
|
|
response = await agent.delete('/api/private/tokens/' + keyId).expect(204);
|
2021-10-08 11:06:44 +00:00
|
|
|
expect(response.body).toStrictEqual({});
|
2022-06-26 21:26:21 +00:00
|
|
|
|
|
|
|
// token should be deleted
|
|
|
|
response = await agent
|
2021-10-15 14:44:43 +00:00
|
|
|
.get('/api/private/tokens/')
|
2021-10-08 11:06:44 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
2022-09-11 16:19:20 +00:00
|
|
|
const tokenList: { keyId: string }[] = response.body;
|
2022-06-26 21:26:21 +00:00
|
|
|
expect(
|
2022-09-11 16:19:20 +00:00
|
|
|
tokenList.find((token) => {
|
2022-06-26 21:26:21 +00:00
|
|
|
return token.keyId === keyId;
|
|
|
|
}),
|
|
|
|
).toBeUndefined();
|
2021-10-08 11:06:44 +00:00
|
|
|
});
|
|
|
|
});
|