diff --git a/.vscode/settings.json b/.vscode/settings.json index a5f44e9..ed4d133 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "dotenv.enableAutocloaking": true + "dotenv.enableAutocloaking": false } diff --git a/integrationTests/Records/getRecordById.spec.ts b/integrationTests/Records/getRecordById.spec.ts index 0c88518..930278c 100644 --- a/integrationTests/Records/getRecordById.spec.ts +++ b/integrationTests/Records/getRecordById.spec.ts @@ -1,5 +1,122 @@ -import { OnspringClient } from './../../src'; +import { OnspringClient, GetRecordRequest, DataFormat } from './../../src'; import { expect } from 'chai'; import { baseURL, apiKey } from '../mochaRootHooks'; -describe('getRecordById', function () {}); +describe('getRecordById', function () { + this.timeout(30000); + this.retries(3); + + it('should get a record', async function () { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_SURVEY_ID === undefined) { + expect.fail('TEST_SURVEY_ID is not defined'); + } + + if (process.env.TEST_SURVEY_RECORD_ID === undefined) { + expect.fail('TEST_SURVEY_RECORD_ID is not defined'); + } + + const appId = parseInt(process.env.TEST_SURVEY_ID); + const recordId = parseInt(process.env.TEST_SURVEY_RECORD_ID); + const request = new GetRecordRequest(appId, recordId); + const response = await client.getRecordById(request); + + expect(response.statusCode).to.equal(200); + 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.appId).to.equal(appId); + expect(response.data.recordId).to.equal(recordId); + expect(response.data.fieldData).to.not.be.null; + + if (response.data.fieldData != null) { + expect(response.data.fieldData.length).to.be.greaterThan(0); + + response.data.fieldData.forEach((field) => { + expect(field.fieldId).to.not.be.null; + expect(field).to.not.be.null; + expect(field.type).to.not.be.null; + }); + } + } + }); + + it('should get a record when fieldIds and data format are passed as parameters', async function () { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_SURVEY_ID === undefined) { + expect.fail('TEST_SURVEY_ID is not defined'); + } + + if (process.env.TEST_SURVEY_RECORD_ID === undefined) { + expect.fail('TEST_SURVEY_RECORD_ID is not defined'); + } + + if (process.env.TEST_FIELD_ID === undefined) { + expect.fail('TEST_FIELD_ID is not defined'); + } + + const appId = parseInt(process.env.TEST_SURVEY_ID); + const recordId = parseInt(process.env.TEST_SURVEY_RECORD_ID); + const fieldId = parseInt(process.env.TEST_FIELD_ID); + const request = new GetRecordRequest( + appId, + recordId, + [fieldId], + DataFormat.Formatted + ); + + const response = await client.getRecordById(request); + + expect(response.statusCode).to.equal(200); + 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.appId).to.equal(appId); + expect(response.data.recordId).to.equal(recordId); + expect(response.data.fieldData).to.not.be.null; + + if (response.data.fieldData != null) { + expect(response.data.fieldData.length).to.be.greaterThan(0); + + response.data.fieldData.forEach((field) => { + expect(field.fieldId).to.not.be.null; + expect(field).to.not.be.null; + expect(field.type).to.not.be.null; + }); + } + } + }); + + it('should return a 401 error when an invalid API key is used', async function () { + const client = new OnspringClient(baseURL, 'invalid'); + const response = await client.getRecordById(new GetRecordRequest(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 404 error when an invalid record id is used', async function () { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_SURVEY_ID === undefined) { + expect.fail('TEST_SURVEY_ID is not defined'); + } + + const response = await client.getRecordById( + new GetRecordRequest(parseInt(process.env.TEST_SURVEY_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; + }); +}); diff --git a/integrationTests/example.env b/integrationTests/example.env index e441ecf..52c6ca3 100644 --- a/integrationTests/example.env +++ b/integrationTests/example.env @@ -24,4 +24,5 @@ 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 +TEST_REPORT_WITH_CHART_DATA=NEEDS_TO_BE_SET +TEST_SURVEY_RECORD_ID=NEEDS_TO_BE_SET \ No newline at end of file