registration.spec.js 7.7 KB

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