end-to-end.spec.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 registration', function() {
  11. describe('successful 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(new Date('2020-04-26T01:00:00.000Z').getTime());
  19. await sequelize.sync();
  20. await VenDb.destroy({ truncate : true});
  21. await app.start();
  22. const clientCrtPem = readFileSync(path.join(__dirname, 'integration-client.crt'), 'utf-8');
  23. ven = new Ven(`http://127.0.0.1:${port}`, clientCrtPem, 'aabbccddeeff', '17:32:59:FD:0E:B5:99:31:27:9C', 'ven.js1');
  24. });
  25. it('should successfully return a vtnId from queryRegistration', async () => {
  26. const queryResponse = await ven.queryRegistration();
  27. expect(queryResponse.vtnId).to.be.a('string');
  28. });
  29. it('should successfully register and receive a vtnId and registrationId', async () => {
  30. const registrationResponse = await ven.register();
  31. expect(registrationResponse.vtnId).to.be.a('string');
  32. expect(registrationResponse.registrationId).to.be.a('string');
  33. });
  34. it('should successfully return a registrationId and venId from queryRegistration', async () => {
  35. const queryResponse = await ven.queryRegistration();
  36. expect(queryResponse.vtnId).to.be.a('string');
  37. expect(queryResponse.registrationId).to.be.a('string');
  38. expect(queryResponse.venId).to.be.a('string');
  39. });
  40. it('should return an event from requestEvents', async () => {
  41. const eventResponse = await ven.requestEvents();
  42. expect(eventResponse.events.length).to.eql(1);
  43. expect(eventResponse.vtnId).to.be.a('string');
  44. });
  45. it('should successfully cancel that registration', async () => {
  46. const cancelResponse = await ven.cancelRegistration();
  47. expect(cancelResponse.registrationId).to.be.a('string');
  48. expect(cancelResponse.venId).to.be.a('string');
  49. });
  50. after(async () => {
  51. await app.stop();
  52. });
  53. });
  54. describe('successful poll', async function() {
  55. let ven;
  56. let clock;
  57. after(async () => {
  58. clock.restore();
  59. });
  60. before(async () => {
  61. clock = sinon.useFakeTimers(new Date('2020-04-26T01:00:00.000Z').getTime());
  62. await sequelize.sync();
  63. await VenDb.destroy({ truncate : true});
  64. await app.start();
  65. const clientCrtPem = readFileSync(path.join(__dirname, 'integration-client.crt'), 'utf-8');
  66. ven = new Ven(`http://127.0.0.1:${port}`, clientCrtPem, 'aabbccddeeff', '17:32:59:FD:0E:B5:99:31:27:9C', 'ven.js1');
  67. await ven.register();
  68. });
  69. it('should successfully poll for events', async () => {
  70. const pollResponse = await ven.poll();
  71. expect(pollResponse._type).to.eql('oadrDistributeEvent');
  72. expect(pollResponse.events.length).to.eql(1);
  73. });
  74. after(async () => {
  75. await app.stop();
  76. });
  77. });
  78. describe('successful optIn', async function() {
  79. let clock;
  80. after(async () => {
  81. clock.restore();
  82. });
  83. let ven, events, pollResponse;
  84. before(async () => {
  85. clock = sinon.useFakeTimers(new Date('2020-04-26T01:00:00.000Z').getTime());
  86. await sequelize.sync();
  87. await VenDb.destroy({ truncate : true});
  88. await app.start();
  89. const clientCrtPem = readFileSync(path.join(__dirname, 'integration-client.crt'), 'utf-8');
  90. ven = new Ven(`http://127.0.0.1:${port}`, clientCrtPem, 'aabbccddeeff', '17:32:59:FD:0E:B5:99:31:27:9C', 'ven.js1');
  91. await ven.register();
  92. pollResponse = await ven.poll();
  93. events = pollResponse.events;
  94. });
  95. it('should successfully poll for events', async () => {
  96. expect(pollResponse._type).to.eql('oadrDistributeEvent');
  97. expect(pollResponse.events.length).to.eql(1);
  98. });
  99. it('should return same events if not opted', async () => {
  100. const pollResponse = await ven.poll();
  101. expect(pollResponse._type).to.eql('oadrDistributeEvent');
  102. expect(pollResponse.events.length).to.eql(1);
  103. });
  104. it('should return no events if opted', async () => {
  105. const eventId = events[0].eventDescriptor.eventId;
  106. const modificationNumber = events[0].eventDescriptor.modificationNumber;
  107. await ven.opt('optIn', eventId, modificationNumber);
  108. const pollResponse = await ven.poll();
  109. expect(pollResponse._type).to.eql('oadrResponse');
  110. });
  111. after(async () => {
  112. await app.stop();
  113. });
  114. });
  115. });