strapi/packages/core/admin/server/tests/admin-api-token-crud.test.api.js

839 lines
21 KiB
JavaScript
Raw Normal View History

'use strict';
2022-08-23 19:19:56 +02:00
const { omit } = require('lodash');
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
const { createAuthRequest } = require('../../../../../test/helpers/request');
2022-08-26 10:33:18 +02:00
const constants = require('../services/constants');
2022-08-08 17:09:17 +02:00
describe('Admin API Token v2 CRUD (e2e)', () => {
let rq;
let strapi;
2022-08-08 17:09:17 +02:00
const deleteAllTokens = async () => {
const tokens = await strapi.admin.services['api-token'].list();
const promises = [];
tokens.forEach(({ id }) => {
promises.push(strapi.admin.services['api-token'].revoke(id));
});
await Promise.all(promises);
};
2021-09-06 15:14:45 +02:00
// Initialization Actions
beforeAll(async () => {
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
2022-08-08 17:09:17 +02:00
// delete tokens
await deleteAllTokens();
});
// Cleanup actions
afterAll(async () => {
await strapi.destroy();
});
2022-08-23 13:51:47 +02:00
// create a predictable valid token that we can test with (delete, list, etc)
2022-08-08 22:51:01 +02:00
let currentTokens = 0;
2022-08-08 17:09:17 +02:00
const createValidToken = async (token = {}) => {
2022-08-24 10:01:36 +02:00
currentTokens += 1;
2022-08-12 09:50:10 +02:00
const body = {
type: 'read-only',
2022-08-24 10:01:36 +02:00
name: `token_${String(currentTokens)}`,
2022-08-12 09:50:10 +02:00
description: 'generic description',
...token,
};
2022-08-08 17:09:17 +02:00
const req = await rq({
url: '/admin/api-tokens',
method: 'POST',
2022-08-12 09:50:10 +02:00
body,
2022-08-08 17:09:17 +02:00
});
2022-08-08 22:53:25 +02:00
expect(req.status).toEqual(201);
2022-08-08 17:09:17 +02:00
return req.body.data;
};
test('Fails to create an api token (missing parameters from the body)', async () => {
const body = {
2022-08-08 17:09:17 +02:00
name: 'api-token_tests-failBody',
description: 'api-token_tests-description',
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
expect(res.statusCode).toBe(400);
2022-08-24 18:05:36 +02:00
expect(res.body).toStrictEqual({
2021-10-20 17:30:05 +02:00
data: null,
error: {
status: 400,
name: 'ValidationError',
message: 'type is a required field',
details: {
errors: [
{
path: ['type'],
name: 'ValidationError',
message: 'type is a required field',
},
],
},
},
});
});
2022-08-08 17:09:17 +02:00
test('Fails to create an api token (invalid `type` in the body)', async () => {
const body = {
2022-08-08 17:09:17 +02:00
name: 'api-token_tests-failType',
description: 'api-token_tests-description',
type: 'invalid-type',
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
expect(res.statusCode).toBe(400);
2022-08-24 18:05:36 +02:00
expect(res.body).toStrictEqual({
2021-10-20 17:30:05 +02:00
data: null,
error: {
status: 400,
name: 'ValidationError',
2022-08-08 15:00:46 +02:00
message: 'type must be one of the following values: read-only, full-access, custom',
2021-10-20 17:30:05 +02:00
details: {
errors: [
{
path: ['type'],
name: 'ValidationError',
2022-08-08 15:00:46 +02:00
message: 'type must be one of the following values: read-only, full-access, custom',
2021-10-20 17:30:05 +02:00
},
],
},
},
});
});
2022-08-23 14:14:04 +02:00
test('Creates a read-only api token', async () => {
const body = {
2022-08-08 17:09:17 +02:00
name: 'api-token_tests-readonly',
description: 'api-token_tests-description',
type: 'read-only',
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
expect(res.statusCode).toBe(201);
2022-08-24 18:05:36 +02:00
expect(res.body.data).toStrictEqual({
accessKey: expect.any(String),
name: body.name,
2022-08-08 15:00:46 +02:00
permissions: [],
description: body.description,
type: body.type,
id: expect.any(Number),
2022-08-24 18:05:36 +02:00
createdAt: expect.toBeISODate(),
2022-08-19 16:36:28 +02:00
lastUsedAt: null,
2022-08-24 18:05:36 +02:00
updatedAt: expect.toBeISODate(),
expiresAt: null,
lifespan: null,
});
2022-08-08 17:09:17 +02:00
});
2021-09-06 15:14:45 +02:00
test('Creates a token without a lifespan', async () => {
const body = {
name: 'api-token_tests-no-lifespan',
description: 'api-token_tests-description',
type: 'read-only',
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
expect(res.statusCode).toBe(201);
2022-08-24 18:05:36 +02:00
expect(res.body.data).toStrictEqual({
accessKey: expect.any(String),
name: body.name,
2022-08-08 15:00:46 +02:00
permissions: [],
description: body.description,
type: body.type,
id: expect.any(Number),
2022-08-24 18:05:36 +02:00
createdAt: expect.toBeISODate(),
2022-08-19 16:36:28 +02:00
lastUsedAt: null,
2022-08-24 18:05:36 +02:00
updatedAt: expect.toBeISODate(),
2022-08-23 13:51:47 +02:00
expiresAt: null,
lifespan: null,
});
2022-08-08 17:09:17 +02:00
});
2021-09-06 15:14:45 +02:00
2022-08-23 14:14:04 +02:00
test('Creates a token with a lifespan', async () => {
2022-08-24 18:05:36 +02:00
const now = Date.now();
jest.useFakeTimers('modern').setSystemTime(now);
2022-08-23 14:14:04 +02:00
const body = {
name: 'api-token_tests-lifespan',
description: 'api-token_tests-description',
type: 'read-only',
2022-08-24 19:02:38 +02:00
lifespan: 7 * 24 * 60 * 60 * 1000, // 7 days
2022-08-23 14:14:04 +02:00
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
expect(res.statusCode).toBe(201);
expect(res.body.data).toStrictEqual({
accessKey: expect.any(String),
name: body.name,
permissions: [],
description: body.description,
type: body.type,
id: expect.any(Number),
2022-08-24 18:05:36 +02:00
createdAt: expect.toBeISODate(),
2022-08-23 14:14:04 +02:00
lastUsedAt: null,
2022-08-24 18:05:36 +02:00
updatedAt: expect.toBeISODate(),
expiresAt: expect.toBeISODate(),
2022-08-23 14:14:04 +02:00
lifespan: body.lifespan,
});
2022-08-24 18:05:36 +02:00
2022-08-24 19:05:13 +02:00
// Datetime stored in some databases may lose ms accuracy, so allow a range of 2 seconds for timing edge cases
2022-08-24 19:02:38 +02:00
expect(Date.parse(res.body.data.expiresAt)).toBeGreaterThan(now + body.lifespan - 2000);
expect(Date.parse(res.body.data.expiresAt)).toBeLessThan(now + body.lifespan + 2000);
2022-08-24 18:05:36 +02:00
jest.useRealTimers();
2022-08-23 14:14:04 +02:00
});
test('Creates a token with a null lifespan', async () => {
const body = {
name: 'api-token_tests-nulllifespan',
description: 'api-token_tests-description',
type: 'read-only',
lifespan: null,
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
expect(res.statusCode).toBe(201);
expect(res.body.data).toStrictEqual({
accessKey: expect.any(String),
name: body.name,
permissions: [],
description: body.description,
type: body.type,
id: expect.any(Number),
2022-08-24 18:05:36 +02:00
createdAt: expect.toBeISODate(),
2022-08-23 14:14:04 +02:00
lastUsedAt: null,
2022-08-24 18:05:36 +02:00
updatedAt: expect.toBeISODate(),
2022-08-23 14:14:04 +02:00
expiresAt: null,
lifespan: body.lifespan,
});
});
test('Fails to create a token with invalid lifespan', async () => {
const body = {
name: 'api-token_tests-lifespan',
description: 'api-token_tests-description',
type: 'read-only',
lifespan: -1,
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
expect(res.statusCode).toBe(400);
2022-08-24 18:05:36 +02:00
expect(res.body).toStrictEqual({
2022-08-23 14:14:04 +02:00
data: null,
error: {
status: 400,
name: 'ValidationError',
2022-08-26 10:33:18 +02:00
message: expect.stringContaining('lifespan must be one of the following values'),
2022-08-24 18:05:36 +02:00
details: {
errors: expect.arrayContaining([
expect.objectContaining({
2022-08-26 10:33:18 +02:00
message: expect.stringContaining('lifespan must be one of the following values'),
2022-08-24 18:05:36 +02:00
name: 'ValidationError',
}),
]),
},
2022-08-23 14:14:04 +02:00
},
});
});
2022-08-09 10:53:16 +02:00
test('Fails to create a non-custom api token with permissions', async () => {
2022-08-08 17:09:17 +02:00
const body = {
2022-08-09 10:49:44 +02:00
name: 'api-token_tests-readonlyFailWithPermissions',
2022-08-08 17:09:17 +02:00
description: 'api-token_tests-description',
2022-08-08 22:54:58 +02:00
type: 'read-only',
permissions: ['admin::thing.action'],
2022-08-08 17:09:17 +02:00
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
2022-08-09 10:49:44 +02:00
expect(res.statusCode).toBe(400);
2022-08-24 18:05:36 +02:00
expect(res.body).toStrictEqual({
2022-08-09 10:49:44 +02:00
data: null,
error: {
status: 400,
2022-08-09 10:53:16 +02:00
name: 'ValidationError',
2022-08-11 10:48:40 +02:00
message: 'Non-custom tokens should not reference permissions',
2022-08-09 10:53:16 +02:00
details: {},
},
});
});
test('Creates a non-custom api token with empty permissions attribute', async () => {
2022-08-09 10:53:16 +02:00
const body = {
name: 'api-token_tests-fullAccessFailWithEmptyPermissions',
description: 'api-token_tests-description',
type: 'full-access',
permissions: [],
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
2022-08-11 10:48:40 +02:00
expect(res.statusCode).toBe(201);
2022-08-23 17:24:29 +02:00
expect(res.body.data).toMatchObject({
2022-08-11 10:48:40 +02:00
accessKey: expect.any(String),
name: body.name,
permissions: [],
description: body.description,
type: body.type,
id: expect.any(Number),
createdAt: expect.any(String),
2022-08-19 16:36:28 +02:00
lastUsedAt: null,
2022-08-18 11:02:24 +02:00
updatedAt: expect.any(String),
2022-08-23 13:51:47 +02:00
expiresAt: null,
lifespan: null,
2022-08-08 17:09:17 +02:00
});
});
2022-08-23 14:14:04 +02:00
test('Creates a custom api token', async () => {
strapi.contentAPI.permissions.providers.action.keys = jest.fn(() => [
'admin::subject.action',
'plugin::foo.bar.action',
]);
const body = {
2022-08-09 10:23:15 +02:00
name: 'api-token_tests-customSuccess',
description: 'api-token_tests-description',
type: 'custom',
2022-08-08 22:54:08 +02:00
permissions: ['admin::subject.action', 'plugin::foo.bar.action'],
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
2022-08-08 22:54:08 +02:00
expect(res.statusCode).toBe(201);
2022-08-23 17:24:29 +02:00
expect(res.body.data).toMatchObject({
accessKey: expect.any(String),
name: body.name,
permissions: body.permissions,
description: body.description,
type: body.type,
id: expect.any(Number),
createdAt: expect.any(String),
2022-08-19 16:36:28 +02:00
lastUsedAt: null,
2022-08-18 11:02:24 +02:00
updatedAt: expect.any(String),
2022-08-23 13:51:47 +02:00
expiresAt: null,
lifespan: null,
});
});
2022-08-09 10:23:15 +02:00
test('Fails to create a custom api token without permissions', async () => {
2022-08-08 22:54:58 +02:00
const body = {
2022-08-09 10:23:15 +02:00
name: 'api-token_tests-customFail',
2022-08-08 22:54:58 +02:00
description: 'api-token_tests-description',
type: 'custom',
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
expect(res.statusCode).toBe(400);
2022-08-09 10:49:44 +02:00
expect(res.body).toMatchObject({
data: null,
error: {
status: 400,
name: 'ValidationError',
2022-08-11 10:48:40 +02:00
message: 'Missing permissions attribute for custom token',
2022-08-09 10:49:44 +02:00
details: {},
},
});
2022-08-08 22:54:58 +02:00
});
test('Fails to create a custom api token with unknown permissions', async () => {
strapi.contentAPI.permissions.providers.action.keys = jest.fn(() => ['action-A', 'action-B']);
const body = {
name: 'api-token_tests-customFail',
description: 'api-token_tests-description',
type: 'custom',
permissions: ['action-A', 'action-C'],
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
expect(res.statusCode).toBe(400);
expect(res.body).toMatchObject({
data: null,
error: {
status: 400,
name: 'ValidationError',
message: 'Unknown permissions provided: action-C',
details: {},
},
});
});
2022-08-08 17:09:17 +02:00
test('Creates an api token without a description (successfully)', async () => {
const body = {
2021-09-28 11:17:44 +02:00
name: 'api-token_tests-without-description',
type: 'full-access',
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
expect(res.statusCode).toBe(201);
2022-08-23 17:24:29 +02:00
expect(res.body.data).toMatchObject({
accessKey: expect.any(String),
name: body.name,
2022-08-08 15:00:46 +02:00
permissions: [],
description: '',
type: body.type,
id: expect.any(Number),
createdAt: expect.any(String),
2022-08-19 16:36:28 +02:00
lastUsedAt: null,
2022-08-18 11:02:24 +02:00
updatedAt: expect.any(String),
2022-08-23 13:51:47 +02:00
expiresAt: null,
lifespan: null,
});
});
2021-08-27 16:35:34 +02:00
2022-08-08 17:09:17 +02:00
test('Creates an api token with trimmed description and name (successfully)', async () => {
2021-08-27 16:35:34 +02:00
const body = {
2022-08-08 17:09:17 +02:00
name: ' api-token_tests-spaces-at-the-end ',
description: ' api-token_tests-description-with-spaces-at-the-end ',
type: 'read-only',
2021-08-27 16:35:34 +02:00
};
const res = await rq({
url: '/admin/api-tokens',
method: 'POST',
body,
});
expect(res.statusCode).toBe(201);
2022-08-23 17:24:29 +02:00
expect(res.body.data).toMatchObject({
2021-08-27 16:35:34 +02:00
accessKey: expect.any(String),
2021-09-28 11:17:44 +02:00
name: 'api-token_tests-spaces-at-the-end',
2022-08-08 15:00:46 +02:00
permissions: [],
2021-08-27 16:35:34 +02:00
description: 'api-token_tests-description-with-spaces-at-the-end',
type: body.type,
id: expect.any(Number),
createdAt: expect.any(String),
2022-08-19 16:36:28 +02:00
lastUsedAt: null,
2022-08-18 11:02:24 +02:00
updatedAt: expect.any(String),
2022-08-23 13:51:47 +02:00
expiresAt: null,
lifespan: null,
2021-08-27 16:35:34 +02:00
});
});
2021-08-27 08:39:08 +02:00
2022-08-08 17:09:17 +02:00
test('List all tokens (successfully)', async () => {
await deleteAllTokens();
strapi.contentAPI.permissions.providers.action.keys = jest.fn(() => [
'admin::model.model.read',
'admin::model.model.create',
]);
2022-08-08 17:09:17 +02:00
// create 4 tokens
2022-08-18 12:24:13 +02:00
const tokens = [];
tokens.push(
await createValidToken({
type: 'custom',
permissions: ['admin::model.model.read', 'admin::model.model.create'],
})
);
tokens.push(await createValidToken({ type: 'full-access' }));
tokens.push(await createValidToken({ type: 'read-only' }));
2022-08-26 10:33:18 +02:00
tokens.push(await createValidToken({ lifespan: constants.API_TOKEN_LIFESPANS.DAYS_7 }));
2022-08-08 17:09:17 +02:00
tokens.push(await createValidToken());
2021-08-27 08:39:08 +02:00
const res = await rq({
url: '/admin/api-tokens',
method: 'GET',
});
expect(res.statusCode).toBe(200);
2022-08-23 14:14:04 +02:00
expect(res.body.data.length).toBe(tokens.length);
2022-08-23 19:19:56 +02:00
// check that each token exists in data
tokens.forEach((token) => {
const t = res.body.data.find((t) => t.id === token.id);
2022-08-23 20:08:45 +02:00
if (t.permissions) {
t.permissions = t.permissions.sort();
2022-08-24 18:05:36 +02:00
Object.assign(token, { permissions: token.permissions.sort() });
2022-08-23 20:08:45 +02:00
}
2022-08-24 18:05:36 +02:00
expect(t).toStrictEqual(omit(token, ['accessKey']));
2022-08-23 19:19:56 +02:00
});
2021-08-27 08:39:08 +02:00
});
2021-08-31 15:31:54 +02:00
2022-08-08 17:09:17 +02:00
test('Deletes a token (successfully)', async () => {
const token = await createValidToken();
2021-08-31 15:31:54 +02:00
const res = await rq({
2022-08-08 17:09:17 +02:00
url: `/admin/api-tokens/${token.id}`,
2021-08-31 15:31:54 +02:00
method: 'DELETE',
});
2021-09-02 10:47:06 +02:00
expect(res.statusCode).toBe(200);
2022-08-23 17:24:29 +02:00
expect(res.body.data).toMatchObject({
2022-08-08 17:09:17 +02:00
name: token.name,
permissions: token.permissions,
description: token.description,
type: token.type,
id: token.id,
createdAt: token.createdAt,
2022-08-19 16:36:28 +02:00
lastUsedAt: null,
2022-08-18 11:02:24 +02:00
updatedAt: expect.any(String),
2022-08-23 13:51:47 +02:00
expiresAt: null,
lifespan: null,
2021-09-02 10:47:06 +02:00
});
2021-08-31 15:31:54 +02:00
});
2022-08-08 17:09:17 +02:00
test('Does not return an error if the ressource to delete does not exist', async () => {
2021-08-31 15:31:54 +02:00
const res = await rq({
url: '/admin/api-tokens/42',
method: 'DELETE',
});
2021-09-02 10:47:06 +02:00
expect(res.statusCode).toBe(200);
expect(res.body.data).toBeNull();
2021-08-31 15:31:54 +02:00
});
test('Retrieves a non-custom token (successfully)', async () => {
const token = await createValidToken({
type: 'read-only',
});
const res = await rq({
url: `/admin/api-tokens/${token.id}`,
method: 'GET',
});
expect(res.statusCode).toBe(200);
2022-08-23 17:24:29 +02:00
expect(res.body.data).toMatchObject({
name: token.name,
permissions: token.permissions,
description: token.description,
type: token.type,
id: token.id,
createdAt: token.createdAt,
2022-08-19 16:36:28 +02:00
lastUsedAt: null,
2022-08-18 11:02:24 +02:00
updatedAt: expect.any(String),
2022-08-23 13:51:47 +02:00
expiresAt: null,
lifespan: null,
});
});
test('Retrieves a custom token (successfully)', async () => {
const token = await createValidToken({
type: 'custom',
permissions: ['admin::model.model.read'],
});
2022-08-08 17:09:17 +02:00
const res = await rq({
2022-08-08 17:09:17 +02:00
url: `/admin/api-tokens/${token.id}`,
method: 'GET',
});
expect(res.statusCode).toBe(200);
2022-08-23 17:24:29 +02:00
expect(res.body.data).toMatchObject({
2022-08-08 17:09:17 +02:00
name: token.name,
permissions: token.permissions,
description: token.description,
type: token.type,
id: token.id,
createdAt: token.createdAt,
2022-08-19 16:36:28 +02:00
lastUsedAt: null,
2022-08-18 11:02:24 +02:00
updatedAt: expect.any(String),
2022-08-23 13:51:47 +02:00
expiresAt: null,
lifespan: null,
});
});
2022-08-08 17:09:17 +02:00
test('Returns a 404 if the ressource to retrieve does not exist', async () => {
const res = await rq({
url: '/admin/api-tokens/42',
method: 'GET',
});
expect(res.statusCode).toBe(404);
2021-10-20 17:30:05 +02:00
expect(res.body).toMatchObject({
data: null,
error: {
status: 404,
name: 'NotFoundError',
message: 'API Token not found',
details: {},
},
});
});
2022-08-08 17:09:17 +02:00
test('Updates a token (successfully)', async () => {
// create a token
const body = {
2022-08-08 17:09:17 +02:00
name: 'api-token_tests-name',
description: 'api-token_tests-description',
type: 'read-only',
};
const res = await rq({
2022-08-08 17:09:17 +02:00
url: '/admin/api-tokens',
method: 'POST',
body,
});
2022-08-08 17:09:17 +02:00
const token = res.body.data;
const updatedBody = {
name: 'api-token_tests-updated-name',
description: 'api-token_tests-description',
type: 'read-only',
};
const updatedRes = await rq({
url: `/admin/api-tokens/${token.id}`,
method: 'PUT',
body: updatedBody,
});
2022-08-08 17:09:17 +02:00
expect(updatedRes.statusCode).toBe(200);
2022-08-23 17:24:29 +02:00
expect(updatedRes.body.data).toMatchObject({
2022-08-08 17:09:17 +02:00
name: updatedBody.name,
permissions: [],
description: updatedBody.description,
type: updatedBody.type,
id: token.id,
createdAt: token.createdAt,
2022-08-19 16:36:28 +02:00
lastUsedAt: null,
2022-08-18 11:02:24 +02:00
updatedAt: expect.any(String),
2022-08-23 13:51:47 +02:00
expiresAt: null,
lifespan: null,
2022-08-08 17:09:17 +02:00
});
2022-08-18 11:02:24 +02:00
// expect(updatedRes.body.data.updated)
});
2022-08-08 17:09:17 +02:00
test('Returns a 404 if the ressource to update does not exist', async () => {
const body = {
name: 'api-token_tests-updated-name',
description: 'api-token_tests-updated-description',
type: 'read-only',
};
const res = await rq({
url: '/admin/api-tokens/42',
method: 'PUT',
body,
});
expect(res.statusCode).toBe(404);
2021-10-20 17:30:05 +02:00
expect(res.body).toMatchObject({
data: null,
error: {
status: 404,
name: 'NotFoundError',
message: 'API Token not found',
details: {},
},
});
});
2022-08-08 17:09:17 +02:00
test('Updates a token with partial payload (successfully)', async () => {
const token = await createValidToken();
const body = {
description: 'api-token_tests-re-updated-description',
};
const res = await rq({
2022-08-08 17:09:17 +02:00
url: `/admin/api-tokens/${token.id}`,
method: 'PUT',
body,
});
expect(res.statusCode).toBe(200);
expect(res.body.data).toMatchObject({
2022-08-08 17:09:17 +02:00
name: token.name,
permissions: token.permissions,
description: body.description, // updated field
type: token.type,
id: token.id,
createdAt: token.createdAt,
2022-08-19 16:36:28 +02:00
lastUsedAt: null,
2022-08-18 11:02:24 +02:00
updatedAt: expect.any(String),
2022-08-23 13:51:47 +02:00
expiresAt: null,
lifespan: null,
});
});
2022-08-08 17:09:17 +02:00
test('Fails to update an api token (invalid `type` in the body)', async () => {
const body = {
name: 'api-token_tests-name',
description: 'api-token_tests-description',
type: 'invalid-type',
};
const res = await rq({
url: '/admin/api-tokens/1',
method: 'PUT',
body,
});
expect(res.statusCode).toBe(400);
expect(res.body).toMatchObject({
2021-10-20 17:30:05 +02:00
data: null,
error: {
details: {
errors: [
{
2022-08-08 15:00:46 +02:00
message: 'type must be one of the following values: read-only, full-access, custom',
2021-10-20 17:30:05 +02:00
name: 'ValidationError',
path: ['type'],
},
],
},
2022-08-08 15:00:46 +02:00
message: 'type must be one of the following values: read-only, full-access, custom',
2021-10-20 17:30:05 +02:00
name: 'ValidationError',
status: 400,
},
});
});
2022-08-08 17:09:17 +02:00
test('Updates a token when passing a `null` description (successfully)', async () => {
const token = await createValidToken();
const body = {
description: null,
};
const res = await rq({
2022-08-08 17:09:17 +02:00
url: `/admin/api-tokens/${token.id}`,
method: 'PUT',
body,
});
expect(res.statusCode).toBe(200);
expect(res.body.data).toMatchObject({
2022-08-08 17:09:17 +02:00
name: token.name,
permissions: token.permissions,
description: '',
2022-08-08 17:09:17 +02:00
type: token.type,
id: token.id,
createdAt: token.createdAt,
2022-08-19 16:36:28 +02:00
lastUsedAt: null,
2022-08-18 11:02:24 +02:00
updatedAt: expect.any(String),
2022-08-23 13:51:47 +02:00
expiresAt: null,
lifespan: null,
});
});
2022-08-08 17:09:17 +02:00
test('Updates a token but not the description if no description is passed (successfully)', async () => {
const token = await createValidToken();
const body = {
2022-08-08 17:09:17 +02:00
name: 'api-token_tests-newNameWithoutDescUpdate',
};
const res = await rq({
2022-08-08 17:09:17 +02:00
url: `/admin/api-tokens/${token.id}`,
method: 'PUT',
body,
});
expect(res.statusCode).toBe(200);
expect(res.body.data).toMatchObject({
name: body.name,
2022-08-08 17:09:17 +02:00
description: token.description,
permissions: token.permissions,
type: token.type,
id: token.id,
createdAt: token.createdAt,
2022-08-19 16:36:28 +02:00
lastUsedAt: null,
2022-08-18 11:02:24 +02:00
updatedAt: expect.any(String),
2022-08-23 13:51:47 +02:00
expiresAt: null,
lifespan: null,
});
});
2022-08-22 09:26:20 +02:00
2022-08-18 14:12:34 +02:00
test('Regenerates an api token access key', async () => {
2022-08-18 14:03:59 +02:00
const token = await createValidToken();
const res = await rq({
url: `/admin/api-tokens/${token.id}/regenerate`,
method: 'POST',
});
2022-08-18 14:12:34 +02:00
expect(res.statusCode).toBe(201);
2022-08-18 14:03:59 +02:00
expect(res.body.data).toMatchObject({
accessKey: expect.any(String),
});
2022-08-22 12:22:36 +02:00
expect(res.body.data.accessKey).not.toEqual(token.accessKey);
2022-08-18 14:03:59 +02:00
});
2022-08-18 14:12:34 +02:00
2022-08-22 12:33:00 +02:00
test('Regenerate throws a NotFound if provided an invalid id', async () => {
const res = await rq({
url: `/admin/api-tokens/999999/regenerate`,
method: 'POST',
});
expect(res.statusCode).toBe(404);
expect(res.body.error).toMatchObject({
name: 'NotFoundError',
status: 404,
});
2022-08-18 14:03:59 +02:00
});
2022-08-18 14:12:34 +02:00
2022-08-23 13:03:15 +02:00
test.todo('Custom token can only be created with valid permissions that exist');
});