Merge pull request #2378 from automatisch/delete-template-endpoint
feat: Implement admin delete template API endpoint
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
import Template from '../../../../../models/template.ee.js';
|
||||||
|
|
||||||
|
export default async (request, response) => {
|
||||||
|
const template = await Template.query()
|
||||||
|
.findById(request.params.templateId)
|
||||||
|
.throwIfNotFound();
|
||||||
|
|
||||||
|
await template.$query().delete();
|
||||||
|
|
||||||
|
response.status(204).end();
|
||||||
|
};
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import { vi, describe, it, expect, beforeEach } from 'vitest';
|
||||||
|
import request from 'supertest';
|
||||||
|
import Crypto from 'crypto';
|
||||||
|
import app from '../../../../../app.js';
|
||||||
|
import createAuthTokenByUserId from '../../../../../helpers/create-auth-token-by-user-id.js';
|
||||||
|
import { createRole } from '../../../../../../test/factories/role.js';
|
||||||
|
import { createUser } from '../../../../../../test/factories/user.js';
|
||||||
|
import { createTemplate } from '../../../../../../test/factories/template.js';
|
||||||
|
import Template from '../../../../../models/template.ee.js';
|
||||||
|
import * as license from '../../../../../helpers/license.ee.js';
|
||||||
|
|
||||||
|
describe('DELETE /api/v1/admin/templates/:templateId', () => {
|
||||||
|
let currentUser, token, role;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
|
||||||
|
|
||||||
|
role = await createRole({ name: 'Admin' });
|
||||||
|
currentUser = await createUser({ roleId: role.id });
|
||||||
|
|
||||||
|
token = await createAuthTokenByUserId(currentUser.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should delete the template', async () => {
|
||||||
|
const template = await createTemplate();
|
||||||
|
|
||||||
|
await request(app)
|
||||||
|
.delete(`/api/v1/admin/templates/${template.id}`)
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(204);
|
||||||
|
|
||||||
|
const deletedTemplate = await Template.query().findById(template.id);
|
||||||
|
expect(deletedTemplate).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return not found response for not existing template UUID', async () => {
|
||||||
|
const notExistingTemplateUUID = Crypto.randomUUID();
|
||||||
|
|
||||||
|
await request(app)
|
||||||
|
.delete(`/api/v1/admin/templates/${notExistingTemplateUUID}`)
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(404);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return bad request response for invalid UUID', async () => {
|
||||||
|
await request(app)
|
||||||
|
.delete('/api/v1/admin/templates/invalidTemplateUUID')
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(400);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -6,6 +6,7 @@ import { checkIsEnterprise } from '../../../../helpers/check-is-enterprise.js';
|
|||||||
import createTemplateAction from '../../../../controllers/api/v1/admin/templates/create-template.ee.js';
|
import createTemplateAction from '../../../../controllers/api/v1/admin/templates/create-template.ee.js';
|
||||||
import getTemplatesAction from '../../../../controllers/api/v1/admin/templates/get-templates.ee.js';
|
import getTemplatesAction from '../../../../controllers/api/v1/admin/templates/get-templates.ee.js';
|
||||||
import updateTemplateAction from '../../../../controllers/api/v1/admin/templates/update-template.ee.js';
|
import updateTemplateAction from '../../../../controllers/api/v1/admin/templates/update-template.ee.js';
|
||||||
|
import deleteTemplateAction from '../../../../controllers/api/v1/admin/templates/delete-template.ee.js';
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
@@ -33,4 +34,12 @@ router.patch(
|
|||||||
updateTemplateAction
|
updateTemplateAction
|
||||||
);
|
);
|
||||||
|
|
||||||
|
router.delete(
|
||||||
|
'/:templateId',
|
||||||
|
authenticateUser,
|
||||||
|
authorizeAdmin,
|
||||||
|
checkIsEnterprise,
|
||||||
|
deleteTemplateAction
|
||||||
|
);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user