Helper.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. this.timeout(30000);
  74. before(function () {
  75. cleanup = jsdom();
  76. });
  77. jsc.property(
  78. 'ignores non-URL content',
  79. 'string',
  80. function (content) {
  81. content = content.replace(/\r|\f/g, '\n').replace(/\u0000/g, '').replace(/\u000b/g, '');
  82. let clean = jsdom();
  83. $('body').html('<div id="foo"></div>');
  84. let e = $('#foo');
  85. e.text(content);
  86. $.PrivateBin.Helper.urls2links(e);
  87. let result = e.text();
  88. clean();
  89. return content === result;
  90. }
  91. );
  92. jsc.property(
  93. 'replaces URLs with anchors',
  94. 'string',
  95. common.jscUrl(),
  96. jsc.array(common.jscHashString()),
  97. 'string',
  98. function (prefix, url, fragment, postfix) {
  99. prefix = prefix.replace(/\r|\f/g, '\n').replace(/\u0000/g, '').replace(/\u000b/g, '');
  100. postfix = ' ' + postfix.replace(/\r/g, '\n').replace(/\u0000/g, '');
  101. url.fragment = fragment.join('');
  102. let urlString = common.urlToString(url),
  103. clean = jsdom();
  104. $('body').html('<div id="foo"></div>');
  105. let e = $('#foo');
  106. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  107. if (
  108. url.query[-1] === '&' &&
  109. (parseInt(url.fragment.charAt(0), 10) >= 0 || url.fragment.charAt(0) === 'x')
  110. ) {
  111. url.query.pop();
  112. urlString = common.urlToString(url);
  113. postfix = '';
  114. }
  115. e.text(prefix + urlString + postfix);
  116. $.PrivateBin.Helper.urls2links(e);
  117. let result = e.html();
  118. clean();
  119. urlString = $('<div />').text(urlString).html();
  120. const expected = $('<div />').text(prefix).html() + '<a href="' + urlString + '" target="_blank" rel="nofollow noopener noreferrer">' + urlString + '</a>' + $('<div />').text(postfix).html();
  121. return $('<div />').text(prefix).html() + '<a href="' + urlString + '" target="_blank" rel="nofollow noopener noreferrer">' + urlString + '</a>' + $('<div />').text(postfix).html() === result;
  122. }
  123. );
  124. jsc.property(
  125. 'replaces magnet links with anchors',
  126. 'string',
  127. jsc.array(common.jscQueryString()),
  128. 'string',
  129. function (prefix, query, postfix) {
  130. prefix = prefix.replace(/\r|\f/g, '\n').replace(/\u0000/g, '').replace(/\u000b/g, '');
  131. postfix = ' ' + postfix.replace(/\r/g, '\n').replace(/\u0000/g, '');
  132. let url = 'magnet:?' + query.join('').replace(/^&+|&+$/gm,''),
  133. clean = jsdom();
  134. $('body').html('<div id="foo"></div>');
  135. let e = $('#foo');
  136. e.text(prefix + url + postfix);
  137. $.PrivateBin.Helper.urls2links(e);
  138. let result = e.html();
  139. clean();
  140. url = $('<div />').text(url).html();
  141. return $('<div />').text(prefix).html() + '<a href="' + url + '" target="_blank" rel="nofollow noopener noreferrer">' + url + '</a>' + $('<div />').text(postfix).html() === result;
  142. }
  143. );
  144. });
  145. describe('sprintf', function () {
  146. jsc.property(
  147. 'replaces %s in strings with first given parameter',
  148. 'string',
  149. '(small nearray) string',
  150. 'string',
  151. function (prefix, params, postfix) {
  152. prefix = prefix.replace(/%(s|d)/g, '%%');
  153. params[0] = params[0].replace(/%(s|d)/g, '%%');
  154. postfix = postfix.replace(/%(s|d)/g, '%%');
  155. var result = prefix + params[0] + postfix;
  156. params.unshift(prefix + '%s' + postfix);
  157. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  158. }
  159. );
  160. jsc.property(
  161. 'replaces %d in strings with first given parameter',
  162. 'string',
  163. '(small nearray) nat',
  164. 'string',
  165. function (prefix, params, postfix) {
  166. prefix = prefix.replace(/%(s|d)/g, '%%');
  167. postfix = postfix.replace(/%(s|d)/g, '%%');
  168. var result = prefix + params[0] + postfix;
  169. params.unshift(prefix + '%d' + postfix);
  170. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  171. }
  172. );
  173. jsc.property(
  174. 'replaces %d in strings with 0 if first parameter is not a number',
  175. 'string',
  176. '(small nearray) falsy',
  177. 'string',
  178. function (prefix, params, postfix) {
  179. prefix = prefix.replace(/%(s|d)/g, '%%');
  180. postfix = postfix.replace(/%(s|d)/g, '%%');
  181. var result = prefix + '0' + postfix;
  182. params.unshift(prefix + '%d' + postfix);
  183. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  184. }
  185. );
  186. jsc.property(
  187. 'replaces %d and %s in strings in order',
  188. 'string',
  189. 'nat',
  190. 'string',
  191. 'string',
  192. 'string',
  193. function (prefix, uint, middle, string, postfix) {
  194. prefix = prefix.replace(/%(s|d)/g, '');
  195. middle = middle.replace(/%(s|d)/g, '');
  196. postfix = postfix.replace(/%(s|d)/g, '');
  197. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  198. result = prefix + uint + middle + string + postfix;
  199. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  200. }
  201. );
  202. jsc.property(
  203. 'replaces %d and %s in strings in reverse order',
  204. 'string',
  205. 'nat',
  206. 'string',
  207. 'string',
  208. 'string',
  209. function (prefix, uint, middle, string, postfix) {
  210. prefix = prefix.replace(/%(s|d)/g, '');
  211. middle = middle.replace(/%(s|d)/g, '');
  212. postfix = postfix.replace(/%(s|d)/g, '');
  213. var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  214. result = prefix + string + middle + uint + postfix;
  215. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  216. }
  217. );
  218. });
  219. describe('getCookie', function () {
  220. this.timeout(30000);
  221. before(function () {
  222. cleanup();
  223. });
  224. /* TODO test fails since jsDOM version 17 - document.cookie remains empty
  225. jsc.property(
  226. 'returns the requested cookie',
  227. jsc.nearray(jsc.nearray(common.jscAlnumString())),
  228. jsc.nearray(jsc.nearray(common.jscAlnumString())),
  229. function (labels, values) {
  230. let selectedKey = '', selectedValue = '';
  231. const clean = jsdom();
  232. labels.forEach(function(item, i) {
  233. const key = item.join(''),
  234. value = (values[i] || values[0]).join('');
  235. document.cookie = key + '=' + value;
  236. if (Math.random() < 1 / i || selectedKey === key)
  237. {
  238. selectedKey = key;
  239. selectedValue = value;
  240. }
  241. });
  242. const result = $.PrivateBin.Helper.getCookie(selectedKey);
  243. $.PrivateBin.Helper.reset();
  244. clean();
  245. return result === selectedValue;
  246. }
  247. ); */
  248. });
  249. describe('baseUri', function () {
  250. this.timeout(30000);
  251. jsc.property(
  252. 'returns the URL without query & fragment',
  253. common.jscSchemas(false),
  254. common.jscUrl(),
  255. function (schema, url) {
  256. url.schema = schema;
  257. const fullUrl = common.urlToString(url);
  258. delete(url.query);
  259. delete(url.fragment);
  260. $.PrivateBin.Helper.reset();
  261. const expected = common.urlToString(url),
  262. clean = jsdom('', {url: fullUrl}),
  263. result = $.PrivateBin.Helper.baseUri();
  264. clean();
  265. return expected === result;
  266. }
  267. );
  268. });
  269. describe('htmlEntities', function () {
  270. before(function () {
  271. cleanup = jsdom();
  272. });
  273. jsc.property(
  274. 'removes all HTML entities from any given string',
  275. 'string',
  276. function (string) {
  277. var result = $.PrivateBin.Helper.htmlEntities(string);
  278. return !(/[<>]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  279. }
  280. );
  281. });
  282. describe('formatBytes', function () {
  283. jsc.property('returns 0 B for 0 bytes', function () {
  284. return $.PrivateBin.Helper.formatBytes(0) === '0 B';
  285. });
  286. jsc.property('formats bytes < 1000 as B', function () {
  287. return $.PrivateBin.Helper.formatBytes(500) === '500 B';
  288. });
  289. jsc.property('formats kilobytes correctly', function () {
  290. return $.PrivateBin.Helper.formatBytes(1500) === '1.5 kB';
  291. });
  292. jsc.property('formats megabytes correctly', function () {
  293. return $.PrivateBin.Helper.formatBytes(2 * 1000 * 1000) === '2 MB';
  294. });
  295. jsc.property('formats gigabytes correctly', function () {
  296. return $.PrivateBin.Helper.formatBytes(3.45 * 1000 * 1000 * 1000) === '3.45 GB';
  297. });
  298. jsc.property('rounds to two decimal places', function () {
  299. return $.PrivateBin.Helper.formatBytes(1234567) === '1.23 MB';
  300. });
  301. });
  302. describe('isBootstrap5', function () {
  303. jsc.property('Bootstrap 5 has been detected', function () {
  304. global.bootstrap = {};
  305. return $.PrivateBin.Helper.isBootstrap5() === true;
  306. });
  307. jsc.property('Bootstrap 5 has not been detected', function () {
  308. delete global.bootstrap;
  309. return $.PrivateBin.Helper.isBootstrap5() === false;
  310. });
  311. });
  312. });