DomainLockNodeTemplate.spec.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. import 'reflect-metadata';
  2. import format from 'string-template';
  3. import { assert } from 'chai';
  4. import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers';
  5. import { ICryptUtils } from '../../../../src/interfaces/utils/ICryptUtils';
  6. import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade';
  7. import { DomainLockNodeTemplate } from '../../../../src/templates/domain-lock-nodes/domain-lock-node/DomainLockNodeTemplate';
  8. import { GlobalVariableTemplate1 } from '../../../../src/templates/GlobalVariableTemplate1';
  9. import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
  10. /**
  11. * @param {string} currentDomain
  12. * @returns {string}
  13. */
  14. function getDocumentDomainTemplate (currentDomain: string): string {
  15. return `
  16. document = {
  17. domain: '${currentDomain}'
  18. };
  19. `
  20. }
  21. /**
  22. * @param {string} currentDomain
  23. * @returns {string}
  24. */
  25. function getDocumentLocationTemplate (currentDomain: string): string {
  26. return `
  27. document = {
  28. location: {
  29. hostname: '${currentDomain}'
  30. }
  31. };
  32. `
  33. }
  34. /**
  35. * @param {string} currentDomain
  36. * @returns {string}
  37. */
  38. function getDocumentDomainAndLocationTemplate (currentDomain: string): string {
  39. return `
  40. document = {
  41. domain: '${currentDomain}',
  42. location: {
  43. hostname: '${currentDomain}'
  44. }
  45. };
  46. `
  47. }
  48. /**
  49. * @param templateData
  50. * @param {string} callsControllerFunctionName
  51. * @param {string} documentTemplate
  52. * @returns {Function}
  53. */
  54. function getFunctionFromTemplate (
  55. templateData: any,
  56. callsControllerFunctionName: string,
  57. documentTemplate: string
  58. ): Function {
  59. const domainLockTemplate: string = format(DomainLockNodeTemplate(), templateData);
  60. return Function(`
  61. ${documentTemplate}
  62. var ${callsControllerFunctionName} = (function(){
  63. return function (context, fn){
  64. return function () {
  65. return fn.apply(context, arguments);
  66. };
  67. }
  68. })();
  69. ${domainLockTemplate}
  70. `)();
  71. }
  72. describe('DomainLockNodeTemplate', () => {
  73. const singleNodeCallControllerFunctionName: string = 'callsController';
  74. let cryptUtils: ICryptUtils;
  75. before(() => {
  76. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  77. inversifyContainerFacade.load('', '', {});
  78. cryptUtils = inversifyContainerFacade.get<ICryptUtils>(ServiceIdentifiers.ICryptUtils);
  79. });
  80. describe('Variant #1: current domain matches with `domainsString`', () => {
  81. const domainsString: string = ['www.example.com'].join(';');
  82. const currentDomain: string = 'www.example.com';
  83. let testFunc: () => void;
  84. before(() => {
  85. const [
  86. hiddenDomainsString,
  87. diff
  88. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  89. testFunc = () => getFunctionFromTemplate(
  90. {
  91. domainLockFunctionName: 'domainLockFunction',
  92. diff: diff,
  93. domains: hiddenDomainsString,
  94. globalVariableTemplate: GlobalVariableTemplate1(),
  95. singleNodeCallControllerFunctionName
  96. },
  97. singleNodeCallControllerFunctionName,
  98. getDocumentDomainTemplate(currentDomain)
  99. );
  100. });
  101. it('should correctly run code inside template', () => {
  102. assert.doesNotThrow(testFunc);
  103. });
  104. });
  105. describe('Variant #2: current domain matches with base domain of `domainsString`', () => {
  106. const domainsString: string = ['.example.com'].join(';');
  107. const currentDomain: string = 'www.example.com';
  108. let testFunc: () => void;
  109. before(() => {
  110. const [
  111. hiddenDomainsString,
  112. diff
  113. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  114. testFunc = () => getFunctionFromTemplate(
  115. {
  116. domainLockFunctionName: 'domainLockFunction',
  117. diff: diff,
  118. domains: hiddenDomainsString,
  119. globalVariableTemplate: GlobalVariableTemplate1(),
  120. singleNodeCallControllerFunctionName
  121. },
  122. singleNodeCallControllerFunctionName,
  123. getDocumentDomainTemplate(currentDomain)
  124. );
  125. });
  126. it('should correctly run code inside template', () => {
  127. assert.doesNotThrow(testFunc);
  128. });
  129. });
  130. describe('Variant #3: current domain matches with all domains of `domainsString`', () => {
  131. describe('Variant #1', () => {
  132. const domainsString: string = ['example.com', '.example.com'].join(';');
  133. const currentDomain: string = 'example.com';
  134. let testFunc: () => void;
  135. before(() => {
  136. const [
  137. hiddenDomainsString,
  138. diff
  139. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  140. testFunc = () => getFunctionFromTemplate(
  141. {
  142. domainLockFunctionName: 'domainLockFunction',
  143. diff: diff,
  144. domains: hiddenDomainsString,
  145. globalVariableTemplate: GlobalVariableTemplate1(),
  146. singleNodeCallControllerFunctionName
  147. },
  148. singleNodeCallControllerFunctionName,
  149. getDocumentDomainTemplate(currentDomain)
  150. );
  151. });
  152. it('should correctly run code inside template', () => {
  153. assert.doesNotThrow(testFunc);
  154. });
  155. });
  156. describe('Variant #2', () => {
  157. const domainsString: string = ['example.com', '.example.com'].join(';');
  158. const currentDomain: string = 'subdomain.example.com';
  159. let testFunc: () => void;
  160. before(() => {
  161. const [
  162. hiddenDomainsString,
  163. diff
  164. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  165. testFunc = () => getFunctionFromTemplate(
  166. {
  167. domainLockFunctionName: 'domainLockFunction',
  168. diff: diff,
  169. domains: hiddenDomainsString,
  170. globalVariableTemplate: GlobalVariableTemplate1(),
  171. singleNodeCallControllerFunctionName
  172. },
  173. singleNodeCallControllerFunctionName,
  174. getDocumentDomainTemplate(currentDomain)
  175. );
  176. });
  177. it('should correctly run code inside template', () => {
  178. assert.doesNotThrow(testFunc);
  179. });
  180. });
  181. describe('Variant #3', () => {
  182. const domainsString: string = ['.example.com', 'example.com'].join(';');
  183. const currentDomain: string = 'subdomain.example.com';
  184. let testFunc: () => void;
  185. before(() => {
  186. const [
  187. hiddenDomainsString,
  188. diff
  189. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  190. testFunc = () => getFunctionFromTemplate(
  191. {
  192. domainLockFunctionName: 'domainLockFunction',
  193. diff: diff,
  194. domains: hiddenDomainsString,
  195. globalVariableTemplate: GlobalVariableTemplate1(),
  196. singleNodeCallControllerFunctionName
  197. },
  198. singleNodeCallControllerFunctionName,
  199. getDocumentDomainTemplate(currentDomain)
  200. );
  201. });
  202. it('should correctly run code inside template', () => {
  203. assert.doesNotThrow(testFunc);
  204. });
  205. });
  206. describe('Variant #4', () => {
  207. const domainsString: string = ['sub1.example.com', 'sub2.example.com'].join(';');
  208. const currentDomain: string = 'sub1.example.com';
  209. let testFunc: () => void;
  210. before(() => {
  211. const [
  212. hiddenDomainsString,
  213. diff
  214. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  215. testFunc = () => getFunctionFromTemplate(
  216. {
  217. domainLockFunctionName: 'domainLockFunction',
  218. diff: diff,
  219. domains: hiddenDomainsString,
  220. globalVariableTemplate: GlobalVariableTemplate1(),
  221. singleNodeCallControllerFunctionName
  222. },
  223. singleNodeCallControllerFunctionName,
  224. getDocumentDomainTemplate(currentDomain)
  225. );
  226. });
  227. it('should correctly run code inside template', () => {
  228. assert.doesNotThrow(testFunc);
  229. });
  230. });
  231. });
  232. describe('Variant #4: current domain matches with base domain of `domainsString` item', () => {
  233. const domainsString: string = ['www.test.com', '.example.com'].join(';');
  234. const currentDomain: string = 'subdomain.example.com';
  235. let testFunc: () => void;
  236. before(() => {
  237. const [
  238. hiddenDomainsString,
  239. diff
  240. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  241. testFunc = () => getFunctionFromTemplate(
  242. {
  243. domainLockFunctionName: 'domainLockFunction',
  244. diff: diff,
  245. domains: hiddenDomainsString,
  246. globalVariableTemplate: GlobalVariableTemplate1(),
  247. singleNodeCallControllerFunctionName
  248. },
  249. singleNodeCallControllerFunctionName,
  250. getDocumentDomainTemplate(currentDomain)
  251. );
  252. });
  253. it('should correctly run code inside template', () => {
  254. assert.doesNotThrow(testFunc);
  255. });
  256. });
  257. describe('Variant #5: current domain doesn\'t match with `domainsString`', () => {
  258. describe('Variant #1', () => {
  259. const domainsString: string = ['www.example.com'].join(';');
  260. const currentDomain: string = 'www.test.com';
  261. let testFunc: () => void;
  262. before(() => {
  263. const [
  264. hiddenDomainsString,
  265. diff
  266. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  267. testFunc = () => getFunctionFromTemplate(
  268. {
  269. domainLockFunctionName: 'domainLockFunction',
  270. diff: diff,
  271. domains: hiddenDomainsString,
  272. globalVariableTemplate: GlobalVariableTemplate1(),
  273. singleNodeCallControllerFunctionName
  274. },
  275. singleNodeCallControllerFunctionName,
  276. getDocumentDomainTemplate(currentDomain)
  277. );
  278. });
  279. it('should throw an error', () => {
  280. assert.throws(testFunc);
  281. });
  282. });
  283. describe('Variant #2', () => {
  284. const domainsString: string = ['sub1.test.com', 'sub2.test.com'].join(';');
  285. const currentDomain: string = 'sub3.test.com';
  286. let testFunc: () => void;
  287. before(() => {
  288. const [
  289. hiddenDomainsString,
  290. diff
  291. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  292. testFunc = () => getFunctionFromTemplate({
  293. domainLockFunctionName: 'domainLockFunction',
  294. diff: diff,
  295. domains: hiddenDomainsString,
  296. globalVariableTemplate: GlobalVariableTemplate1(),
  297. singleNodeCallControllerFunctionName
  298. },
  299. singleNodeCallControllerFunctionName,
  300. getDocumentDomainTemplate(currentDomain)
  301. );
  302. });
  303. it('should throw an error', () => {
  304. assert.throws(testFunc);
  305. });
  306. });
  307. describe('Variant #3', () => {
  308. const domainsString: string = ['www.example.com', '.example.com', 'sub.test.com'].join(';');
  309. const currentDomain: string = 'www.test.com';
  310. let testFunc: () => void;
  311. before(() => {
  312. const [
  313. hiddenDomainsString,
  314. diff
  315. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  316. testFunc = () => getFunctionFromTemplate(
  317. {
  318. domainLockFunctionName: 'domainLockFunction',
  319. diff: diff,
  320. domains: hiddenDomainsString,
  321. globalVariableTemplate: GlobalVariableTemplate1(),
  322. singleNodeCallControllerFunctionName
  323. },
  324. singleNodeCallControllerFunctionName,
  325. getDocumentDomainTemplate(currentDomain)
  326. );
  327. });
  328. it('should throw an error', () => {
  329. assert.throws(testFunc);
  330. });
  331. });
  332. });
  333. describe('Variant #6: location.hostname', () => {
  334. describe('Variant #1: current location.hostname matches with `domainsString`', () => {
  335. const domainsString: string = ['www.example.com'].join(';');
  336. const currentHostName: string = 'www.example.com';
  337. let testFunc: () => void;
  338. before(() => {
  339. const [
  340. hiddenDomainsString,
  341. diff
  342. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  343. testFunc = () => getFunctionFromTemplate(
  344. {
  345. domainLockFunctionName: 'domainLockFunction',
  346. diff: diff,
  347. domains: hiddenDomainsString,
  348. globalVariableTemplate: GlobalVariableTemplate1(),
  349. singleNodeCallControllerFunctionName
  350. },
  351. singleNodeCallControllerFunctionName,
  352. getDocumentLocationTemplate(currentHostName)
  353. );
  354. });
  355. it('should correctly run code inside template', () => {
  356. assert.doesNotThrow(testFunc);
  357. });
  358. });
  359. describe('Variant #2: current location.hostname doesn\'t match with `domainsString`', () => {
  360. const domainsString: string = ['www.example.com'].join(';');
  361. const currentHostName: string = 'www.test.com';
  362. let testFunc: () => void;
  363. before(() => {
  364. const [
  365. hiddenDomainsString,
  366. diff
  367. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  368. testFunc = () => getFunctionFromTemplate(
  369. {
  370. domainLockFunctionName: 'domainLockFunction',
  371. diff: diff,
  372. domains: hiddenDomainsString,
  373. globalVariableTemplate: GlobalVariableTemplate1(),
  374. singleNodeCallControllerFunctionName
  375. },
  376. singleNodeCallControllerFunctionName,
  377. getDocumentLocationTemplate(currentHostName)
  378. );
  379. });
  380. it('should throw an error', () => {
  381. assert.throws(testFunc);
  382. });
  383. });
  384. });
  385. describe('Variant #7: domain and location.hostname presented', () => {
  386. describe('Variant #1: current domain matches with `domainsString`', () => {
  387. const domainsString: string = ['www.example.com'].join(';');
  388. const currentHostName: string = 'www.example.com';
  389. let testFunc: () => void;
  390. before(() => {
  391. const [
  392. hiddenDomainsString,
  393. diff
  394. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  395. testFunc = () => getFunctionFromTemplate(
  396. {
  397. domainLockFunctionName: 'domainLockFunction',
  398. diff: diff,
  399. domains: hiddenDomainsString,
  400. globalVariableTemplate: GlobalVariableTemplate1(),
  401. singleNodeCallControllerFunctionName
  402. },
  403. singleNodeCallControllerFunctionName,
  404. getDocumentDomainAndLocationTemplate(currentHostName)
  405. );
  406. });
  407. it('should correctly run code inside template', () => {
  408. assert.doesNotThrow(testFunc);
  409. });
  410. });
  411. describe('Variant #2: current domain doesn\'t match with `domainsString`', () => {
  412. const domainsString: string = ['www.example.com'].join(';');
  413. const currentHostName: string = 'www.test.com';
  414. let testFunc: () => void;
  415. before(() => {
  416. const [
  417. hiddenDomainsString,
  418. diff
  419. ] = cryptUtils.hideString(domainsString, domainsString.length * 3);
  420. testFunc = () => getFunctionFromTemplate(
  421. {
  422. domainLockFunctionName: 'domainLockFunction',
  423. diff: diff,
  424. domains: hiddenDomainsString,
  425. globalVariableTemplate: GlobalVariableTemplate1(),
  426. singleNodeCallControllerFunctionName
  427. },
  428. singleNodeCallControllerFunctionName,
  429. getDocumentDomainAndLocationTemplate(currentHostName)
  430. );
  431. });
  432. it('should throw an error', () => {
  433. assert.throws(testFunc);
  434. });
  435. });
  436. });
  437. });