fix: in can connect test mock underlying axios instance's get method

This commit is contained in:
StevanFreeborn
2023-01-27 22:21:38 -06:00
parent cad208cd4c
commit fa1a952cb2
5 changed files with 212 additions and 7 deletions
+3 -3
View File
@@ -29,9 +29,9 @@ export class ApiResponseFactory {
*/ */
private static TryToGetMessage(response: AxiosResponse): string { private static TryToGetMessage(response: AxiosResponse): string {
if ( if (
response.status == HttpStatusCode.Unauthorized || response.status === HttpStatusCode.Unauthorized ||
response.status == HttpStatusCode.Forbidden || response.status === HttpStatusCode.Forbidden ||
response.status == HttpStatusCode.NotFound response.status === HttpStatusCode.NotFound
) { ) {
return response.data?.message; return response.data?.message;
} }
+149 -1
View File
@@ -16,7 +16,7 @@ describe('ApiResponseFactory', function () {
expect(ApiResponseFactory.getApiResponse).to.have.lengthOf(1); expect(ApiResponseFactory.getApiResponse).to.have.lengthOf(1);
}); });
it('should return an ApiResponse object', function () { it('should return an ApiResponse object when request is successful', function () {
const response: AxiosResponse = { const response: AxiosResponse = {
data: null, data: null,
status: 200, status: 200,
@@ -35,5 +35,153 @@ describe('ApiResponseFactory', function () {
expect(apiResponse.message).to.equal(''); expect(apiResponse.message).to.equal('');
expect(apiResponse.data).to.equal(null); expect(apiResponse.data).to.equal(null);
}); });
it('should return an ApiResponse object without a message value when request is forbidden and the response does not contain a message property', function () {
const response: AxiosResponse = {
data: null,
status: 403,
statusText: 'Forbidden',
headers: {},
config: {} as InternalAxiosRequestConfig,
};
const apiResponse = ApiResponseFactory.getApiResponse(response);
expect(apiResponse).to.not.be.undefined;
expect(apiResponse).to.have.property('statusCode');
expect(apiResponse).to.have.property('message');
expect(apiResponse).to.have.property('data');
expect(apiResponse.statusCode).to.equal(403);
expect(apiResponse.message).to.equal(undefined);
expect(apiResponse.data).to.equal(null);
});
it('should return an ApiResponse object when request is forbidden and the response contains a message property', function () {
const response: AxiosResponse = {
data: {
message: 'Does not have permission to access this resource.'
},
status: 403,
statusText: 'Forbidden',
headers: {},
config: {} as InternalAxiosRequestConfig,
};
const apiResponse = ApiResponseFactory.getApiResponse(response);
expect(apiResponse).to.not.be.undefined;
expect(apiResponse).to.have.property('statusCode');
expect(apiResponse).to.have.property('message');
expect(apiResponse).to.have.property('data');
expect(apiResponse.statusCode).to.equal(403);
expect(apiResponse.message).to.equal('Does not have permission to access this resource.');
expect(apiResponse.data).to.equal(null);
});
it('should return an ApiResponse object without a message value when request is not found and the response does not contain a message property', function () {
const response: AxiosResponse = {
data: null,
status: 404,
statusText: 'Not Found',
headers: {},
config: {} as InternalAxiosRequestConfig,
};
const apiResponse = ApiResponseFactory.getApiResponse(response);
expect(apiResponse).to.not.be.undefined;
expect(apiResponse).to.have.property('statusCode');
expect(apiResponse).to.have.property('message');
expect(apiResponse).to.have.property('data');
expect(apiResponse.statusCode).to.equal(404);
expect(apiResponse.message).to.equal(undefined);
expect(apiResponse.data).to.equal(null);
});
it('should return an ApiResponse object with a message value when request is not found and the response contains a message property', function () {
const response: AxiosResponse = {
data: {
message: 'Resource not found.'
},
status: 404,
statusText: 'Not Found',
headers: {},
config: {} as InternalAxiosRequestConfig,
};
const apiResponse = ApiResponseFactory.getApiResponse(response);
expect(apiResponse).to.not.be.undefined;
expect(apiResponse).to.have.property('statusCode');
expect(apiResponse).to.have.property('message');
expect(apiResponse).to.have.property('data');
expect(apiResponse.statusCode).to.equal(404);
expect(apiResponse.message).to.equal('Resource not found.');
expect(apiResponse.data).to.equal(null);
});
it('should return an ApiResponse object without a message value when request is unauthorized and the response does not contain a message property', function () {
const response: AxiosResponse = {
data: null,
status: 401,
statusText: 'Unauthorized',
headers: {},
config: {} as InternalAxiosRequestConfig,
};
const apiResponse = ApiResponseFactory.getApiResponse(response);
expect(apiResponse).to.not.be.undefined;
expect(apiResponse).to.have.property('statusCode');
expect(apiResponse).to.have.property('message');
expect(apiResponse).to.have.property('data');
expect(apiResponse.statusCode).to.equal(401);
expect(apiResponse.message).to.equal(undefined);
expect(apiResponse.data).to.equal(null);
});
it('should return an ApiResponse object with a message when request is unauthorized and the response contains a message property', function () {
const response: AxiosResponse = {
data: {
message: 'Unauthorized.'
},
status: 401,
statusText: 'Unauthorized',
headers: {},
config: {} as InternalAxiosRequestConfig,
};
const apiResponse = ApiResponseFactory.getApiResponse(response);
expect(apiResponse).to.not.be.undefined;
expect(apiResponse).to.have.property('statusCode');
expect(apiResponse).to.have.property('message');
expect(apiResponse).to.have.property('data');
expect(apiResponse.statusCode).to.equal(401);
expect(apiResponse.message).to.equal('Unauthorized.');
expect(apiResponse.data).to.equal(null);
});
it('should return an ApiResponse object with a message when request is a bad request', function () {
const response: AxiosResponse = {
data: {
field: 'Invalid input.'
},
status: 400,
statusText: 'Bad Request',
headers: {},
config: {} as InternalAxiosRequestConfig,
};
const apiResponse = ApiResponseFactory.getApiResponse(response);
expect(apiResponse).to.not.be.undefined;
expect(apiResponse).to.have.property('statusCode');
expect(apiResponse).to.have.property('message');
expect(apiResponse).to.have.property('data');
expect(apiResponse.statusCode).to.equal(400);
expect(apiResponse.message).to.equal('{"field":"Invalid input."}');
expect(apiResponse.data).to.equal(null);
});
}); });
}); });
+11
View File
@@ -17,4 +17,15 @@ describe('App', function () {
it('should create a new instance of the App class', function () { it('should create a new instance of the App class', function () {
expect(() => new App('href', 1, 'name')).to.not.throw(); expect(() => new App('href', 1, 'name')).to.not.throw();
}); });
it('should create a new instance of the App class with the correct properties and values', function () {
const app = new App('href', 1, 'name');
expect(app).to.have.property('href');
expect(app).to.have.property('id');
expect(app).to.have.property('name');
expect(app.href).to.equal('href');
expect(app.id).to.equal(1);
expect(app.name).to.equal('name');
});
}); });
+40 -3
View File
@@ -1,5 +1,6 @@
import { OnspringClient } from '../src/OnspringClient'; import { OnspringClient } from '../src/OnspringClient';
import { ApiResponse } from '../src/models/ApiResponse'; import { ApiResponse } from '../src/models/ApiResponse';
import axios, { AxiosResponse, InternalAxiosRequestConfig } from 'axios';
import { expect } from 'chai'; import { expect } from 'chai';
import * as sinon from 'sinon'; import * as sinon from 'sinon';
@@ -85,7 +86,7 @@ describe('OnspringClient', function () {
expect(() => new OnspringClient(baseUrl, apiKey)).to.not.throw(); expect(() => new OnspringClient(baseUrl, apiKey)).to.not.throw();
}); });
it('should have a client property', function () { it('should create a new instance of an onspring client with proper properties', function () {
expect(new OnspringClient(baseUrl, apiKey)).to.have.property('_client'); expect(new OnspringClient(baseUrl, apiKey)).to.have.property('_client');
}); });
@@ -114,14 +115,50 @@ describe('OnspringClient', function () {
it('should return a promise that resolves to true when able to connect to the Onspring API', async function () { 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 client = new OnspringClient(baseUrl, apiKey);
sinon.stub(client, 'get' as any).returns(Promise.resolve(new ApiResponse(200, 'OK', null)));
const mockClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockClient, 'get').returns(Promise.resolve({
status: 200,
statusText: 'OK',
data: null,
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse));
sinon.stub(client, '_client' as any).value(mockClient);
const result = await client.canConnect(); const result = await client.canConnect();
expect(result).to.be.true; expect(result).to.be.true;
}); });
it('should return a promise that resolves to false when unable to connect to the Onspring API', async function () { 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); const client = new OnspringClient(baseUrl, apiKey);
sinon.stub(client, 'get' as any).returns(Promise.resolve(new ApiResponse(500, 'Internal Server Error', null))); // sinon.stub(client, 'get' as any).returns(Promise.resolve(new ApiResponse(500, 'Internal Server Error', null)));
const mockClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockClient, 'get').returns(Promise.reject({
status: 500,
statusText: 'Internal Server Error',
data: null,
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse));
sinon.stub(client, '_client' as any).value(mockClient);
const result = await client.canConnect(); const result = await client.canConnect();
expect(result).to.be.false; expect(result).to.be.false;
}); });
+9
View File
@@ -29,4 +29,13 @@ describe('PagingRequest', function () {
it('should create a new instance of the PagingRequest class when the pageNumber is greater than 0 and the pageSize is between 1 and 1000', function () { it('should create a new instance of the PagingRequest class when the pageNumber is greater than 0 and the pageSize is between 1 and 1000', function () {
expect(() => new PagingRequest(1, 1)).to.not.throw(); expect(() => new PagingRequest(1, 1)).to.not.throw();
}); });
it('should create a new instance of the PagingRequest class with the correct properties and values', function () {
const pagingRequest = new PagingRequest(1, 1);
expect(pagingRequest).to.have.property('pageNumber');
expect(pagingRequest).to.have.property('pageSize');
expect(pagingRequest.pageNumber).to.equal(1);
expect(pagingRequest.pageSize).to.equal(1);
});
}); });