registration.spec.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. 'use strict';
  2. const { expect } = require('chai');
  3. const { v4 } = require('uuid');
  4. const { sequelize } = require('../../../db');
  5. const {
  6. cancelParty,
  7. query,
  8. registerParty,
  9. } = require('../../../processes/registration');
  10. describe('VEN registration', function() {
  11. before(async () => {
  12. await sequelize.sync();
  13. });
  14. describe('registerParty', function() {
  15. let venId, commonName, registrationResponse;
  16. beforeEach(async () => {
  17. venId = v4().replace(/-/g, '').substring(0, 20).toUpperCase().match(/.{2}/g).join(':');
  18. const requestId = v4().replace(/-/g, '');
  19. commonName = v4().replace(/-/g, '').substring(0, 12);
  20. const request = {
  21. requestId: requestId,
  22. venId: venId,
  23. oadrProfileName: '2.0b',
  24. oadrTransportName: 'simplehttp',
  25. oadrReportOnly: false,
  26. oadrXmlSignature: false,
  27. oadrVenName: `VEN ${commonName}`,
  28. oadrHttpPullModel: true
  29. };
  30. registrationResponse = await registerParty(request, commonName, venId);
  31. });
  32. it('allows registration of a new VEN', async () => {
  33. expect(registrationResponse.registrationId).to.be.a('string');
  34. });
  35. it('rejects VEN with non-matching venId', async () => {
  36. const venId = v4().replace(/-/g, '').substring(0, 20).toUpperCase().match(/.{2}/g).join(':');
  37. const requestId = v4().replace(/-/g, '');
  38. const commonName = v4().replace(/-/g, '').substring(0, 12);
  39. const request = {
  40. requestId: requestId,
  41. venId: venId,
  42. oadrProfileName: '2.0b',
  43. oadrTransportName: 'simplehttp',
  44. oadrReportOnly: false,
  45. oadrXmlSignature: false,
  46. oadrVenName: `VEN ${commonName}`,
  47. oadrHttpPullModel: true
  48. };
  49. let exception;
  50. try {
  51. await registerParty(request, commonName, `${venId}:FF`);
  52. } catch (e) {
  53. exception = e;
  54. }
  55. expect(exception).is.an('error');
  56. expect(exception.message).to.eql('VenID does not match certificate');
  57. });
  58. it('rejects registration when common name changes', async () => {
  59. const requestId = v4().replace(/-/g, '');
  60. const commonName2 = v4().replace(/-/g, '').substring(0, 12);
  61. const request = {
  62. requestId: requestId,
  63. venId: venId,
  64. oadrProfileName: '2.0b',
  65. oadrTransportName: 'simplehttp',
  66. oadrReportOnly: false,
  67. oadrXmlSignature: false,
  68. oadrVenName: `VEN ${commonName}`,
  69. oadrHttpPullModel: true
  70. };
  71. let exception;
  72. try {
  73. await registerParty(request, commonName2, venId);
  74. } catch (e) {
  75. exception = e;
  76. }
  77. expect(exception).is.an('error');
  78. expect(exception.message).to.eql('Client certificate CN mismatch');
  79. });
  80. });
  81. describe('query', function() {
  82. let venId, commonName, queryResponse;
  83. beforeEach(async () => {
  84. venId = v4().replace(/-/g, '').substring(0, 20).toUpperCase().match(/.{2}/g).join(':');
  85. const requestId = v4().replace(/-/g, '');
  86. commonName = v4().replace(/-/g, '').substring(0, 12);
  87. const request = {
  88. requestId: requestId
  89. };
  90. queryResponse = await query(request, commonName, venId);
  91. });
  92. it('does not return venId or registrationId for new device', async () => {
  93. expect(queryResponse.registrationId).to.be.undefined;
  94. expect(queryResponse.venId).to.be.undefined;
  95. });
  96. it('returns registrationId if already registered', async () => {
  97. venId = v4().replace(/-/g, '').substring(0,20).toUpperCase().match(/.{2}/g).join(':');
  98. const requestId = v4().replace(/-/g, '');
  99. commonName = v4().replace(/-/g, '').substring(0, 12);
  100. const registerRequest = {
  101. requestId: requestId,
  102. venId: venId,
  103. oadrProfileName: '2.0b',
  104. oadrTransportName: 'simplehttp',
  105. oadrReportOnly: false,
  106. oadrXmlSignature: false,
  107. oadrVenName: `VEN ${commonName}`,
  108. oadrHttpPullModel: true
  109. };
  110. const registrationResponse = await registerParty(registerRequest, commonName, venId);
  111. const initialRegistrationId = registrationResponse.registrationId;
  112. const queryRequest = {
  113. requestId: requestId
  114. };
  115. queryResponse = await query(queryRequest, commonName, venId);
  116. expect(queryResponse.registrationId).to.eql(initialRegistrationId);
  117. expect(queryResponse.venId).to.eql(venId);
  118. });
  119. });
  120. describe('cancelParty', function() {
  121. let venId, commonName, registrationResponse;
  122. beforeEach(async () => {
  123. venId = v4().replace(/-/g, '').substring(0, 20).toUpperCase().match(/.{2}/g).join(':');
  124. const requestId = v4().replace(/-/g, '');
  125. commonName = v4().replace(/-/g, '').substring(0, 12);
  126. const request = {
  127. requestId: requestId,
  128. venId: venId,
  129. oadrProfileName: '2.0b',
  130. oadrTransportName: 'simplehttp',
  131. oadrReportOnly: false,
  132. oadrXmlSignature: false,
  133. oadrVenName: `VEN ${commonName}`,
  134. oadrHttpPullModel: true
  135. };
  136. registrationResponse = await registerParty(request, commonName, venId);
  137. });
  138. it('successfully cancels an existing registration', async () => {
  139. const cancelRequestId = v4().replace(/-/g, '');
  140. const cancelRequest = {
  141. requestId: cancelRequestId,
  142. registrationId: registrationResponse.registrationId,
  143. venId: venId
  144. };
  145. const cancelResponse = await cancelParty(cancelRequest, commonName, venId);
  146. expect(cancelResponse.responseCode).to.eql('200');
  147. expect(cancelResponse.responseDescription).to.eql('OK');
  148. expect(cancelResponse.responseRequestId).to.eql(cancelRequestId);
  149. expect(cancelResponse.venId).to.eql(venId);
  150. });
  151. it('fails if no registrationId is specified', async () => {
  152. const cancelRequestId = v4().replace(/-/g, '');
  153. const cancelRequest = {
  154. requestId: cancelRequestId,
  155. venId: venId
  156. };
  157. let error;
  158. try {
  159. await cancelParty(cancelRequest, commonName, venId);
  160. } catch (e) {
  161. error = e;
  162. }
  163. expect(error).to.be.an('error');
  164. expect(error.message).to.eql('No registrationID in request');
  165. });
  166. it('fails if venID does not match certificate', async () => {
  167. const otherVenId = v4().replace(/-/g, '');
  168. const cancelRequestId = v4().replace(/-/g, '');
  169. const cancelRequest = {
  170. requestId: cancelRequestId,
  171. registrationId: registrationResponse.registrationId,
  172. venId: venId
  173. };
  174. let error;
  175. try {
  176. await cancelParty(cancelRequest, commonName, otherVenId);
  177. } catch (e) {
  178. error = e;
  179. }
  180. expect(error).to.be.an('error');
  181. expect(error.message).to.eql('VenID does not match certificate');
  182. });
  183. it('fails if no current registration to cancel', async () => {
  184. const cancelRequestId = v4().replace(/-/g, '');
  185. const cancelRequest = {
  186. requestId: cancelRequestId,
  187. registrationId: registrationResponse.registrationId,
  188. venId: venId
  189. };
  190. // first cancellation
  191. await cancelParty(cancelRequest, commonName, venId);
  192. let error;
  193. try {
  194. // second cancellation
  195. await cancelParty(cancelRequest, commonName, venId);
  196. } catch (e) {
  197. error = e;
  198. }
  199. expect(error).to.be.an('error');
  200. expect(error.message).to.eql('No current registration for VenID');
  201. });
  202. });
  203. });