diff --git a/.env.vault b/.env.vault new file mode 100644 index 0000000..1b3b8e1 --- /dev/null +++ b/.env.vault @@ -0,0 +1,12 @@ +################################################################################# +# # +# This file uniquely identifies your project in dotenv-vault. # +# You SHOULD commit this file to source control. # +# # +# Generated with 'npx dotenv-vault new' # +# # +# Learn more at https://dotenv.org/env-vault # +# # +################################################################################# + +DOTENV_VAULT=vlt_ec81c3b4428f6b230fe7295edceef4214cd1b7b5b04982ab652b12393652128a \ No newline at end of file diff --git a/.gitignore b/.gitignore index c6bba59..50d1703 100644 --- a/.gitignore +++ b/.gitignore @@ -128,3 +128,8 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* + +.env* +.flaskenv* +!.env.project +!.env.vault \ No newline at end of file diff --git a/integrationTests/example.env b/example.env similarity index 91% rename from integrationTests/example.env rename to example.env index e441ecf..5ab6af3 100644 --- a/integrationTests/example.env +++ b/example.env @@ -1,27 +1,28 @@ -API_BASE_URL=https://api.onspring.com -SANDBOX_API_KEY=NEEDS_TO_BE_SET -TEST_APP_ID=NEEDS_TO_BE_SET -TEST_SURVEY_ID=NEEDS_TO_BE_SET -TEST_APP_ID_NO_ACCESS=NEEDS_TO_BE_SET -TEST_APP_IDS=NEEDS_TO_BE_SET -TEST_APP_IDS_NO_ACCESS=NEEDS_TO_BE_SET -TEST_FIELD_ID=NEEDS_TO_BE_SET -TEST_FIELD_ID_NO_ACCESS=NEEDS_TO_BE_SET -TEST_FIELD_IDS=NEEDS_TO_BE_SET -TEST_FIELD_IDS_NO_ACCESS=NEEDS_TO_BE_SET -TEST_RECORD=NEEDS_TO_BE_SET -TEST_ATTACHMENT_FIELD=NEEDS_TO_BE_SET -TEST_ATTACHMENT_FIELD_NO_ACCESS_FIELD=NEEDS_TO_BE_SET -TEST_ATTACHMENT_FIELD_NO_ACCESS_APP=NEEDS_TO_BE_SET -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 +API_BASE_URL=https://api.onspring.com +SANDBOX_API_KEY=NEEDS_TO_BE_SET +TEST_APP_ID=NEEDS_TO_BE_SET +TEST_SURVEY_ID=NEEDS_TO_BE_SET +TEST_APP_ID_NO_ACCESS=NEEDS_TO_BE_SET +TEST_APP_IDS=NEEDS_TO_BE_SET +TEST_APP_IDS_NO_ACCESS=NEEDS_TO_BE_SET +TEST_FIELD_ID=NEEDS_TO_BE_SET +TEST_FIELD_ID_NO_ACCESS=NEEDS_TO_BE_SET +TEST_FIELD_IDS=NEEDS_TO_BE_SET +TEST_FIELD_IDS_NO_ACCESS=NEEDS_TO_BE_SET +TEST_RECORD=NEEDS_TO_BE_SET +TEST_ATTACHMENT_FIELD=NEEDS_TO_BE_SET +TEST_ATTACHMENT_FIELD_NO_ACCESS_FIELD=NEEDS_TO_BE_SET +TEST_ATTACHMENT_FIELD_NO_ACCESS_APP=NEEDS_TO_BE_SET +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 +TEST_SURVEY_RECORD_ID=NEEDS_TO_BE_SET \ No newline at end of file diff --git a/integrationTests/Records/deleteRecordById.spec.ts b/integrationTests/Records/deleteRecordById.spec.ts index a537f06..87f7cbd 100644 --- a/integrationTests/Records/deleteRecordById.spec.ts +++ b/integrationTests/Records/deleteRecordById.spec.ts @@ -1,5 +1,74 @@ -import { OnspringClient } from './../../src'; -import { expect } from 'chai'; -import { baseURL, apiKey } from '../mochaRootHooks'; - -describe('deleteRecordById', function () {}); +import { OnspringClient } from '../../src'; +import { expect } from 'chai'; +import { baseURL, apiKey } from '../mochaRootHooks'; +import { addRecord } from '../utils/addRecord'; + +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_SURVEY_ID === undefined) { + expect.fail('TEST_SURVEY_ID is not defined'); + } + + const recordId = await addRecord(baseURL, apiKey); + const response = await client.deleteRecordById( + parseInt(process.env.TEST_SURVEY_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; + }); +}); diff --git a/integrationTests/Records/deleteRecordsByIds.spec.ts b/integrationTests/Records/deleteRecordsByIds.spec.ts new file mode 100644 index 0000000..923e498 --- /dev/null +++ b/integrationTests/Records/deleteRecordsByIds.spec.ts @@ -0,0 +1,77 @@ +import { OnspringClient } from '../../src'; +import { expect } from 'chai'; +import { baseURL, apiKey } from '../mochaRootHooks'; +import { addRecord } from '../utils/addRecord'; + +describe('deleteRecordsByIds', function () { + this.timeout(30000); + this.retries(3); + + it('should delete records', async function () { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_SURVEY_ID === undefined) { + expect.fail('TEST_SURVEY_ID is not defined'); + } + + const recordId1 = await addRecord(baseURL, apiKey); + const recordId2 = await addRecord(baseURL, apiKey); + + const response = await client.deleteRecordsByIds( + parseInt(process.env.TEST_SURVEY_ID), + [recordId1, recordId2] + ); + + 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 400 error when no record ids are provided', 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.deleteRecordsByIds( + parseInt(process.env.TEST_SURVEY_ID), + [] + ); + + 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 error when an invalid API key is used', async function () { + const client = new OnspringClient(baseURL, 'invalid'); + + const response = await client.deleteRecordsByIds(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 app', 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.deleteRecordsByIds( + 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; + }); +}); diff --git a/integrationTests/Records/getRecordById.spec.ts b/integrationTests/Records/getRecordById.spec.ts index 0c88518..909e39b 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_TEXT_FIELD === undefined) { + expect.fail('TEST_TEXT_FIELD 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_TEXT_FIELD); + 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/Records/getRecordsByAppId.spec.ts b/integrationTests/Records/getRecordsByAppId.spec.ts index 7abef44..e3967c8 100644 --- a/integrationTests/Records/getRecordsByAppId.spec.ts +++ b/integrationTests/Records/getRecordsByAppId.spec.ts @@ -1,5 +1,137 @@ -import { OnspringClient } from './../../src'; +import { GetRecordsByAppIdRequest } from './../../src/models/GetRecordsByAppIdRequest'; +import { DataFormat, OnspringClient, PagingRequest } from './../../src'; import { expect } from 'chai'; import { baseURL, apiKey } from '../mochaRootHooks'; -describe('getRecordsByAppId', function () {}); +describe('getRecordsByAppId', function () { + this.timeout(30000); + this.retries(3); + + it('should get records', 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 request = new GetRecordsByAppIdRequest(appId); + + const response = await client.getRecordsByAppId(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.pageNumber).to.not.be.null; + expect(response.data.pageSize).to.not.be.null; + expect(response.data.totalRecords).to.not.be.null; + expect(response.data.totalPages).to.not.be.null; + expect(response.data.items).to.not.be.null; + + response.data.items.forEach((record) => { + expect(record.appId).to.equal(appId); + expect(record.recordId).to.not.be.null; + expect(record.fieldData).to.not.be.null; + + if (record.fieldData != null) { + expect(record.fieldData.length).to.be.greaterThan(0); + + record.fieldData.forEach((field) => { + expect(field.fieldId).to.not.be.null; + expect(field.value).to.not.be.null; + expect(field.type).to.not.be.null; + }); + } + }); + } + }); + + it('should get records when fieldIds, paging information, 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_TEXT_FIELD === undefined) { + expect.fail('TEST_TEXT_FIELD is not defined'); + } + + const appId = parseInt(process.env.TEST_SURVEY_ID); + const fieldId = parseInt(process.env.TEST_TEXT_FIELD); + const request = new GetRecordsByAppIdRequest( + appId, + [fieldId], + DataFormat.Formatted, + new PagingRequest(1, 1) + ); + + const response = await client.getRecordsByAppId(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.pageNumber).to.not.be.null; + expect(response.data.pageSize).to.not.be.null; + expect(response.data.totalRecords).to.not.be.null; + expect(response.data.totalPages).to.not.be.null; + expect(response.data.items).to.not.be.null; + + response.data.items.forEach((record) => { + expect(record.appId).to.equal(appId); + expect(record.recordId).to.not.be.null; + expect(record.fieldData).to.not.be.null; + + if (record.fieldData != null) { + expect(record.fieldData.length).to.be.greaterThan(0); + + record.fieldData.forEach((field) => { + expect(field.fieldId).to.not.be.null; + expect(field.value).to.not.be.null; + expect(field.type).to.not.be.null; + }); + } + }); + } + }); + + it('should return a 401 error when an invalid API key is passed', async function () { + const client = new OnspringClient(baseURL, 'invalid'); + const request = new GetRecordsByAppIdRequest(0); + const response = await client.getRecordsByAppId(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 error when the api key does not have access to the app', 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 request = new GetRecordsByAppIdRequest( + parseInt(process.env.TEST_APP_ID_NO_ACCESS) + ); + + const response = await client.getRecordsByAppId(request); + + 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; + }); +}); diff --git a/integrationTests/Records/getRecordsByIds.spec.ts b/integrationTests/Records/getRecordsByIds.spec.ts index 7d38c5c..2773cec 100644 --- a/integrationTests/Records/getRecordsByIds.spec.ts +++ b/integrationTests/Records/getRecordsByIds.spec.ts @@ -1,5 +1,144 @@ -import { OnspringClient } from './../../src'; +import { OnspringClient, GetRecordsRequest, DataFormat } from './../../src'; import { expect } from 'chai'; import { baseURL, apiKey } from '../mochaRootHooks'; -describe('getRecordsByIds', function () {}); +describe('getRecordsByIds', function () { + this.timeout(30000); + this.retries(3); + + it('should get records', 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 GetRecordsRequest(appId, [recordId]); + const response = await client.getRecordsByIds(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.count).to.not.be.null; + expect(response.data.items.length).to.be.greaterThan(0); + + response.data.items.forEach((record) => { + expect(record.appId).to.equal(appId); + expect(record.recordId).to.not.be.null; + expect(record.fieldData).to.not.be.null; + + if (record.fieldData != null) { + expect(record.fieldData.length).to.be.greaterThan(0); + + record.fieldData.forEach((field) => { + expect(field.fieldId).to.not.be.null; + expect(field.value).to.not.be.null; + expect(field.type).to.not.be.null; + }); + } + }); + } + }); + + it('should get records when field ids 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_TEXT_FIELD === undefined) { + expect.fail('TEST_TEXT_FIELD is not defined'); + } + + const appId = parseInt(process.env.TEST_SURVEY_ID); + const recordId = parseInt(process.env.TEST_SURVEY_RECORD_ID); + const textFieldId = parseInt(process.env.TEST_TEXT_FIELD); + const request = new GetRecordsRequest( + appId, + [recordId], + [textFieldId], + DataFormat.Formatted + ); + const response = await client.getRecordsByIds(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.count).to.not.be.null; + expect(response.data.items.length).to.be.greaterThan(0); + + response.data.items.forEach((record) => { + expect(record.appId).to.equal(appId); + expect(record.recordId).to.not.be.null; + expect(record.fieldData).to.not.be.null; + + if (record.fieldData != null) { + expect(record.fieldData.length).to.be.greaterThan(0); + + record.fieldData.forEach((field) => { + expect(field.fieldId).to.not.be.null; + expect(field.value).to.not.be.null; + expect(field.type).to.not.be.null; + }); + } + }); + } + }); + + it('should return a 400 error if too many record ids are passed', async function () { + const client = new OnspringClient(baseURL, apiKey); + const recordIds = new Array(101).fill(1); + const request = new GetRecordsRequest(1, recordIds); + const response = await client.getRecordsByIds(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 error if the api key is invalid', async function () { + const client = new OnspringClient(baseURL, 'invalid'); + const request = new GetRecordsRequest(1, [1]); + const response = await client.getRecordsByIds(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 error if the user does not have access to the app', 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 appId = parseInt(process.env.TEST_APP_ID_NO_ACCESS); + const request = new GetRecordsRequest(appId, [1]); + const response = await client.getRecordsByIds(request); + + 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; + }); +}); diff --git a/integrationTests/Records/queryRecords.spec.ts b/integrationTests/Records/queryRecords.spec.ts index 8595d66..34986fd 100644 --- a/integrationTests/Records/queryRecords.spec.ts +++ b/integrationTests/Records/queryRecords.spec.ts @@ -1,5 +1,159 @@ -import { OnspringClient } from './../../src'; -import { expect } from 'chai'; -import { baseURL, apiKey } from '../mochaRootHooks'; - -describe('queryRecords', function () {}); +import { + DataFormat, + FilterOperators, + OnspringClient, + PagingRequest, + QueryFilter, + QueryRecordsRequest, +} from '../../src'; +import { expect } from 'chai'; +import { baseURL, apiKey } from '../mochaRootHooks'; + +describe('queryRecords', function () { + this.timeout(30000); + this.retries(3); + + it('should return records', 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_AUTO_NUMBER_FIELD === undefined) { + expect.fail('TEST_SURVEY_AUTO_NUMBER_FIELD is not defined'); + } + + const appId = parseInt(process.env.TEST_SURVEY_ID); + const fieldId = parseInt(process.env.TEST_SURVEY_AUTO_NUMBER_FIELD); + const filter = new QueryFilter(fieldId, FilterOperators.GreaterThan, 0); + const request = new QueryRecordsRequest(appId, filter.toString()); + const response = await client.queryRecords(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.pageNumber).to.not.be.null; + expect(response.data.pageSize).to.not.be.null; + expect(response.data.totalPages).to.not.be.null; + expect(response.data.totalRecords).to.not.be.null; + + response.data.items.forEach((record) => { + expect(record.appId).to.equal(appId); + expect(record.recordId).to.not.be.null; + expect(record.fieldData).to.not.be.null; + + if (record.fieldData != null) { + record.fieldData.forEach((field) => { + expect(field.fieldId).to.not.be.null; + expect(field.value).to.not.be.null; + expect(field.type).to.not.be.undefined; + }); + } + }); + } + }); + + it('should return records when data format, paging information, and fields 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_AUTO_NUMBER_FIELD === undefined) { + expect.fail('TEST_SURVEY_AUTO_NUMBER_FIELD is not defined'); + } + + const appId = parseInt(process.env.TEST_SURVEY_ID); + const fieldId = parseInt(process.env.TEST_SURVEY_AUTO_NUMBER_FIELD); + const filter = new QueryFilter(fieldId, FilterOperators.GreaterThan, 0); + const request = new QueryRecordsRequest( + appId, + filter.toString(), + [fieldId], + DataFormat.Formatted, + new PagingRequest(1, 1) + ); + + const response = await client.queryRecords(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.pageNumber).to.not.be.null; + expect(response.data.pageSize).to.not.be.null; + expect(response.data.totalPages).to.not.be.null; + expect(response.data.totalRecords).to.not.be.null; + + response.data.items.forEach((record) => { + expect(record.appId).to.equal(appId); + expect(record.recordId).to.not.be.null; + expect(record.fieldData).to.not.be.null; + + if (record.fieldData != null) { + record.fieldData.forEach((field) => { + expect(field.fieldId).to.not.be.null; + expect(field.value).to.not.be.null; + expect(field.type).to.not.be.undefined; + }); + } + }); + } + }); + + it('should return a 400 error if page size is invalid', async function () { + const client = new OnspringClient(baseURL, apiKey); + const request = new QueryRecordsRequest(1, '', [], DataFormat.Formatted, { + pageNumber: 1, + pageSize: 1001, + }); + + const response = await client.queryRecords(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 error if the API key is invalid', async function () { + const client = new OnspringClient(baseURL, 'invalid'); + const request = new QueryRecordsRequest(1, ''); + const response = await client.queryRecords(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 error if the API key does not have access to the app', 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'); + } + + if (process.env.TEST_SURVEY_AUTO_NUMBER_FIELD === undefined) { + expect.fail('TEST_SURVEY_AUTO_NUMBER_FIELD is not defined'); + } + + const appId = parseInt(process.env.TEST_APP_ID_NO_ACCESS); + const fieldId = parseInt(process.env.TEST_SURVEY_AUTO_NUMBER_FIELD); + const filter = new QueryFilter(fieldId, FilterOperators.GreaterThan, 0); + const request = new QueryRecordsRequest(appId, filter.toString()); + const response = await client.queryRecords(request); + + 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; + }); +}); diff --git a/integrationTests/Records/saveRecord.spec.ts b/integrationTests/Records/saveRecord.spec.ts index 84db01d..48bed8c 100644 --- a/integrationTests/Records/saveRecord.spec.ts +++ b/integrationTests/Records/saveRecord.spec.ts @@ -1,5 +1,167 @@ -import { OnspringClient } from './../../src'; +import { StringRecordValue } from './../../src/models/StringRecordValue'; +import { OnspringClient, Record } from './../../src'; import { expect } from 'chai'; import { baseURL, apiKey } from '../mochaRootHooks'; -describe('saveRecord', function () {}); +describe('saveRecord', function () { + this.timeout(30000); + this.retries(3); + + const newRecords: any[] = []; + + after(async function () { + for (const record of newRecords) { + await deleteRecord(record.appId, record.recordId); + } + }); + + it('should add a record when no record id is passed', 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_TEXT_FIELD === undefined) { + expect.fail('TEST_TEXT_FIELD is not defined'); + } + + const appId = parseInt(process.env.TEST_SURVEY_ID); + const fieldId = parseInt(process.env.TEST_TEXT_FIELD); + const record = new Record(appId, null, [ + new StringRecordValue(fieldId, 'Test'), + ]); + + const response = await client.saveRecord(record); + 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; + expect(response.data.warnings).to.not.be.null; + + newRecords.push({ + appId, + recordId: response.data.id, + }); + } + }); + + it('should update a record when a record id is passed', 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_TEXT_FIELD === undefined) { + expect.fail('TEST_TEXT_FIELD is not defined'); + } + + const appId = parseInt(process.env.TEST_SURVEY_ID); + const fieldId = parseInt(process.env.TEST_TEXT_FIELD); + const newRecord = new Record(appId, null, [ + new StringRecordValue(fieldId, 'Test'), + ]); + + const newRecordResponse = await client.saveRecord(newRecord); + const newRecordId = newRecordResponse.data?.id; + + if (newRecordId === undefined) { + expect.fail('newRecordId is undefined'); + } + + newRecords.push({ + appId, + recordId: newRecordId, + }); + + const record = new Record(appId, newRecordId, [ + new StringRecordValue(fieldId, 'updated'), + ]); + + const response = await client.saveRecord(record); + + 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.id).to.not.be.null; + expect(response.data.warnings).to.not.be.null; + + newRecords.push({ + appId, + recordId: response.data.id, + }); + } + }); + + it('should return a 400 error when field data is empty', async function () { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_SURVEY_ID === undefined) { + expect.fail('TEST_SURVEY_ID is not defined'); + } + + const appId = parseInt(process.env.TEST_SURVEY_ID); + const record = new Record(appId, null, []); + const response = await client.saveRecord(record); + + 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 error when the api key is invalid', async function () { + const client = new OnspringClient(baseURL, 'invalid'); + const response = await client.saveRecord(new Record(0, null, [])); + + 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 app', 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 appId = parseInt(process.env.TEST_APP_ID_NO_ACCESS); + const response = await client.saveRecord(new Record(appId, null, [])); + + 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 id is not found', async function () { + const client = new OnspringClient(baseURL, apiKey); + + if (process.env.TEST_SURVEY_ID === undefined) { + expect.fail('TEST_SURVEY_ID is not defined'); + } + + const appId = parseInt(process.env.TEST_SURVEY_ID); + const record = new Record(appId, 0, []); + const response = await client.saveRecord(record); + + expect(response.statusCode).to.equal(404); + expect(response.isSuccessful).to.be.false; + expect(response.message).to.not.be.null.and.not.be.undefined; + expect(response.data).to.be.null; + }); +}); + +async function deleteRecord(appId: number, recordId: number): Promise { + const client = new OnspringClient(baseURL, apiKey); + await client.deleteRecordById(appId, recordId); +} diff --git a/integrationTests/mochaRootHooks.ts b/integrationTests/mochaRootHooks.ts index ff3939c..af3b35b 100644 --- a/integrationTests/mochaRootHooks.ts +++ b/integrationTests/mochaRootHooks.ts @@ -1,9 +1,7 @@ import { type RootHookObject, type Context } from 'mocha'; import { expect } from 'chai'; import * as dotenv from 'dotenv'; -import path from 'path'; -const envPath = path.resolve(__dirname, '.env'); -dotenv.config({ path: envPath }); +dotenv.config(); let baseURL: string | undefined; let apiKey: string | undefined; diff --git a/integrationTests/utils/addRecord.ts b/integrationTests/utils/addRecord.ts new file mode 100644 index 0000000..97aea54 --- /dev/null +++ b/integrationTests/utils/addRecord.ts @@ -0,0 +1,33 @@ +import { OnspringClient, Record, StringRecordValue } from '../../src'; +import { expect } from 'chai'; + +async function addRecord( + baseURL: string | undefined, + apiKey: string | undefined +): Promise { + 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_TEXT_FIELD === undefined) { + expect.fail('TEST_TEXT_FIELD is not defined'); + } + + const request = new Record(parseInt(process.env.TEST_SURVEY_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; +} + +export { addRecord }; diff --git a/src/models/OnspringClient.ts b/src/models/OnspringClient.ts index b79696b..64ee765 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; @@ -452,6 +452,24 @@ export class OnspringClient { return apiResponse; } + /** + * @method deleteRecordsByIds - Deletes records by their ids. + * @param {number} appId - The id of the app that the records belong to. + * @param {number[]} recordIds - The ids of the records to delete. + * @returns {Promise>} - A promise that resolves to an ApiResponse of type any. + */ + public async deleteRecordsByIds( + appId: number, + recordIds: number[] + ): Promise> { + const endpoint = EndpointFactory.getDeleteRecordsByIdsEndpoint(); + const apiResponse = await this.post(endpoint, { + appId, + recordIds, + }); + return apiResponse; + } + /** * @method getReportsByAppId - Gets a paged list of reports by the app id. * @param {number} appId - The id of the app to get the reports for. 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/OnspringClient.spec.ts b/tests/OnspringClient.spec.ts index 0a70b31..9f0ec37 100644 --- a/tests/OnspringClient.spec.ts +++ b/tests/OnspringClient.spec.ts @@ -3769,6 +3769,148 @@ describe('OnspringClient', function () { }); }); + describe('deleteRecordsByIds', function () { + it('should be defined', function () { + expect(OnspringClient.prototype.deleteRecordsByIds).to.not.be.undefined; + }); + + it('should be a function', function () { + expect(OnspringClient.prototype.deleteRecordsByIds).to.be.a('function'); + }); + + it('should return a promise', function () { + expect( + new OnspringClient(baseUrl, apiKey).deleteRecordsByIds(1, [1]) + ).to.be.instanceOf(Promise); + }); + + it('should return a promise that resolves to an api response when request is successful', async function () { + const client = new OnspringClient(baseUrl, apiKey); + + const mockAxiosClient = axios.create({ + baseURL: baseUrl, + headers: { + 'x-apikey': apiKey, + 'x-api-version': '2', + }, + }); + + sinon.stub(mockAxiosClient, 'post').returns( + Promise.resolve({ + status: 204, + statusText: 'No Content', + headers: {}, + config: {} as InternalAxiosRequestConfig, + } as AxiosResponse) + ); + + sinon.stub(client, '_client' as any).value(mockAxiosClient); + + const result = await client.deleteRecordsByIds(1, [1]); + + expect(result).to.be.instanceOf(ApiResponse); + expect(result).to.have.property('statusCode', 204); + expect(result).to.have.property('isSuccessful', true); + expect(result).to.have.property('message', ''); + expect(result).to.have.property('data', undefined); + }); + + it('should return a promise that resolves to an api response when request receives a 400 response', async function () { + const client = new OnspringClient(baseUrl, apiKey); + + const mockAxiosClient = axios.create({ + baseURL: baseUrl, + headers: { + 'x-apikey': apiKey, + 'x-api-version': '2', + }, + }); + + sinon.stub(mockAxiosClient, 'post').returns( + Promise.resolve({ + status: 400, + statusText: 'Bad Request', + data: { message: 'Bad Request' }, + headers: {}, + config: {} as InternalAxiosRequestConfig, + } as AxiosResponse) + ); + + sinon.stub(client, '_client' as any).value(mockAxiosClient); + + const result = await client.deleteRecordsByIds(1, []); + + expect(result).to.be.instanceOf(ApiResponse); + expect(result).to.have.property('statusCode', 400); + expect(result).to.have.property('isSuccessful', false); + expect(result).to.have.property('message', '{"message":"Bad Request"}'); + expect(result).to.have.property('data', null); + }); + + it('should return a promise that resolves to an api response when request receives a 401 response', async function () { + const client = new OnspringClient(baseUrl, apiKey); + + const mockAxiosClient = axios.create({ + baseURL: baseUrl, + headers: { + 'x-apikey': apiKey, + 'x-api-version': '2', + }, + }); + + sinon.stub(mockAxiosClient, 'post').returns( + Promise.resolve({ + status: 401, + statusText: 'Unauthorized', + headers: {}, + config: {} as InternalAxiosRequestConfig, + } as AxiosResponse) + ); + + sinon.stub(client, '_client' as any).value(mockAxiosClient); + + const result = await client.deleteRecordsByIds(1, [1]); + + expect(result).to.be.instanceOf(ApiResponse); + expect(result).to.have.property('statusCode', 401); + expect(result).to.have.property('isSuccessful', false); + expect(result).to.have.property('message', undefined); + expect(result).to.have.property('data', null); + }); + + it('should return a promise that resolves to an api response when request receives a 403 response', async function () { + const client = new OnspringClient(baseUrl, apiKey); + + const mockAxiosClient = axios.create({ + baseURL: baseUrl, + headers: { + 'x-apikey': apiKey, + 'x-api-version': '2', + }, + }); + + sinon.stub(mockAxiosClient, 'post').returns( + Promise.resolve({ + status: 403, + statusText: 'Forbidden', + data: { message: 'Forbidden' }, + headers: {}, + config: {} as InternalAxiosRequestConfig, + } as AxiosResponse) + ); + + sinon.stub(client, '_client' as any).value(mockAxiosClient); + + const result = await client.deleteRecordsByIds(1, [1]); + + expect(result).to.be.instanceOf(ApiResponse); + expect(result).to.have.property('statusCode', 403); + expect(result).to.have.property('isSuccessful', false); + expect(result).to.have.property('message', 'Forbidden'); + expect(result).to.have.property('data', null); + }); + }); + describe('saveRecord', function () { it('should be defined', function () { expect(OnspringClient.prototype.saveRecord).to.not.be.undefined; 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); + }); + }); });