feat: finish implementing getReportsByAppId method

This commit is contained in:
StevanFreeborn
2023-02-08 23:33:27 -06:00
parent 9c3a8da485
commit 58de996f1e
5 changed files with 294 additions and 17 deletions
+2 -1
View File
@@ -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<ApiResponse<GetPagedReportsResponse>> {
const endpoint = EndpointFactory.getReportsByAppIdEndpoint(
appId,
pagingRequest
+36 -5
View File
@@ -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,7 +964,12 @@ describe('ApiResponse', function () {
});
it('should return an ApiResponse<GetPagedReportsResponse>', function () {
const mockResponseData = [
const mockResponseData = {
pageSize: 2,
pageNumber: 1,
totalRecords: 2,
totalPages: 1,
items: [
{
appId: 1,
id: 1,
@@ -973,14 +980,38 @@ describe('ApiResponse', function () {
appId: 2,
id: 2,
name: 'string',
description: 'description'
}
]
description: 'description',
},
],
};
const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const getPagedReportsResponse = apiResponse.asGetPagedReportsResponseType();
const getPagedReportsResponse =
apiResponse.asGetPagedReportsResponseType();
expect(getPagedReportsResponse).to.be.an.instanceof(
ApiResponse<GetPagedReportsResponse>
);
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);
});
}
});
});
});
+31
View File
@@ -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);
});
});
+184
View File
@@ -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<GetPagedReportsResponse>);
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);
});
});
});
+30
View File
@@ -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');
});
});