refactor: Only return JSON for flow export

This commit is contained in:
Faruk AYDIN
2025-01-13 12:14:16 +01:00
parent 51291889cd
commit f15d1ac7b1
4 changed files with 11 additions and 86 deletions

View File

@@ -1,9 +1,11 @@
import { renderObject } from '../../../../helpers/renderer.js';
export default async (request, response) => {
const flow = await request.currentUser.authorizedFlows
.findById(request.params.flowId)
.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 });
};

View File

@@ -67,20 +67,12 @@ describe('POST /api/v1/flows/:flowId/export', () => {
.set('Authorization', token)
.expect(201);
// Test headers for file attachment
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, [
const expectedPayload = await exportFlowMock(currentUserFlow, [
triggerStep,
actionStep,
]);
expect(response.body).toStrictEqual(expectedFileStructure);
expect(response.body).toStrictEqual(expectedPayload);
});
it('should export the flow data of another user', async () => {
@@ -132,19 +124,12 @@ describe('POST /api/v1/flows/:flowId/export', () => {
.set('Authorization', token)
.expect(201);
expect(response.headers['content-disposition']).toStrictEqual(
'attachment; filename="name-your-flow.json"'
);
expect(response.headers['content-type']).toStrictEqual(
'application/json; charset=utf-8'
);
const expectedFileStructure = await exportFlowMock(anotherUserFlow, [
const expectedPayload = await exportFlowMock(anotherUserFlow, [
triggerStep,
actionStep,
]);
expect(response.body).toStrictEqual(expectedFileStructure);
expect(response.body).toStrictEqual(expectedPayload);
});
it('should return not found response for not existing flow UUID', async () => {

View File

@@ -1,5 +1,4 @@
import { ValidationError } from 'objection';
import slugify from 'slugify';
import Base from './base.js';
import Step from './step.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() {
const exportedFlow = await exportFlow(this);
const exportedFlowAsString = JSON.stringify(exportedFlow, null, 2);
const slug = this.slugifyNameAsFilename();
return { exportedFlowAsString, slug };
return await exportFlow(this);
}
async $beforeUpdate(opt, queryContext) {

View File

@@ -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', () => {
it('should call slugifyNameAsFilename method', 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 () => {
it('should return exportedFlow', async () => {
const flow = await createFlow();
const exportedFlowAsString = {
name: 'My Flow Name',
};
const slug = 'slug';
vi.spyOn(exportFlow, 'default').mockReturnValue(exportedFlowAsString);
vi.spyOn(flow, 'slugifyNameAsFilename').mockReturnValue(slug);
const expectedExportedFlowAsString = JSON.stringify(
exportedFlowAsString,
null,
2
);
expect(await flow.export()).toStrictEqual({
exportedFlowAsString: expectedExportedFlowAsString,
slug: 'slug',
name: 'My Flow Name',
});
});
});