DomainLockNodeTemplate.spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import * as format from 'string-template';
  2. import { assert } from 'chai';
  3. import { DomainLockNodeTemplate } from '../../../../../src/templates/custom-nodes/domain-lock-nodes/domain-lock-node/DomainLockNodeTemplate';
  4. import { CryptUtils } from '../../../../../src/utils/CryptUtils';
  5. /**
  6. * @param templateData
  7. * @param callsControllerFunctionName
  8. * @param currentDomain
  9. * @returns {Function}
  10. */
  11. function getFunctionFromTemplate (templateData: any, callsControllerFunctionName: string, currentDomain: string) {
  12. const domainLockTemplate: string = format(DomainLockNodeTemplate(), templateData);
  13. return Function(`
  14. document = {
  15. domain: '${currentDomain}'
  16. };
  17. var ${callsControllerFunctionName} = (function(){
  18. return function (context, fn){
  19. return function () {
  20. return fn.apply(context, arguments);
  21. };
  22. }
  23. })();
  24. ${domainLockTemplate}
  25. `)();
  26. }
  27. describe('DomainLockNodeTemplate (): string', () => {
  28. const singleNodeCallControllerFunctionName: string = 'callsController';
  29. describe('variant #1: current domain matches with `domainsString`', () => {
  30. const domainsString: string = ['www.example.com'].join(';');
  31. const currentDomain: string = 'www.example.com';
  32. let testFunc: () => void;
  33. before(() => {
  34. const [
  35. hiddenDomainsString,
  36. diff
  37. ] = CryptUtils.hideString(domainsString, domainsString.length * 3);
  38. testFunc = () => getFunctionFromTemplate({
  39. domainLockFunctionName: 'domainLockFunction',
  40. diff: diff,
  41. domains: hiddenDomainsString,
  42. singleNodeCallControllerFunctionName
  43. }, singleNodeCallControllerFunctionName, currentDomain);
  44. });
  45. it('should correctly runs code inside template', () => {
  46. assert.doesNotThrow(testFunc);
  47. });
  48. });
  49. describe('variant #2: urrent domain matches with base domain of `domainsString` item', () => {
  50. const domainsString: string = ['www.test.com', '.example.com'].join(';');
  51. const currentDomain: string = 'subdomain.example.com';
  52. let testFunc: () => void;
  53. before(() => {
  54. const [
  55. hiddenDomainsString,
  56. diff
  57. ] = CryptUtils.hideString(domainsString, domainsString.length * 3);
  58. testFunc = () => getFunctionFromTemplate({
  59. domainLockFunctionName: 'domainLockFunction',
  60. diff: diff,
  61. domains: hiddenDomainsString,
  62. singleNodeCallControllerFunctionName
  63. }, singleNodeCallControllerFunctionName, currentDomain);
  64. });
  65. it('should correctly runs code inside template', () => {
  66. assert.doesNotThrow(testFunc);
  67. });
  68. });
  69. describe('variant #3: current domain doesn\'t match with `domainsString`', () => {
  70. const domainsString: string = ['www.example.com'].join(';');
  71. const currentDomain: string = 'www.test.com';
  72. let testFunc: () => void;
  73. before(() => {
  74. const [
  75. hiddenDomainsString,
  76. diff
  77. ] = CryptUtils.hideString(domainsString, domainsString.length * 3);
  78. testFunc = () => getFunctionFromTemplate({
  79. domainLockFunctionName: 'domainLockFunction',
  80. diff: diff,
  81. domains: hiddenDomainsString,
  82. singleNodeCallControllerFunctionName
  83. }, singleNodeCallControllerFunctionName, currentDomain);
  84. });
  85. it('should throw an error', () => {
  86. assert.throws(testFunc);
  87. });
  88. });
  89. });