Helper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. // TODO: As per https://github.com/tmpvar/jsdom/issues/321 there is no getSelection in jsdom, yet.
  63. // Once there is one, uncomment the block below to actually check the result.
  64. /*
  65. ids.forEach(function(item, i) {
  66. $.PrivateBin.Helper.selectText(item.join(''));
  67. result *= (contents[i] || contents[0]) === window.getSelection().toString();
  68. });
  69. */
  70. clean();
  71. return Boolean(result);
  72. }
  73. );
  74. });
  75. describe('urls2links', function () {
  76. after(function () {
  77. cleanup();
  78. });
  79. jsc.property(
  80. 'ignores non-URL content',
  81. 'string',
  82. function (content) {
  83. return content === $.PrivateBin.Helper.urls2links(content);
  84. }
  85. );
  86. jsc.property(
  87. 'replaces URLs with anchors',
  88. 'string',
  89. jsc.elements(['http', 'https', 'ftp']),
  90. jsc.nearray(common.jscA2zString()),
  91. jsc.array(common.jscQueryString()),
  92. jsc.array(common.jscHashString()),
  93. 'string',
  94. function (prefix, schema, address, query, fragment, postfix) {
  95. var query = query.join(''),
  96. fragment = fragment.join(''),
  97. url = schema + '://' + address.join('') + '/?' + query + '#' + fragment,
  98. prefix = common.htmlEntities(prefix),
  99. postfix = ' ' + common.htmlEntities(postfix);
  100. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  101. if (
  102. query.slice(-1) === '&' &&
  103. (parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' )
  104. )
  105. {
  106. url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1);
  107. postfix = '';
  108. }
  109. return prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a>' + postfix === $.PrivateBin.Helper.urls2links(prefix + url + postfix);
  110. }
  111. );
  112. jsc.property(
  113. 'replaces magnet links with anchors',
  114. 'string',
  115. jsc.array(common.jscQueryString()),
  116. 'string',
  117. function (prefix, query, postfix) {
  118. var url = 'magnet:?' + query.join('').replace(/^&+|&+$/gm,''),
  119. prefix = common.htmlEntities(prefix),
  120. postfix = common.htmlEntities(postfix);
  121. return prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a> ' + postfix === $.PrivateBin.Helper.urls2links(prefix + url + ' ' + postfix);
  122. }
  123. );
  124. });
  125. describe('sprintf', function () {
  126. after(function () {
  127. cleanup();
  128. });
  129. jsc.property(
  130. 'replaces %s in strings with first given parameter',
  131. 'string',
  132. '(small nearray) string',
  133. 'string',
  134. function (prefix, params, postfix) {
  135. prefix = prefix.replace(/%(s|d)/g, '%%');
  136. params[0] = params[0].replace(/%(s|d)/g, '%%');
  137. postfix = postfix.replace(/%(s|d)/g, '%%');
  138. var result = prefix + params[0] + postfix;
  139. params.unshift(prefix + '%s' + postfix);
  140. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  141. }
  142. );
  143. jsc.property(
  144. 'replaces %d in strings with first given parameter',
  145. 'string',
  146. '(small nearray) nat',
  147. 'string',
  148. function (prefix, params, postfix) {
  149. prefix = prefix.replace(/%(s|d)/g, '%%');
  150. postfix = postfix.replace(/%(s|d)/g, '%%');
  151. var result = prefix + params[0] + postfix;
  152. params.unshift(prefix + '%d' + postfix);
  153. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  154. }
  155. );
  156. jsc.property(
  157. 'replaces %d in strings with 0 if first parameter is not a number',
  158. 'string',
  159. '(small nearray) falsy',
  160. 'string',
  161. function (prefix, params, postfix) {
  162. prefix = prefix.replace(/%(s|d)/g, '%%');
  163. postfix = postfix.replace(/%(s|d)/g, '%%');
  164. var result = prefix + '0' + postfix;
  165. params.unshift(prefix + '%d' + postfix);
  166. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  167. }
  168. );
  169. jsc.property(
  170. 'replaces %d and %s in strings in order',
  171. 'string',
  172. 'nat',
  173. 'string',
  174. 'string',
  175. 'string',
  176. function (prefix, uint, middle, string, postfix) {
  177. prefix = prefix.replace(/%(s|d)/g, '%%');
  178. middle = middle.replace(/%(s|d)/g, '%%');
  179. postfix = postfix.replace(/%(s|d)/g, '%%');
  180. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  181. result = prefix + uint + middle + string + postfix;
  182. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  183. }
  184. );
  185. jsc.property(
  186. 'replaces %d and %s in strings in reverse order',
  187. 'string',
  188. 'nat',
  189. 'string',
  190. 'string',
  191. 'string',
  192. function (prefix, uint, middle, string, postfix) {
  193. prefix = prefix.replace(/%(s|d)/g, '%%');
  194. middle = middle.replace(/%(s|d)/g, '%%');
  195. postfix = postfix.replace(/%(s|d)/g, '%%');
  196. var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  197. result = prefix + string + middle + uint + postfix;
  198. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  199. }
  200. );
  201. });
  202. describe('getCookie', function () {
  203. this.timeout(30000);
  204. jsc.property(
  205. 'returns the requested cookie',
  206. jsc.nearray(jsc.nearray(common.jscAlnumString())),
  207. jsc.nearray(jsc.nearray(common.jscAlnumString())),
  208. function (labels, values) {
  209. var selectedKey = '', selectedValue = '',
  210. cookieArray = [];
  211. labels.forEach(function(item, i) {
  212. var key = item.join(''),
  213. value = (values[i] || values[0]).join('');
  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. $.PrivateBin.Helper.reset();
  224. clean();
  225. return result === selectedValue;
  226. }
  227. );
  228. });
  229. describe('baseUri', function () {
  230. this.timeout(30000);
  231. jsc.property(
  232. 'returns the URL without query & fragment',
  233. jsc.elements(['http', 'https']),
  234. jsc.nearray(common.jscA2zString()),
  235. jsc.array(common.jscQueryString()),
  236. 'asciistring',
  237. function (schema, address, query, fragment) {
  238. $.PrivateBin.Helper.reset();
  239. var expected = schema + '://' + address.join('') + '/',
  240. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  241. result = $.PrivateBin.Helper.baseUri();
  242. clean();
  243. return expected === result;
  244. }
  245. );
  246. });
  247. describe('htmlEntities', function () {
  248. after(function () {
  249. cleanup();
  250. });
  251. jsc.property(
  252. 'removes all HTML entities from any given string',
  253. 'string',
  254. function (string) {
  255. var result = common.htmlEntities(string);
  256. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  257. }
  258. );
  259. });
  260. });