end-to-end.spec.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. 'use strict';
  2. const { expect } = require('chai');
  3. const sinon = require('sinon');
  4. const { pki } = require('node-forge');
  5. const {
  6. calculatePartialFingerprintOfEscapedPemCertificate,
  7. } = require('../../modules/certificate');
  8. const { Ven } = require('../../client/ven');
  9. const app = require('../../server');
  10. const { port } = require('../../config');
  11. function getVenClient() {
  12. const commonName = 'aabbccddeeff';
  13. const keypair = pki.rsa.generateKeyPair({ bits: 1024, e: 0x10001 });
  14. const cert = pki.createCertificate();
  15. cert.publicKey = keypair.publicKey;
  16. cert.serialNumber = '01';
  17. cert.validity.notBefore = new Date('2000-01-01T00:00:00.000Z');
  18. cert.validity.notAfter = new Date('2100-01-01T00:00:00.000Z');
  19. const attrs = [
  20. {
  21. name: 'commonName',
  22. value: commonName,
  23. },
  24. {
  25. name: 'countryName',
  26. value: 'US',
  27. },
  28. {
  29. shortName: 'ST',
  30. value: 'New York',
  31. },
  32. {
  33. name: 'localityName',
  34. value: 'New York',
  35. },
  36. {
  37. name: 'organizationName',
  38. value: 'Test',
  39. },
  40. {
  41. shortName: 'OU',
  42. value: 'Test',
  43. },
  44. ];
  45. cert.setSubject(attrs);
  46. cert.setIssuer(attrs);
  47. cert.sign(keypair.privateKey);
  48. const clientCrtPem = pki.certificateToPem(cert);
  49. const fingerprint = calculatePartialFingerprintOfEscapedPemCertificate(
  50. clientCrtPem,
  51. );
  52. return new Ven(
  53. `http://127.0.0.1:${port}`,
  54. clientCrtPem,
  55. commonName,
  56. fingerprint,
  57. 'ven.js1',
  58. );
  59. }
  60. describe('VEN to VTN interactions', function() {
  61. describe('registration and event retrieval', async function() {
  62. let clock;
  63. after(async () => {
  64. clock.restore();
  65. });
  66. let ven;
  67. before(async () => {
  68. clock = sinon.useFakeTimers(
  69. new Date('2020-04-26T01:00:00.000Z').getTime(),
  70. );
  71. await app.start();
  72. ven = getVenClient();
  73. });
  74. it('should successfully return a vtnId from queryRegistration', async () => {
  75. const queryResponse = await ven.queryRegistration();
  76. expect(queryResponse.vtnId).to.be.a('string');
  77. });
  78. it('should successfully register and receive a vtnId and registrationId', async () => {
  79. const registrationResponse = await ven.register();
  80. expect(registrationResponse.vtnId).to.be.a('string');
  81. expect(registrationResponse.registrationId).to.be.a('string');
  82. });
  83. it('should successfully return a registrationId and venId from queryRegistration', async () => {
  84. const queryResponse = await ven.queryRegistration();
  85. expect(queryResponse.vtnId).to.be.a('string');
  86. expect(queryResponse.registrationId).to.be.a('string');
  87. expect(queryResponse.venId).to.be.a('string');
  88. });
  89. it('should return an event from requestEvents', async () => {
  90. const eventResponse = await ven.requestEvents();
  91. expect(eventResponse.events.length).to.eql(1);
  92. expect(eventResponse.vtnId).to.be.a('string');
  93. });
  94. it('should successfully cancel that registration', async () => {
  95. const cancelResponse = await ven.cancelRegistration();
  96. expect(cancelResponse.registrationId).to.be.a('string');
  97. expect(cancelResponse.venId).to.be.a('string');
  98. });
  99. after(async () => {
  100. await app.stop();
  101. });
  102. }).timeout(5000);
  103. describe('poll', async function() {
  104. let ven;
  105. let clock;
  106. after(async () => {
  107. clock.restore();
  108. });
  109. before(async () => {
  110. clock = sinon.useFakeTimers(
  111. new Date('2020-04-26T01:00:00.000Z').getTime(),
  112. );
  113. await app.start();
  114. ven = getVenClient();
  115. await ven.register();
  116. });
  117. it('should successfully poll for events', async () => {
  118. const pollResponse = await ven.poll();
  119. expect(pollResponse._type).to.eql('oadrDistributeEvent');
  120. expect(pollResponse.events.length).to.eql(1);
  121. });
  122. it('should not return the same event twice', async () => {
  123. const pollResponse = await ven.poll();
  124. expect(pollResponse._type).to.eql('oadrResponse');
  125. });
  126. after(async () => {
  127. await app.stop();
  128. });
  129. }).timeout(5000);
  130. describe('report', async function() {
  131. let ven;
  132. let clock;
  133. after(async () => {
  134. clock.restore();
  135. });
  136. before(async () => {
  137. clock = sinon.useFakeTimers(
  138. new Date('2020-04-26T01:00:00.000Z').getTime(),
  139. );
  140. await app.start();
  141. ven = getVenClient();
  142. await ven.register();
  143. await ven.poll(); // poll for any events that may be waiting
  144. });
  145. it('should successfully subscribe to reports and receive data', async () => {
  146. const reqs = [
  147. {
  148. reportRequestId: '31c5ce71a68a73ece370',
  149. reportSpecifierId: 'TELEMETRY_STATUS',
  150. createdDateTime: '2020-05-07T10:05:41.421-06:00',
  151. duration: 'PT1H',
  152. reportName: 'METADATA_TELEMETRY_STATUS',
  153. descriptions: [
  154. {
  155. reportId: 'TelemetryStatusReport',
  156. reportType: 'x-resourceStatus',
  157. readingType: 'x-notApplicable',
  158. samplingRate: {
  159. minPeriod: 'PT1M',
  160. maxPeriod: 'PT1H',
  161. onChange: false,
  162. },
  163. },
  164. ],
  165. },
  166. {
  167. reportRequestId: '3d92d98e0b65d94e60a7',
  168. reportSpecifierId: 'TELEMETRY_USAGE',
  169. createdDateTime: '2020-05-07T10:05:41.421-06:00',
  170. duration: 'PT1H',
  171. reportName: 'METADATA_TELEMETRY_USAGE',
  172. descriptions: [
  173. {
  174. reportId: 'TelemetryUsageReport',
  175. reportType: 'usage',
  176. readingType: 'Direct Read',
  177. samplingRate: {
  178. minPeriod: 'PT1M',
  179. maxPeriod: 'PT1H',
  180. onChange: false,
  181. },
  182. },
  183. ],
  184. },
  185. ];
  186. // register reports
  187. await ven.registerReports(reqs);
  188. const pollResponse = await ven.poll();
  189. // poll has a request to create reports
  190. expect(pollResponse._type).to.eql('oadrCreateReport');
  191. const createRequestId = pollResponse.reportRequestId;
  192. const [
  193. telemetryStatusReportRequestId,
  194. telemetryUsageReportRequestId,
  195. ] = pollResponse.requests.map(x => x.reportRequestId);
  196. // notify vtn that reports have been created
  197. await ven.notifyCreatedReports(createRequestId, [
  198. telemetryStatusReportRequestId,
  199. telemetryUsageReportRequestId,
  200. ]);
  201. // poll is empty, no reports to create
  202. const pollResponse2 = await ven.poll();
  203. expect(pollResponse2._type).to.eql('oadrResponse');
  204. // send report
  205. const reports = [
  206. {
  207. createdDateTime: '2020-05-08T21:27:49.591-06:00',
  208. duration: 'PT1M',
  209. intervals: [
  210. {
  211. duration: 'PT1M',
  212. reportPayloads: [
  213. {
  214. dataQuality: 'Quality Good - Non Specific',
  215. payloadFloat: 161.97970171999845,
  216. reportId: 'TelemetryUsageReport',
  217. },
  218. ],
  219. startDate: '2020-05-08T21:26:49.562-06:00',
  220. },
  221. ],
  222. reportName: 'TELEMETRY_USAGE',
  223. reportRequestId: telemetryUsageReportRequestId,
  224. reportSpecifierId: 'TELEMETRY_USAGE',
  225. startDate: '2020-05-08T21:26:49.562-06:00',
  226. },
  227. {
  228. createdDateTime: '2020-05-13T10:56:11.058-06:00',
  229. duration: 'PT1M',
  230. intervals: [
  231. {
  232. duration: 'PT1M',
  233. reportPayloads: [
  234. {
  235. dataQuality: 'Quality Good - Non Specific',
  236. payloadStatus: {
  237. online: true,
  238. manualOverride: false,
  239. loadControlState: {
  240. oadrLevelOffset: {
  241. oadrNormal: 40,
  242. oadrCurrent: 50,
  243. },
  244. },
  245. },
  246. reportId: 'TelemetryStatusReport',
  247. },
  248. ],
  249. startDate: '2020-05-13T10:56:11.058-06:00',
  250. },
  251. ],
  252. reportName: 'TELEMETRY_STATUS',
  253. reportRequestId: telemetryStatusReportRequestId,
  254. reportSpecifierId: 'TELEMETRY_STATUS',
  255. startDate: '2020-05-13T10:56:11.058-06:00',
  256. },
  257. ];
  258. await ven.sendReportData(reports);
  259. }).timeout(10000);
  260. after(async () => {
  261. await app.stop();
  262. });
  263. }).timeout(5000);
  264. describe('optIn', async function() {
  265. let clock;
  266. after(async () => {
  267. clock.restore();
  268. });
  269. let ven, events, pollResponse;
  270. before(async () => {
  271. clock = sinon.useFakeTimers(
  272. new Date('2020-04-26T01:00:00.000Z').getTime(),
  273. );
  274. await app.start();
  275. ven = getVenClient();
  276. await ven.register();
  277. pollResponse = await ven.poll();
  278. events = pollResponse.events;
  279. });
  280. it('should successfully opt in', async () => {
  281. const eventId = events[0].eventDescriptor.eventId;
  282. const modificationNumber = events[0].eventDescriptor.modificationNumber;
  283. await ven.opt('optIn', eventId, modificationNumber);
  284. });
  285. it('should be able to opt out after opting in', async () => {
  286. const eventId = events[0].eventDescriptor.eventId;
  287. const modificationNumber = events[0].eventDescriptor.modificationNumber;
  288. await ven.opt('optOut', eventId, modificationNumber);
  289. });
  290. after(async () => {
  291. await app.stop();
  292. });
  293. }).timeout(5000);
  294. });