feat: implement getAppsByIds method

This commit is contained in:
StevanFreeborn
2023-02-02 20:24:20 -06:00
parent d00683ea3c
commit a38a6240bd
10 changed files with 334 additions and 23 deletions
+58 -9
View File
@@ -2,6 +2,7 @@ import { ApiResponse } from '../src/models/ApiResponse';
import { expect } from 'chai';
import { GetPagedAppsResponse } from '../src/models/GetPagedAppsResponse';
import { App } from '../src/models/App';
import { CollectionResponse } from '../src/models/CollectionResponse';
describe('ApiResponse', function () {
it('should be defined', function () {
@@ -85,7 +86,7 @@ describe('ApiResponse', function () {
});
it('should return an ApiResponse<GetPagedAppsResponse> when data contains app items', function () {
var mockApiResponse = {
const mockResponseData = {
pageNumber: 1,
pageSize: 2,
totalPages: 1,
@@ -104,8 +105,8 @@ describe('ApiResponse', function () {
],
};
var apiResponse = new ApiResponse<any>(200, 'OK', mockApiResponse);
var appsPagedResponse = apiResponse.AsGetPagedAppsResponseType();
const apiResponse = new ApiResponse<any>(200, 'OK', mockResponseData);
const appsPagedResponse = apiResponse.AsGetPagedAppsResponseType();
expect(appsPagedResponse).to.be.instanceOf(ApiResponse);
expect(appsPagedResponse.data).to.be.instanceOf(GetPagedAppsResponse);
@@ -123,7 +124,7 @@ describe('ApiResponse', function () {
});
it('should return an ApiResponse<GetPagedAppsResponse> when data contains app items', function () {
var mockApiResponse = {
const mockResponseData = {
pageNumber: 0,
pageSize: 0,
totalPages: 0,
@@ -131,8 +132,8 @@ describe('ApiResponse', function () {
items: [],
};
var apiResponse = new ApiResponse<any>(200, 'OK', mockApiResponse);
var appsPagedResponse = apiResponse.AsGetPagedAppsResponseType();
const apiResponse = new ApiResponse<any>(200, 'OK', mockResponseData);
const appsPagedResponse = apiResponse.AsGetPagedAppsResponseType();
expect(appsPagedResponse).to.be.instanceOf(ApiResponse);
expect(appsPagedResponse.data).to.be.instanceOf(GetPagedAppsResponse);
@@ -158,14 +159,14 @@ describe('ApiResponse', function () {
});
it('should return an ApiResponse<App> when data contain an app', function () {
var mockApiResponse = {
const mockResponseData = {
href: 'https://api.onspring.dev/apps/id/1',
id: 1,
name: 'Test App',
};
var apiResponse = new ApiResponse<any>(200, 'OK', mockApiResponse);
var appResponse = apiResponse.AsAppType();
const apiResponse = new ApiResponse<any>(200, 'OK', mockResponseData);
const appResponse = apiResponse.AsAppType();
expect(appResponse).to.be.instanceOf(ApiResponse);
expect(appResponse.data).to.be.instanceOf(App);
@@ -179,4 +180,52 @@ describe('ApiResponse', function () {
}
});
});
describe('AsAppCollectionType', function () {
it('should be defined', function () {
expect(ApiResponse.prototype.AsAppCollectionType).to.not.be.undefined;
});
it('should have no parameters', function () {
expect(ApiResponse.prototype.AsAppCollectionType).to.have.lengthOf(0);
});
it('should return an ApiResponse<CollectionResponse<App[]>> when data contains app items', function () {
const mockResponseData = {
items: [
{
href: 'https://api.onspring.dev/apps/id/1',
id: 1,
name: 'Test App 1',
},
{
href: 'https://api.onspring.dev/apps/id/2',
id: 2,
name: 'Test App 2',
},
],
};
const apiResponse = new ApiResponse<any>(200, 'OK', mockResponseData);
const appCollectionResponse = apiResponse.AsAppCollectionType();
expect(appCollectionResponse).to.be.instanceOf(
ApiResponse<CollectionResponse<App>>
);
expect(appCollectionResponse.data).to.be.instanceOf(
CollectionResponse<App>
);
expect(appCollectionResponse.data).to.not.be.null;
if (appCollectionResponse.data != null) {
expect(appCollectionResponse.data.items).to.be.instanceOf(Array);
expect(appCollectionResponse.data.items).to.have.lengthOf(2);
appCollectionResponse.data.items.forEach((item) => {
expect(item).to.be.instanceOf(App);
expect(item).to.have.property('id');
expect(item).to.have.property('name');
expect(item).to.have.property('href');
});
}
});
});
});
+127 -1
View File
@@ -5,6 +5,7 @@ import { expect } from 'chai';
import * as sinon from 'sinon';
import { GetPagedAppsResponse } from '../src/models/GetPagedAppsResponse';
import { App } from '../src/models/App';
import { CollectionResponse } from '../src/models/CollectionResponse';
describe('OnspringClient', function () {
const baseUrl = 'https://api.onspring.dev';
@@ -454,7 +455,7 @@ describe('OnspringClient', function () {
sinon.stub(mockAxiosClient, 'get').returns(
Promise.resolve({
status: 403,
statusText: 'Unauthorized',
statusText: 'Forbidden',
data: {
message: 'Client does not have access to read app: 1',
},
@@ -532,5 +533,130 @@ describe('OnspringClient', function () {
});
// TODO: complete wriiting tests for getAppsByIds method
it('should return a promise that resolves to an api response of a collection of apps 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: 200,
statusText: 'OK',
data: {
count: 2,
items: [
{
href: 'https://api.onspring.dev/Apps/id/1',
id: 1,
name: 'Test App 1',
},
{
href: 'https://api.onspring.dev/Apps/id/2',
id: 2,
name: 'Test App 2',
},
],
},
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getAppsByIds([1, 2]);
expect(result).to.be.instanceOf(ApiResponse<CollectionResponse<App[]>>);
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');
if (result.data != null) {
expect(result.data).to.be.instanceOf(CollectionResponse);
expect(result.data).to.have.property('count', 2);
expect(result.data).to.have.property('items');
if (result.data.items != null) {
expect(result.data.items).to.be.an('array');
expect(result.data.items).to.have.lengthOf(2);
result.data.items.forEach((item) => {
expect(item).to.be.instanceOf(App);
expect(item).to.have.property('id');
expect(item).to.have.property('name');
expect(item).to.have.property('href');
});
}
}
});
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.getAppsByIds([1, 2]);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 401);
expect(result).to.have.property('isSuccessful', false);
expect(result.message).to.be.undefined;
expect(result.data).to.be.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: 'Client does not have access to read app: 1, 2',
},
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getAppsByIds([1, 2]);
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',
'Client does not have access to read app: 1, 2'
);
expect(result.data).to.be.null;
});
});
});