2023-02-02 14:03:59 -06:00
|
|
|
import { OnspringClient } from '../src/models/OnspringClient';
|
2023-01-27 15:00:34 -06:00
|
|
|
import { ApiResponse } from '../src/models/ApiResponse';
|
2023-01-27 22:21:38 -06:00
|
|
|
import axios, { AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
2023-01-25 22:28:45 -06:00
|
|
|
import { expect } from 'chai';
|
2023-01-27 15:00:34 -06:00
|
|
|
import * as sinon from 'sinon';
|
2023-02-01 21:21:39 -06:00
|
|
|
import { GetPagedAppsResponse } from '../src/models/GetPagedAppsResponse';
|
|
|
|
|
import { App } from '../src/models/App';
|
2023-01-25 22:28:45 -06:00
|
|
|
|
2023-01-26 22:40:13 -06:00
|
|
|
describe('OnspringClient', function () {
|
2023-01-27 15:00:34 -06:00
|
|
|
const baseUrl = 'https://api.onspring.dev';
|
2023-01-26 22:40:13 -06:00
|
|
|
const apiKey = 'apiKey';
|
|
|
|
|
|
2023-01-25 22:28:45 -06:00
|
|
|
it('should be defined', function () {
|
|
|
|
|
expect(OnspringClient).to.not.be.undefined;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should have a constructor', function () {
|
|
|
|
|
expect(OnspringClient).to.have.property('constructor');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should have 2 parameters', function () {
|
|
|
|
|
expect(OnspringClient).to.have.lengthOf(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error when the baseUrl is null', function () {
|
2023-01-26 22:40:13 -06:00
|
|
|
expect(() => new OnspringClient(null, apiKey)).to.throw(
|
2023-01-25 22:28:45 -06:00
|
|
|
'baseUrl must be an absolute and well-formed URI.'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error when the baseUrl is undefined', function () {
|
2023-01-26 22:40:13 -06:00
|
|
|
expect(() => new OnspringClient(undefined, apiKey)).to.throw(
|
2023-01-25 22:28:45 -06:00
|
|
|
'baseUrl must be an absolute and well-formed URI.'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error when the baseUrl is an empty string', function () {
|
2023-01-26 22:40:13 -06:00
|
|
|
expect(() => new OnspringClient('', apiKey)).to.throw(
|
2023-01-25 22:28:45 -06:00
|
|
|
'baseUrl must be an absolute and well-formed URI.'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error when the baseUrl is a string with only spaces', function () {
|
2023-01-26 22:40:13 -06:00
|
|
|
expect(() => new OnspringClient(' ', apiKey)).to.throw(
|
2023-01-25 22:28:45 -06:00
|
|
|
'baseUrl must be an absolute and well-formed URI.'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error when the baseUrl is not a valid url', function () {
|
2023-01-26 22:40:13 -06:00
|
|
|
expect(() => new OnspringClient('api.onspring.com', apiKey)).to.throw(
|
2023-01-25 22:28:45 -06:00
|
|
|
'baseUrl must be an absolute and well-formed URI.'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error when the apiKey is null', function () {
|
2023-01-26 22:40:13 -06:00
|
|
|
expect(() => new OnspringClient(baseUrl, null)).to.throw(
|
2023-01-25 22:28:45 -06:00
|
|
|
'apiKey cannot be null/empty/whitespace.'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error when the apiKey is undefined', function () {
|
2023-01-26 22:40:13 -06:00
|
|
|
expect(() => new OnspringClient(baseUrl, undefined)).to.throw(
|
2023-01-25 22:28:45 -06:00
|
|
|
'apiKey cannot be null/empty/whitespace.'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error when the apiKey is an empty string', function () {
|
2023-01-26 22:40:13 -06:00
|
|
|
expect(() => new OnspringClient(baseUrl, '')).to.throw(
|
2023-01-25 22:28:45 -06:00
|
|
|
'apiKey cannot be null/empty/whitespace.'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error when the apiKey is a string with only spaces', function () {
|
2023-01-26 22:40:13 -06:00
|
|
|
expect(() => new OnspringClient(baseUrl, ' ')).to.throw(
|
2023-01-25 22:28:45 -06:00
|
|
|
'apiKey cannot be null/empty/whitespace.'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not throw an error when the baseUrl is a valid url', function () {
|
2023-01-26 22:40:13 -06:00
|
|
|
expect(() => new OnspringClient(baseUrl, apiKey)).to.not.throw();
|
2023-01-25 22:28:45 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not throw an error when the baseUrl is a valid url', function () {
|
2023-02-01 22:44:30 -06:00
|
|
|
expect(
|
|
|
|
|
() => new OnspringClient('http://api.onspring.com', apiKey)
|
|
|
|
|
).to.not.throw();
|
2023-01-25 22:28:45 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not throw an error when the apiKey is a valid string', function () {
|
2023-01-26 22:40:13 -06:00
|
|
|
expect(() => new OnspringClient(baseUrl, apiKey)).to.not.throw();
|
2023-01-25 22:28:45 -06:00
|
|
|
});
|
|
|
|
|
|
2023-01-27 22:21:38 -06:00
|
|
|
it('should create a new instance of an onspring client with proper properties', function () {
|
2023-01-26 22:40:13 -06:00
|
|
|
expect(new OnspringClient(baseUrl, apiKey)).to.have.property('_client');
|
2023-01-25 22:28:45 -06:00
|
|
|
});
|
2023-01-27 15:00:34 -06:00
|
|
|
|
|
|
|
|
describe('canConnect', function () {
|
|
|
|
|
it('should be defined', function () {
|
2023-02-01 22:44:30 -06:00
|
|
|
expect(new OnspringClient(baseUrl, apiKey).canConnect).to.not.be
|
|
|
|
|
.undefined;
|
2023-01-27 15:00:34 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be a function', function () {
|
2023-02-01 22:44:30 -06:00
|
|
|
expect(new OnspringClient(baseUrl, apiKey).canConnect).to.be.a(
|
|
|
|
|
'function'
|
|
|
|
|
);
|
2023-01-27 15:00:34 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should have 0 parameters', function () {
|
2023-02-01 22:44:30 -06:00
|
|
|
expect(new OnspringClient(baseUrl, apiKey).canConnect).to.have.lengthOf(
|
|
|
|
|
0
|
|
|
|
|
);
|
2023-01-27 15:00:34 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a promise', function () {
|
2023-02-01 22:44:30 -06:00
|
|
|
expect(new OnspringClient(baseUrl, apiKey).canConnect()).to.be.a(
|
|
|
|
|
'promise'
|
|
|
|
|
);
|
2023-01-27 15:00:34 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a promise that resolves to a boolean', async function () {
|
|
|
|
|
const client = new OnspringClient(baseUrl, apiKey);
|
2023-02-01 22:44:30 -06:00
|
|
|
|
2023-02-01 21:21:39 -06:00
|
|
|
const mockAxiosClient = axios.create({
|
2023-01-27 22:21:38 -06:00
|
|
|
baseURL: baseUrl,
|
|
|
|
|
headers: {
|
|
|
|
|
'x-apikey': apiKey,
|
|
|
|
|
'x-api-version': '2',
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-02-01 21:21:39 -06:00
|
|
|
|
2023-02-01 22:44:30 -06:00
|
|
|
sinon.stub(mockAxiosClient, 'get').returns(
|
|
|
|
|
Promise.resolve({
|
|
|
|
|
status: 200,
|
|
|
|
|
statusText: 'OK',
|
|
|
|
|
data: null,
|
|
|
|
|
headers: {},
|
|
|
|
|
config: {} as InternalAxiosRequestConfig,
|
|
|
|
|
} as AxiosResponse)
|
|
|
|
|
);
|
2023-01-27 22:21:38 -06:00
|
|
|
|
2023-02-01 21:21:39 -06:00
|
|
|
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
|
|
|
|
|
|
|
|
|
const result = await client.canConnect();
|
|
|
|
|
expect(result).to.be.a('boolean');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a promise that resolves to true when able to connect to the Onspring API', async function () {
|
|
|
|
|
const client = new OnspringClient(baseUrl, apiKey);
|
|
|
|
|
|
|
|
|
|
const mockAxiosClient = axios.create({
|
|
|
|
|
baseURL: baseUrl,
|
|
|
|
|
headers: {
|
|
|
|
|
'x-apikey': apiKey,
|
|
|
|
|
'x-api-version': '2',
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-02-01 22:44:30 -06:00
|
|
|
|
|
|
|
|
sinon.stub(mockAxiosClient, 'get').returns(
|
|
|
|
|
Promise.resolve({
|
|
|
|
|
status: 200,
|
|
|
|
|
statusText: 'OK',
|
|
|
|
|
data: null,
|
|
|
|
|
headers: {},
|
|
|
|
|
config: {} as InternalAxiosRequestConfig,
|
|
|
|
|
} as AxiosResponse)
|
|
|
|
|
);
|
2023-02-01 21:21:39 -06:00
|
|
|
|
|
|
|
|
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
2023-01-27 22:21:38 -06:00
|
|
|
|
2023-01-27 15:00:34 -06:00
|
|
|
const result = await client.canConnect();
|
|
|
|
|
expect(result).to.be.true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a promise that resolves to false when unable to connect to the Onspring API', async function () {
|
|
|
|
|
const client = new OnspringClient(baseUrl, apiKey);
|
2023-02-01 21:21:39 -06:00
|
|
|
|
|
|
|
|
const mockAxiosClient = axios.create({
|
2023-01-27 22:21:38 -06:00
|
|
|
baseURL: baseUrl,
|
|
|
|
|
headers: {
|
|
|
|
|
'x-apikey': apiKey,
|
|
|
|
|
'x-api-version': '2',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-01 22:44:30 -06:00
|
|
|
sinon.stub(mockAxiosClient, 'get').returns(
|
|
|
|
|
Promise.resolve({
|
|
|
|
|
status: 500,
|
|
|
|
|
statusText: 'Internal Server Error',
|
|
|
|
|
data: null,
|
|
|
|
|
headers: {},
|
|
|
|
|
config: {} as InternalAxiosRequestConfig,
|
|
|
|
|
} as AxiosResponse)
|
|
|
|
|
);
|
2023-01-27 22:21:38 -06:00
|
|
|
|
2023-02-01 21:21:39 -06:00
|
|
|
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
2023-02-01 22:44:30 -06:00
|
|
|
|
2023-01-27 15:00:34 -06:00
|
|
|
const result = await client.canConnect();
|
|
|
|
|
expect(result).to.be.false;
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-02-01 21:21:39 -06:00
|
|
|
|
|
|
|
|
describe('getApps', function () {
|
|
|
|
|
it('should be defined', function () {
|
|
|
|
|
expect(new OnspringClient(baseUrl, apiKey).getApps).to.not.be.undefined;
|
|
|
|
|
});
|
2023-02-01 22:44:30 -06:00
|
|
|
|
2023-02-01 21:21:39 -06:00
|
|
|
it('should be a function', function () {
|
|
|
|
|
expect(new OnspringClient(baseUrl, apiKey).getApps).to.be.a('function');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should have 0 parameters', function () {
|
|
|
|
|
expect(new OnspringClient(baseUrl, apiKey).getApps).to.have.lengthOf(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a promise', function () {
|
|
|
|
|
expect(new OnspringClient(baseUrl, apiKey).getApps()).to.be.a('promise');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a promise that resolves to a paged response 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, 'get').returns(
|
|
|
|
|
Promise.resolve({
|
|
|
|
|
status: 200,
|
|
|
|
|
statusText: 'OK',
|
|
|
|
|
data: {
|
|
|
|
|
pageNumber: 1,
|
|
|
|
|
pageSize: 2,
|
|
|
|
|
totalPages: 1,
|
|
|
|
|
totalRecords: 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.getApps();
|
|
|
|
|
expect(result).to.be.instanceOf(ApiResponse<GetPagedAppsResponse>);
|
|
|
|
|
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.instanceOf(GetPagedAppsResponse);
|
|
|
|
|
expect(result.data).to.have.property('pageNumber', 1);
|
|
|
|
|
expect(result.data).to.have.property('pageSize', 2);
|
|
|
|
|
expect(result.data).to.have.property('totalPages', 1);
|
|
|
|
|
expect(result.data).to.have.property('totalRecords', 2);
|
|
|
|
|
expect(result.data).to.have.property('items');
|
2023-02-02 13:53:02 -06:00
|
|
|
expect(result.data).to.not.be.null;
|
|
|
|
|
if (result.data != null) {
|
|
|
|
|
expect(result.data.items).to.be.instanceOf(Array);
|
|
|
|
|
expect(result.data.items).to.have.lengthOf(2);
|
|
|
|
|
expect(result.data.items[0]).to.be.instanceOf(App);
|
|
|
|
|
expect(result.data.items[1]).to.be.instanceOf(App);
|
|
|
|
|
}
|
2023-02-01 21:21:39 -06:00
|
|
|
});
|
2023-02-01 22:44:30 -06:00
|
|
|
|
|
|
|
|
it('should return a promise that resolves to an api response when request returns a 400 status code', 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: {
|
|
|
|
|
PageSize: ['The field PageSize must be between 0 and 1000.'],
|
|
|
|
|
},
|
|
|
|
|
headers: {},
|
|
|
|
|
config: {} as InternalAxiosRequestConfig,
|
|
|
|
|
} as AxiosResponse)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
|
|
|
|
|
|
|
|
|
const result = await client.getApps();
|
|
|
|
|
|
|
|
|
|
expect(result).to.be.instanceOf(ApiResponse);
|
|
|
|
|
expect(result).to.have.property('statusCode', 400);
|
|
|
|
|
expect(result).to.have.property('isSuccessful', false);
|
2023-02-01 23:45:14 -06:00
|
|
|
expect(result).to.have.property(
|
|
|
|
|
'message',
|
|
|
|
|
'{"PageSize":["The field PageSize must be between 0 and 1000."]}'
|
|
|
|
|
);
|
2023-02-01 22:44:30 -06:00
|
|
|
expect(result).to.have.property('data');
|
|
|
|
|
expect(result.data).to.be.null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a promise that resolves to an api response when request returns a 401 status code', 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.getApps();
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('getAppById', function () {
|
|
|
|
|
it('should be defined', function () {
|
2023-02-01 23:45:14 -06:00
|
|
|
expect(new OnspringClient(baseUrl, apiKey).getAppById).to.not.be
|
|
|
|
|
.undefined;
|
2023-02-01 22:44:30 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be a function', function () {
|
2023-02-01 23:45:14 -06:00
|
|
|
expect(new OnspringClient(baseUrl, apiKey).getAppById).to.be.a(
|
|
|
|
|
'function'
|
|
|
|
|
);
|
2023-02-01 22:44:30 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should have 1 parameter', function () {
|
2023-02-01 23:45:14 -06:00
|
|
|
expect(new OnspringClient(baseUrl, apiKey).getAppById).to.have.lengthOf(
|
|
|
|
|
1
|
|
|
|
|
);
|
2023-02-01 22:44:30 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a promise', function () {
|
2023-02-01 23:45:14 -06:00
|
|
|
expect(new OnspringClient(baseUrl, apiKey).getAppById(1)).to.be.a(
|
|
|
|
|
'promise'
|
|
|
|
|
);
|
2023-02-01 22:44:30 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a promise that resolves to an api response of an app 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: {
|
|
|
|
|
href: 'https://api.onspring.dev/Apps/id/1',
|
|
|
|
|
id: 1,
|
|
|
|
|
name: 'Test App 1',
|
|
|
|
|
},
|
|
|
|
|
headers: {},
|
|
|
|
|
config: {} as InternalAxiosRequestConfig,
|
|
|
|
|
} as AxiosResponse)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
|
|
|
|
|
|
|
|
|
const result = await client.getAppById(1);
|
|
|
|
|
expect(result).to.be.instanceOf(ApiResponse<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');
|
|
|
|
|
expect(result.data).to.be.instanceOf(App);
|
|
|
|
|
expect(result.data).to.have.property('id', 1);
|
|
|
|
|
expect(result.data).to.have.property('name', 'Test App 1');
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-02 15:43:09 -06:00
|
|
|
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.getAppById(1);
|
|
|
|
|
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, 'get').returns(
|
|
|
|
|
Promise.resolve({
|
|
|
|
|
status: 403,
|
|
|
|
|
statusText: 'Unauthorized',
|
|
|
|
|
data: {
|
|
|
|
|
message: 'Client does not have access to read app: 1',
|
|
|
|
|
},
|
|
|
|
|
headers: {},
|
|
|
|
|
config: {} as InternalAxiosRequestConfig,
|
|
|
|
|
} as AxiosResponse)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
|
|
|
|
|
|
|
|
|
const result = await client.getAppById(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',
|
|
|
|
|
'Client does not have access to read app: 1'
|
|
|
|
|
);
|
|
|
|
|
expect(result).to.have.property('data', null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a promise that resolves to an api response when request receives a 404 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: 404,
|
|
|
|
|
statusText: 'Not Found',
|
|
|
|
|
headers: {},
|
|
|
|
|
config: {} as InternalAxiosRequestConfig,
|
|
|
|
|
} as AxiosResponse)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
|
|
|
|
|
|
|
|
|
const result = await client.getAppById(1);
|
|
|
|
|
expect(result).to.be.instanceOf(ApiResponse);
|
|
|
|
|
expect(result).to.have.property('statusCode', 404);
|
|
|
|
|
expect(result).to.have.property('isSuccessful', false);
|
|
|
|
|
expect(result.message).to.be.undefined;
|
|
|
|
|
expect(result.data).to.be.null;
|
|
|
|
|
});
|
2023-02-01 21:21:39 -06:00
|
|
|
});
|
2023-01-25 22:28:45 -06:00
|
|
|
});
|