feat: add integration tests for queryRecords and deleteRecordsByIds methods. fix: implemented deleteRecordsByIds method and wrote unit tests

This commit is contained in:
StevanFreeborn
2023-02-19 23:55:47 -06:00
parent 9c44efe567
commit 3bf65f24d9
6 changed files with 503 additions and 105 deletions
+74 -100
View File
@@ -1,100 +1,74 @@
import { StringRecordValue } from './../../src/models/StringRecordValue'; import { OnspringClient } from '../../src';
import { OnspringClient, Record } from './../../src'; import { expect } from 'chai';
import { expect } from 'chai'; import { baseURL, apiKey } from '../mochaRootHooks';
import { baseURL, apiKey } from '../mochaRootHooks'; import { addRecord } from '../utils/addRecord';
describe('deleteRecordById', function () { describe('deleteRecordById', function () {
this.timeout(30000); this.timeout(30000);
this.retries(3); this.retries(3);
it('should delete a record', async function () { it('should delete a record', async function () {
const client = new OnspringClient(baseURL, apiKey); const client = new OnspringClient(baseURL, apiKey);
if (process.env.TEST_SURVEY_ID === undefined) { if (process.env.TEST_SURVEY_ID === undefined) {
expect.fail('TEST_SURVEY_ID is not defined'); expect.fail('TEST_SURVEY_ID is not defined');
} }
const recordId = await addRecord(); const recordId = await addRecord(baseURL, apiKey);
const response = await client.deleteRecordById( const response = await client.deleteRecordById(
parseInt(process.env.TEST_SURVEY_ID), parseInt(process.env.TEST_SURVEY_ID),
recordId recordId
); );
expect(response.statusCode).to.equal(204); expect(response.statusCode).to.equal(204);
expect(response.isSuccessful).to.be.true; expect(response.isSuccessful).to.be.true;
expect(response.message).to.equal(''); expect(response.message).to.equal('');
expect(response.data).to.not.be.null; expect(response.data).to.not.be.null;
}); });
it('should return a 401 error when the API key is invalid', async function () { it('should return a 401 error when the API key is invalid', async function () {
const client = new OnspringClient(baseURL, 'invalid'); const client = new OnspringClient(baseURL, 'invalid');
const response = await client.deleteRecordById(1, 1); const response = await client.deleteRecordById(1, 1);
expect(response.statusCode).to.equal(401); expect(response.statusCode).to.equal(401);
expect(response.isSuccessful).to.be.false; expect(response.isSuccessful).to.be.false;
expect(response.message).to.be.undefined; expect(response.message).to.be.undefined;
expect(response.data).to.be.null; 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 () { 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); const client = new OnspringClient(baseURL, apiKey);
if (process.env.TEST_APP_ID_NO_ACCESS === undefined) { if (process.env.TEST_APP_ID_NO_ACCESS === undefined) {
expect.fail('TEST_APP_ID_NO_ACCESS is not defined'); expect.fail('TEST_APP_ID_NO_ACCESS is not defined');
} }
const response = await client.deleteRecordById( const response = await client.deleteRecordById(
parseInt(process.env.TEST_APP_ID_NO_ACCESS), parseInt(process.env.TEST_APP_ID_NO_ACCESS),
1 1
); );
expect(response.statusCode).to.equal(403); expect(response.statusCode).to.equal(403);
expect(response.isSuccessful).to.be.false; expect(response.isSuccessful).to.be.false;
expect(response.message).to.not.be.null.and.not.be.undefined; expect(response.message).to.not.be.null.and.not.be.undefined;
expect(response.data).to.be.null; expect(response.data).to.be.null;
}); });
it('should return a 404 error when the record does not exist', async function () { it('should return a 404 error when the record does not exist', async function () {
const client = new OnspringClient(baseURL, apiKey); const client = new OnspringClient(baseURL, apiKey);
if (process.env.TEST_APP_ID === undefined) { if (process.env.TEST_APP_ID === undefined) {
expect.fail('TEST_APP_ID is not defined'); expect.fail('TEST_APP_ID is not defined');
} }
const response = await client.deleteRecordById( const response = await client.deleteRecordById(
parseInt(process.env.TEST_APP_ID), parseInt(process.env.TEST_APP_ID),
0 0
); );
expect(response.statusCode).to.equal(404); expect(response.statusCode).to.equal(404);
expect(response.isSuccessful).to.be.false; expect(response.isSuccessful).to.be.false;
expect(response.message).to.be.undefined; expect(response.message).to.be.undefined;
expect(response.data).to.be.null; expect(response.data).to.be.null;
}); });
}); });
async function addRecord(): Promise<number> {
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;
}
@@ -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;
});
});
+159 -5
View File
@@ -1,5 +1,159 @@
import { OnspringClient } from './../../src'; import {
import { expect } from 'chai'; DataFormat,
import { baseURL, apiKey } from '../mochaRootHooks'; FilterOperators,
OnspringClient,
describe('queryRecords', function () {}); 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;
});
});
+33
View File
@@ -0,0 +1,33 @@
import { OnspringClient, Record, StringRecordValue } from '../../src';
import { expect } from 'chai';
async function addRecord(
baseURL: string | undefined,
apiKey: string | undefined
): Promise<number> {
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 };
+18
View File
@@ -452,6 +452,24 @@ export class OnspringClient {
return apiResponse; 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<ApiResponse<any>>} - A promise that resolves to an ApiResponse of type any.
*/
public async deleteRecordsByIds(
appId: number,
recordIds: number[]
): Promise<ApiResponse<any>> {
const endpoint = EndpointFactory.getDeleteRecordsByIdsEndpoint();
const apiResponse = await this.post<any>(endpoint, {
appId,
recordIds,
});
return apiResponse;
}
/** /**
* @method getReportsByAppId - Gets a paged list of reports by the app id. * @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. * @param {number} appId - The id of the app to get the reports for.
+142
View File
@@ -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 () { describe('saveRecord', function () {
it('should be defined', function () { it('should be defined', function () {
expect(OnspringClient.prototype.saveRecord).to.not.be.undefined; expect(OnspringClient.prototype.saveRecord).to.not.be.undefined;