event.spec.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict';
  2. const { expect } = require('chai');
  3. const { v4 } = require('uuid');
  4. const sinon = require('sinon');
  5. const rewire = require('rewire');
  6. const {
  7. requestEventMax,
  8. createdEventMax,
  9. createdEventInvalidEventId1,
  10. } = require('../xml/event/js-requests');
  11. const { poll } = require('../xml/poll/js-requests');
  12. const { generatedFromNantumEvent1 } = require('../xml/event/js-responses');
  13. const { sampleEvent1 } = require('../modules/nantum-responses');
  14. describe('Event', function() {
  15. let clock;
  16. let sandbox, rewired;
  17. let fetchEventStub;
  18. after(async () => {
  19. clock.restore();
  20. });
  21. before(async () => {
  22. clock = sinon.useFakeTimers(new Date('2020-04-26T01:00:00.000Z').getTime());
  23. sandbox = sinon.createSandbox();
  24. let registration = {};
  25. let opted = {};
  26. fetchEventStub = sandbox.stub().resolves(sampleEvent1);
  27. rewired = rewire('../../../processes/event.js');
  28. rewired.__set__({
  29. nantum: {
  30. fetchRegistration: () => Promise.resolve(registration),
  31. fetchEvent: fetchEventStub,
  32. updateRegistration: async newRegistration => {
  33. registration = newRegistration;
  34. },
  35. fetchOpted: venId => Promise.resolve(opted[venId] || []),
  36. updateOpted: async (venId, newOpted) => {
  37. opted[venId] = newOpted || [];
  38. },
  39. },
  40. });
  41. });
  42. describe('retrieveEvents', function() {
  43. it('translates nantum event to oadr event', async () => {
  44. const venId = requestEventMax.venId;
  45. const commonName = v4()
  46. .replace(/-/g, '')
  47. .substring(0, 12);
  48. const events = await rewired.retrieveEvents(
  49. requestEventMax,
  50. commonName,
  51. venId,
  52. );
  53. expect(events).to.eql(generatedFromNantumEvent1);
  54. });
  55. });
  56. describe('poll and updateOptType', function() {
  57. it('should return the same event on subsequent polls if it has not been opted', async () => {
  58. const venId = poll.venId;
  59. const commonName = v4()
  60. .replace(/-/g, '')
  61. .substring(0, 12);
  62. const pollResponse1 = await rewired.pollForEvents(
  63. poll,
  64. commonName,
  65. venId,
  66. );
  67. expect(pollResponse1.events.length).to.eql(1);
  68. const pollResponse2 = await rewired.pollForEvents(
  69. poll,
  70. commonName,
  71. venId,
  72. );
  73. expect(pollResponse2.events.length).to.eql(1);
  74. });
  75. it('should not return an opted event in subsequent poll response', async () => {
  76. const venId = poll.venId;
  77. const commonName = v4()
  78. .replace(/-/g, '')
  79. .substring(0, 12);
  80. const pollResponse1 = await rewired.pollForEvents(
  81. poll,
  82. commonName,
  83. venId,
  84. );
  85. expect(pollResponse1.events.length).to.eql(1);
  86. await rewired.updateOptType(createdEventMax, commonName, venId);
  87. const pollResponse2 = await rewired.pollForEvents(
  88. poll,
  89. commonName,
  90. venId,
  91. );
  92. expect(pollResponse2).to.be.undefined;
  93. });
  94. it('should fail when an invalid eventId is optedIn', async () => {
  95. const venId = poll.venId;
  96. const commonName = v4()
  97. .replace(/-/g, '')
  98. .substring(0, 12);
  99. const response = await rewired.updateOptType(
  100. createdEventInvalidEventId1,
  101. commonName,
  102. venId,
  103. );
  104. expect(response.responseCode).to.eql('454');
  105. expect(response.responseDescription).to.eql(
  106. 'Event response references invalid event',
  107. );
  108. });
  109. it('should fail when an old modificationNumber is specified', async () => {
  110. const venId = poll.venId;
  111. const commonName = v4()
  112. .replace(/-/g, '')
  113. .substring(0, 12);
  114. const response = await rewired.updateOptType(
  115. createdEventInvalidEventId1,
  116. commonName,
  117. venId,
  118. );
  119. expect(response.responseCode).to.eql('454');
  120. expect(response.responseDescription).to.eql(
  121. 'Event response references invalid event',
  122. );
  123. });
  124. });
  125. });