return deleted token

This commit is contained in:
Dieter Stinglhamber 2021-09-02 10:47:06 +02:00
parent 946d3a0ea3
commit 930156a574
5 changed files with 32 additions and 19 deletions

View File

@ -123,7 +123,7 @@ describe('API Token Controller', () => {
await apiTokenController.revoke(ctx);
expect(revoke).toHaveBeenCalledWith(token.id);
expect(deleted).toHaveBeenCalledWith();
expect(deleted).toHaveBeenCalledWith({ data: token });
});
test('Does not return an error if the ressource does not exists', async () => {
@ -144,7 +144,7 @@ describe('API Token Controller', () => {
await apiTokenController.revoke(ctx);
expect(revoke).toHaveBeenCalledWith(token.id);
expect(deleted).toHaveBeenCalledWith();
expect(deleted).toHaveBeenCalledWith({ data: null });
});
});
});

View File

@ -45,9 +45,8 @@ module.exports = {
async revoke(ctx) {
const { id } = ctx.params;
const apiTokenService = getService('api-token');
const apiToken = await apiTokenService.revoke(id);
await apiTokenService.revoke(id);
ctx.deleted();
ctx.deleted({ data: apiToken });
},
};

View File

@ -173,7 +173,10 @@ describe('API Token', () => {
const res = await apiTokenService.revoke(token.id);
expect(mockedDelete).toHaveBeenCalledWith({ where: { id: token.id } });
expect(mockedDelete).toHaveBeenCalledWith({
select: ['id', 'name', 'description', 'type'],
where: { id: token.id },
});
expect(res).toEqual(token);
});
@ -188,7 +191,10 @@ describe('API Token', () => {
const res = await apiTokenService.revoke(42);
expect(mockedDelete).toHaveBeenCalledWith({ where: { id: 42 } });
expect(mockedDelete).toHaveBeenCalledWith({
select: ['id', 'name', 'description', 'type'],
where: { id: 42 },
});
expect(res).toEqual(null);
});
});

View File

@ -16,6 +16,9 @@ const crypto = require('crypto');
* @property {TokenType} type
*/
/** @constant {Array<string>} */
const SELECT_FIELDS = ['id', 'name', 'description', 'type'];
/**
* @param {Object} whereParams
* @param {string} whereParams.name
@ -53,7 +56,7 @@ const create = async attributes => {
const accessKey = crypto.randomBytes(128).toString('hex');
const apiToken = await strapi.query('admin::api-token').create({
select: ['id', 'name', 'description', 'type'],
select: SELECT_FIELDS,
data: {
...attributes,
accessKey: hash(accessKey),
@ -86,11 +89,11 @@ const createSaltIfNotDefined = () => {
};
/**
* @returns {Promise<{id: number|string, name: string, description: string, type: TokenType}>}
* @returns {Promise<Omit<ApiToken, 'accessKey'>>}
*/
const list = async () => {
return strapi.query('admin::api-token').findMany({
select: ['id', 'name', 'description', 'type'],
select: SELECT_FIELDS,
orderBy: { name: 'ASC' },
});
};
@ -98,10 +101,10 @@ const list = async () => {
/**
* @param {string|number} id
*
* @returns {Promise<void>}
* @returns {Promise<Omit<ApiToken, 'accessKey'>>}
*/
const revoke = async id => {
return strapi.query('admin::api-token').delete({ where: { id } });
return strapi.query('admin::api-token').delete({ select: SELECT_FIELDS, where: { id } });
};
module.exports = {

View File

@ -92,7 +92,7 @@ describe('Admin API Token CRUD (e2e)', () => {
});
expect(res.statusCode).toBe(201);
expect(res.body.data).toMatchObject({
expect(res.body.data).toStrictEqual({
accessKey: expect.any(String),
name: body.name,
description: body.description,
@ -114,7 +114,7 @@ describe('Admin API Token CRUD (e2e)', () => {
});
expect(res.statusCode).toBe(201);
expect(res.body.data).toMatchObject({
expect(res.body.data).toStrictEqual({
accessKey: expect.any(String),
name: body.name,
description: '',
@ -137,7 +137,7 @@ describe('Admin API Token CRUD (e2e)', () => {
});
expect(res.statusCode).toBe(201);
expect(res.body.data).toMatchObject({
expect(res.body.data).toStrictEqual({
accessKey: expect.any(String),
name: 'api-token_tests-name-with-spaces-at-the-end',
description: 'api-token_tests-description-with-spaces-at-the-end',
@ -182,8 +182,13 @@ describe('Admin API Token CRUD (e2e)', () => {
method: 'DELETE',
});
expect(res.statusCode).toBe(204);
expect(res.body.data).toBeUndefined();
expect(res.statusCode).toBe(200);
expect(res.body.data).toStrictEqual({
name: 'api-token_tests-name-with-spaces-at-the-end',
description: 'api-token_tests-description-with-spaces-at-the-end',
type: 'read-only',
id: 3,
});
});
test('8. Does not return an error if the ressource does not exists', async () => {
@ -192,7 +197,7 @@ describe('Admin API Token CRUD (e2e)', () => {
method: 'DELETE',
});
expect(res.statusCode).toBe(204);
expect(res.body.data).toBeUndefined();
expect(res.statusCode).toBe(200);
expect(res.body.data).toBeNull();
});
});