Merge pull request #2460 from automatisch/aut-1550
feat(api): add get template endpoint
This commit is contained in:
@@ -0,0 +1,10 @@
|
|||||||
|
import { renderObject } from '../../../../helpers/renderer.js';
|
||||||
|
import Template from '../../../../models/template.ee.js';
|
||||||
|
|
||||||
|
export default async (request, response) => {
|
||||||
|
const template = await Template.query()
|
||||||
|
.findById(request.params.templateId)
|
||||||
|
.throwIfNotFound();
|
||||||
|
|
||||||
|
renderObject(response, template, { serializer: 'PublicTemplate' });
|
||||||
|
};
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import request from 'supertest';
|
||||||
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
import { createApiToken } from '../../../../../test/factories/api-token.js';
|
||||||
|
import { createTemplate } from '../../../../../test/factories/template.js';
|
||||||
|
import getTemplateMock from '../../../../../test/mocks/rest/api/v1/templates/get-template.ee.js';
|
||||||
|
import app from '../../../../app.js';
|
||||||
|
import * as license from '../../../../helpers/license.ee.js';
|
||||||
|
|
||||||
|
describe('GET /api/v1/templates/:templateId', () => {
|
||||||
|
let token;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
|
||||||
|
|
||||||
|
token = (await createApiToken()).token;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return templates', async () => {
|
||||||
|
const template = await createTemplate();
|
||||||
|
|
||||||
|
const response = await request(app)
|
||||||
|
.get(`/api/v1/templates/${template.id}`)
|
||||||
|
.set('x-api-token', token)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
const expectedPayload = await getTemplateMock(template);
|
||||||
|
|
||||||
|
expect(response.body).toStrictEqual(expectedPayload);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
import { Router } from 'express';
|
import { Router } from 'express';
|
||||||
|
import getTemplateAction from '../../../controllers/api/v1/templates/get-template.ee.js';
|
||||||
import getTemplatesAction from '../../../controllers/api/v1/templates/get-templates.ee.js';
|
import getTemplatesAction from '../../../controllers/api/v1/templates/get-templates.ee.js';
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.get('/', getTemplatesAction);
|
router.get('/', getTemplatesAction);
|
||||||
|
router.get('/:templateId', getTemplateAction);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
const getTemplateMock = async (template) => {
|
||||||
|
const data = {
|
||||||
|
id: template.id,
|
||||||
|
name: template.name,
|
||||||
|
createdAt: template.createdAt.getTime(),
|
||||||
|
updatedAt: template.updatedAt.getTime(),
|
||||||
|
flowData: template.getFlowDataWithIconUrls(),
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: data,
|
||||||
|
meta: {
|
||||||
|
count: 1,
|
||||||
|
currentPage: null,
|
||||||
|
isArray: false,
|
||||||
|
totalPages: null,
|
||||||
|
type: 'Template',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default getTemplateMock;
|
||||||
@@ -29,8 +29,8 @@ export default defineConfig({
|
|||||||
thresholds: {
|
thresholds: {
|
||||||
autoUpdate: true,
|
autoUpdate: true,
|
||||||
statements: 99.41,
|
statements: 99.41,
|
||||||
branches: 98.34,
|
branches: 98.35,
|
||||||
functions: 99.06,
|
functions: 99.07,
|
||||||
lines: 99.41,
|
lines: 99.41,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user