Helper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 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. var query = query.join(''),
  93. fragment = fragment.join(''),
  94. url = schema + '://' + address.join('') + '/?' + query + '#' + fragment,
  95. prefix = $.PrivateBin.Helper.htmlEntities(prefix),
  96. postfix = ' ' + $.PrivateBin.Helper.htmlEntities(postfix);
  97. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  98. if (
  99. query.slice(-1) === '&' &&
  100. (parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' )
  101. )
  102. {
  103. url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1);
  104. postfix = '';
  105. }
  106. return prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a>' + postfix === $.PrivateBin.Helper.urls2links(prefix + url + postfix);
  107. }
  108. );
  109. jsc.property(
  110. 'replaces magnet links with anchors',
  111. 'string',
  112. jsc.array(common.jscQueryString()),
  113. 'string',
  114. function (prefix, query, postfix) {
  115. var url = 'magnet:?' + query.join('').replace(/^&+|&+$/gm,''),
  116. prefix = $.PrivateBin.Helper.htmlEntities(prefix),
  117. postfix = $.PrivateBin.Helper.htmlEntities(postfix);
  118. return prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a> ' + postfix === $.PrivateBin.Helper.urls2links(prefix + url + ' ' + postfix);
  119. }
  120. );
  121. });
  122. describe('sprintf', function () {
  123. jsc.property(
  124. 'replaces %s in strings with first given parameter',
  125. 'string',
  126. '(small nearray) string',
  127. 'string',
  128. function (prefix, params, postfix) {
  129. prefix = prefix.replace(/%(s|d)/g, '%%');
  130. params[0] = params[0].replace(/%(s|d)/g, '%%');
  131. postfix = postfix.replace(/%(s|d)/g, '%%');
  132. var result = prefix + params[0] + postfix;
  133. params.unshift(prefix + '%s' + postfix);
  134. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  135. }
  136. );
  137. jsc.property(
  138. 'replaces %d in strings with first given parameter',
  139. 'string',
  140. '(small nearray) nat',
  141. 'string',
  142. function (prefix, params, postfix) {
  143. prefix = prefix.replace(/%(s|d)/g, '%%');
  144. postfix = postfix.replace(/%(s|d)/g, '%%');
  145. var result = prefix + params[0] + postfix;
  146. params.unshift(prefix + '%d' + postfix);
  147. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  148. }
  149. );
  150. jsc.property(
  151. 'replaces %d in strings with 0 if first parameter is not a number',
  152. 'string',
  153. '(small nearray) falsy',
  154. 'string',
  155. function (prefix, params, postfix) {
  156. prefix = prefix.replace(/%(s|d)/g, '%%');
  157. postfix = postfix.replace(/%(s|d)/g, '%%');
  158. var result = prefix + '0' + postfix;
  159. params.unshift(prefix + '%d' + postfix);
  160. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  161. }
  162. );
  163. jsc.property(
  164. 'replaces %d and %s in strings in order',
  165. 'string',
  166. 'nat',
  167. 'string',
  168. 'string',
  169. 'string',
  170. function (prefix, uint, middle, string, postfix) {
  171. prefix = prefix.replace(/%(s|d)/g, '%%');
  172. middle = middle.replace(/%(s|d)/g, '%%');
  173. postfix = postfix.replace(/%(s|d)/g, '%%');
  174. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  175. result = prefix + uint + middle + string + postfix;
  176. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  177. }
  178. );
  179. jsc.property(
  180. 'replaces %d and %s in strings in reverse order',
  181. 'string',
  182. 'nat',
  183. 'string',
  184. 'string',
  185. 'string',
  186. function (prefix, uint, middle, string, postfix) {
  187. prefix = prefix.replace(/%(s|d)/g, '%%');
  188. middle = middle.replace(/%(s|d)/g, '%%');
  189. postfix = postfix.replace(/%(s|d)/g, '%%');
  190. var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  191. result = prefix + string + middle + uint + postfix;
  192. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  193. }
  194. );
  195. });
  196. describe('getCookie', function () {
  197. this.timeout(30000);
  198. before(function () {
  199. cleanup();
  200. });
  201. jsc.property(
  202. 'returns the requested cookie',
  203. 'nearray asciinestring',
  204. 'nearray asciistring',
  205. function (labels, values) {
  206. var selectedKey = '', selectedValue = '',
  207. cookieArray = [];
  208. labels.forEach(function(item, i) {
  209. // deliberatly using a non-ascii key for replacing invalid characters
  210. var key = item.replace(/[\s;,=]/g, Array(i+2).join('£')),
  211. value = (values[i] || values[0]).replace(/[\s;,=]/g, '');
  212. cookieArray.push(key + '=' + value);
  213. if (Math.random() < 1 / i || selectedKey === key)
  214. {
  215. selectedKey = key;
  216. selectedValue = value;
  217. }
  218. });
  219. var clean = jsdom('', {cookie: cookieArray}),
  220. result = $.PrivateBin.Helper.getCookie(selectedKey);
  221. clean();
  222. return result === selectedValue;
  223. }
  224. );
  225. });
  226. describe('baseUri', function () {
  227. this.timeout(30000);
  228. before(function () {
  229. $.PrivateBin.Helper.reset();
  230. });
  231. jsc.property(
  232. 'returns the URL without query & fragment',
  233. common.jscSchemas(),
  234. jsc.nearray(common.jscA2zString()),
  235. jsc.array(common.jscQueryString()),
  236. 'string',
  237. function (schema, address, query, fragment) {
  238. var expected = schema + '://' + address.join('') + '/',
  239. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  240. result = $.PrivateBin.Helper.baseUri();
  241. $.PrivateBin.Helper.reset();
  242. clean();
  243. return expected === result;
  244. }
  245. );
  246. });
  247. describe('htmlEntities', function () {
  248. before(function () {
  249. cleanup = jsdom();
  250. });
  251. jsc.property(
  252. 'removes all HTML entities from any given string',
  253. 'string',
  254. function (string) {
  255. var result = $.PrivateBin.Helper.htmlEntities(string);
  256. return !(/[<>]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  257. }
  258. );
  259. });
  260. });