diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a5f44e9 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "dotenv.enableAutocloaking": true +} diff --git a/integrationTests/Records/deleteRecordById.spec.ts b/integrationTests/Records/deleteRecordById.spec.ts index a537f06..a48346c 100644 --- a/integrationTests/Records/deleteRecordById.spec.ts +++ b/integrationTests/Records/deleteRecordById.spec.ts @@ -1,5 +1,100 @@ -import { OnspringClient } from './../../src'; +import { StringRecordValue } from './../../src/models/StringRecordValue'; +import { OnspringClient, Record, RecordValue } from './../../src'; import { expect } from 'chai'; import { baseURL, apiKey } from '../mochaRootHooks'; -describe('deleteRecordById', function () {}); +describe('deleteRecordById', function () { + this.timeout(30000); + this.retries(3); + + it('should delete a record', async function () { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_APP_ID === undefined) { + expect.fail('TEST_APP_ID is not defined'); + } + + const recordId = await addRecord(); + const response = await client.deleteRecordById( + parseInt(process.env.TEST_APP_ID), + recordId + ); + + expect(response.statusCode).to.equal(204); + expect(response.isSuccessful).to.be.true; + expect(response.message).to.equal(''); + expect(response.data).to.not.be.null; + }); + + it('should return a 401 error when the API key is invalid', async function () { + const client = new OnspringClient(baseURL, 'invalid'); + const response = await client.deleteRecordById(1, 1); + + 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 error when the API key does not have access to the record', async function () { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_APP_ID_NO_ACCESS === undefined) { + expect.fail('TEST_APP_ID_NO_ACCESS is not defined'); + } + + const response = await client.deleteRecordById( + parseInt(process.env.TEST_APP_ID_NO_ACCESS), + 1 + ); + + expect(response.statusCode).to.equal(403); + 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 404 error when the record does not exist', async function () { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_APP_ID === undefined) { + expect.fail('TEST_APP_ID is not defined'); + } + + const response = await client.deleteRecordById( + parseInt(process.env.TEST_APP_ID), + 0 + ); + + expect(response.statusCode).to.equal(404); + expect(response.isSuccessful).to.be.false; + expect(response.message).to.be.undefined; + expect(response.data).to.be.null; + }); +}); + +async function addRecord(): Promise { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_APP_ID === undefined) { + expect.fail('TEST_APP_ID is not defined'); + } + + if (process.env.TEST_TEXT_FIELD === undefined) { + expect.fail('TEST_TEXT_FIELD is not defined'); + } + + const request = new Record(parseInt(process.env.TEST_APP_ID), null); + + request.addValue( + new StringRecordValue(parseInt(process.env.TEST_TEXT_FIELD), 'test') + ); + + const response = await client.saveRecord(request); + + if (response.data === null || response.data.id === undefined) { + expect.fail('Record ID is not defined'); + } + + return response.data.id; +} diff --git a/integrationTests/example.env b/integrationTests/example.env index 51ce265..e441ecf 100644 --- a/integrationTests/example.env +++ b/integrationTests/example.env @@ -17,6 +17,11 @@ TEST_TEXT_FIELD=NEEDS_TO_BE_SET TEST_ATTACHMENT=NEEDS_TO_BE_SET TEST_IMAGE_FIELD=NEEDS_TO_BE_SET TEST_IMAGE=NEEDS_TO_BE_SET +TEST_LIST_FIELD=NEEDS_TO_BE_SET +TEST_LIST_FIELD_NO_ACCESS=NEEDS_TO_BE_SET +TEST_LIST_ID=NEEDS_TO_BE_SET +TEST_LIST_ID_NO_ACCESS=NEEDS_TO_BE_SET +TEST_LIST_ITEM_ID_NO_ACCESS=NEEDS_TO_BE_SET TEST_REPORT=NEEDS_TO_BE_SET TEST_REPORT_NO_ACCESS=NEEDS_TO_BE_SET TEST_REPORT_WITH_CHART_DATA=NEEDS_TO_BE_SET \ No newline at end of file diff --git a/src/models/OnspringClient.ts b/src/models/OnspringClient.ts index b79696b..a4275c5 100644 --- a/src/models/OnspringClient.ts +++ b/src/models/OnspringClient.ts @@ -425,7 +425,7 @@ export class OnspringClient { : request; const endpoint = EndpointFactory.getAddOrUpdateRecordEndpoint(); - const apiResponse = await this.put(endpoint, request); + const apiResponse = await this.put(endpoint, request.toJSON()); if (apiResponse.isSuccessful === false) { return apiResponse; diff --git a/src/models/SaveRecordRequest.ts b/src/models/SaveRecordRequest.ts index 8294599..112a7ab 100644 --- a/src/models/SaveRecordRequest.ts +++ b/src/models/SaveRecordRequest.ts @@ -25,4 +25,12 @@ export class SaveRecordRequest { this.recordId = recordId; this.fields = fields; } + + public toJSON(): any { + return { + appId: this.appId, + recordId: this.recordId, + fields: Object.fromEntries(this.fields), + }; + } } diff --git a/tests/SaveRecordRequest.spec.ts b/tests/SaveRecordRequest.spec.ts index 4668e87..ee05838 100644 --- a/tests/SaveRecordRequest.spec.ts +++ b/tests/SaveRecordRequest.spec.ts @@ -54,4 +54,54 @@ describe('SaveRecordRequest', function () { expect(saveRecordRequest.fields).to.not.be.null.and.not.be.undefined; expect(saveRecordRequest.fields).to.have.lengthOf(2); }); + + describe('toJSON', function () { + it('should be defined', function () { + expect(SaveRecordRequest.prototype.toJSON).to.not.be.undefined; + }); + + it('should return an object', function () { + expect(new SaveRecordRequest(1).toJSON()).to.be.an('object'); + }); + + it('should return an object with an appId property', function () { + expect(new SaveRecordRequest(1).toJSON()).to.have.property('appId'); + }); + + it('should return an object with a recordId property', function () { + expect(new SaveRecordRequest(1).toJSON()).to.have.property('recordId'); + }); + + it('should return an object with a fields property', function () { + expect(new SaveRecordRequest(1).toJSON()).to.have.property('fields'); + }); + + it('should return an object with an appId property that is equal to the value of the appId property', function () { + expect(new SaveRecordRequest(1).toJSON()).to.have.property('appId', 1); + }); + + it('should return an object with a recordId property that is equal to the value of the recordId property', function () { + expect(new SaveRecordRequest(1, 2).toJSON()).to.have.property( + 'recordId', + 2 + ); + }); + + it('should return an object with a fields property that is equal to the value of the fields property', function () { + const map = new Map([ + [1, 'test'], + [2, 'test'], + ]); + + const obj = { + 1: 'test', + 2: 'test', + }; + + const saveRecordRequest = new SaveRecordRequest(1, 2, map); + const json = saveRecordRequest.toJSON(); + + expect(json.fields).to.deep.equal(obj); + }); + }); });