registration.spec.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. 'use strict';
  2. const { expect } = require('chai');
  3. const { v4 } = require('uuid');
  4. const rewire = require('rewire');
  5. const FakeNantumModule = require('../utils/fake-nantum-module');
  6. describe('VEN registration', function() {
  7. let rewired;
  8. before(async () => {
  9. rewired = rewire('../../../processes/registration.js');
  10. rewired.__set__({
  11. nantum: new FakeNantumModule(),
  12. });
  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 rewired.registerParty(
  38. request,
  39. commonName,
  40. venId,
  41. );
  42. });
  43. it('allows registration of a new VEN', async () => {
  44. expect(registrationResponse.registrationId).to.be.a('string');
  45. });
  46. it('rejects VEN with non-matching venId', async () => {
  47. const venId = v4()
  48. .replace(/-/g, '')
  49. .substring(0, 20)
  50. .toUpperCase()
  51. .match(/.{2}/g)
  52. .join(':');
  53. const requestId = v4().replace(/-/g, '');
  54. const commonName = v4()
  55. .replace(/-/g, '')
  56. .substring(0, 12);
  57. const request = {
  58. requestId: requestId,
  59. venId: venId,
  60. oadrProfileName: '2.0b',
  61. oadrTransportName: 'simpleHttp',
  62. oadrReportOnly: false,
  63. oadrXmlSignature: false,
  64. oadrVenName: `VEN ${commonName}`,
  65. oadrHttpPullModel: true,
  66. };
  67. let exception;
  68. try {
  69. await rewired.registerParty(request, commonName, `${venId}:FF`);
  70. } catch (e) {
  71. exception = e;
  72. }
  73. expect(exception).is.an('error');
  74. expect(exception.message).to.eql('VenID does not match certificate');
  75. });
  76. it('rejects registration when common name changes', async () => {
  77. const requestId = v4().replace(/-/g, '');
  78. const commonName2 = v4()
  79. .replace(/-/g, '')
  80. .substring(0, 12);
  81. const request = {
  82. requestId: requestId,
  83. venId: venId,
  84. oadrProfileName: '2.0b',
  85. oadrTransportName: 'simpleHttp',
  86. oadrReportOnly: false,
  87. oadrXmlSignature: false,
  88. oadrVenName: `VEN ${commonName}`,
  89. oadrHttpPullModel: true,
  90. };
  91. let exception;
  92. try {
  93. await rewired.registerParty(request, commonName2, venId);
  94. } catch (e) {
  95. exception = e;
  96. }
  97. expect(exception).is.an('error');
  98. expect(exception.message).to.eql('Client certificate CN mismatch');
  99. });
  100. });
  101. describe('query', function() {
  102. let venId, commonName, queryResponse;
  103. beforeEach(async () => {
  104. venId = v4()
  105. .replace(/-/g, '')
  106. .substring(0, 20)
  107. .toUpperCase()
  108. .match(/.{2}/g)
  109. .join(':');
  110. const requestId = v4().replace(/-/g, '');
  111. commonName = v4()
  112. .replace(/-/g, '')
  113. .substring(0, 12);
  114. const request = {
  115. requestId: requestId,
  116. };
  117. queryResponse = await rewired.query(request, commonName, venId);
  118. });
  119. it('does not return venId or registrationId for new device', async () => {
  120. expect(queryResponse.registrationId).to.be.undefined;
  121. expect(queryResponse.venId).to.be.undefined;
  122. });
  123. it('returns registrationId if already registered', async () => {
  124. venId = v4()
  125. .replace(/-/g, '')
  126. .substring(0, 20)
  127. .toUpperCase()
  128. .match(/.{2}/g)
  129. .join(':');
  130. const requestId = v4().replace(/-/g, '');
  131. commonName = v4()
  132. .replace(/-/g, '')
  133. .substring(0, 12);
  134. const registerRequest = {
  135. requestId: requestId,
  136. venId: venId,
  137. oadrProfileName: '2.0b',
  138. oadrTransportName: 'simpleHttp',
  139. oadrReportOnly: false,
  140. oadrXmlSignature: false,
  141. oadrVenName: `VEN ${commonName}`,
  142. oadrHttpPullModel: true,
  143. };
  144. const registrationResponse = await rewired.registerParty(
  145. registerRequest,
  146. commonName,
  147. venId,
  148. );
  149. const initialRegistrationId = registrationResponse.registrationId;
  150. const queryRequest = {
  151. requestId: requestId,
  152. };
  153. queryResponse = await rewired.query(queryRequest, commonName, venId);
  154. expect(queryResponse.registrationId).to.eql(initialRegistrationId);
  155. expect(queryResponse.venId).to.eql(venId);
  156. });
  157. it('rejects XMPP client', async () => {
  158. venId = v4()
  159. .replace(/-/g, '')
  160. .substring(0, 20)
  161. .toUpperCase()
  162. .match(/.{2}/g)
  163. .join(':');
  164. const requestId = v4().replace(/-/g, '');
  165. commonName = v4()
  166. .replace(/-/g, '')
  167. .substring(0, 12);
  168. const registerRequest = {
  169. requestId: requestId,
  170. venId: venId,
  171. oadrProfileName: '2.0b',
  172. oadrTransportName: 'xmpp',
  173. oadrReportOnly: false,
  174. oadrXmlSignature: false,
  175. oadrVenName: `VEN ${commonName}`,
  176. oadrHttpPullModel: true,
  177. };
  178. let lastError;
  179. try {
  180. await rewired.registerParty(registerRequest, commonName, venId);
  181. } catch (e) {
  182. lastError = e;
  183. }
  184. expect(lastError).to.be.an('error');
  185. expect(lastError.message).to.eql('Transport name must be simpleHttp');
  186. });
  187. it('rejects 2.0a client', async () => {
  188. venId = v4()
  189. .replace(/-/g, '')
  190. .substring(0, 20)
  191. .toUpperCase()
  192. .match(/.{2}/g)
  193. .join(':');
  194. const requestId = v4().replace(/-/g, '');
  195. commonName = v4()
  196. .replace(/-/g, '')
  197. .substring(0, 12);
  198. const registerRequest = {
  199. requestId: requestId,
  200. venId: venId,
  201. oadrProfileName: '2.0a',
  202. oadrTransportName: 'simpleHttp',
  203. oadrReportOnly: false,
  204. oadrXmlSignature: false,
  205. oadrVenName: `VEN ${commonName}`,
  206. oadrHttpPullModel: true,
  207. };
  208. let lastError;
  209. try {
  210. await rewired.registerParty(registerRequest, commonName, venId);
  211. } catch (e) {
  212. lastError = e;
  213. }
  214. expect(lastError).to.be.an('error');
  215. expect(lastError.message).to.eql('Profile name must be 2.0b');
  216. });
  217. });
  218. describe('cancelParty', function() {
  219. let venId, commonName, registrationResponse;
  220. beforeEach(async () => {
  221. venId = v4()
  222. .replace(/-/g, '')
  223. .substring(0, 20)
  224. .toUpperCase()
  225. .match(/.{2}/g)
  226. .join(':');
  227. const requestId = v4().replace(/-/g, '');
  228. commonName = v4()
  229. .replace(/-/g, '')
  230. .substring(0, 12);
  231. const request = {
  232. requestId: requestId,
  233. venId: venId,
  234. oadrProfileName: '2.0b',
  235. oadrTransportName: 'simpleHttp',
  236. oadrReportOnly: false,
  237. oadrXmlSignature: false,
  238. oadrVenName: `VEN ${commonName}`,
  239. oadrHttpPullModel: true,
  240. };
  241. registrationResponse = await rewired.registerParty(
  242. request,
  243. commonName,
  244. venId,
  245. );
  246. });
  247. it('successfully cancels an existing registration', async () => {
  248. const cancelRequestId = v4().replace(/-/g, '');
  249. const cancelRequest = {
  250. requestId: cancelRequestId,
  251. registrationId: registrationResponse.registrationId,
  252. venId: venId,
  253. };
  254. const cancelResponse = await rewired.cancelParty(
  255. cancelRequest,
  256. commonName,
  257. venId,
  258. );
  259. expect(cancelResponse.responseCode).to.eql('200');
  260. expect(cancelResponse.responseDescription).to.eql('OK');
  261. expect(cancelResponse.responseRequestId).to.eql(cancelRequestId);
  262. expect(cancelResponse.venId).to.eql(venId);
  263. });
  264. it('fails if venID does not match certificate', async () => {
  265. const otherVenId = v4().replace(/-/g, '');
  266. const cancelRequestId = v4().replace(/-/g, '');
  267. const cancelRequest = {
  268. requestId: cancelRequestId,
  269. registrationId: registrationResponse.registrationId,
  270. venId: venId,
  271. };
  272. let error;
  273. try {
  274. await rewired.cancelParty(cancelRequest, commonName, otherVenId);
  275. } catch (e) {
  276. error = e;
  277. }
  278. expect(error).to.be.an('error');
  279. expect(error.message).to.eql('VenID does not match certificate');
  280. });
  281. });
  282. });