From 58de996f1e66da96a84cb109f54fc4d8f7c15978 Mon Sep 17 00:00:00 2001 From: StevanFreeborn Date: Wed, 8 Feb 2023 23:33:27 -0600 Subject: [PATCH] feat: finish implementing getReportsByAppId method --- src/models/OnspringClient.ts | 3 +- tests/ApiResponse.spec.ts | 63 ++++++--- tests/GetPagedReportsResponse.spec.ts | 31 +++++ tests/OnspringClient.spec.ts | 184 ++++++++++++++++++++++++++ tests/Report.spec.ts | 30 +++++ 5 files changed, 294 insertions(+), 17 deletions(-) create mode 100644 tests/GetPagedReportsResponse.spec.ts create mode 100644 tests/Report.spec.ts diff --git a/src/models/OnspringClient.ts b/src/models/OnspringClient.ts index f40047b..717cb7f 100644 --- a/src/models/OnspringClient.ts +++ b/src/models/OnspringClient.ts @@ -16,6 +16,7 @@ import { type FileInfo } from './FileInfo'; import { type File } from './File'; import { type ListItemResponse } from './ListItemResponse'; import { type ListItemRequest } from './ListItemRequest'; +import { type GetPagedReportsResponse } from './GetPagedReportsResponse'; /** * @class OnspringClient - A client that can communicate with the Onspring API. @@ -309,7 +310,7 @@ export class OnspringClient { public async getReportsByAppId( appId: number, pagingRequest: PagingRequest = new PagingRequest(1, 50) - ) { + ): Promise> { const endpoint = EndpointFactory.getReportsByAppIdEndpoint( appId, pagingRequest diff --git a/tests/ApiResponse.spec.ts b/tests/ApiResponse.spec.ts index b011aad..c4d378b 100644 --- a/tests/ApiResponse.spec.ts +++ b/tests/ApiResponse.spec.ts @@ -19,6 +19,8 @@ import fs from 'fs'; import { type AxiosResponse } from 'axios'; import path from 'path'; import { ListItemResponse } from '../src/models/ListItemResponse'; +import { GetPagedReportsResponse } from '../src/models/GetPagedReportsResponse'; +import { Report } from '../src/models/Report'; describe('ApiResponse', function () { it('should be defined', function () { @@ -962,25 +964,54 @@ describe('ApiResponse', function () { }); it('should return an ApiResponse', function () { - const mockResponseData = [ - { - appId: 1, - id: 1, - name: 'string', - description: 'description', - }, - { - appId: 2, - id: 2, - name: 'string', - description: 'description' - } - ] + const mockResponseData = { + pageSize: 2, + pageNumber: 1, + totalRecords: 2, + totalPages: 1, + items: [ + { + appId: 1, + id: 1, + name: 'string', + description: 'description', + }, + { + appId: 2, + id: 2, + name: 'string', + description: 'description', + }, + ], + }; const apiResponse = new ApiResponse(200, 'OK', mockResponseData); - const getPagedReportsResponse = apiResponse.asGetPagedReportsResponseType(); + const getPagedReportsResponse = + apiResponse.asGetPagedReportsResponseType(); - + expect(getPagedReportsResponse).to.be.an.instanceof( + ApiResponse + ); + expect(getPagedReportsResponse.data).to.be.an.instanceof( + GetPagedReportsResponse + ); + expect(getPagedReportsResponse.data).to.have.property('pageSize', 2); + expect(getPagedReportsResponse.data).to.have.property('pageNumber', 1); + expect(getPagedReportsResponse.data).to.have.property('totalRecords', 2); + expect(getPagedReportsResponse.data).to.have.property('totalPages', 1); + + expect(getPagedReportsResponse.data) + .to.have.property('items') + .to.be.an('array') + .that.has.lengthOf(2); + + expect(getPagedReportsResponse.data).to.not.be.null; + + if (getPagedReportsResponse.data != null) { + getPagedReportsResponse.data.items.forEach((report) => { + expect(report).to.be.an.instanceof(Report); + }); + } }); }); }); diff --git a/tests/GetPagedReportsResponse.spec.ts b/tests/GetPagedReportsResponse.spec.ts new file mode 100644 index 0000000..6733d81 --- /dev/null +++ b/tests/GetPagedReportsResponse.spec.ts @@ -0,0 +1,31 @@ +import { GetPagedReportsResponse } from '../src/models/GetPagedReportsResponse'; +import { expect } from 'chai'; + +describe('GetPagedReportsResponse', function () { + it('should be defined', function () { + expect(GetPagedReportsResponse).to.be.not.undefined; + }); + + it('should have a constructor', function () { + expect(GetPagedReportsResponse).to.have.property('constructor'); + }); + + it('should have a constructor that takes 5 arguments', function () { + expect(GetPagedReportsResponse).to.have.lengthOf(5); + }); + + it('should create a new instance of GetPagedReportsResponse', function () { + expect(new GetPagedReportsResponse([], 1, 1, 1, 1)).to.be.an.instanceOf( + GetPagedReportsResponse + ); + }); + + it('should set its properties correctly', function () { + const response = new GetPagedReportsResponse([], 1, 1, 1, 1); + expect(response.items).to.be.an('array'); + expect(response.pageNumber).to.be.equal(1); + expect(response.pageSize).to.be.equal(1); + expect(response.totalPages).to.be.equal(1); + expect(response.totalRecords).to.be.equal(1); + }); +}); diff --git a/tests/OnspringClient.spec.ts b/tests/OnspringClient.spec.ts index de24ea9..133b606 100644 --- a/tests/OnspringClient.spec.ts +++ b/tests/OnspringClient.spec.ts @@ -22,6 +22,8 @@ import fs from 'fs'; import path from 'path'; import { ListItemRequest } from '../src/models/ListItemRequest'; import { ListItemResponse } from '../src/models/ListItemResponse'; +import { Report } from '../src/models/Report'; +import { GetPagedReportsResponse } from '../src/models/GetPagedReportsResponse'; describe('OnspringClient', function () { const baseUrl = 'https://api.onspring.dev'; @@ -2447,4 +2449,186 @@ describe('OnspringClient', function () { expect(result.data).to.be.null; }); }); + + describe('getReportsByAppId', function () { + it('should be defined', function () { + expect(OnspringClient.prototype.getReportsByAppId).to.not.be.undefined; + }); + + it('should be a function', function () { + expect(OnspringClient.prototype.getReportsByAppId).to.be.a('function'); + }); + + it('should return a promise', function () { + expect( + new OnspringClient(baseUrl, apiKey).getReportsByAppId(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: 2, + pageNumber: 1, + totalRecords: 2, + totalPages: 1, + items: [ + { + id: 1, + appId: 1, + name: 'Report 1', + description: 'Report 1 Description', + }, + { + id: 1, + appId: 1, + name: 'Report 2', + description: 'Report 2 Description', + }, + ], + }, + headers: {}, + config: {} as InternalAxiosRequestConfig, + } as AxiosResponse) + ); + + sinon.stub(client, '_client' as any).value(mockAxiosClient); + + const result = await client.getReportsByAppId(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.not.be.null; + expect(result.data).to.be.instanceOf(GetPagedReportsResponse); + expect(result.data).to.have.property('pageSize', 2); + expect(result.data).to.have.property('pageNumber', 1); + expect(result.data).to.have.property('totalRecords', 2); + expect(result.data).to.have.property('totalPages', 1); + expect(result.data) + .to.have.property('items') + .that.is.an('array') + .with.lengthOf(2); + + if (result.data != null) { + result.data.items.forEach((item) => { + expect(item).to.be.instanceOf(Report); + expect(item).to.have.property('id'); + expect(item).to.have.property('appId'); + expect(item).to.have.property('name'); + expect(item).to.have.property('description'); + }); + } + }); + + 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.getReportsByAppId(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.getReportsByAppId(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.getReportsByAppId(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); + }); + }); }); diff --git a/tests/Report.spec.ts b/tests/Report.spec.ts new file mode 100644 index 0000000..a94c8ab --- /dev/null +++ b/tests/Report.spec.ts @@ -0,0 +1,30 @@ +import { Report } from '../src/models/Report'; +import { expect } from 'chai'; + +describe('Report', function () { + it('should be defined', function () { + expect(Report).to.be.not.undefined; + }); + + it('should have a constructor', function () { + expect(Report).to.have.property('constructor'); + }); + + it('should have a constructor that takes 4 arguments', function () { + expect(Report).to.have.lengthOf(4); + }); + + it('should create a new instance of Report', function () { + expect( + new Report(1, 1, 'report', 'report description') + ).to.be.an.instanceOf(Report); + }); + + it('should set its properties correctly', function () { + const report = new Report(1, 1, 'report', 'report description'); + expect(report.id).to.be.equal(1); + expect(report.appId).to.be.equal(1); + expect(report.name).to.be.equal('report'); + expect(report.description).to.be.equal('report description'); + }); +});