UNPKG

@elastic.io/component-commons-library

Version:
106 lines (105 loc) 3.89 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = __importDefault(require("chai")); const nock_1 = __importDefault(require("nock")); const sinon_1 = __importDefault(require("sinon")); const src_1 = require("../../src"); const { expect } = chai_1.default; let options; let emitter; const url = 'https://example.com'; const resourceServerUrl = 'https://resourceServerUrl.com'; const successStatusCode = 200; const notFoundStatusCode = 404; const successBody = 'Ok'; const notFoundBody = 'Not found'; const secretId = 'secretId'; const cfg = { resourceServerUrl, secretId, authorizationServerTokenEndpointUrl: 'https://some.url', oauth2_field_client_id: 'some_key', oauth2_field_client_secret: 'some_secret', }; const secret = { data: { attributes: { credentials: { access_token: 'accessToken', }, }, }, }; process.env.ELASTICIO_API_URI = 'https://app.example.io'; process.env.ELASTICIO_API_USERNAME = 'user'; process.env.ELASTICIO_API_KEY = 'apiKey'; process.env.ELASTICIO_WORKSPACE_ID = 'workspaceId'; describe('FacelessRestClient', () => { beforeEach(() => { options = { url, method: 'GET', data: {}, headers: {}, }; emitter = { emit: sinon_1.default.spy(), logger: src_1.Logger.getLogger(), }; (0, nock_1.default)(process.env.ELASTICIO_API_URI) .get(`/v2/workspaces/${process.env.ELASTICIO_WORKSPACE_ID}/secrets/${secretId}`) .reply(200, secret); }); afterEach(() => { sinon_1.default.restore(); }); it('Should succeed, urlIsSegment: true', async () => { const client = new src_1.FacelessRestClient(emitter, cfg); (0, nock_1.default)(resourceServerUrl) .get(`/${url}`) .reply(successStatusCode, successBody); const result = await client.makeRequest(options); expect(result.data).to.be.deep.equal(successBody); }); it('Should succeed, urlIsSegment: false', async () => { const client = new src_1.FacelessRestClient(emitter, cfg); options.urlIsSegment = false; (0, nock_1.default)(url) .get('/') .reply(successStatusCode, successBody); const result = await client.makeRequest(options); expect(result.data).to.be.deep.equal(successBody); }); it('Should fail, 404', async () => { const client = new src_1.FacelessRestClient(emitter, cfg); options.urlIsSegment = false; (0, nock_1.default)(url) .get('/') .reply(notFoundStatusCode, notFoundBody); await client.makeRequest(options) .then(() => { throw new Error('Test case does not expect success response'); }) .catch((e) => { expect(e.message).to.be.equal('Request failed with status code 404'); }); }); it('Test makeRequest with expired access_token', async () => { const expiredTokenNock = (0, nock_1.default)(url) .get('/') .reply(401) .get('/') .reply(successStatusCode, successBody); (0, nock_1.default)(process.env.ELASTICIO_API_URI) .get(`/v2/workspaces/${process.env.ELASTICIO_WORKSPACE_ID}/secrets/${secretId}`) .reply(200, secret); const clientExpiredToken = new src_1.FacelessRestClient(emitter, cfg); options.urlIsSegment = false; const result = await clientExpiredToken.makeRequest(options); expect(result.data).to.be.deep.equal(successBody); expect(expiredTokenNock.isDone()).to.be.equal(true); }); });