diff --git a/src/models/OnspringClient.ts b/src/models/OnspringClient.ts index 7aa7dc0..843ff6a 100644 --- a/src/models/OnspringClient.ts +++ b/src/models/OnspringClient.ts @@ -313,11 +313,11 @@ export class OnspringClient { } /** - * @method getRecordByAppId - Gets records by an app id. + * @method getRecordsByAppId - Gets records by an app id. * @param {GetRecordsByAppIdRequest} request - The request that will be used to get the records. * @returns {Promise>} - A promise that resolves to an ApiResponse of type GetPagedRecordsResponse. */ - public async getRecordByAppId( + public async getRecordsByAppId( request: GetRecordsByAppIdRequest ): Promise> { const { appId, pagingRequest, fieldIds, dataFormat } = request; diff --git a/tests/ApiResponse.spec.ts b/tests/ApiResponse.spec.ts index c6b0b73..67e868c 100644 --- a/tests/ApiResponse.spec.ts +++ b/tests/ApiResponse.spec.ts @@ -25,6 +25,7 @@ import { ReportData } from '../src/models/ReportData'; import { Row } from '../src/models/Row'; import { Record } from '../src/models/Record'; import { RecordValue } from '../src/models/RecordValue'; +import { GetPagedRecordsResponse } from '../src/models/GetPagedRecordsResponse'; describe('ApiResponse', function () { it('should be defined', function () { @@ -1173,4 +1174,84 @@ describe('ApiResponse', function () { } }); }); + + describe('asGetPagedRecordsResponseType', function () { + it('should be defined', function () { + expect(ApiResponse.prototype.asGetPagedRecordsResponseType).to.not.be + .undefined; + }); + + it('should be a function', function () { + expect(ApiResponse.prototype.asGetPagedRecordsResponseType).to.be.a( + 'function' + ); + }); + + it('should have no parameters', function () { + expect( + ApiResponse.prototype.asGetPagedRecordsResponseType + ).to.have.lengthOf(0); + }); + + it('should return an ApiResponse', function () { + const mockResponseData = { + pageSize: 2, + pageNumber: 1, + totalRecords: 2, + totalPages: 1, + items: [ + { + appId: 1, + recordId: 1, + fieldData: [ + { + type: 'Text', + fieldId: 'field 1', + value: 'field value 1', + }, + ], + }, + { + appId: 2, + recordId: 2, + fieldData: [ + { + type: 'Text', + fieldId: 'field 1', + value: 'field value 1', + }, + ], + }, + ], + }; + + const apiResponse = new ApiResponse(200, 'OK', mockResponseData); + const getPagedRecordsResponse = + apiResponse.asGetPagedRecordsResponseType(); + + expect(getPagedRecordsResponse).to.be.an.instanceof( + ApiResponse + ); + expect(getPagedRecordsResponse.data).to.be.an.instanceof( + GetPagedRecordsResponse + ); + expect(getPagedRecordsResponse.data).to.have.property('pageSize', 2); + expect(getPagedRecordsResponse.data).to.have.property('pageNumber', 1); + expect(getPagedRecordsResponse.data).to.have.property('totalRecords', 2); + expect(getPagedRecordsResponse.data).to.have.property('totalPages', 1); + + expect(getPagedRecordsResponse.data) + .to.have.property('items') + .to.be.an('array') + .that.has.lengthOf(2); + + expect(getPagedRecordsResponse.data).to.not.be.null; + + if (getPagedRecordsResponse.data != null) { + getPagedRecordsResponse.data.items.forEach((record) => { + expect(record).to.be.an.instanceof(Record); + }); + } + }); + }); }); diff --git a/tests/OnspringClient.spec.ts b/tests/OnspringClient.spec.ts index 3c42461..3746e78 100644 --- a/tests/OnspringClient.spec.ts +++ b/tests/OnspringClient.spec.ts @@ -22,12 +22,14 @@ import { ListItemResponse } from '../src/models/ListItemResponse'; import { Report } from '../src/models/Report'; import { GetPagedReportsResponse } from '../src/models/GetPagedReportsResponse'; import { ReportData } from '../src/models/ReportData'; -import fs from 'fs'; -import path from 'path'; -import * as sinon from 'sinon'; import { GetRecordRequest } from '../src/models/GetRecordRequest'; import { Record } from '../src/models/Record'; import { RecordValue } from '../src/models/RecordValue'; +import { GetRecordsByAppIdRequest } from '../src/models/GetRecordsByAppIdRequest'; +import { GetPagedRecordsResponse } from '../src/models/GetPagedRecordsResponse'; +import fs from 'fs'; +import path from 'path'; +import * as sinon from 'sinon'; describe('OnspringClient', function () { const baseUrl = 'https://api.onspring.dev'; @@ -2999,4 +3001,210 @@ describe('OnspringClient', function () { expect(result).to.have.property('data', null); }); }); + + describe('getRecordsByAppId', function () { + it('should be defined', function () { + expect(OnspringClient.prototype.getRecordsByAppId).to.not.be.undefined; + }); + + it('should be a function', function () { + expect(OnspringClient.prototype.getRecordsByAppId).to.be.a('function'); + }); + + it('should return a promise', function () { + expect( + new OnspringClient(baseUrl, apiKey).getRecordsByAppId( + new GetRecordsByAppIdRequest(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, 'get').returns( + Promise.resolve({ + status: 200, + statusText: 'OK', + data: { + pageSize: 1, + pageNumber: 1, + totalPages: 1, + totalRecords: 1, + items: [ + { + appId: 1, + recordId: 1, + fieldData: [ + { + type: 'Text', + fieldId: 1, + value: 'Test', + }, + { + type: 'Text', + fieldId: 2, + value: 'Test', + }, + ], + }, + ], + }, + headers: {}, + config: {} as InternalAxiosRequestConfig, + } as AxiosResponse) + ); + + sinon.stub(client, '_client' as any).value(mockAxiosClient); + + const result = await client.getRecordsByAppId( + new GetRecordsByAppIdRequest(1) + ); + + expect(result).to.be.instanceOf(ApiResponse); + expect(result).to.have.property('statusCode', 200); + expect(result).to.have.property('isSuccessful', true); + expect(result).to.have.property('message', ''); + expect(result).to.have.property('data'); + expect(result.data).to.be.an.instanceOf(GetPagedRecordsResponse); + expect(result.data).to.not.be.null; + + if (result.data != null) { + expect(result.data).to.have.property('pageSize', 1); + expect(result.data).to.have.property('pageNumber', 1); + expect(result.data).to.have.property('totalPages', 1); + expect(result.data).to.have.property('totalRecords', 1); + expect(result.data).to.have.property('items'); + expect(result.data.items).to.be.an('array').that.has.lengthOf(1); + + result.data.items.forEach((record) => { + expect(record).to.be.instanceOf(Record); + expect(record).to.have.property('appId', 1); + expect(record).to.have.property('recordId', 1); + expect(record).to.have.property('fieldData'); + expect(record.fieldData).to.not.be.null; + + if (record.fieldData != null) { + expect(record.fieldData).to.be.an('array').that.has.lengthOf(2); + + record.fieldData.forEach((recordValue) => { + expect(recordValue).to.be.instanceOf(RecordValue); + expect(recordValue).to.have.property('type'); + expect(recordValue).to.have.property('fieldId'); + expect(recordValue).to.have.property('value'); + }); + } + }); + } + }); + + 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, 'get').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.getRecordsByAppId( + new GetRecordsByAppIdRequest(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, 'get').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.getRecordsByAppId( + new GetRecordsByAppIdRequest(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, 'get').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.getRecordsByAppId( + new GetRecordsByAppIdRequest(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); + }); + }); });