'use strict'; const { expect } = require('chai'); const { v4 } = require('uuid'); const sinon = require('sinon'); const rewire = require('rewire'); const { requestEventMax, createdEventMax, createdEventInvalidEventId1, } = require('../xml/event/js-requests'); const { oadrPoll1 } = require('../xml/poll/js-requests'); const { generatedFromNantumEvent1 } = require('../xml/event/js-responses'); const { sampleEvent1 } = require('../modules/nantum-responses'); describe('Event', function() { let clock; let sandbox, rewired; let fetchEventStub; after(async () => { clock.restore(); }); before(async () => { clock = sinon.useFakeTimers(new Date('2020-04-26T01:00:00.000Z').getTime()); sandbox = sinon.createSandbox(); let registration = {}; let opted = {}; fetchEventStub = sandbox.stub().resolves(sampleEvent1); rewired = rewire('../../../processes/event.js'); rewired.__set__({ nantum: { fetchRegistration: () => Promise.resolve(registration), fetchEvent: fetchEventStub, updateRegistration: async newRegistration => { registration = newRegistration; }, fetchOpted: venId => Promise.resolve(opted[venId] || []), updateOpted: async (venId, newOpted) => { opted[venId] = newOpted || []; }, }, }); }); describe('retrieveEvents', function() { it('translates nantum event to oadr event', async () => { const venId = requestEventMax.venId; const commonName = v4() .replace(/-/g, '') .substring(0, 12); const events = await rewired.retrieveEvents( requestEventMax, commonName, venId, ); expect(events).to.eql(generatedFromNantumEvent1); }); }); describe('poll and updateOptType', function() { it('should return the same event on subsequent polls if it has not been opted', async () => { const venId = oadrPoll1.venId; const commonName = v4() .replace(/-/g, '') .substring(0, 12); const pollResponse1 = await rewired.pollForEvents( oadrPoll1, commonName, venId, ); expect(pollResponse1.events.length).to.eql(1); const pollResponse2 = await rewired.pollForEvents( oadrPoll1, commonName, venId, ); expect(pollResponse2.events.length).to.eql(1); }); it('should not return an opted event in subsequent poll response', async () => { const venId = oadrPoll1.venId; const commonName = v4() .replace(/-/g, '') .substring(0, 12); const pollResponse1 = await rewired.pollForEvents( oadrPoll1, commonName, venId, ); expect(pollResponse1.events.length).to.eql(1); await rewired.updateOptType(createdEventMax, commonName, venId); const pollResponse2 = await rewired.pollForEvents( oadrPoll1, commonName, venId, ); expect(pollResponse2).to.be.undefined; }); it('should fail when an invalid eventId is optedIn', async () => { const venId = oadrPoll1.venId; const commonName = v4() .replace(/-/g, '') .substring(0, 12); const response = await rewired.updateOptType( createdEventInvalidEventId1, commonName, venId, ); expect(response.responseCode).to.eql('454'); expect(response.responseDescription).to.eql( 'Event response references invalid event', ); }); it('should fail when an old modificationNumber is specified', async () => { const venId = oadrPoll1.venId; const commonName = v4() .replace(/-/g, '') .substring(0, 12); const response = await rewired.updateOptType( createdEventInvalidEventId1, commonName, venId, ); expect(response.responseCode).to.eql('454'); expect(response.responseDescription).to.eql( 'Event response references invalid event', ); }); }); });