import axios, { AxiosResponse } from 'axios'; import { expect } from 'chai'; import IApiConnection from '../src/interfaces/IApiConnection'; import IApiPost from "../src/interfaces/IApiPost"; import CoreCaller from '../src/index'; import * as sinon from 'sinon'; import TokenRepository from '../src/repository/tokenRepository'; import { token_data } from './data/tokenData'; import { token_response } from './data/tokenResponse'; import { identifier_body_data_post, identifier_body_data_put } from './data/identifierDate'; import { AuthService } from '../src/services/authService'; import { ParameterStoreService } from '../src/services/parameterStoreService'; import { ILoginParameters } from '../src/interfaces/ILoginParameters'; import { format } from 'date-fns'; import IApiGet from '../src/interfaces/IApiGet'; const sandbox = sinon.createSandbox(); const connection_info: IApiConnection = { base_url: 'https://httpbin.org/', }; const data = { service: 'wallet', action: 'add', status: 'ACTIVE', }; const api_response = { data, status: 201, statusText: 'OK', headers: token_data, config: null, } as any as AxiosResponse; const auth_url = 'https://httpbin.org/post'; const post_request: IApiPost = { path: 'https://httpbin.org/post', body: identifier_body_data_post, }; const put_request: IApiPost = { path: 'https://httpbin.org/put', body: identifier_body_data_put, }; const get_request: IApiGet = { path: 'get' }; const parameters: ILoginParameters = { username: { value: 'test' }, password: { value: '123' } } as any as ILoginParameters; describe('Api Caller Tests', () => { afterEach(async () => { sandbox.restore(); }); it('SUCCESS: Instances Class', () => { new CoreCaller(connection_info, auth_url); new AuthService(auth_url, axios); }); it('SUCCESS: Call Api Post Method', async () => { const api = new CoreCaller(connection_info, auth_url); let response; try { sandbox.stub(CoreCaller.prototype, 'verifyToken').resolves(token_data); sandbox.stub(axios, 'post').resolves(api_response); response = await api.post(post_request) } catch (err) { response = err; } expect(response).to.be.equal(response); }); it('SUCCESS: Call Api Put Method', async () => { const api = new CoreCaller(connection_info, auth_url); let response; try { sandbox.stub(CoreCaller.prototype, 'verifyToken').resolves(token_data); sandbox.stub(axios, 'put').resolves(api_response); response = await api.put(put_request); } catch (err) { response = err; } expect(response).to.be.equal(response); }); it('SUCCESS - Call Api Get Method', async () => { const api = new CoreCaller(connection_info, auth_url); let response; try { sandbox.stub(CoreCaller.prototype, 'verifyToken').resolves(token_data); sandbox.stub(axios, 'get').resolves(api_response); response = await api.get(get_request); } catch (err) { response = err; } expect(response).to.be.equal(response); }); it('ERROR - Call Api Get Method', async () => { const api = new CoreCaller(connection_info, auth_url); let response; try { sandbox.stub(axios, 'get').rejects(); response = await api.get(get_request); } catch (err) { response = err; } expect(response).to.be.equal(response); }); it('ERROR - Call Api Post Method', async () => { const api = new CoreCaller(connection_info, auth_url); let response; try { sandbox.stub(axios, 'post').rejects(); response = await api.post(post_request); } catch (err) { response = err; } expect(response).to.be.equal(response); }); it('ERROR - Call Api Put Method', async () => { const api = new CoreCaller(connection_info, auth_url); let response; try { sandbox.stub(axios, 'put').rejects(); response = await api.put(post_request); } catch (err) { response = err; } expect(response).to.be.equal(response); }); it('SUCCESS: Verify token with token', async () => { sandbox.stub(CoreCaller.prototype, 'post').resolves(); sandbox.stub(TokenRepository.prototype, 'getToken').resolves(token_data); sandbox.stub(TokenRepository.prototype, 'saveToken').resolves(); sandbox.stub(CoreCaller.prototype, 'authenticate').resolves(); const valid_token = new CoreCaller(connection_info, auth_url); const response = await valid_token.verifyToken(); expect(response.date).be.to.equal('2022-01-01'); }); it('SUCCESS: Verify token without token', async () => { sandbox.stub(CoreCaller.prototype, 'post').resolves(); sandbox.stub(TokenRepository.prototype, 'getToken').resolves(); sandbox.stub(TokenRepository.prototype, 'saveToken').resolves(); sandbox.stub(CoreCaller.prototype, 'authenticate').resolves(token_response); const valid_token = new CoreCaller(connection_info, auth_url); const response = await valid_token.verifyToken(); expect(response.date).be.to.equal(format(new Date(), 'yyyy-MM-dd')); }); it('SUCCESS: Authenticate', async () => { sandbox.stub(ParameterStoreService.prototype, 'getPartnerParameters').resolves(parameters); sandbox.stub(AuthService.prototype, 'registerToken').resolves(token_response); const valid_token = new CoreCaller(connection_info, auth_url); const response = await valid_token.authenticate(); expect(response.body.access_token).be.to.equal('token'); }); });