import { expect } from 'chai'; import * as sinon from 'sinon'; import * as proxyquire from 'proxyquire'; import { TestConfiguration } from '../../src/configuration'; import { Licence } from '../../src/models/licence'; import { licenceData } from '../fixtures/licence-data'; describe('Licence SDK', () => { const sandbox = sinon.createSandbox(); const licenceId = 'licenceId'; const userId = 'id'; const accessDuration = 'accessDuration'; let LicenceSdk: any; let instance: any; let config: any; let mockResponse: any; beforeEach(() => { mockResponse = { accessLicenceId: 'licenceId', userId: 'id', joinedDate: '2019-06-11T13:33:08.641Z', seatExpiryDate: null }; config = new TestConfiguration({ licenceApiKey: 'secret', originSystemId: 'n-membership-sdk test', }); const mapAllocateSeat = sandbox.stub().returns(mockResponse); LicenceSdk = proxyquire('../../src/licence', { './mappers/allocate-seat': { mapAllocateSeatResponse: mapAllocateSeat } }).Licence; instance = new LicenceSdk(config); sandbox.stub(instance, 'requestGet').resolves({}); sandbox.stub(instance, 'requestPost').resolves({}); }); afterEach(() => { sandbox.restore(); }); it('throws an error when host and key configuration properties do not exist', () => { try { new LicenceSdk({} as any); expect.fail('It should throw'); } catch (error) { } }); describe('allocateSeat', () => { context('should pass correct parameters to requestPost', () => { let postParams: any; beforeEach(async () => { await instance.allocateSeat(licenceId, userId, accessDuration); postParams = instance.requestPost.getCall(0).args[0]; }); it('URL', async () => { const url = `${instance.membershipApi}/licences/${licenceId}/seats/allocate`; expect(postParams.url).to.equal(url); }); context('body', () => { it('property exists', () => { expect(postParams).to.have.property('body'); }); it('userId property exists', () => { expect(postParams.body).to.have.property('userId'); }); it('accessDuration property exists', async () => { expect(postParams.body).to.have.property('accessDuration'); }); }); }); context('when no errors are returned', () => { beforeEach(() => { instance.requestPost.resolves(mockResponse); }); it('returns the result', async () => { const data = await instance.allocateSeat(licenceId, userId, accessDuration); expect(data).to.deep.equal(mockResponse); }); }); context('when an error is thrown', () => { const errorResponse = new Error('Test failure'); beforeEach(() => { instance.requestPost.rejects(errorResponse); }); it('it throws the same error', async () => { try { await instance.allocateSeat(licenceId, userId, accessDuration); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); describe('getUserAdminLicences', () => { const mockAccessLicences = 'mock-response!'; beforeEach(() => { instance.requestGet.resolves({ accessLicences: mockAccessLicences }); }); it('calls correct URL', async () => { const expectedUrl = `${instance.membershipApi}/licences?adminuserid=${userId}`; await instance.getUserAdminLicences(userId); const { url } = instance.requestGet.getCall(0).args[0]; expect(url).to.equal(expectedUrl); }); it('returns accessLicences property', async () => { const result = await instance.getUserAdminLicences(userId); expect(result).to.equal(mockAccessLicences); }); it('throws any error if request fails', async () => { instance.requestGet.rejects(new Error('test error')); try { const result = await instance.getUserAdminLicences(userId); expect.fail('It should throw'); } catch (error) { } }); }); describe('mapLicenceResponse', () => { it('should return Licence object when called with raw licence data', async () => { const licence = instance.mapLicenceResponse(licenceData); expect(licence instanceof Licence).to.be.true; expect(licence.id).equals(licenceData.id); expect(licence.seatLimit).equals(licenceData.details.seatLimit); expect(licence.isB2b).equals(true); }); }); });