feat: Implement admin update template API endpoint

This commit is contained in:
Faruk AYDIN
2025-02-28 16:26:48 +01:00
parent f3ba8517c9
commit 1cb380a077
4 changed files with 124 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
import { renderObject } from '../../../../../helpers/renderer.js';
import Template from '../../../../../models/template.ee.js';
export default async (request, response) => {
const template = await Template.query()
.patchAndFetchById(request.params.templateId, templateParams(request))
.throwIfNotFound();
renderObject(response, template, {
serializer: 'AdminTemplate',
});
};
const templateParams = (request) => {
const { name } = request.body;
return { name };
};

View File

@@ -0,0 +1,76 @@
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 updateTemplateMock from '../../../../../../test/mocks/rest/api/v1/admin/templates/update-template.ee.js';
import * as license from '../../../../../helpers/license.ee.js';
describe('PATCH /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 return the updated template', async () => {
const template = await createTemplate();
const updatedName = 'Updated Template Name';
const response = await request(app)
.patch(`/api/v1/admin/templates/${template.id}`)
.set('Authorization', token)
.send({ name: updatedName })
.expect(200);
const refetchedTemplate = await template.$query();
const expectedPayload = await updateTemplateMock({
...refetchedTemplate,
name: updatedName,
});
expect(response.body).toStrictEqual(expectedPayload);
});
it('should return unprocessable entity response for invalid name', async () => {
const template = await createTemplate();
const response = await request(app)
.patch(`/api/v1/admin/templates/${template.id}`)
.set('Authorization', token)
.send({ name: '' })
.expect(422);
expect(response.body).toStrictEqual({
errors: {
name: ['must NOT have fewer than 1 characters'],
},
meta: { type: 'ModelValidation' },
});
});
it('should return not found response for not existing template UUID', async () => {
const notExistingTemplateUUID = Crypto.randomUUID();
await request(app)
.patch(`/api/v1/admin/templates/${notExistingTemplateUUID}`)
.set('Authorization', token)
.expect(404);
});
it('should return bad request response for invalid UUID', async () => {
await request(app)
.patch('/api/v1/admin/templates/invalidTemplateUUID')
.set('Authorization', token)
.expect(400);
});
});