diff --git a/integrationTests/Files/getFileById.spec.ts b/integrationTests/Files/getFileById.spec.ts index fec3840..4c58bed 100644 --- a/integrationTests/Files/getFileById.spec.ts +++ b/integrationTests/Files/getFileById.spec.ts @@ -145,7 +145,7 @@ describe('getFileById', function () { const client = new OnspringClient(baseURL, apiKey); 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); diff --git a/integrationTests/Files/getFileInfoById.spec.ts b/integrationTests/Files/getFileInfoById.spec.ts index c35c099..b10a37d 100644 --- a/integrationTests/Files/getFileInfoById.spec.ts +++ b/integrationTests/Files/getFileInfoById.spec.ts @@ -153,7 +153,7 @@ describe('getFileInfoById', function () { const client = new OnspringClient(baseURL, apiKey); 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); diff --git a/integrationTests/Files/saveFile.spec.ts b/integrationTests/Files/saveFile.spec.ts new file mode 100644 index 0000000..7e527d0 --- /dev/null +++ b/integrationTests/Files/saveFile.spec.ts @@ -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 { + 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 + ); +} diff --git a/integrationTests/testData/test-attachment.txt b/integrationTests/testData/test-attachment.txt new file mode 100644 index 0000000..3eae1d0 --- /dev/null +++ b/integrationTests/testData/test-attachment.txt @@ -0,0 +1 @@ +This is a test attachment. \ No newline at end of file diff --git a/integrationTests/testData/test-image.jpeg b/integrationTests/testData/test-image.jpeg new file mode 100644 index 0000000..b493033 Binary files /dev/null and b/integrationTests/testData/test-image.jpeg differ diff --git a/src/index.ts b/src/index.ts index cd13ae9..95acd23 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import { OnspringClient } from './models/OnspringClient'; import { PagingRequest } from './models/PagingRequest'; +import { SaveFileRequest } from './models/SaveFileRequest'; -export { OnspringClient, PagingRequest }; +export { OnspringClient, PagingRequest, SaveFileRequest }; diff --git a/src/models/EndpointFactory.ts b/src/models/EndpointFactory.ts index 1f6aa99..b96430a 100644 --- a/src/models/EndpointFactory.ts +++ b/src/models/EndpointFactory.ts @@ -88,7 +88,7 @@ export class EndpointFactory { fieldId: number, fileId: number ): string { - return `/Files/recordId/${recordId}/fieldId/${fieldId}/fileId/${fileId}/file`; + return `/Files/recordId/${recordId}/fieldId/${fieldId}/fileId/${fileId}`; } /** diff --git a/tests/EndpointFactory.spec.ts b/tests/EndpointFactory.spec.ts index 82e41b0..50b3a99 100644 --- a/tests/EndpointFactory.spec.ts +++ b/tests/EndpointFactory.spec.ts @@ -61,7 +61,7 @@ describe('EndpointFactory', function () { describe('getDeleteFileByIdEndpoint', function () { it('should return the correct delete file endpoint', function () { 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'); }); });