test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. 'use strict';
  2. var jsc = require('jsverify'),
  3. jsdom = require('jsdom-global'),
  4. cleanup = jsdom(),
  5. a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  6. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  7. alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']),
  8. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  9. base64String = alnumString.concat(['+','/','=']).concat(
  10. a2zString.map(function(c) {
  11. return c.toUpperCase();
  12. })
  13. );
  14. global.$ = global.jQuery = require('./jquery-3.1.1');
  15. global.sjcl = require('./sjcl-1.0.6');
  16. global.Base64 = require('./base64-2.1.9');
  17. global.RawDeflate = require('./rawdeflate-0.5');
  18. require('./rawinflate-0.3');
  19. require('./privatebin');
  20. describe('helper', function () {
  21. describe('secondsToHuman', function () {
  22. after(function () {
  23. cleanup();
  24. });
  25. jsc.property('returns an array with a number and a word', 'integer', function (number) {
  26. var result = $.PrivateBin.helper.secondsToHuman(number);
  27. return Array.isArray(result) &&
  28. result.length === 2 &&
  29. result[0] === parseInt(result[0], 10) &&
  30. typeof result[1] === 'string';
  31. });
  32. jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
  33. return $.PrivateBin.helper.secondsToHuman(number)[0] === number;
  34. });
  35. jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
  36. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'second';
  37. });
  38. jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
  39. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  40. });
  41. jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
  42. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'minute';
  43. });
  44. jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
  45. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  46. });
  47. jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
  48. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'hour';
  49. });
  50. jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
  51. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  52. });
  53. jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
  54. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'day';
  55. });
  56. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  57. jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
  58. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  59. });
  60. jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
  61. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'month';
  62. });
  63. });
  64. describe('scriptLocation', function () {
  65. jsc.property(
  66. 'returns the URL without query & fragment',
  67. jsc.nearray(jsc.elements(a2zString)),
  68. jsc.nearray(jsc.elements(a2zString)),
  69. jsc.array(jsc.elements(queryString)),
  70. 'string',
  71. function (schema, address, query, fragment) {
  72. var expected = schema.join('') + '://' + address.join('') + '/',
  73. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  74. result = $.PrivateBin.helper.scriptLocation();
  75. clean();
  76. return expected === result;
  77. }
  78. );
  79. });
  80. describe('pasteId', function () {
  81. jsc.property(
  82. 'returns the query string without separator, if any',
  83. jsc.nearray(jsc.elements(a2zString)),
  84. jsc.nearray(jsc.elements(a2zString)),
  85. jsc.array(jsc.elements(queryString)),
  86. 'string',
  87. function (schema, address, query, fragment) {
  88. var queryString = query.join(''),
  89. clean = jsdom('', {
  90. url: schema.join('') + '://' + address.join('') +
  91. '/?' + queryString + '#' + fragment
  92. }),
  93. result = $.PrivateBin.helper.pasteId();
  94. clean();
  95. return queryString === result;
  96. }
  97. );
  98. });
  99. describe('pageKey', function () {
  100. jsc.property(
  101. 'returns the fragment of the URL',
  102. jsc.nearray(jsc.elements(a2zString)),
  103. jsc.nearray(jsc.elements(a2zString)),
  104. jsc.array(jsc.elements(queryString)),
  105. jsc.array(jsc.elements(base64String)),
  106. function (schema, address, query, fragment) {
  107. var fragmentString = fragment.join(''),
  108. clean = jsdom('', {
  109. url: schema.join('') + '://' + address.join('') +
  110. '/?' + query.join('') + '#' + fragmentString
  111. }),
  112. result = $.PrivateBin.helper.pageKey();
  113. clean();
  114. return fragmentString === result;
  115. }
  116. );
  117. jsc.property(
  118. 'returns the fragment stripped of trailing query parts',
  119. jsc.nearray(jsc.elements(a2zString)),
  120. jsc.nearray(jsc.elements(a2zString)),
  121. jsc.array(jsc.elements(queryString)),
  122. jsc.array(jsc.elements(base64String)),
  123. jsc.array(jsc.elements(queryString)),
  124. function (schema, address, query, fragment, trail) {
  125. var fragmentString = fragment.join(''),
  126. clean = jsdom('', {
  127. url: schema.join('') + '://' + address.join('') + '/?' +
  128. query.join('') + '#' + fragmentString + '&' + trail.join('')
  129. }),
  130. result = $.PrivateBin.helper.pageKey();
  131. clean();
  132. return fragmentString === result;
  133. }
  134. );
  135. });
  136. describe('htmlEntities', function () {
  137. after(function () {
  138. cleanup();
  139. });
  140. jsc.property(
  141. 'removes all HTML entities from any given string',
  142. 'string',
  143. function (string) {
  144. var result = $.PrivateBin.helper.htmlEntities(string);
  145. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  146. }
  147. );
  148. });
  149. });