UNPKG

liveperson-functions-client

Version:

JavaScript client for LivePerson Functions.

131 lines 5.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const csdsClient_1 = require("../../src/helper/csdsClient"); const nock_1 = __importDefault(require("nock")); const common_1 = require("../../src/helper/common"); const csdsApiResultQA = { baseURIs: [ { service: 'foo', account: 'le4711', baseURI: 'foo.liveperson.net', }, { service: 'bar', account: 'le4711', baseURI: 'bar.liveperson.net', }, ], }; const csdsApiResultProd = { baseURIs: [ { service: 'foo', account: '4711', baseURI: 'foo.liveperson.net', }, { service: 'bar', account: '4711', baseURI: 'bar.liveperson.net', }, ], }; describe('CsdsClient', () => { beforeEach(() => { nock_1.default.cleanAll(); }); afterEach(jest.clearAllMocks); describe('Success flows', () => { it('should get csds entry if it exists', async () => { const scope = (0, nock_1.default)('http://csds-app.qa.int.gw.lpcloud.io') .get('/api/account/le4711/service/baseURI.json?version=1.0') .once() .reply(200, csdsApiResultQA) .persist(); const csdsClient = new csdsClient_1.CsdsClient(); const uri = await csdsClient.get('le4711', 'foo'); expect(uri).toEqual('foo.liveperson.net'); expect(scope.isDone()).toBe(true); }); it('should cache requests', async () => { const scope = (0, nock_1.default)('http://csds-app.qa.int.gw.lpcloud.io') .get('/api/account/le4711/service/baseURI.json?version=1.0') .once() .reply(200, csdsApiResultQA); const csdsClient = new csdsClient_1.CsdsClient(); await csdsClient.get('le4711', 'foo'); await csdsClient.get('le4711', 'bar'); expect(scope.isDone()).toBe(true); }); it('should do request if cache expired', async () => { const scope = (0, nock_1.default)('http://adminlogin.liveperson.net') .get('/api/account/4711/service/baseURI.json?version=1.0') .twice() .reply(200, csdsApiResultProd); const csdsClient = new csdsClient_1.CsdsClient(1); await csdsClient.get('4711', 'foo'); await (0, common_1.sleep)(1200); await csdsClient.get('4711', 'bar'); expect(scope.isDone()).toBe(true); }); }); describe('Unhappy flows', () => { it('should throw if domain can not be found', async () => { const scope = (0, nock_1.default)('http://csds-app.qa.int.gw.lpcloud.io') .get('/api/account/le4711/service/baseURI.json?version=1.0') .once() .reply(200, csdsApiResultQA); const csdsClient = new csdsClient_1.CsdsClient(); try { await csdsClient.get('le4711', 'does-not-exist'); } catch (error) { expect(error).toMatchObject({ message: 'Service "does-not-exist" could not be found', name: 'CSDSDomainNotFound', }); expect(scope.isDone()).toBe(true); } }); it('should throw if result is unexpected', async () => { const errorCode = { code: 'ECONNRESET' }; const scope = (0, nock_1.default)('http://csds-app.qa.int.gw.lpcloud.io') .get('/api/account/le4711/service/baseURI.json?version=1.0') .thrice() .replyWithError(errorCode); const csdsClient = new csdsClient_1.CsdsClient(); try { await csdsClient.get('le4711', 'foo'); } catch (error) { expect(error).toMatchObject({ message: 'Error while fetching CSDS entries: ', name: 'CSDSFailure', }); expect(scope.isDone()).toBe(true); } }); it('should throw if response is empty', async () => { const scope = (0, nock_1.default)('http://csds-app.qa.int.gw.lpcloud.io') .get('/api/account/le4711/service/baseURI.json?version=1.0') .once() .reply(200, []); const csdsClient = new csdsClient_1.CsdsClient(); try { await csdsClient.get('le4711', 'foo'); } catch (error) { expect(error).toMatchObject({ message: expect.stringContaining('Service "foo" could not be found'), name: 'CSDSDomainNotFound', }); expect(scope.isDone()).toBe(true); } }); }); }); //# sourceMappingURL=csdsClient.test.js.map