| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- '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',
- );
- });
- });
- });
|