Merge pull request #2385 from automatisch/user-templates
Implement user templates API 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 templates = await Template.query().orderBy('created_at', 'asc');
|
||||
|
||||
renderObject(response, templates, {
|
||||
serializer: 'Template',
|
||||
});
|
||||
};
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -33,6 +33,10 @@ const authorizationList = {
|
||||
action: 'delete',
|
||||
subject: 'Flow',
|
||||
},
|
||||
'GET /api/v1/templates/': {
|
||||
action: 'create',
|
||||
subject: 'Flow',
|
||||
},
|
||||
'GET /api/v1/steps/:stepId/connection': {
|
||||
action: 'read',
|
||||
subject: 'Flow',
|
||||
|
||||
12
packages/backend/src/helpers/check-templates-enabled.js
Normal file
12
packages/backend/src/helpers/check-templates-enabled.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import Config from '../models/config.js';
|
||||
import NotAuthorizedError from '../errors/not-authorized.js';
|
||||
|
||||
export const checkTemplatesEnabled = async (request, response, next) => {
|
||||
const config = await Config.get();
|
||||
|
||||
if (!config.enableTemplates) {
|
||||
throw new NotAuthorizedError();
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
7
packages/backend/src/helpers/generate-icon-url.js
Normal file
7
packages/backend/src/helpers/generate-icon-url.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import appConfig from '../config/app.js';
|
||||
|
||||
export const generateIconUrl = (appKey) => {
|
||||
if (!appKey) return null;
|
||||
|
||||
return `${appConfig.baseUrl}/apps/${appKey}/assets/favicon.svg`;
|
||||
};
|
||||
@@ -9,6 +9,7 @@ import appConfig from '../config/app.js';
|
||||
import globalVariable from '../helpers/global-variable.js';
|
||||
import computeParameters from '../helpers/compute-parameters.js';
|
||||
import testRun from '../services/test-run.js';
|
||||
import { generateIconUrl } from '../helpers/generate-icon-url.js';
|
||||
|
||||
class Step extends Base {
|
||||
static tableName = 'steps';
|
||||
@@ -88,9 +89,7 @@ class Step extends Base {
|
||||
}
|
||||
|
||||
get iconUrl() {
|
||||
if (!this.appKey) return null;
|
||||
|
||||
return `${appConfig.baseUrl}/apps/${this.appKey}/assets/favicon.svg`;
|
||||
return generateIconUrl(this.appKey);
|
||||
}
|
||||
|
||||
get isTrigger() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import Base from './base.js';
|
||||
import Flow from './flow.js';
|
||||
import { generateIconUrl } from '../helpers/generate-icon-url.js';
|
||||
|
||||
class Template extends Base {
|
||||
static tableName = 'templates';
|
||||
@@ -23,6 +24,18 @@ class Template extends Base {
|
||||
|
||||
return this.query().insertAndFetch({ name, flowData });
|
||||
}
|
||||
|
||||
getFlowDataWithIconUrls() {
|
||||
if (!this.flowData) return null;
|
||||
|
||||
return {
|
||||
...this.flowData,
|
||||
steps: this.flowData.steps?.map((step) => ({
|
||||
...step,
|
||||
iconUrl: generateIconUrl(step.appKey),
|
||||
})),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default Template;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import Crypto from 'crypto';
|
||||
import Template from './template.ee.js';
|
||||
import { createFlow } from '../../test/factories/flow';
|
||||
import { createStep } from '../../test/factories/step';
|
||||
import appConfig from '../config/app.js';
|
||||
|
||||
describe('Template model', () => {
|
||||
it('tableName should return correct name', () => {
|
||||
@@ -76,4 +77,96 @@ describe('Template model', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFlowDataWithIconUrls', () => {
|
||||
beforeEach(() => {
|
||||
vi.spyOn(appConfig, 'baseUrl', 'get').mockReturnValue(
|
||||
'https://automatisch.io'
|
||||
);
|
||||
});
|
||||
|
||||
it('should add iconUrl to each step in flowData', () => {
|
||||
const template = new Template();
|
||||
template.flowData = {
|
||||
id: 'flow-id',
|
||||
name: 'Test Flow',
|
||||
steps: [
|
||||
{
|
||||
id: 'step-1',
|
||||
appKey: 'webhook',
|
||||
type: 'trigger',
|
||||
},
|
||||
{
|
||||
id: 'step-2',
|
||||
appKey: 'formatter',
|
||||
type: 'action',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = template.getFlowDataWithIconUrls();
|
||||
|
||||
expect(result.steps[0].iconUrl).toBe(
|
||||
'https://automatisch.io/apps/webhook/assets/favicon.svg'
|
||||
);
|
||||
expect(result.steps[1].iconUrl).toBe(
|
||||
'https://automatisch.io/apps/formatter/assets/favicon.svg'
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle steps with null appKey', () => {
|
||||
const template = new Template();
|
||||
template.flowData = {
|
||||
id: 'flow-id',
|
||||
name: 'Test Flow',
|
||||
steps: [
|
||||
{
|
||||
id: 'step-1',
|
||||
appKey: null,
|
||||
type: 'trigger',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = template.getFlowDataWithIconUrls();
|
||||
|
||||
expect(result.steps[0].iconUrl).toBeNull();
|
||||
});
|
||||
|
||||
it('should preserve all other flowData properties', () => {
|
||||
const template = new Template();
|
||||
template.flowData = {
|
||||
id: 'flow-id',
|
||||
name: 'Test Flow',
|
||||
customField: 'test',
|
||||
steps: [
|
||||
{
|
||||
id: 'step-1',
|
||||
appKey: 'webhook',
|
||||
type: 'trigger',
|
||||
position: 1,
|
||||
parameters: { test: true },
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = template.getFlowDataWithIconUrls();
|
||||
|
||||
expect(result).toEqual({
|
||||
id: 'flow-id',
|
||||
name: 'Test Flow',
|
||||
customField: 'test',
|
||||
steps: [
|
||||
{
|
||||
id: 'step-1',
|
||||
appKey: 'webhook',
|
||||
type: 'trigger',
|
||||
position: 1,
|
||||
parameters: { test: true },
|
||||
iconUrl: 'https://automatisch.io/apps/webhook/assets/favicon.svg',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
20
packages/backend/src/routes/api/v1/templates.ee.js
Normal file
20
packages/backend/src/routes/api/v1/templates.ee.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Router } from 'express';
|
||||
import { authenticateUser } from '../../../helpers/authentication.js';
|
||||
import { authorizeUser } from '../../../helpers/authorization.js';
|
||||
import { checkIsEnterprise } from '../../../helpers/check-is-enterprise.js';
|
||||
import { checkTemplatesEnabled } from '../../../helpers/check-templates-enabled.js';
|
||||
|
||||
import getTemplatesAction from '../../../controllers/api/v1/templates/get-templates.ee.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get(
|
||||
'/',
|
||||
authenticateUser,
|
||||
authorizeUser,
|
||||
checkIsEnterprise,
|
||||
checkTemplatesEnabled,
|
||||
getTemplatesAction
|
||||
);
|
||||
|
||||
export default router;
|
||||
@@ -16,6 +16,7 @@ import adminAppsRouter from './api/v1/admin/apps.ee.js';
|
||||
import adminConfigRouter from './api/v1/admin/config.ee.js';
|
||||
import adminSamlAuthProvidersRouter from './api/v1/admin/saml-auth-providers.ee.js';
|
||||
import adminTemplatesRouter from './api/v1/admin/templates.ee.js';
|
||||
import templatesRouter from './api/v1/templates.ee.js';
|
||||
import rolesRouter from './api/v1/admin/roles.ee.js';
|
||||
import permissionsRouter from './api/v1/admin/permissions.ee.js';
|
||||
import adminUsersRouter from './api/v1/admin/users.ee.js';
|
||||
@@ -44,6 +45,7 @@ router.use('/api/v1/admin/roles', rolesRouter);
|
||||
router.use('/api/v1/admin/permissions', permissionsRouter);
|
||||
router.use('/api/v1/admin/saml-auth-providers', adminSamlAuthProvidersRouter);
|
||||
router.use('/api/v1/admin/templates', adminTemplatesRouter);
|
||||
router.use('/api/v1/templates', templatesRouter);
|
||||
router.use('/api/v1/installation/users', installationUsersRouter);
|
||||
router.use('/api/v1/folders', foldersRouter);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import roleSerializer from './role.js';
|
||||
import permissionSerializer from './permission.js';
|
||||
import adminSamlAuthProviderSerializer from './admin-saml-auth-provider.ee.js';
|
||||
import adminTemplateSerializer from './admin/template.ee.js';
|
||||
import templateSerializer from './template.ee.js';
|
||||
import samlAuthProviderSerializer from './saml-auth-provider.ee.js';
|
||||
import samlAuthProviderRoleMappingSerializer from './role-mapping.ee.js';
|
||||
import oauthClientSerializer from './oauth-client.js';
|
||||
@@ -29,6 +30,7 @@ const serializers = {
|
||||
Permission: permissionSerializer,
|
||||
AdminSamlAuthProvider: adminSamlAuthProviderSerializer,
|
||||
AdminTemplate: adminTemplateSerializer,
|
||||
Template: templateSerializer,
|
||||
SamlAuthProvider: samlAuthProviderSerializer,
|
||||
RoleMapping: samlAuthProviderRoleMappingSerializer,
|
||||
OAuthClient: oauthClientSerializer,
|
||||
|
||||
11
packages/backend/src/serializers/template.ee.js
Normal file
11
packages/backend/src/serializers/template.ee.js
Normal file
@@ -0,0 +1,11 @@
|
||||
const templateSerializer = (template) => {
|
||||
return {
|
||||
id: template.id,
|
||||
name: template.name,
|
||||
flowData: template.getFlowDataWithIconUrls(),
|
||||
createdAt: template.createdAt.getTime(),
|
||||
updatedAt: template.updatedAt.getTime(),
|
||||
};
|
||||
};
|
||||
|
||||
export default templateSerializer;
|
||||
22
packages/backend/src/serializers/template.ee.test.js
Normal file
22
packages/backend/src/serializers/template.ee.test.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import templateSerializer from './template.ee.js';
|
||||
import { createTemplate } from '../../test/factories/template.js';
|
||||
describe('templateSerializer', () => {
|
||||
let template;
|
||||
|
||||
beforeEach(async () => {
|
||||
template = await createTemplate();
|
||||
});
|
||||
|
||||
it('should return flow data', async () => {
|
||||
const expectedPayload = {
|
||||
id: template.id,
|
||||
name: template.name,
|
||||
flowData: template.getFlowDataWithIconUrls(),
|
||||
createdAt: template.createdAt.getTime(),
|
||||
updatedAt: template.updatedAt.getTime(),
|
||||
};
|
||||
|
||||
expect(templateSerializer(template)).toStrictEqual(expectedPayload);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
const getTemplatesMock = async (templates) => {
|
||||
const data = templates.map((template) => {
|
||||
return {
|
||||
id: template.id,
|
||||
name: template.name,
|
||||
flowData: template.getFlowDataWithIconUrls(),
|
||||
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;
|
||||
Reference in New Issue
Block a user