Helper.js 12 KB

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