first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import type { IncomingMessage } from 'http';
|
||||
import type { MockProxy } from 'jest-mock-extended';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { type IHttpRequestMethods, type IExecuteFunctions, ApplicationError } from 'n8n-workflow';
|
||||
|
||||
import * as genericFunctions from '../../GenericFunctions';
|
||||
import { MicrosoftOneDrive } from '../../MicrosoftOneDrive.node';
|
||||
|
||||
jest.mock('../../GenericFunctions', () => ({
|
||||
...jest.requireActual('../../GenericFunctions'),
|
||||
microsoftApiRequest: jest.fn(async function (_: IHttpRequestMethods, resource: string) {
|
||||
if (resource === '/drive/items/fileID') {
|
||||
return {
|
||||
name: 'MyFile',
|
||||
'@microsoft.graph.downloadUrl': 'https://test.com/file',
|
||||
file: {
|
||||
mimeType: 'image/png',
|
||||
},
|
||||
};
|
||||
}
|
||||
if (resource === '/drive/items/fileID/content') {
|
||||
throw new ApplicationError('Error');
|
||||
}
|
||||
}),
|
||||
}));
|
||||
|
||||
describe('Test MicrosoftOneDrive, file > download', () => {
|
||||
let mockExecuteFunctions: MockProxy<IExecuteFunctions>;
|
||||
let microsoftOneDrive: MicrosoftOneDrive;
|
||||
const httpRequest = jest.fn(async () => ({ body: mock<IncomingMessage>() }));
|
||||
const prepareBinaryData = jest.fn(async () => ({ data: 'testBinary' }));
|
||||
|
||||
const mockNode = {
|
||||
id: 'test-node-id',
|
||||
name: 'Microsoft OneDrive Test',
|
||||
type: 'n8n-nodes-base.microsoftOneDrive',
|
||||
typeVersion: 1.1,
|
||||
position: [0, 0] as [number, number],
|
||||
parameters: {},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockExecuteFunctions = mock<IExecuteFunctions>();
|
||||
microsoftOneDrive = new MicrosoftOneDrive();
|
||||
|
||||
mockExecuteFunctions.helpers = {
|
||||
httpRequest,
|
||||
prepareBinaryData,
|
||||
returnJsonArray: jest.fn((data) => [data]),
|
||||
constructExecutionMetaData: jest.fn((data) => data),
|
||||
} as any;
|
||||
|
||||
mockExecuteFunctions.getNode.mockReturnValue(mockNode);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should call helpers.httpRequest when request to /drive/items/{fileId}/content fails', async () => {
|
||||
const items = [{ json: { data: 'test' } }];
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNodeParameter.mockImplementation((key: string) => {
|
||||
if (key === 'resource') return 'file';
|
||||
if (key === 'operation') return 'download';
|
||||
if (key === 'fileId') return 'fileID';
|
||||
if (key === 'binaryPropertyName') return 'data';
|
||||
});
|
||||
|
||||
const result = await microsoftOneDrive.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(genericFunctions.microsoftApiRequest).toHaveBeenCalledTimes(2);
|
||||
expect(httpRequest).toHaveBeenCalledTimes(1);
|
||||
expect(httpRequest).toHaveBeenCalledWith({
|
||||
encoding: 'arraybuffer',
|
||||
json: false,
|
||||
method: 'GET',
|
||||
returnFullResponse: true,
|
||||
url: 'https://test.com/file',
|
||||
});
|
||||
expect(prepareBinaryData).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual([
|
||||
[{ binary: { data: { data: 'testBinary' } }, json: { data: 'test' } }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,165 @@
|
||||
import type { MockProxy } from 'jest-mock-extended';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import type { IExecuteFunctions, IBinaryData } from 'n8n-workflow';
|
||||
|
||||
import * as genericFunctions from '../../GenericFunctions';
|
||||
import { MicrosoftOneDrive } from '../../MicrosoftOneDrive.node';
|
||||
|
||||
jest.mock('../../GenericFunctions', () => ({
|
||||
...jest.requireActual('../../GenericFunctions'),
|
||||
microsoftApiRequest: jest.fn(async function () {
|
||||
return JSON.stringify({
|
||||
id: 'uploadedFileId',
|
||||
name: 'uploadedFile.txt',
|
||||
});
|
||||
}),
|
||||
}));
|
||||
|
||||
describe('Test MicrosoftOneDrive, file > upload', () => {
|
||||
let mockExecuteFunctions: MockProxy<IExecuteFunctions>;
|
||||
let microsoftOneDrive: MicrosoftOneDrive;
|
||||
const getBinaryDataBuffer = jest.fn(async () => Buffer.from('test content'));
|
||||
|
||||
const mockNode = {
|
||||
id: 'test-node-id',
|
||||
name: 'Microsoft OneDrive Test',
|
||||
type: 'n8n-nodes-base.microsoftOneDrive',
|
||||
typeVersion: 1.1,
|
||||
position: [0, 0] as [number, number],
|
||||
parameters: {},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockExecuteFunctions = mock<IExecuteFunctions>();
|
||||
microsoftOneDrive = new MicrosoftOneDrive();
|
||||
|
||||
mockExecuteFunctions.helpers = {
|
||||
assertBinaryData: jest.fn(() => {
|
||||
return {
|
||||
data: 'base64data',
|
||||
mimeType: 'text/plain',
|
||||
fileName: 'binaryFileName.txt',
|
||||
} as IBinaryData;
|
||||
}),
|
||||
getBinaryDataBuffer,
|
||||
returnJsonArray: jest.fn((data) => [data]),
|
||||
constructExecutionMetaData: jest.fn((data) => data),
|
||||
} as any;
|
||||
|
||||
mockExecuteFunctions.getNode.mockReturnValue(mockNode);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should use filename from node parameters even when binary has a filename (version 1.1+)', async () => {
|
||||
const items = [{ json: { data: 'test' }, binary: { data: {} as IBinaryData } }];
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNodeParameter.mockImplementation((key: string) => {
|
||||
if (key === 'resource') return 'file';
|
||||
if (key === 'operation') return 'upload';
|
||||
if (key === 'parentId') return 'parentFolderId';
|
||||
if (key === 'fileName') return 'customFileName.txt';
|
||||
if (key === 'binaryData') return true;
|
||||
if (key === 'binaryPropertyName') return 'data';
|
||||
});
|
||||
|
||||
await microsoftOneDrive.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(genericFunctions.microsoftApiRequest).toHaveBeenCalledTimes(1);
|
||||
expect(genericFunctions.microsoftApiRequest).toHaveBeenCalledWith(
|
||||
'PUT',
|
||||
'/drive/items/parentFolderId:/customFileName.txt:/content',
|
||||
expect.any(Buffer),
|
||||
{},
|
||||
undefined,
|
||||
{ 'Content-Type': 'text/plain', 'Content-length': expect.any(Number) },
|
||||
{},
|
||||
);
|
||||
});
|
||||
|
||||
it('should use binary filename when node parameter filename is empty', async () => {
|
||||
const items = [{ json: { data: 'test' }, binary: { data: {} as IBinaryData } }];
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNodeParameter.mockImplementation((key: string) => {
|
||||
if (key === 'resource') return 'file';
|
||||
if (key === 'operation') return 'upload';
|
||||
if (key === 'parentId') return 'parentFolderId';
|
||||
if (key === 'fileName') return '';
|
||||
if (key === 'binaryData') return true;
|
||||
if (key === 'binaryPropertyName') return 'data';
|
||||
});
|
||||
|
||||
await microsoftOneDrive.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(genericFunctions.microsoftApiRequest).toHaveBeenCalledTimes(1);
|
||||
expect(genericFunctions.microsoftApiRequest).toHaveBeenCalledWith(
|
||||
'PUT',
|
||||
'/drive/items/parentFolderId:/binaryFileName.txt:/content',
|
||||
expect.any(Buffer),
|
||||
{},
|
||||
undefined,
|
||||
{ 'Content-Type': 'text/plain', 'Content-length': expect.any(Number) },
|
||||
{},
|
||||
);
|
||||
});
|
||||
|
||||
it('should properly encode special characters in filename from node parameters', async () => {
|
||||
const items = [{ json: { data: 'test' }, binary: { data: {} as IBinaryData } }];
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNodeParameter.mockImplementation((key: string) => {
|
||||
if (key === 'resource') return 'file';
|
||||
if (key === 'operation') return 'upload';
|
||||
if (key === 'parentId') return 'parentFolderId';
|
||||
if (key === 'fileName') return 'file with spaces & special.txt';
|
||||
if (key === 'binaryData') return true;
|
||||
if (key === 'binaryPropertyName') return 'data';
|
||||
});
|
||||
|
||||
await microsoftOneDrive.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(genericFunctions.microsoftApiRequest).toHaveBeenCalledTimes(1);
|
||||
expect(genericFunctions.microsoftApiRequest).toHaveBeenCalledWith(
|
||||
'PUT',
|
||||
'/drive/items/parentFolderId:/file%20with%20spaces%20%26%20special.txt:/content',
|
||||
expect.any(Buffer),
|
||||
{},
|
||||
undefined,
|
||||
{ 'Content-Type': 'text/plain', 'Content-length': expect.any(Number) },
|
||||
{},
|
||||
);
|
||||
});
|
||||
|
||||
it('should always use binary filename even when node parameter has a filename (version 1 - legacy bug)', async () => {
|
||||
mockExecuteFunctions.getNode.mockReturnValue({
|
||||
...mockNode,
|
||||
typeVersion: 1,
|
||||
});
|
||||
|
||||
const items = [{ json: { data: 'test' }, binary: { data: {} as IBinaryData } }];
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNodeParameter.mockImplementation((key: string) => {
|
||||
if (key === 'resource') return 'file';
|
||||
if (key === 'operation') return 'upload';
|
||||
if (key === 'parentId') return 'parentFolderId';
|
||||
if (key === 'fileName') return 'customFileName.txt';
|
||||
if (key === 'binaryData') return true;
|
||||
if (key === 'binaryPropertyName') return 'data';
|
||||
});
|
||||
|
||||
await microsoftOneDrive.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(genericFunctions.microsoftApiRequest).toHaveBeenCalledTimes(1);
|
||||
// In version 1, binary filename always overwrites the parameter filename
|
||||
expect(genericFunctions.microsoftApiRequest).toHaveBeenCalledWith(
|
||||
'PUT',
|
||||
'/drive/items/parentFolderId:/binaryFileName.txt:/content',
|
||||
expect.any(Buffer),
|
||||
{},
|
||||
undefined,
|
||||
{ 'Content-Type': 'text/plain', 'Content-length': expect.any(Number) },
|
||||
{},
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user