refactor: Only return JSON for flow export
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
|
import { renderObject } from '../../../../helpers/renderer.js';
|
||||||
|
|
||||||
export default async (request, response) => {
|
export default async (request, response) => {
|
||||||
const flow = await request.currentUser.authorizedFlows
|
const flow = await request.currentUser.authorizedFlows
|
||||||
.findById(request.params.flowId)
|
.findById(request.params.flowId)
|
||||||
.throwIfNotFound();
|
.throwIfNotFound();
|
||||||
|
|
||||||
const { exportedFlowAsString, slug } = await flow.export();
|
const exportedFlow = await flow.export();
|
||||||
|
|
||||||
response.status(201).attachment(slug).send(exportedFlowAsString);
|
return renderObject(response, exportedFlow, { status: 201 });
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -67,20 +67,12 @@ describe('POST /api/v1/flows/:flowId/export', () => {
|
|||||||
.set('Authorization', token)
|
.set('Authorization', token)
|
||||||
.expect(201);
|
.expect(201);
|
||||||
|
|
||||||
// Test headers for file attachment
|
const expectedPayload = await exportFlowMock(currentUserFlow, [
|
||||||
expect(response.headers['content-disposition']).toContain(
|
|
||||||
'attachment; filename="name-your-flow.json"'
|
|
||||||
);
|
|
||||||
expect(response.headers['content-type']).toBe(
|
|
||||||
'application/json; charset=utf-8'
|
|
||||||
);
|
|
||||||
|
|
||||||
const expectedFileStructure = await exportFlowMock(currentUserFlow, [
|
|
||||||
triggerStep,
|
triggerStep,
|
||||||
actionStep,
|
actionStep,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(response.body).toStrictEqual(expectedFileStructure);
|
expect(response.body).toStrictEqual(expectedPayload);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should export the flow data of another user', async () => {
|
it('should export the flow data of another user', async () => {
|
||||||
@@ -132,19 +124,12 @@ describe('POST /api/v1/flows/:flowId/export', () => {
|
|||||||
.set('Authorization', token)
|
.set('Authorization', token)
|
||||||
.expect(201);
|
.expect(201);
|
||||||
|
|
||||||
expect(response.headers['content-disposition']).toStrictEqual(
|
const expectedPayload = await exportFlowMock(anotherUserFlow, [
|
||||||
'attachment; filename="name-your-flow.json"'
|
|
||||||
);
|
|
||||||
expect(response.headers['content-type']).toStrictEqual(
|
|
||||||
'application/json; charset=utf-8'
|
|
||||||
);
|
|
||||||
|
|
||||||
const expectedFileStructure = await exportFlowMock(anotherUserFlow, [
|
|
||||||
triggerStep,
|
triggerStep,
|
||||||
actionStep,
|
actionStep,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(response.body).toStrictEqual(expectedFileStructure);
|
expect(response.body).toStrictEqual(expectedPayload);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return not found response for not existing flow UUID', async () => {
|
it('should return not found response for not existing flow UUID', async () => {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { ValidationError } from 'objection';
|
import { ValidationError } from 'objection';
|
||||||
import slugify from 'slugify';
|
|
||||||
import Base from './base.js';
|
import Base from './base.js';
|
||||||
import Step from './step.js';
|
import Step from './step.js';
|
||||||
import User from './user.js';
|
import User from './user.js';
|
||||||
@@ -428,22 +427,8 @@ class Flow extends Base {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
slugifyNameAsFilename() {
|
|
||||||
const slug = slugify(this.name, {
|
|
||||||
lower: true,
|
|
||||||
strict: true,
|
|
||||||
replacement: '-',
|
|
||||||
});
|
|
||||||
|
|
||||||
return `${slug}.json`;
|
|
||||||
}
|
|
||||||
|
|
||||||
async export() {
|
async export() {
|
||||||
const exportedFlow = await exportFlow(this);
|
return await exportFlow(this);
|
||||||
const exportedFlowAsString = JSON.stringify(exportedFlow, null, 2);
|
|
||||||
const slug = this.slugifyNameAsFilename();
|
|
||||||
|
|
||||||
return { exportedFlowAsString, slug };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async $beforeUpdate(opt, queryContext) {
|
async $beforeUpdate(opt, queryContext) {
|
||||||
|
|||||||
@@ -507,65 +507,18 @@ describe('Flow model', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('slugifyNameAsFilename', () => {
|
|
||||||
it('should generate a slug file name from flow name', async () => {
|
|
||||||
const flow = await createFlow({
|
|
||||||
name: 'My Flow Name',
|
|
||||||
});
|
|
||||||
|
|
||||||
const slug = flow.slugifyNameAsFilename();
|
|
||||||
expect(slug).toBe('my-flow-name.json');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('export', () => {
|
describe('export', () => {
|
||||||
it('should call slugifyNameAsFilename method', async () => {
|
it('should return exportedFlow', async () => {
|
||||||
const flow = await createFlow({
|
|
||||||
name: 'My Flow Name',
|
|
||||||
});
|
|
||||||
|
|
||||||
const slugifyNameAsFilenameSpy = vi
|
|
||||||
.spyOn(flow, 'slugifyNameAsFilename')
|
|
||||||
.mockImplementation(() => 'my-flow-name.json');
|
|
||||||
|
|
||||||
await flow.export();
|
|
||||||
|
|
||||||
expect(slugifyNameAsFilenameSpy).toHaveBeenCalledOnce();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should call exportFlow method', async () => {
|
|
||||||
const flow = await createFlow();
|
|
||||||
|
|
||||||
const exportFlowSpy = vi
|
|
||||||
.spyOn(exportFlow, 'default')
|
|
||||||
.mockImplementation(() => {});
|
|
||||||
|
|
||||||
await flow.export();
|
|
||||||
|
|
||||||
expect(exportFlowSpy).toHaveBeenCalledOnce();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return exportedFlowAsString and slug', async () => {
|
|
||||||
const flow = await createFlow();
|
const flow = await createFlow();
|
||||||
|
|
||||||
const exportedFlowAsString = {
|
const exportedFlowAsString = {
|
||||||
name: 'My Flow Name',
|
name: 'My Flow Name',
|
||||||
};
|
};
|
||||||
|
|
||||||
const slug = 'slug';
|
|
||||||
|
|
||||||
vi.spyOn(exportFlow, 'default').mockReturnValue(exportedFlowAsString);
|
vi.spyOn(exportFlow, 'default').mockReturnValue(exportedFlowAsString);
|
||||||
vi.spyOn(flow, 'slugifyNameAsFilename').mockReturnValue(slug);
|
|
||||||
|
|
||||||
const expectedExportedFlowAsString = JSON.stringify(
|
|
||||||
exportedFlowAsString,
|
|
||||||
null,
|
|
||||||
2
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(await flow.export()).toStrictEqual({
|
expect(await flow.export()).toStrictEqual({
|
||||||
exportedFlowAsString: expectedExportedFlowAsString,
|
name: 'My Flow Name',
|
||||||
slug: 'slug',
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user