feat: Add user API endpoint to get templates

This commit is contained in:
Faruk AYDIN
2025-03-06 15:34:09 +01:00
parent 609360b0e6
commit 4a626016b2
3 changed files with 100 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
import { renderObject } from '../../../../helpers/renderer.js';
import Template from '../../../../models/template.ee.js';
export default async (request, response) => {
const templates = await Template.query().orderBy('created_at', 'asc');
renderObject(response, templates, {
serializer: 'Template',
});
};

View File

@@ -0,0 +1,68 @@
import { vi, describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../../../app.js';
import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id.js';
import { createUser } from '../../../../../test/factories/user.js';
import { createTemplate } from '../../../../../test/factories/template.js';
import { updateConfig } from '../../../../../test/factories/config.js';
import { createPermission } from '../../../../../test/factories/permission.js';
import getTemplatesMock from '../../../../../test/mocks/rest/api/v1/templates/get-templates.ee.js';
import * as license from '../../../../helpers/license.ee.js';
describe('GET /api/v1/templates', () => {
let currentUser, currentUserRole, token;
beforeEach(async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
currentUser = await createUser();
currentUserRole = await currentUser.$relatedQuery('role');
token = await createAuthTokenByUserId(currentUser.id);
await updateConfig({ enableTemplates: true });
});
it('should return templates when templates are enabled and user has create flow permission', async () => {
await createPermission({
action: 'create',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: [],
});
const templateOne = await createTemplate();
const templateTwo = await createTemplate();
const response = await request(app)
.get('/api/v1/templates')
.set('Authorization', token)
.expect(200);
const expectedPayload = await getTemplatesMock([templateOne, templateTwo]);
expect(response.body).toStrictEqual(expectedPayload);
});
it('should return 403 when templates are disabled', async () => {
await createPermission({
action: 'create',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: [],
});
await updateConfig({ enableTemplates: false });
await request(app)
.get('/api/v1/templates')
.set('Authorization', token)
.expect(403);
});
it('should return 403 when user does not have create flow permission', async () => {
await request(app)
.get('/api/v1/templates')
.set('Authorization', token)
.expect(403);
});
});

View File

@@ -0,0 +1,22 @@
const getTemplatesMock = async (templates) => {
const data = templates.map((template) => ({
id: template.id,
name: template.name,
flowData: template.flowData,
createdAt: template.createdAt.getTime(),
updatedAt: template.updatedAt.getTime(),
}));
return {
data: data,
meta: {
count: data.length,
currentPage: null,
isArray: true,
totalPages: null,
type: 'Template',
},
};
};
export default getTemplatesMock;