Helper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. 'use strict';
  2. var common = require('../common');
  3. describe('Helper', function () {
  4. describe('secondsToHuman', function () {
  5. jsc.property('returns an array with a number and a word', 'integer', function (number) {
  6. var result = $.PrivateBin.Helper.secondsToHuman(number);
  7. return Array.isArray(result) &&
  8. result.length === 2 &&
  9. result[0] === parseInt(result[0], 10) &&
  10. typeof result[1] === 'string';
  11. });
  12. jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
  13. return $.PrivateBin.Helper.secondsToHuman(number)[0] === number;
  14. });
  15. jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
  16. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'second';
  17. });
  18. jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
  19. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  20. });
  21. jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
  22. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'minute';
  23. });
  24. jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
  25. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  26. });
  27. jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
  28. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'hour';
  29. });
  30. jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
  31. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  32. });
  33. jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
  34. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'day';
  35. });
  36. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  37. jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
  38. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  39. });
  40. jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
  41. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'month';
  42. });
  43. });
  44. // this test is not yet meaningful using jsdom, as it does not contain getSelection support.
  45. // TODO: This needs to be tested using a browser.
  46. describe('selectText', function () {
  47. this.timeout(30000);
  48. jsc.property(
  49. 'selection contains content of given ID',
  50. jsc.nearray(jsc.nearray(common.jscAlnumString())),
  51. 'nearray string',
  52. function (ids, contents) {
  53. var html = '',
  54. result = true,
  55. clean = jsdom(html);
  56. ids.forEach(function(item, i) {
  57. html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  58. });
  59. // TODO: As per https://github.com/tmpvar/jsdom/issues/321 there is no getSelection in jsdom, yet.
  60. // Once there is one, uncomment the block below to actually check the result.
  61. /*
  62. ids.forEach(function(item, i) {
  63. $.PrivateBin.Helper.selectText(item.join(''));
  64. result *= (contents[i] || contents[0]) === window.getSelection().toString();
  65. });
  66. */
  67. clean();
  68. return Boolean(result);
  69. }
  70. );
  71. });
  72. describe('urls2links', function () {
  73. before(function () {
  74. cleanup = jsdom();
  75. });
  76. jsc.property(
  77. 'ignores non-URL content',
  78. 'string',
  79. function (content) {
  80. return $.PrivateBin.Helper.htmlEntities(content) === $.PrivateBin.Helper.urls2links(content);
  81. }
  82. );
  83. jsc.property(
  84. 'replaces URLs with anchors',
  85. 'string',
  86. jsc.elements(['http', 'https', 'ftp']),
  87. jsc.nearray(common.jscA2zString()),
  88. jsc.array(common.jscQueryString()),
  89. jsc.array(common.jscHashString()),
  90. 'string',
  91. function (prefix, schema, address, query, fragment, postfix) {
  92. query = query.join('');
  93. fragment = fragment.join('');
  94. postfix = ' ' + postfix;
  95. let url = schema + '://' + address.join('') + '/?' + query + '#' + fragment;
  96. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  97. if (
  98. query.slice(-1) === '&' &&
  99. (parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' )
  100. )
  101. {
  102. url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1);
  103. postfix = '';
  104. }
  105. return $.PrivateBin.Helper.htmlEntities(prefix) + '<a href="' + url + '" rel="nofollow">' + $.PrivateBin.Helper.htmlEntities(url) + '</a>' + $.PrivateBin.Helper.htmlEntities(postfix) === $.PrivateBin.Helper.urls2links(prefix + url + postfix);
  106. }
  107. );
  108. jsc.property(
  109. 'replaces magnet links with anchors',
  110. 'string',
  111. jsc.array(common.jscQueryString()),
  112. 'string',
  113. function (prefix, query, postfix) {
  114. let url = 'magnet:?' + query.join('').replace(/^&+|&+$/gm,'');
  115. return $.PrivateBin.Helper.htmlEntities(prefix) + '<a href="' + url + '" rel="nofollow">' + $.PrivateBin.Helper.htmlEntities(url) + '</a> ' + $.PrivateBin.Helper.htmlEntities(postfix) === $.PrivateBin.Helper.urls2links(prefix + url + ' ' + postfix);
  116. }
  117. );
  118. });
  119. describe('sprintf', function () {
  120. jsc.property(
  121. 'replaces %s in strings with first given parameter',
  122. 'string',
  123. '(small nearray) string',
  124. 'string',
  125. function (prefix, params, postfix) {
  126. prefix = prefix.replace(/%(s|d)/g, '%%');
  127. params[0] = params[0].replace(/%(s|d)/g, '%%');
  128. postfix = postfix.replace(/%(s|d)/g, '%%');
  129. var result = prefix + params[0] + postfix;
  130. params.unshift(prefix + '%s' + postfix);
  131. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  132. }
  133. );
  134. jsc.property(
  135. 'replaces %d in strings with first given parameter',
  136. 'string',
  137. '(small nearray) nat',
  138. 'string',
  139. function (prefix, params, postfix) {
  140. prefix = prefix.replace(/%(s|d)/g, '%%');
  141. postfix = postfix.replace(/%(s|d)/g, '%%');
  142. var result = prefix + params[0] + postfix;
  143. params.unshift(prefix + '%d' + postfix);
  144. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  145. }
  146. );
  147. jsc.property(
  148. 'replaces %d in strings with 0 if first parameter is not a number',
  149. 'string',
  150. '(small nearray) falsy',
  151. 'string',
  152. function (prefix, params, postfix) {
  153. prefix = prefix.replace(/%(s|d)/g, '%%');
  154. postfix = postfix.replace(/%(s|d)/g, '%%');
  155. var result = prefix + '0' + postfix;
  156. params.unshift(prefix + '%d' + postfix);
  157. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  158. }
  159. );
  160. jsc.property(
  161. 'replaces %d and %s in strings in order',
  162. 'string',
  163. 'nat',
  164. 'string',
  165. 'string',
  166. 'string',
  167. function (prefix, uint, middle, string, postfix) {
  168. prefix = prefix.replace(/%(s|d)/g, '');
  169. middle = middle.replace(/%(s|d)/g, '');
  170. postfix = postfix.replace(/%(s|d)/g, '');
  171. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  172. result = prefix + uint + middle + string + postfix;
  173. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  174. }
  175. );
  176. jsc.property(
  177. 'replaces %d and %s in strings in reverse order',
  178. 'string',
  179. 'nat',
  180. 'string',
  181. 'string',
  182. 'string',
  183. function (prefix, uint, middle, string, postfix) {
  184. prefix = prefix.replace(/%(s|d)/g, '');
  185. middle = middle.replace(/%(s|d)/g, '');
  186. postfix = postfix.replace(/%(s|d)/g, '');
  187. var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  188. result = prefix + string + middle + uint + postfix;
  189. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  190. }
  191. );
  192. });
  193. describe('getCookie', function () {
  194. this.timeout(30000);
  195. before(function () {
  196. cleanup();
  197. });
  198. jsc.property(
  199. 'returns the requested cookie',
  200. jsc.nearray(jsc.nearray(common.jscAlnumString())),
  201. jsc.nearray(jsc.nearray(common.jscAlnumString())),
  202. function (labels, values) {
  203. var selectedKey = '', selectedValue = '',
  204. cookieArray = [];
  205. labels.forEach(function(item, i) {
  206. var key = item.join(''),
  207. value = (values[i] || values[0]).join('');
  208. cookieArray.push(key + '=' + value);
  209. if (Math.random() < 1 / i || selectedKey === key)
  210. {
  211. selectedKey = key;
  212. selectedValue = value;
  213. }
  214. });
  215. var clean = jsdom('', {cookie: cookieArray}),
  216. result = $.PrivateBin.Helper.getCookie(selectedKey);
  217. $.PrivateBin.Helper.reset();
  218. clean();
  219. return result === selectedValue;
  220. }
  221. );
  222. });
  223. describe('baseUri', function () {
  224. this.timeout(30000);
  225. jsc.property(
  226. 'returns the URL without query & fragment',
  227. jsc.elements(['http', 'https']),
  228. jsc.nearray(common.jscA2zString()),
  229. jsc.array(common.jscA2zString()),
  230. jsc.array(common.jscQueryString()),
  231. 'string',
  232. function (schema, address, path, query, fragment) {
  233. $.PrivateBin.Helper.reset();
  234. var path = path.join('') + (path.length > 0 ? '/' : ''),
  235. expected = schema + '://' + address.join('') + '/' + path,
  236. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  237. result = $.PrivateBin.Helper.baseUri();
  238. clean();
  239. return expected === result;
  240. }
  241. );
  242. });
  243. describe('htmlEntities', function () {
  244. before(function () {
  245. cleanup = jsdom();
  246. });
  247. jsc.property(
  248. 'removes all HTML entities from any given string',
  249. 'string',
  250. function (string) {
  251. var result = $.PrivateBin.Helper.htmlEntities(string);
  252. return !(/[<>]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  253. }
  254. );
  255. });
  256. });