diff --git a/packages/backend/src/controllers/api/v1/templates/get-templates.ee.js b/packages/backend/src/controllers/api/v1/templates/get-templates.ee.js new file mode 100644 index 00000000..e9e32613 --- /dev/null +++ b/packages/backend/src/controllers/api/v1/templates/get-templates.ee.js @@ -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', + }); +}; diff --git a/packages/backend/src/controllers/api/v1/templates/get-templates.ee.test.js b/packages/backend/src/controllers/api/v1/templates/get-templates.ee.test.js new file mode 100644 index 00000000..c5634c02 --- /dev/null +++ b/packages/backend/src/controllers/api/v1/templates/get-templates.ee.test.js @@ -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); + }); +}); diff --git a/packages/backend/test/mocks/rest/api/v1/templates/get-templates.ee.js b/packages/backend/test/mocks/rest/api/v1/templates/get-templates.ee.js new file mode 100644 index 00000000..e296e44b --- /dev/null +++ b/packages/backend/test/mocks/rest/api/v1/templates/get-templates.ee.js @@ -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;