end-to-end.spec.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. 'use strict';
  2. const { readFileSync } = require('fs');
  3. const path = require('path');
  4. const { expect } = require('chai');
  5. const sinon = require('sinon');
  6. const { Ven } = require('../../client/ven');
  7. const app = require('../../server');
  8. const { sequelize, Ven: VenDb } = require('../../db');
  9. const { port } = require('../../config');
  10. describe('VEN to VTN interactions', function() {
  11. describe('registration and event retrieval', async function() {
  12. let clock;
  13. after(async () => {
  14. clock.restore();
  15. });
  16. let ven;
  17. before(async () => {
  18. clock = sinon.useFakeTimers(
  19. new Date('2020-04-26T01:00:00.000Z').getTime(),
  20. );
  21. await sequelize.sync();
  22. await VenDb.destroy({ truncate: true });
  23. await app.start();
  24. const clientCrtPem = readFileSync(
  25. path.join(__dirname, 'integration-client.crt'),
  26. 'utf-8',
  27. );
  28. ven = new Ven(
  29. `http://127.0.0.1:${port}`,
  30. clientCrtPem,
  31. 'aabbccddeeff',
  32. '17:32:59:FD:0E:B5:99:31:27:9C',
  33. 'ven.js1',
  34. );
  35. });
  36. it('should successfully return a vtnId from queryRegistration', async () => {
  37. const queryResponse = await ven.queryRegistration();
  38. expect(queryResponse.vtnId).to.be.a('string');
  39. });
  40. it('should successfully register and receive a vtnId and registrationId', async () => {
  41. const registrationResponse = await ven.register();
  42. expect(registrationResponse.vtnId).to.be.a('string');
  43. expect(registrationResponse.registrationId).to.be.a('string');
  44. });
  45. it('should successfully return a registrationId and venId from queryRegistration', async () => {
  46. const queryResponse = await ven.queryRegistration();
  47. expect(queryResponse.vtnId).to.be.a('string');
  48. expect(queryResponse.registrationId).to.be.a('string');
  49. expect(queryResponse.venId).to.be.a('string');
  50. });
  51. it('should return an event from requestEvents', async () => {
  52. const eventResponse = await ven.requestEvents();
  53. expect(eventResponse.events.length).to.eql(1);
  54. expect(eventResponse.vtnId).to.be.a('string');
  55. });
  56. it('should successfully cancel that registration', async () => {
  57. const cancelResponse = await ven.cancelRegistration();
  58. expect(cancelResponse.registrationId).to.be.a('string');
  59. expect(cancelResponse.venId).to.be.a('string');
  60. });
  61. after(async () => {
  62. await app.stop();
  63. });
  64. });
  65. describe('poll', async function() {
  66. let ven;
  67. let clock;
  68. after(async () => {
  69. clock.restore();
  70. });
  71. before(async () => {
  72. clock = sinon.useFakeTimers(
  73. new Date('2020-04-26T01:00:00.000Z').getTime(),
  74. );
  75. await sequelize.sync();
  76. await VenDb.destroy({ truncate: true });
  77. await app.start();
  78. const clientCrtPem = readFileSync(
  79. path.join(__dirname, 'integration-client.crt'),
  80. 'utf-8',
  81. );
  82. ven = new Ven(
  83. `http://127.0.0.1:${port}`,
  84. clientCrtPem,
  85. 'aabbccddeeff',
  86. '17:32:59:FD:0E:B5:99:31:27:9C',
  87. 'ven.js1',
  88. );
  89. await ven.register();
  90. });
  91. it('should successfully poll for events', async () => {
  92. const pollResponse = await ven.poll();
  93. expect(pollResponse._type).to.eql('oadrDistributeEvent');
  94. expect(pollResponse.events.length).to.eql(1);
  95. });
  96. after(async () => {
  97. await app.stop();
  98. });
  99. });
  100. describe('optIn', async function() {
  101. let clock;
  102. after(async () => {
  103. clock.restore();
  104. });
  105. let ven, events, pollResponse;
  106. before(async () => {
  107. clock = sinon.useFakeTimers(
  108. new Date('2020-04-26T01:00:00.000Z').getTime(),
  109. );
  110. await sequelize.sync();
  111. await VenDb.destroy({ truncate: true });
  112. await app.start();
  113. const clientCrtPem = readFileSync(
  114. path.join(__dirname, 'integration-client.crt'),
  115. 'utf-8',
  116. );
  117. ven = new Ven(
  118. `http://127.0.0.1:${port}`,
  119. clientCrtPem,
  120. 'aabbccddeeff',
  121. '17:32:59:FD:0E:B5:99:31:27:9C',
  122. 'ven.js1',
  123. );
  124. await ven.register();
  125. pollResponse = await ven.poll();
  126. events = pollResponse.events;
  127. });
  128. it('should successfully poll for events', async () => {
  129. expect(pollResponse._type).to.eql('oadrDistributeEvent');
  130. expect(pollResponse.events.length).to.eql(1);
  131. });
  132. it('should return same events if not opted', async () => {
  133. const pollResponse = await ven.poll();
  134. expect(pollResponse._type).to.eql('oadrDistributeEvent');
  135. expect(pollResponse.events.length).to.eql(1);
  136. });
  137. it('should return no events if opted', async () => {
  138. const eventId = events[0].eventDescriptor.eventId;
  139. const modificationNumber = events[0].eventDescriptor.modificationNumber;
  140. await ven.opt('optIn', eventId, modificationNumber);
  141. const pollResponse = await ven.poll();
  142. expect(pollResponse._type).to.eql('oadrResponse');
  143. });
  144. after(async () => {
  145. await app.stop();
  146. });
  147. });
  148. });