Merge pull request #14192 from strapi/api-token-v2/regenerate-tokens-api

Api Token v2/add regenerate controller tests
This commit is contained in:
Ben Irvin 2022-08-29 10:38:42 +02:00 committed by GitHub
commit 7ed24a9edf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -258,6 +258,62 @@ describe('API Token Controller', () => {
});
});
describe('Regenerate an API token', () => {
const token = {
id: 1,
name: 'api-token_tests-regenerate',
description: 'api-token_tests-description',
type: 'read-only',
};
test('Regenerates an API token successfully', async () => {
const regenerate = jest.fn().mockResolvedValue(token);
const getById = jest.fn().mockResolvedValue(token);
const created = jest.fn();
const ctx = createContext({ params: { id: token.id } }, { created });
global.strapi = {
admin: {
services: {
'api-token': {
regenerate,
getById,
},
},
},
};
await apiTokenController.regenerate(ctx);
expect(regenerate).toHaveBeenCalledWith(token.id);
});
test('Fails if token not found', async () => {
const regenerate = jest.fn().mockResolvedValue(token);
const getById = jest.fn().mockResolvedValue(null);
const created = jest.fn();
const notFound = jest.fn();
const ctx = createContext({ params: { id: token.id } }, { created, notFound });
global.strapi = {
admin: {
services: {
'api-token': {
regenerate,
getById,
},
},
},
};
await apiTokenController.regenerate(ctx);
expect(regenerate).not.toHaveBeenCalled();
expect(getById).toHaveBeenCalledWith(token.id);
expect(notFound).toHaveBeenCalledWith('API Token not found');
});
});
describe('Retrieve an API token', () => {
const token = {
id: 1,