feat: added integration tests for saveFile method
This commit is contained in:
@@ -145,7 +145,7 @@ describe('getFileById', function () {
|
|||||||
const client = new OnspringClient(baseURL, apiKey);
|
const client = new OnspringClient(baseURL, apiKey);
|
||||||
|
|
||||||
if (process.env.TEST_ATTACHMENT_FIELD_NO_ACCESS_APP === undefined) {
|
if (process.env.TEST_ATTACHMENT_FIELD_NO_ACCESS_APP === undefined) {
|
||||||
expect.fail('EST_ATTACHMENT_FIELD_NO_ACCESS_APP is not defined');
|
expect.fail('TEST_ATTACHMENT_FIELD_NO_ACCESS_APP is not defined');
|
||||||
}
|
}
|
||||||
|
|
||||||
const fieldId = parseInt(process.env.TEST_ATTACHMENT_FIELD_NO_ACCESS_APP);
|
const fieldId = parseInt(process.env.TEST_ATTACHMENT_FIELD_NO_ACCESS_APP);
|
||||||
|
|||||||
@@ -153,7 +153,7 @@ describe('getFileInfoById', function () {
|
|||||||
const client = new OnspringClient(baseURL, apiKey);
|
const client = new OnspringClient(baseURL, apiKey);
|
||||||
|
|
||||||
if (process.env.TEST_ATTACHMENT_FIELD_NO_ACCESS_APP === undefined) {
|
if (process.env.TEST_ATTACHMENT_FIELD_NO_ACCESS_APP === undefined) {
|
||||||
expect.fail('EST_ATTACHMENT_FIELD_NO_ACCESS_APP is not defined');
|
expect.fail('TEST_ATTACHMENT_FIELD_NO_ACCESS_APP is not defined');
|
||||||
}
|
}
|
||||||
|
|
||||||
const fieldId = parseInt(process.env.TEST_ATTACHMENT_FIELD_NO_ACCESS_APP);
|
const fieldId = parseInt(process.env.TEST_ATTACHMENT_FIELD_NO_ACCESS_APP);
|
||||||
|
|||||||
@@ -0,0 +1,290 @@
|
|||||||
|
import { OnspringClient, SaveFileRequest } from '../../src/index';
|
||||||
|
import { expect } from 'chai';
|
||||||
|
import { baseURL, apiKey } from '../mochaRootHooks';
|
||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
describe('saveFile', function () {
|
||||||
|
this.timeout(30000);
|
||||||
|
this.retries(3);
|
||||||
|
|
||||||
|
// Delete any files that were created during the test
|
||||||
|
const newFileIds: number[] = [];
|
||||||
|
|
||||||
|
after(async function () {
|
||||||
|
for (const newFileId of newFileIds) {
|
||||||
|
await deleteFile(newFileId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should save a file into an attachment field', async function () {
|
||||||
|
const client = new OnspringClient(baseURL, apiKey);
|
||||||
|
|
||||||
|
if (process.env.TEST_RECORD === undefined) {
|
||||||
|
expect.fail('TEST_RECORD is not defined');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.env.TEST_ATTACHMENT_FIELD === undefined) {
|
||||||
|
expect.fail('TEST_ATTACHMENT_FIELD is not defined');
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentDirectory = __dirname;
|
||||||
|
const parentDirectory = path.resolve(currentDirectory, '..');
|
||||||
|
const filePath = path.join(parentDirectory, 'testData/test-attachment.txt');
|
||||||
|
const fileStream = fs.createReadStream(filePath);
|
||||||
|
|
||||||
|
const request = new SaveFileRequest(
|
||||||
|
parseInt(process.env.TEST_RECORD),
|
||||||
|
parseInt(process.env.TEST_ATTACHMENT_FIELD),
|
||||||
|
'notes',
|
||||||
|
new Date(),
|
||||||
|
'test-attachment.txt',
|
||||||
|
'text/plain',
|
||||||
|
fileStream
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await client.saveFile(request);
|
||||||
|
|
||||||
|
expect(response.statusCode).to.equal(201);
|
||||||
|
expect(response.isSuccessful).to.be.true;
|
||||||
|
expect(response.message).to.equal('');
|
||||||
|
expect(response.data).to.not.be.null;
|
||||||
|
|
||||||
|
if (response.data != null) {
|
||||||
|
expect(response.data.id).to.not.be.null;
|
||||||
|
newFileIds.push(response.data.id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should save a file into an image field', async function () {
|
||||||
|
const client = new OnspringClient(baseURL, apiKey);
|
||||||
|
|
||||||
|
if (process.env.TEST_RECORD === undefined) {
|
||||||
|
expect.fail('TEST_RECORD is not defined');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.env.TEST_IMAGE_FIELD === undefined) {
|
||||||
|
expect.fail('TEST_IMAGE_FIELD is not defined');
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentDirectory = __dirname;
|
||||||
|
const parentDirectory = path.resolve(currentDirectory, '..');
|
||||||
|
const filePath = path.join(parentDirectory, 'testData/test-image.jpeg');
|
||||||
|
const fileStream = fs.createReadStream(filePath);
|
||||||
|
|
||||||
|
const request = new SaveFileRequest(
|
||||||
|
parseInt(process.env.TEST_RECORD),
|
||||||
|
parseInt(process.env.TEST_IMAGE_FIELD),
|
||||||
|
'notes',
|
||||||
|
new Date(),
|
||||||
|
'test-image.jpeg',
|
||||||
|
'image/jpeg',
|
||||||
|
fileStream
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await client.saveFile(request);
|
||||||
|
|
||||||
|
expect(response.statusCode).to.equal(201);
|
||||||
|
expect(response.isSuccessful).to.be.true;
|
||||||
|
expect(response.message).to.equal('');
|
||||||
|
expect(response.data).to.not.be.null;
|
||||||
|
|
||||||
|
if (response.data != null) {
|
||||||
|
expect(response.data.id).to.not.be.null;
|
||||||
|
newFileIds.push(response.data.id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a 400 response when fieldId is not for a file field', async function () {
|
||||||
|
const client = new OnspringClient(baseURL, apiKey);
|
||||||
|
|
||||||
|
if (process.env.TEST_RECORD === undefined) {
|
||||||
|
expect.fail('TEST_RECORD is not defined');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.env.TEST_TEXT_FIELD === undefined) {
|
||||||
|
expect.fail('TEST_TEXT_FIELD is not defined');
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentDirectory = __dirname;
|
||||||
|
const parentDirectory = path.resolve(currentDirectory, '..');
|
||||||
|
const filePath = path.join(parentDirectory, 'testData/test-attachment.txt');
|
||||||
|
const fileStream = fs.createReadStream(filePath);
|
||||||
|
|
||||||
|
const request = new SaveFileRequest(
|
||||||
|
parseInt(process.env.TEST_RECORD),
|
||||||
|
parseInt(process.env.TEST_TEXT_FIELD),
|
||||||
|
'notes',
|
||||||
|
new Date(),
|
||||||
|
'test-attachment.txt',
|
||||||
|
'text/plain',
|
||||||
|
fileStream
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await client.saveFile(request);
|
||||||
|
|
||||||
|
expect(response.statusCode).to.equal(400);
|
||||||
|
expect(response.isSuccessful).to.be.false;
|
||||||
|
expect(response.message).to.not.be.null.and.not.be.undefined;
|
||||||
|
expect(response.data).to.be.null;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a 401 response when the api key is invalid', async function () {
|
||||||
|
const client = new OnspringClient(baseURL, 'invalid');
|
||||||
|
|
||||||
|
const currentDirectory = __dirname;
|
||||||
|
const parentDirectory = path.resolve(currentDirectory, '..');
|
||||||
|
const filePath = path.join(parentDirectory, 'testData/test-attachment.txt');
|
||||||
|
const fileStream = fs.createReadStream(filePath);
|
||||||
|
|
||||||
|
const request = new SaveFileRequest(
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
'notes',
|
||||||
|
new Date(),
|
||||||
|
'test-attachment.txt',
|
||||||
|
'text/plain',
|
||||||
|
fileStream
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await client.saveFile(request);
|
||||||
|
|
||||||
|
expect(response.statusCode).to.equal(401);
|
||||||
|
expect(response.isSuccessful).to.be.false;
|
||||||
|
expect(response.message).to.be.undefined;
|
||||||
|
expect(response.data).to.be.null;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a 403 response when the api key does not have access to the field where the file is held', async function () {
|
||||||
|
const client = new OnspringClient(baseURL, apiKey);
|
||||||
|
|
||||||
|
if (process.env.TEST_ATTACHMENT_FIELD_NO_ACCESS_FIELD === undefined) {
|
||||||
|
expect.fail('TEST_ATTACHMENT_FIELD_NO_ACCESS_FIELD is not defined');
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentDirectory = __dirname;
|
||||||
|
const parentDirectory = path.resolve(currentDirectory, '..');
|
||||||
|
const filePath = path.join(parentDirectory, 'testData/test-attachment.txt');
|
||||||
|
const fileStream = fs.createReadStream(filePath);
|
||||||
|
|
||||||
|
const request = new SaveFileRequest(
|
||||||
|
1,
|
||||||
|
parseInt(process.env.TEST_ATTACHMENT_FIELD_NO_ACCESS_FIELD),
|
||||||
|
'notes',
|
||||||
|
new Date(),
|
||||||
|
'test-attachment.txt',
|
||||||
|
'text/plain',
|
||||||
|
fileStream
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await client.saveFile(request);
|
||||||
|
|
||||||
|
expect(response.statusCode).to.equal(403);
|
||||||
|
expect(response.isSuccessful).to.be.false;
|
||||||
|
expect(response.message).to.not.be.null.and.to.not.be.undefined;
|
||||||
|
expect(response.data).to.be.null;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a 403 response when the api key does not have access to the app where the file is held', async function () {
|
||||||
|
const client = new OnspringClient(baseURL, apiKey);
|
||||||
|
|
||||||
|
if (process.env.TEST_ATTACHMENT_FIELD_NO_ACCESS_APP === undefined) {
|
||||||
|
expect.fail('TEST_ATTACHMENT_FIELD_NO_ACCESS_APP is not defined');
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentDirectory = __dirname;
|
||||||
|
const parentDirectory = path.resolve(currentDirectory, '..');
|
||||||
|
const filePath = path.join(parentDirectory, 'testData/test-attachment.txt');
|
||||||
|
const fileStream = fs.createReadStream(filePath);
|
||||||
|
|
||||||
|
const request = new SaveFileRequest(
|
||||||
|
1,
|
||||||
|
parseInt(process.env.TEST_ATTACHMENT_FIELD_NO_ACCESS_APP),
|
||||||
|
'notes',
|
||||||
|
new Date(),
|
||||||
|
'test-attachment.txt',
|
||||||
|
'text/plain',
|
||||||
|
fileStream
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await client.saveFile(request);
|
||||||
|
|
||||||
|
expect(response.statusCode).to.equal(403);
|
||||||
|
expect(response.isSuccessful).to.be.false;
|
||||||
|
expect(response.message).to.not.be.null.and.to.not.be.undefined;
|
||||||
|
expect(response.data).to.be.null;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a 404 response when the file field cannot be found', async function () {
|
||||||
|
const client = new OnspringClient(baseURL, apiKey);
|
||||||
|
|
||||||
|
const currentDirectory = __dirname;
|
||||||
|
const parentDirectory = path.resolve(currentDirectory, '..');
|
||||||
|
const filePath = path.join(parentDirectory, 'testData/test-attachment.txt');
|
||||||
|
const fileStream = fs.createReadStream(filePath);
|
||||||
|
|
||||||
|
const request = new SaveFileRequest(
|
||||||
|
1,
|
||||||
|
0,
|
||||||
|
'notes',
|
||||||
|
new Date(),
|
||||||
|
'test-attachment.txt',
|
||||||
|
'text/plain',
|
||||||
|
fileStream
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await client.saveFile(request);
|
||||||
|
|
||||||
|
expect(response.statusCode).to.equal(404);
|
||||||
|
expect(response.isSuccessful).to.be.false;
|
||||||
|
expect(response.message).to.not.be.null.and.to.not.be.undefined;
|
||||||
|
expect(response.data).to.be.null;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a 404 response when the file record cannot be found', async function () {
|
||||||
|
const client = new OnspringClient(baseURL, apiKey);
|
||||||
|
|
||||||
|
if (process.env.TEST_ATTACHMENT_FIELD === undefined) {
|
||||||
|
expect.fail('TEST_ATTACHMENT_FIELD is not defined');
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentDirectory = __dirname;
|
||||||
|
const parentDirectory = path.resolve(currentDirectory, '..');
|
||||||
|
const filePath = path.join(parentDirectory, 'testData/test-attachment.txt');
|
||||||
|
const fileStream = fs.createReadStream(filePath);
|
||||||
|
|
||||||
|
const request = new SaveFileRequest(
|
||||||
|
0,
|
||||||
|
parseInt(process.env.TEST_ATTACHMENT_FIELD),
|
||||||
|
'notes',
|
||||||
|
new Date(),
|
||||||
|
'test-attachment.txt',
|
||||||
|
'text/plain',
|
||||||
|
fileStream
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await client.saveFile(request);
|
||||||
|
|
||||||
|
expect(response.statusCode).to.equal(404);
|
||||||
|
expect(response.isSuccessful).to.be.false;
|
||||||
|
expect(response.message).to.not.be.null.and.to.not.be.undefined;
|
||||||
|
expect(response.data).to.be.null;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
async function deleteFile(newFileId: number): Promise<void> {
|
||||||
|
const client = new OnspringClient(baseURL, apiKey);
|
||||||
|
|
||||||
|
if (
|
||||||
|
process.env.TEST_RECORD === undefined ||
|
||||||
|
process.env.TEST_ATTACHMENT_FIELD === undefined
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await client.deleteFileById(
|
||||||
|
parseInt(process.env.TEST_RECORD),
|
||||||
|
parseInt(process.env.TEST_ATTACHMENT_FIELD),
|
||||||
|
newFileId
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
This is a test attachment.
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 96 KiB |
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
import { OnspringClient } from './models/OnspringClient';
|
import { OnspringClient } from './models/OnspringClient';
|
||||||
import { PagingRequest } from './models/PagingRequest';
|
import { PagingRequest } from './models/PagingRequest';
|
||||||
|
import { SaveFileRequest } from './models/SaveFileRequest';
|
||||||
|
|
||||||
export { OnspringClient, PagingRequest };
|
export { OnspringClient, PagingRequest, SaveFileRequest };
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ export class EndpointFactory {
|
|||||||
fieldId: number,
|
fieldId: number,
|
||||||
fileId: number
|
fileId: number
|
||||||
): string {
|
): string {
|
||||||
return `/Files/recordId/${recordId}/fieldId/${fieldId}/fileId/${fileId}/file`;
|
return `/Files/recordId/${recordId}/fieldId/${fieldId}/fileId/${fileId}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ describe('EndpointFactory', function () {
|
|||||||
describe('getDeleteFileByIdEndpoint', function () {
|
describe('getDeleteFileByIdEndpoint', function () {
|
||||||
it('should return the correct delete file endpoint', function () {
|
it('should return the correct delete file endpoint', function () {
|
||||||
const result = EndpointFactory.getDeleteFileByIdEndpoint(1, 2, 3);
|
const result = EndpointFactory.getDeleteFileByIdEndpoint(1, 2, 3);
|
||||||
expect(result).to.equal('/Files/recordId/1/fieldId/2/fileId/3/file');
|
expect(result).to.equal('/Files/recordId/1/fieldId/2/fileId/3');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user