-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Microsoft OneDrive Node): Try to download file using downloadUrl (#…
…13200) Co-authored-by: Michael Kret <[email protected]>
- Loading branch information
1 parent
56426e9
commit 67cd05c
Showing
2 changed files
with
106 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/nodes-base/nodes/Microsoft/OneDrive/test/node/file.download.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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' })); | ||
|
||
beforeEach(() => { | ||
mockExecuteFunctions = mock<IExecuteFunctions>(); | ||
microsoftOneDrive = new MicrosoftOneDrive(); | ||
|
||
mockExecuteFunctions.helpers = { | ||
httpRequest, | ||
prepareBinaryData, | ||
returnJsonArray: jest.fn((data) => [data]), | ||
constructExecutionMetaData: jest.fn((data) => data), | ||
} as any; | ||
}); | ||
|
||
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' } }], | ||
]); | ||
}); | ||
}); |