chore: test file upload for baserow node

This commit is contained in:
Cedric Ziel 2024-12-06 08:30:38 +00:00
parent 84133eeace
commit 350eb81078
3 changed files with 101 additions and 16 deletions

View file

@ -17,8 +17,6 @@ import {
toOptions,
} from './GenericFunctions';
import { operationFields } from './OperationDescription';
import type {
BaserowCredentials,
FieldsUiValues,

View file

@ -1,9 +1,10 @@
import type {
IDataObject,
IExecuteFunctions,
IHookFunctions,
IHttpRequestMethods,
IHttpRequestOptions,
ILoadOptionsFunctions,
IRequestOptions,
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
@ -11,21 +12,21 @@ import { NodeApiError } from 'n8n-workflow';
import type { Accumulator, BaserowCredentials, LoadedResource } from './types';
export async function baserowFileUploadRequest(
this: IExecuteFunctions,
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
jwtToken: string,
file: Buffer,
fileName: string,
mimeType: string,
) {
const credentials = (await this.getCredentials('baserowApi')) as BaserowCredentials;
const credentials = await this.getCredentials<BaserowCredentials>('baserowApi');
const options: OptionsWithUri = {
const options: IHttpRequestOptions = {
headers: {
Authorization: `JWT ${jwtToken}`,
'Content-Type': 'multipart/form-data',
},
method: 'POST',
uri: `${credentials.host}/api/user-files/upload-file/`,
url: `${credentials.host}/api/user-files/upload-file/`,
json: false,
body: {
file: {
@ -39,7 +40,7 @@ export async function baserowFileUploadRequest(
};
try {
return await this.helpers.request(options);
return await this.helpers.httpRequest(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
@ -49,7 +50,7 @@ export async function baserowFileUploadRequest(
* Make a request to Baserow API.
*/
export async function baserowApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions,
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: IHttpRequestMethods,
endpoint: string,
jwtToken: string,
@ -58,25 +59,24 @@ export async function baserowApiRequest(
) {
const credentials = await this.getCredentials<BaserowCredentials>('baserowApi');
const options: IRequestOptions = {
const options: IHttpRequestOptions = {
headers: {
Authorization: `JWT ${jwtToken}`,
},
method,
body,
qs,
uri: `${credentials.host}${endpoint}`,
url: `${credentials.host}${endpoint}`,
json: true,
};
if (body.formData) {
options.json = false;
// set content type to multipart/form-data
options.headers = {
...options.headers,
'Content-Type': 'multipart/form-data',
};
options.body.resolveWithFullResponse = true;
options.returnFullResponse = true;
}
if (Object.keys(qs).length === 0) {
@ -88,7 +88,7 @@ export async function baserowApiRequest(
}
try {
return await this.helpers.request(options);
return await this.helpers.httpRequest(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
@ -135,13 +135,13 @@ export async function getJwtToken(
this: IExecuteFunctions | ILoadOptionsFunctions,
{ username, password, host }: BaserowCredentials,
) {
const options: IRequestOptions = {
const options: IHttpRequestOptions = {
method: 'POST',
body: {
username,
password,
},
uri: `${host}/api/user/token-auth/`,
url: `${host}/api/user/token-auth/`,
json: true,
};

View file

@ -0,0 +1,87 @@
import type {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IN8nHttpFullResponse,
IN8nHttpResponse,
INode,
} from 'n8n-workflow';
import { baserowFileUploadRequest } from '../GenericFunctions';
import { mock } from 'node:test';
export const node: INode = {
id: 'c4a5ca75-18c7-4cc8-bf7d-5d57bb7d84da',
name: 'Baserow Upload',
type: 'n8n-nodes-base.Baserow',
typeVersion: 1,
position: [0, 0],
parameters: {
operation: 'upload',
domain: 'file',
},
};
describe('Baserow', () => {
describe('baserowFileUploadRequest', () => {
const mockThis = {
helpers: {
requestWithAuthentication: jest.fn().mockResolvedValue({ statusCode: 200 }),
httpRequest: jest.fn(),
},
getNode() {
return node;
},
getCredentials: jest.fn(),
getNodeParameter: jest.fn(),
} as unknown as IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions;
it('should make an authenticated API file-upload request to Baserow', async () => {
mockThis.getCredentials.mockResolvedValue({
host: 'https://my-host.com',
});
// see https://api.baserow.io/api/redoc/#tag/User-files/operation/upload_file
mockThis.helpers.httpRequest.mockResolvedValue({
statusCode: 200,
body: {
size: 2147483647,
mime_type: 'string',
is_image: true,
image_width: 32767,
image_height: 32767,
uploaded_at: '2019-08-24T14:15:22Z',
url: 'http://example.com',
thumbnails: {
property1: null,
property2: null,
},
name: 'string',
original_name: 'string',
},
} as IN8nHttpFullResponse | IN8nHttpResponse);
const jwtToken = 'jwt';
const file = Buffer.from('file');
const filename = 'filename.txt';
const mimeType = 'text/plain';
await baserowFileUploadRequest.call(mockThis, jwtToken, file, filename, mimeType);
expect(mockThis.getCredentials).toHaveBeenCalledWith('baserowApi');
expect(mockThis.helpers.httpRequest).toHaveBeenCalledWith({
body: {
file: {
options: { contentType: 'text/plain', filename: 'filename.txt' },
value: file,
},
},
headers: { Authorization: 'JWT jwt', 'Content-Type': 'multipart/form-data' },
json: false,
method: 'POST',
url: 'https://my-host.com/api/user-files/upload-file/',
});
});
});
});