1
0

test.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. 'use strict';
  2. var jsc = require('jsverify'),
  3. jsdom = require('jsdom-global'),
  4. cleanup = jsdom(),
  5. a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  6. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  7. alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']),
  8. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  9. base64String = alnumString.concat(['+','/','=']).concat(
  10. a2zString.map(function(c) {
  11. return c.toUpperCase();
  12. })
  13. ),
  14. // schemas supported by the whatwg-url library
  15. schemas = ['ftp','gopher','http','https','ws','wss'],
  16. supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
  17. logFile = require('fs').createWriteStream('test.log');
  18. global.$ = global.jQuery = require('./jquery-3.1.1');
  19. global.sjcl = require('./sjcl-1.0.6');
  20. global.Base64 = require('./base64-2.1.9').Base64;
  21. global.RawDeflate = require('./rawdeflate-0.5').RawDeflate;
  22. global.RawDeflate.inflate = require('./rawinflate-0.3').RawDeflate.inflate;
  23. require('./privatebin');
  24. // redirect console messages to log file
  25. console.info = console.warn = console.error = function () {
  26. logFile.write(Array.prototype.slice.call(arguments).join('') + '\n');
  27. }
  28. describe('Helper', function () {
  29. describe('secondsToHuman', function () {
  30. after(function () {
  31. cleanup();
  32. });
  33. jsc.property('returns an array with a number and a word', 'integer', function (number) {
  34. var result = $.PrivateBin.Helper.secondsToHuman(number);
  35. return Array.isArray(result) &&
  36. result.length === 2 &&
  37. result[0] === parseInt(result[0], 10) &&
  38. typeof result[1] === 'string';
  39. });
  40. jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
  41. return $.PrivateBin.Helper.secondsToHuman(number)[0] === number;
  42. });
  43. jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
  44. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'second';
  45. });
  46. jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
  47. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  48. });
  49. jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
  50. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'minute';
  51. });
  52. jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
  53. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  54. });
  55. jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
  56. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'hour';
  57. });
  58. jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
  59. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  60. });
  61. jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
  62. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'day';
  63. });
  64. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  65. jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
  66. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  67. });
  68. jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
  69. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'month';
  70. });
  71. });
  72. // this test is not yet meaningful using jsdom, as it does not contain getSelection support.
  73. // TODO: This needs to be tested using a browser.
  74. describe('selectText', function () {
  75. this.timeout(30000);
  76. jsc.property(
  77. 'selection contains content of given ID',
  78. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  79. 'nearray string',
  80. function (ids, contents) {
  81. var html = '',
  82. result = true;
  83. ids.forEach(function(item, i) {
  84. html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  85. });
  86. var clean = jsdom(html);
  87. ids.forEach(function(item, i) {
  88. $.PrivateBin.Helper.selectText(item.join(''));
  89. // TODO: As per https://github.com/tmpvar/jsdom/issues/321 there is no getSelection in jsdom, yet.
  90. // Once there is one, uncomment the line below to actually check the result.
  91. //result *= (contents[i] || contents[0]) === window.getSelection().toString();
  92. });
  93. clean();
  94. return Boolean(result);
  95. }
  96. );
  97. });
  98. describe('setElementText', function () {
  99. after(function () {
  100. cleanup();
  101. });
  102. jsc.property(
  103. 'replaces the content of an element',
  104. jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
  105. 'nearray string',
  106. 'string',
  107. function (ids, contents, replacingContent) {
  108. var html = '',
  109. result = true;
  110. ids.forEach(function(item, i) {
  111. html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
  112. });
  113. var elements = $('<body />').html(html);
  114. ids.forEach(function(item, i) {
  115. var id = item.join(''),
  116. element = elements.find('#' + id).first();
  117. $.PrivateBin.Helper.setElementText(element, replacingContent);
  118. result *= replacingContent === element.text();
  119. });
  120. return Boolean(result);
  121. }
  122. );
  123. });
  124. describe('urls2links', function () {
  125. after(function () {
  126. cleanup();
  127. });
  128. jsc.property(
  129. 'ignores non-URL content',
  130. 'string',
  131. function (content) {
  132. var element = $('<div>' + content + '</div>'),
  133. before = element.html();
  134. $.PrivateBin.Helper.urls2links(element);
  135. return before === element.html();
  136. }
  137. );
  138. jsc.property(
  139. 'replaces URLs with anchors',
  140. 'string',
  141. jsc.elements(['http', 'https', 'ftp']),
  142. jsc.nearray(jsc.elements(a2zString)),
  143. jsc.array(jsc.elements(queryString)),
  144. jsc.array(jsc.elements(queryString)),
  145. 'string',
  146. function (prefix, schema, address, query, fragment, postfix) {
  147. var query = query.join(''),
  148. fragment = fragment.join(''),
  149. url = schema + '://' + address.join('') + '/?' + query + '#' + fragment,
  150. prefix = $.PrivateBin.Helper.htmlEntities(prefix),
  151. postfix = ' ' + $.PrivateBin.Helper.htmlEntities(postfix),
  152. element = $('<div>' + prefix + url + postfix + '</div>');
  153. // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. &#0 or &#x
  154. if (
  155. query.slice(-1) === '&' &&
  156. (parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' )
  157. )
  158. {
  159. url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1);
  160. postfix = '';
  161. element = $('<div>' + prefix + url + '</div>');
  162. }
  163. $.PrivateBin.Helper.urls2links(element);
  164. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a>' + postfix + '</div>').html();
  165. }
  166. );
  167. jsc.property(
  168. 'replaces magnet links with anchors',
  169. 'string',
  170. jsc.array(jsc.elements(queryString)),
  171. 'string',
  172. function (prefix, query, postfix) {
  173. var url = 'magnet:?' + query.join(''),
  174. prefix = $.PrivateBin.Helper.htmlEntities(prefix),
  175. postfix = $.PrivateBin.Helper.htmlEntities(postfix),
  176. element = $('<div>' + prefix + url + ' ' + postfix + '</div>');
  177. $.PrivateBin.Helper.urls2links(element);
  178. return element.html() === $('<div>' + prefix + '<a href="' + url + '" rel="nofollow">' + url + '</a> ' + postfix + '</div>').html();
  179. }
  180. );
  181. });
  182. describe('sprintf', function () {
  183. after(function () {
  184. cleanup();
  185. });
  186. jsc.property(
  187. 'replaces %s in strings with first given parameter',
  188. 'string',
  189. '(small nearray) string',
  190. 'string',
  191. function (prefix, params, postfix) {
  192. prefix = prefix.replace(/%(s|d)/g, '%%');
  193. params[0] = params[0].replace(/%(s|d)/g, '%%');
  194. postfix = postfix.replace(/%(s|d)/g, '%%');
  195. var result = prefix + params[0] + postfix;
  196. params.unshift(prefix + '%s' + postfix);
  197. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  198. }
  199. );
  200. jsc.property(
  201. 'replaces %d in strings with first given parameter',
  202. 'string',
  203. '(small nearray) nat',
  204. 'string',
  205. function (prefix, params, postfix) {
  206. prefix = prefix.replace(/%(s|d)/g, '%%');
  207. postfix = postfix.replace(/%(s|d)/g, '%%');
  208. var result = prefix + params[0] + postfix;
  209. params.unshift(prefix + '%d' + postfix);
  210. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  211. }
  212. );
  213. jsc.property(
  214. 'replaces %d in strings with 0 if first parameter is not a number',
  215. 'string',
  216. '(small nearray) falsy',
  217. 'string',
  218. function (prefix, params, postfix) {
  219. prefix = prefix.replace(/%(s|d)/g, '%%');
  220. postfix = postfix.replace(/%(s|d)/g, '%%');
  221. var result = prefix + '0' + postfix;
  222. params.unshift(prefix + '%d' + postfix);
  223. return result === $.PrivateBin.Helper.sprintf.apply(this, params)
  224. }
  225. );
  226. jsc.property(
  227. 'replaces %d and %s in strings in order',
  228. 'string',
  229. 'nat',
  230. 'string',
  231. 'string',
  232. 'string',
  233. function (prefix, uint, middle, string, postfix) {
  234. prefix = prefix.replace(/%(s|d)/g, '%%');
  235. middle = middle.replace(/%(s|d)/g, '%%');
  236. postfix = postfix.replace(/%(s|d)/g, '%%');
  237. var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
  238. result = prefix + uint + middle + string + postfix;
  239. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  240. }
  241. );
  242. jsc.property(
  243. 'replaces %d and %s in strings in reverse order',
  244. 'string',
  245. 'nat',
  246. 'string',
  247. 'string',
  248. 'string',
  249. function (prefix, uint, middle, string, postfix) {
  250. prefix = prefix.replace(/%(s|d)/g, '%%');
  251. middle = middle.replace(/%(s|d)/g, '%%');
  252. postfix = postfix.replace(/%(s|d)/g, '%%');
  253. var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
  254. result = prefix + string + middle + uint + postfix;
  255. return result === $.PrivateBin.Helper.sprintf.apply(this, params);
  256. }
  257. );
  258. });
  259. describe('getCookie', function () {
  260. this.timeout(30000);
  261. jsc.property(
  262. 'returns the requested cookie',
  263. 'nearray asciinestring',
  264. 'nearray asciistring',
  265. function (labels, values) {
  266. var selectedKey = '', selectedValue = '',
  267. cookieArray = [],
  268. count = 0;
  269. labels.forEach(function(item, i) {
  270. // deliberatly using a non-ascii key for replacing invalid characters
  271. var key = item.replace(/[\s;,=]/g, Array(i+2).join('£')),
  272. value = (values[i] || values[0]).replace(/[\s;,=]/g, '');
  273. cookieArray.push(key + '=' + value);
  274. if (Math.random() < 1 / i || selectedKey === key)
  275. {
  276. selectedKey = key;
  277. selectedValue = value;
  278. }
  279. });
  280. var clean = jsdom('', {cookie: cookieArray}),
  281. result = $.PrivateBin.Helper.getCookie(selectedKey);
  282. clean();
  283. return result === selectedValue;
  284. }
  285. );
  286. });
  287. describe('baseUri', function () {
  288. this.timeout(30000);
  289. before(function () {
  290. $.PrivateBin.Helper.reset();
  291. });
  292. jsc.property(
  293. 'returns the URL without query & fragment',
  294. jsc.elements(schemas),
  295. jsc.nearray(jsc.elements(a2zString)),
  296. jsc.array(jsc.elements(queryString)),
  297. 'string',
  298. function (schema, address, query, fragment) {
  299. var expected = schema + '://' + address.join('') + '/',
  300. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  301. result = $.PrivateBin.Helper.baseUri();
  302. $.PrivateBin.Helper.reset();
  303. clean();
  304. return expected === result;
  305. }
  306. );
  307. });
  308. describe('htmlEntities', function () {
  309. after(function () {
  310. cleanup();
  311. });
  312. jsc.property(
  313. 'removes all HTML entities from any given string',
  314. 'string',
  315. function (string) {
  316. var result = $.PrivateBin.Helper.htmlEntities(string);
  317. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  318. }
  319. );
  320. });
  321. });
  322. describe('I18n', function () {
  323. describe('translate', function () {
  324. before(function () {
  325. $.PrivateBin.I18n.reset();
  326. });
  327. jsc.property(
  328. 'returns message ID unchanged if no translation found',
  329. 'string',
  330. function (messageId) {
  331. messageId = messageId.replace(/%(s|d)/g, '%%');
  332. var plurals = [messageId, messageId + 's'],
  333. fake = [messageId],
  334. result = $.PrivateBin.I18n.translate(messageId);
  335. $.PrivateBin.I18n.reset();
  336. var alias = $.PrivateBin.I18n._(messageId);
  337. $.PrivateBin.I18n.reset();
  338. var p_result = $.PrivateBin.I18n.translate(plurals);
  339. $.PrivateBin.I18n.reset();
  340. var p_alias = $.PrivateBin.I18n._(plurals);
  341. $.PrivateBin.I18n.reset();
  342. var f_result = $.PrivateBin.I18n.translate(fake);
  343. $.PrivateBin.I18n.reset();
  344. var f_alias = $.PrivateBin.I18n._(fake);
  345. $.PrivateBin.I18n.reset();
  346. return messageId === result && messageId === alias &&
  347. messageId === p_result && messageId === p_alias &&
  348. messageId === f_result && messageId === f_alias;
  349. }
  350. );
  351. jsc.property(
  352. 'replaces %s in strings with first given parameter',
  353. 'string',
  354. '(small nearray) string',
  355. 'string',
  356. function (prefix, params, postfix) {
  357. prefix = prefix.replace(/%(s|d)/g, '%%');
  358. params[0] = params[0].replace(/%(s|d)/g, '%%');
  359. postfix = postfix.replace(/%(s|d)/g, '%%');
  360. var translation = prefix + params[0] + postfix;
  361. params.unshift(prefix + '%s' + postfix);
  362. var result = $.PrivateBin.I18n.translate.apply(this, params);
  363. $.PrivateBin.I18n.reset();
  364. var alias = $.PrivateBin.I18n._.apply(this, params);
  365. $.PrivateBin.I18n.reset();
  366. return translation === result && translation === alias;
  367. }
  368. );
  369. });
  370. describe('getPluralForm', function () {
  371. before(function () {
  372. $.PrivateBin.I18n.reset();
  373. });
  374. jsc.property(
  375. 'returns valid key for plural form',
  376. jsc.elements(supportedLanguages),
  377. 'integer',
  378. function(language, n) {
  379. $.PrivateBin.I18n.reset(language);
  380. var result = $.PrivateBin.I18n.getPluralForm(n);
  381. // arabic seems to have the highest plural count with 6 forms
  382. return result >= 0 && result <= 5;
  383. }
  384. );
  385. });
  386. // loading of JSON via AJAX needs to be tested in the browser, this just mocks it
  387. // TODO: This needs to be tested using a browser.
  388. describe('loadTranslations', function () {
  389. this.timeout(30000);
  390. before(function () {
  391. $.PrivateBin.I18n.reset();
  392. });
  393. jsc.property(
  394. 'downloads and handles any supported language',
  395. jsc.elements(supportedLanguages),
  396. function(language) {
  397. var clean = jsdom('', {url: 'https://privatebin.net/', cookie: ['lang=' + language]});
  398. $.PrivateBin.I18n.reset('en');
  399. $.PrivateBin.I18n.loadTranslations();
  400. $.PrivateBin.I18n.reset(language, require('../i18n/' + language + '.json'));
  401. var result = $.PrivateBin.I18n.translate('en'),
  402. alias = $.PrivateBin.I18n._('en');
  403. clean();
  404. return language === result && language === alias;
  405. }
  406. );
  407. });
  408. });
  409. describe('CryptTool', function () {
  410. describe('cipher & decipher', function () {
  411. this.timeout(30000);
  412. it('can en- and decrypt any message', function () {
  413. jsc.check(jsc.forall(
  414. 'string',
  415. 'string',
  416. 'string',
  417. function (key, password, message) {
  418. return message === $.PrivateBin.CryptTool.decipher(
  419. key,
  420. password,
  421. $.PrivateBin.CryptTool.cipher(key, password, message)
  422. );
  423. }
  424. ),
  425. // reducing amount of checks as running 100 takes about 5 minutes
  426. {tests: 5, quiet: true});
  427. });
  428. // The below static unit tests are included to ensure deciphering of "classic"
  429. // SJCL based pastes still works
  430. it(
  431. 'supports PrivateBin v1 ciphertext (SJCL & Base64 2.1.9)',
  432. function () {
  433. // Of course you can easily decipher the following texts, if you like.
  434. // Bonus points for finding their sources and hidden meanings.
  435. var paste1 = $.PrivateBin.CryptTool.decipher(
  436. '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
  437. // -- "That's amazing. I've got the same combination on my luggage."
  438. Array.apply(0, Array(6)).map(function(_,b) { return b + 1; }).join(''),
  439. '{"iv":"4HNFIl7eYbCh6HuShctTIA==","v":1,"iter":10000,"ks":256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","salt":"u0lQvePq6L0=","ct":"fGPUVrDyaVr1ZDGb+kqQ3CPEW8x4YKGfzHDmA0Vjkh250aWNe7Cnigkps9aaFVMX9AaerrTp3yZbojJtNqVGMfLdUTu+53xmZHqRKxCCqSfDNSNoW4Oxk5OVgAtRyuG4bXHDsWTXDNz2xceqzVFqhkwTwlUchrV7uuFK/XUKTNjPFM744moivIcBbfM2FOeKlIFs8RYPYuvqQhp2rMLlNGwwKh//4kykQsHMQDeSDuJl8stMQzgWR/btUBZuwNZEydkMH6IPpTdf5WTSrZ+wC2OK0GutCm4UaEe6txzaTMfu+WRVu4PN6q+N+2zljWJ1XdpVcN/i0Sv4QVMym0Xa6y0eccEhj/69o47PmExmMMeEwExImPalMNT9JUSiZdOZJ/GdzwrwoIuq1mdQR6vSH+XJ/8jXJQ7bjjJVJYXTcT0Di5jixArI2Kpp1GGlGVFbLgPugwU1wczg+byqeDOAECXRRnQcogeaJtVcRwXwfy4j3ORFcblYMilxyHqKBewcYPRVBGtBs50cVjSIkAfR84rnc1nfvnxK/Gmm+4VBNHI6ODWNpRolVMCzXjbKYnV3Are5AgSpsTqaGl41VJGpcco6cAwi4K0Bys1seKR+bLSdUgqRrkEqSRSdu3/VTu9HhEk8an0rjTE4CBB5/LMn16p0TGLoOb32odKFIEtpanVvLjeyiVMvSxcgYLNnTi/5FiaAC4pJxRD+AZHedU1FICUeEXxIcac/4E5qjkHjX9SpQtLl80QLIVnjNliZm7QLB/nKu7W8Jb0+/CiTdV3Q9LhxlH4ciprnX+W0B00BKYFHnL9jRVzKdXhf1EHydbXMAfpCjHAXIVCkFakJinQBDIIw/SC6Yig0u0ddEID2B7LYAP1iE4RZwzTrxCB+ke2jQr8c20Jj6u6ShFOPC9DCw9XupZ4HAalVG00kSgjus+b8zrVji3/LKEhb4EBzp1ctBJCFTeXwej8ZETLoXTylev5dlwZSYAbuBPPcbFR/xAIPx3uDabd1E1gTqUc68ICIGhd197Mb2eRWiSvHr5SPsASerMxId6XA6+iQlRiI+NDR+TGVNmCnfxSlyPFMOHGTmslXOGIqGfBR8l4ft8YVZ70lCwmwTuViGc75ULSf9mM57/LmRzQFMYQtvI8IFK9JaQEMY5xz0HLtR4iyQUUdwR9e0ytBNdWF2a2WPDEnJuY/QJo4GzTlgv4QUxMXI5htsn2rf0HxCFu7Po8DNYLxTS+67hYjDIYWYaEIc8LXWMLyDm9C5fARPJ4F2BIWgzgzkNj+dVjusft2XnziamWdbS5u3kuRlVuz5LQj+R5imnqQAincdZTkTT1nYx+DatlOLllCYIHffpI="}'
  440. ),
  441. paste2 = $.PrivateBin.CryptTool.decipher(
  442. 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
  443. '', // no password
  444. '{"iv":"WA42mdxIVXUwBqZu7JYNiw==","v":1,"iter":10000,"ks":256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","salt":"jN6CjbQMJCM=","ct":"kYYMo5DFG1+w0UHiYXT5pdV0IUuXxzOlslkW/c3DRCbGFROCVkAskHce7HoRczee1N9c5MhHjVMJUIZE02qIS8UyHdJ/GqcPVidTUcj9rnDNWsTXkjVv8jCwHS/cwmAjDTWpwp5ThECN+ov/wNp/NdtTj8Qj7f/T3rfZIOCWfwLH9s4Des35UNcUidfPTNQ1l0Gm0X+r98CCUSYZjQxkZc6hRZBLPQ8EaNVooUwd5eP4GiYlmSDNA0wOSA+5isPYxomVCt+kFf58VBlNhpfNi7BLYAUTPpXT4SfH5drR9+C7NTeZ+tTCYjbU94PzYItOpu8vgnB1/a6BAM5h3m9w+giUb0df4hgTWeZnZxLjo5BN8WV+kdTXMj3/Vv0gw0DQrDcCuX/cBAjpy3lQGwlAN1vXoOIyZJUjMpQRrOLdKvLB+zcmVNtGDbgnfP2IYBzk9NtodpUa27ne0T0ZpwOPlVwevsIVZO224WLa+iQmmHOWDFFpVDlS0t0fLfOk7Hcb2xFsTxiCIiyKMho/IME1Du3X4e6BVa3hobSSZv0rRtNgY1KcyYPrUPW2fxZ+oik3y9SgGvb7XpjVIta8DWlDWRfZ9kzoweWEYqz9IA8Xd373RefpyuWI25zlHoX3nwljzsZU6dC//h/Dt2DNr+IAvKO3+u23cWoB9kgcZJ2FJuqjLvVfCF+OWcig7zs2pTYJW6Rg6lqbBCxiUUlae6xJrjfv0pzD2VYCLY7v1bVTagppwKzNI3WaluCOrdDYUCxUSe56yd1oAoLPRVbYvomRboUO6cjQhEknERyvt45og2kORJOEJayHW+jZgR0Y0jM3Nk17ubpij2gHxNx9kiLDOiCGSV5mn9mV7qd3HHcOMSykiBgbyzjobi96LT2dIGLeDXTIdPOog8wyobO4jWq0GGs0vBB8oSYXhHvixZLcSjX2KQuHmEoWzmJcr3DavdoXZmAurGWLKjzEdJc5dSD/eNr99gjHX7wphJ6umKMM+fn6PcbYJkhDh2GlJL5COXjXfm/5aj/vuyaRRWZMZtmnYpGAtAPg7AUG"}'
  445. );
  446. if (!paste1.includes('securely packed in iron') || !paste2.includes('Sol is right')) {
  447. throw Error('v1 (SJCL based) pastes could not be deciphered');
  448. }
  449. }
  450. );
  451. it(
  452. 'supports ZeroBin ciphertext (SJCL & Base64 1.7)',
  453. function () {
  454. var newBase64 = global.Base64;
  455. global.Base64 = require('./base64-1.7').Base64;
  456. jsdom();
  457. delete require.cache[require.resolve('./privatebin')];
  458. require('./privatebin');
  459. // Of course you can easily decipher the following texts, if you like.
  460. // Bonus points for finding their sources and hidden meanings.
  461. var paste1 = $.PrivateBin.CryptTool.decipher(
  462. '6t2qsmLyfXIokNCL+3/yl15rfTUBQvm5SOnFPvNE7Q8=',
  463. // -- "That's amazing. I've got the same combination on my luggage."
  464. Array.apply(0, Array(6)).map(function(_,b) { return b + 1; }).join(''),
  465. '{"iv":"aTnR2qBL1CAmLX8FdWe3VA==","v":1,"iter":10000,"ks":256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","salt":"u0lQvePq6L0=","ct":"A3nBTvICZtYy6xqbIJE0c8Veored5lMJUGgGUm4581wjrPFlU0Q0tUZSf+RUUoZj2jqDa4kiyyZ5YNMe30hNMV0oVSalNhRgD9svVMnPuF162IbyhVCwr7ULjT981CHxVlGNqGqmIU6L/XixgdArxAA8x1GCrfAkBWWGeq8Qw5vJPG/RCHpwR4Wy3azrluqeyERBzmaOQjO/kM35TiI6IrLYFyYyL7upYlxAaxS0XBMZvN8QU8Lnerwvh5JVC6OkkKrhogajTJIKozCF79yI78c50LUh7tTuI3Yoh7+fXxhoODvQdYFmoiUlrutN7Y5ZMRdITvVu8fTYtX9c7Fiufmcq5icEimiHp2g1bvfpOaGOsFT+XNFgC9215jcp5mpBdN852xs7bUtw+nDrf+LsDEX6iRpRZ+PYgLDN5xQT1ByEtYbeP+tO38pnx72oZdIB3cj8UkOxnxdNiZM5YB5egn4jUj1fHot1I69WoTiUJipZ5PIATv7ScymRB+AYzjxjurQ9lVfX9QtAbEH2dhdmoUo3IDRSXpWNCe9RC1aUIyWfZO7oI7FEohNscHNTLEcT+wFnFUPByLlXmjNZ7FKeNpvUm3jTY4t4sbZH8o2dUl624PAw1INcJ6FKqWGWwoFT2j1MYC+YV/LkLTdjuWfayvwLMh27G/FfKCRbW36vqinegqpPDylsx9+3oFkEw3y5Z8+44oN91rE/4Md7JhPJeRVlFC9TNCj4dA+EVhbbQqscvSnIH2uHkMw7mNNo7xba/YT9KoPDaniqnYqb+q2pX1WNWE7dLS2wfroMAS3kh8P22DAV37AeiNoD2PcI6ZcHbRdPa+XRrRcJhSPPW7UQ0z4OvBfjdu/w390QxAxSxvZewoh49fKKB6hTsRnZb4tpHkjlww=="}'
  466. ),
  467. paste2 = $.PrivateBin.CryptTool.decipher(
  468. 's9pmKZKOBN7EVvHpTA8jjLFH3Xlz/0l8lB4+ONPACrM=',
  469. '', // no password
  470. '{"iv":"Z7lAZQbkrqGMvruxoSm6Pw==","v":1,"iter":10000,"ks":256,"ts":128,"mode":"gcm","adata":"","cipher":"aes","salt":"jN6CjbQMJCM=","ct":"PuOPWB3i2FPcreSrLYeQf84LdE8RHjsc+MGtiOr4b7doNyWKYtkNorbRadxaPnEee2/Utrp1MIIfY5juJSy8RGwEPX5ciWcYe6EzsXWznsnvhmpKNj9B7eIIrfSbxfy8E2e/g7xav1nive+ljToka3WT1DZ8ILQd/NbnJeHWaoSEOfvz8+d8QJPb1tNZvs7zEY95DumQwbyOsIMKAvcZHJ9OJNpujXzdMyt6DpcFcqlldWBZ/8q5rAUTw0HNx/rCgbhAxRYfNoTLIcMM4L0cXbPSgCjwf5FuO3EdE13mgEDhcClW79m0QvcnIh8xgzYoxLbp0+AwvC/MbZM8savN/0ieWr2EKkZ04ggiOIEyvfCUuNprQBYO+y8kKduNEN6by0Yf4LRCPfmwN+GezDLuzTnZIMhPbGqUAdgV6ExqK2ULEEIrQEMoOuQIxfoMhqLlzG79vXGt2O+BY+4IiYfvmuRLks4UXfyHqxPXTJg48IYbGs0j4TtJPUgp3523EyYLwEGyVTAuWhYAmVIwd/hoV7d7tmfcF73w9dufDFI3LNca2KxzBnWNPYvIZKBwWbq8ncxkb191dP6mjEi7NnhqVk5A6vIBbu4AC5PZf76l6yep4xsoy/QtdDxCMocCXeAML9MQ9uPQbuspOKrBvMfN5igA1kBqasnxI472KBNXsdZnaDddSVUuvhTcETM="}'
  471. );
  472. global.Base64 = newBase64;
  473. jsdom();
  474. delete require.cache[require.resolve('./privatebin')];
  475. require('./privatebin');
  476. if (!paste1.includes('securely packed in iron') || !paste2.includes('Sol is right')) {
  477. throw Error('v1 (SJCL based) pastes could not be deciphered');
  478. }
  479. }
  480. );
  481. });
  482. describe('isEntropyReady & addEntropySeedListener', function () {
  483. it(
  484. 'lets us know that enough entropy is collected or make us wait for it',
  485. function(done) {
  486. if ($.PrivateBin.CryptTool.isEntropyReady()) {
  487. done();
  488. } else {
  489. $.PrivateBin.CryptTool.addEntropySeedListener(function() {
  490. done();
  491. });
  492. }
  493. }
  494. );
  495. });
  496. describe('getSymmetricKey', function () {
  497. var keys = [];
  498. // the parameter is used to ensure the test is run more then one time
  499. jsc.property(
  500. 'returns random, non-empty keys',
  501. 'nat',
  502. function(n) {
  503. var key = $.PrivateBin.CryptTool.getSymmetricKey(),
  504. result = (key !== '' && keys.indexOf(key) === -1);
  505. keys.push(key);
  506. return result;
  507. }
  508. );
  509. });
  510. describe('Base64.js vs SJCL.js vs abab.js', function () {
  511. jsc.property(
  512. 'these all return the same base64 string',
  513. 'string',
  514. function(string) {
  515. var base64 = Base64.toBase64(string),
  516. sjcl = global.sjcl.codec.base64.fromBits(global.sjcl.codec.utf8String.toBits(string)),
  517. abab = window.btoa(Base64.utob(string));
  518. return base64 === sjcl && sjcl === abab;
  519. }
  520. );
  521. });
  522. });
  523. describe('Model', function () {
  524. describe('getExpirationDefault', function () {
  525. before(function () {
  526. $.PrivateBin.Model.reset();
  527. cleanup();
  528. });
  529. jsc.property(
  530. 'returns the contents of the element with id "pasteExpiration"',
  531. 'array asciinestring',
  532. 'string',
  533. 'small nat',
  534. function (keys, value, key) {
  535. keys = keys.map($.PrivateBin.Helper.htmlEntities);
  536. value = $.PrivateBin.Helper.htmlEntities(value);
  537. var content = keys.length > key ? keys[key] : (keys.length > 0 ? keys[0] : 'null'),
  538. contents = '<select id="pasteExpiration" name="pasteExpiration">';
  539. keys.forEach(function(item) {
  540. contents += '<option value="' + item + '"';
  541. if (item === content) {
  542. contents += ' selected="selected"';
  543. }
  544. contents += '>' + value + '</option>';
  545. });
  546. contents += '</select>';
  547. $('body').html(contents);
  548. var result = $.PrivateBin.Helper.htmlEntities(
  549. $.PrivateBin.Model.getExpirationDefault()
  550. );
  551. $.PrivateBin.Model.reset();
  552. return content === result;
  553. }
  554. );
  555. });
  556. describe('getFormatDefault', function () {
  557. before(function () {
  558. $.PrivateBin.Model.reset();
  559. cleanup();
  560. });
  561. jsc.property(
  562. 'returns the contents of the element with id "pasteFormatter"',
  563. 'array asciinestring',
  564. 'string',
  565. 'small nat',
  566. function (keys, value, key) {
  567. keys = keys.map($.PrivateBin.Helper.htmlEntities);
  568. value = $.PrivateBin.Helper.htmlEntities(value);
  569. var content = keys.length > key ? keys[key] : (keys.length > 0 ? keys[0] : 'null'),
  570. contents = '<select id="pasteFormatter" name="pasteFormatter">';
  571. keys.forEach(function(item) {
  572. contents += '<option value="' + item + '"';
  573. if (item === content) {
  574. contents += ' selected="selected"';
  575. }
  576. contents += '>' + value + '</option>';
  577. });
  578. contents += '</select>';
  579. $('body').html(contents);
  580. var result = $.PrivateBin.Helper.htmlEntities(
  581. $.PrivateBin.Model.getFormatDefault()
  582. );
  583. $.PrivateBin.Model.reset();
  584. return content === result;
  585. }
  586. );
  587. });
  588. describe('hasCipherData', function () {
  589. before(function () {
  590. $.PrivateBin.Model.reset();
  591. cleanup();
  592. });
  593. jsc.property(
  594. 'checks if the element with id "cipherdata" contains any data',
  595. 'asciistring',
  596. function (value) {
  597. value = $.PrivateBin.Helper.htmlEntities(value).trim();
  598. $('body').html('<div id="cipherdata">' + value + '</div>');
  599. $.PrivateBin.Model.init();
  600. var result = $.PrivateBin.Model.hasCipherData();
  601. $.PrivateBin.Model.reset();
  602. return (value.length > 0) === result;
  603. }
  604. );
  605. });
  606. describe('getCipherData', function () {
  607. before(function () {
  608. $.PrivateBin.Model.reset();
  609. cleanup();
  610. });
  611. jsc.property(
  612. 'returns the contents of the element with id "cipherdata"',
  613. 'asciistring',
  614. function (value) {
  615. value = $.PrivateBin.Helper.htmlEntities(value).trim();
  616. $('body').html('<div id="cipherdata">' + value + '</div>');
  617. $.PrivateBin.Model.init();
  618. var result = $.PrivateBin.Helper.htmlEntities(
  619. $.PrivateBin.Model.getCipherData()
  620. );
  621. $.PrivateBin.Model.reset();
  622. return value === result;
  623. }
  624. );
  625. });
  626. describe('getPasteId', function () {
  627. this.timeout(30000);
  628. before(function () {
  629. $.PrivateBin.Model.reset();
  630. cleanup();
  631. });
  632. jsc.property(
  633. 'returns the query string without separator, if any',
  634. jsc.nearray(jsc.elements(a2zString)),
  635. jsc.nearray(jsc.elements(a2zString)),
  636. jsc.nearray(jsc.elements(queryString)),
  637. 'string',
  638. function (schema, address, query, fragment) {
  639. var queryString = query.join(''),
  640. clean = jsdom('', {
  641. url: schema.join('') + '://' + address.join('') +
  642. '/?' + queryString + '#' + fragment
  643. }),
  644. result = $.PrivateBin.Model.getPasteId();
  645. $.PrivateBin.Model.reset();
  646. clean();
  647. return queryString === result;
  648. }
  649. );
  650. jsc.property(
  651. 'throws exception on empty query string',
  652. jsc.nearray(jsc.elements(a2zString)),
  653. jsc.nearray(jsc.elements(a2zString)),
  654. 'string',
  655. function (schema, address, fragment) {
  656. var clean = jsdom('', {
  657. url: schema.join('') + '://' + address.join('') +
  658. '/#' + fragment
  659. }),
  660. result = false;
  661. try {
  662. $.PrivateBin.Model.getPasteId();
  663. }
  664. catch(err) {
  665. result = true;
  666. }
  667. $.PrivateBin.Model.reset();
  668. clean();
  669. return result;
  670. }
  671. );
  672. });
  673. describe('getPasteKey', function () {
  674. this.timeout(30000);
  675. jsc.property(
  676. 'returns the fragment of the URL',
  677. jsc.nearray(jsc.elements(a2zString)),
  678. jsc.nearray(jsc.elements(a2zString)),
  679. jsc.array(jsc.elements(queryString)),
  680. jsc.nearray(jsc.elements(base64String)),
  681. function (schema, address, query, fragment) {
  682. var fragmentString = fragment.join(''),
  683. clean = jsdom('', {
  684. url: schema.join('') + '://' + address.join('') +
  685. '/?' + query.join('') + '#' + fragmentString
  686. }),
  687. result = $.PrivateBin.Model.getPasteKey();
  688. $.PrivateBin.Model.reset();
  689. clean();
  690. return fragmentString === result;
  691. }
  692. );
  693. jsc.property(
  694. 'returns the fragment stripped of trailing query parts',
  695. jsc.nearray(jsc.elements(a2zString)),
  696. jsc.nearray(jsc.elements(a2zString)),
  697. jsc.array(jsc.elements(queryString)),
  698. jsc.nearray(jsc.elements(base64String)),
  699. jsc.array(jsc.elements(queryString)),
  700. function (schema, address, query, fragment, trail) {
  701. var fragmentString = fragment.join(''),
  702. clean = jsdom('', {
  703. url: schema.join('') + '://' + address.join('') + '/?' +
  704. query.join('') + '#' + fragmentString + '&' + trail.join('')
  705. }),
  706. result = $.PrivateBin.Model.getPasteKey();
  707. $.PrivateBin.Model.reset();
  708. clean();
  709. return fragmentString === result;
  710. }
  711. );
  712. jsc.property(
  713. 'throws exception on empty fragment of the URL',
  714. jsc.nearray(jsc.elements(a2zString)),
  715. jsc.nearray(jsc.elements(a2zString)),
  716. jsc.array(jsc.elements(queryString)),
  717. function (schema, address, query) {
  718. var clean = jsdom('', {
  719. url: schema.join('') + '://' + address.join('') +
  720. '/?' + query.join('')
  721. }),
  722. result = false;
  723. try {
  724. $.PrivateBin.Model.getPasteKey();
  725. }
  726. catch(err) {
  727. result = true;
  728. }
  729. $.PrivateBin.Model.reset();
  730. clean();
  731. return result;
  732. }
  733. );
  734. });
  735. describe('getTemplate', function () {
  736. before(function () {
  737. $.PrivateBin.Model.reset();
  738. cleanup();
  739. });
  740. jsc.property(
  741. 'returns the contents of the element with id "[name]template"',
  742. jsc.nearray(jsc.elements(alnumString)),
  743. jsc.nearray(jsc.elements(a2zString)),
  744. jsc.nearray(jsc.elements(alnumString)),
  745. function (id, element, value) {
  746. id = id.join('');
  747. element = element.join('');
  748. value = value.join('').trim();
  749. // <br>, <hr> and <wbr> tags can't contain strings, table tags can't be alone, so test with a <p> instead
  750. if (['br', 'hr', 'tr', 'td', 'th', 'wbr'].indexOf(element) >= 0) {
  751. element = 'p';
  752. }
  753. $('body').html(
  754. '<div id="templates"><' + element + ' id="' + id +
  755. 'template">' + value + '</' + element + '></div>'
  756. );
  757. $.PrivateBin.Model.init();
  758. var template = '<' + element + ' id="' + id + '">' + value +
  759. '</' + element + '>',
  760. result = $.PrivateBin.Model.getTemplate(id).wrap('<p/>').parent().html();
  761. $.PrivateBin.Model.reset();
  762. return template === result;
  763. }
  764. );
  765. });
  766. });
  767. describe('UiHelper', function () {
  768. // TODO: As per https://github.com/tmpvar/jsdom/issues/1565 there is no navigation support in jsdom, yet.
  769. // for now we use a mock function to trigger the event
  770. describe('historyChange', function () {
  771. this.timeout(30000);
  772. before(function () {
  773. $.PrivateBin.Helper.reset();
  774. });
  775. jsc.property(
  776. 'redirects to home, when the state is null',
  777. jsc.elements(schemas),
  778. jsc.nearray(jsc.elements(a2zString)),
  779. function (schema, address) {
  780. var expected = schema + '://' + address.join('') + '/',
  781. clean = jsdom('', {url: expected});
  782. // make window.location.href writable
  783. Object.defineProperty(window.location, 'href', {
  784. writable: true,
  785. value: window.location.href
  786. });
  787. $.PrivateBin.UiHelper.mockHistoryChange();
  788. $.PrivateBin.Helper.reset();
  789. var result = window.location.href;
  790. clean();
  791. return expected === result;
  792. }
  793. );
  794. jsc.property(
  795. 'does not redirect to home, when a new paste is created',
  796. jsc.elements(schemas),
  797. jsc.nearray(jsc.elements(a2zString)),
  798. jsc.array(jsc.elements(queryString)),
  799. jsc.nearray(jsc.elements(base64String)),
  800. function (schema, address, query, fragment) {
  801. var expected = schema + '://' + address.join('') + '/' + '?' +
  802. query.join('') + '#' + fragment.join(''),
  803. clean = jsdom('', {url: expected});
  804. // make window.location.href writable
  805. Object.defineProperty(window.location, 'href', {
  806. writable: true,
  807. value: window.location.href
  808. });
  809. $.PrivateBin.UiHelper.mockHistoryChange([
  810. {type: 'newpaste'}, '', expected
  811. ]);
  812. $.PrivateBin.Helper.reset();
  813. var result = window.location.href;
  814. clean();
  815. return expected === result;
  816. }
  817. );
  818. });
  819. describe('reloadHome', function () {
  820. this.timeout(30000);
  821. before(function () {
  822. $.PrivateBin.Helper.reset();
  823. });
  824. jsc.property(
  825. 'redirects to home',
  826. jsc.elements(schemas),
  827. jsc.nearray(jsc.elements(a2zString)),
  828. jsc.array(jsc.elements(queryString)),
  829. jsc.nearray(jsc.elements(base64String)),
  830. function (schema, address, query, fragment) {
  831. var expected = schema + '://' + address.join('') + '/',
  832. clean = jsdom('', {
  833. url: expected + '?' + query.join('') + '#' + fragment.join('')
  834. });
  835. // make window.location.href writable
  836. Object.defineProperty(window.location, 'href', {
  837. writable: true,
  838. value: window.location.href
  839. });
  840. $.PrivateBin.UiHelper.reloadHome();
  841. $.PrivateBin.Helper.reset();
  842. var result = window.location.href;
  843. clean();
  844. return expected === result;
  845. }
  846. );
  847. });
  848. describe('isVisible', function () {
  849. // TODO As per https://github.com/tmpvar/jsdom/issues/1048 there is no layout support in jsdom, yet.
  850. // once it is supported or a workaround is found, uncomment the section below
  851. /*
  852. before(function () {
  853. $.PrivateBin.Helper.reset();
  854. });
  855. jsc.property(
  856. 'detect visible elements',
  857. jsc.nearray(jsc.elements(alnumString)),
  858. jsc.nearray(jsc.elements(a2zString)),
  859. function (id, element) {
  860. id = id.join('');
  861. element = element.join('');
  862. var clean = jsdom(
  863. '<' + element + ' id="' + id + '"></' + element + '>'
  864. );
  865. var result = $.PrivateBin.UiHelper.isVisible($('#' + id));
  866. clean();
  867. return result;
  868. }
  869. );
  870. */
  871. });
  872. describe('scrollTo', function () {
  873. // TODO Did not find a way to test that, see isVisible test above
  874. });
  875. });
  876. describe('Alert', function () {
  877. describe('showStatus', function () {
  878. before(function () {
  879. cleanup();
  880. });
  881. jsc.property(
  882. 'shows a status message',
  883. jsc.array(jsc.elements(alnumString)),
  884. jsc.array(jsc.elements(alnumString)),
  885. function (icon, message) {
  886. icon = icon.join('');
  887. message = message.join('');
  888. var expected = '<div id="status" role="alert" ' +
  889. 'class="statusmessage alert alert-info"><span ' +
  890. 'class="glyphicon glyphicon-' + icon +
  891. '" aria-hidden="true"></span> ' + message + '</div>';
  892. $('body').html(
  893. '<div id="status" role="alert" class="statusmessage ' +
  894. 'alert alert-info hidden"><span class="glyphicon ' +
  895. 'glyphicon-info-sign" aria-hidden="true"></span> </div>'
  896. );
  897. $.PrivateBin.Alert.init();
  898. $.PrivateBin.Alert.showStatus(message, icon);
  899. var result = $('body').html();
  900. return expected === result;
  901. }
  902. );
  903. });
  904. describe('showError', function () {
  905. before(function () {
  906. cleanup();
  907. });
  908. jsc.property(
  909. 'shows an error message',
  910. jsc.array(jsc.elements(alnumString)),
  911. jsc.array(jsc.elements(alnumString)),
  912. function (icon, message) {
  913. icon = icon.join('');
  914. message = message.join('');
  915. var expected = '<div id="errormessage" role="alert" ' +
  916. 'class="statusmessage alert alert-danger"><span ' +
  917. 'class="glyphicon glyphicon-' + icon +
  918. '" aria-hidden="true"></span> ' + message + '</div>';
  919. $('body').html(
  920. '<div id="errormessage" role="alert" class="statusmessage ' +
  921. 'alert alert-danger hidden"><span class="glyphicon ' +
  922. 'glyphicon-alert" aria-hidden="true"></span> </div>'
  923. );
  924. $.PrivateBin.Alert.init();
  925. $.PrivateBin.Alert.showError(message, icon);
  926. var result = $('body').html();
  927. return expected === result;
  928. }
  929. );
  930. });
  931. describe('showRemaining', function () {
  932. before(function () {
  933. cleanup();
  934. });
  935. jsc.property(
  936. 'shows remaining time',
  937. jsc.array(jsc.elements(alnumString)),
  938. jsc.array(jsc.elements(alnumString)),
  939. 'integer',
  940. function (message, string, number) {
  941. message = message.join('');
  942. string = string.join('');
  943. var expected = '<div id="remainingtime" role="alert" ' +
  944. 'class="alert alert-info"><span ' +
  945. 'class="glyphicon glyphicon-fire" aria-hidden="true">' +
  946. '</span> ' + string + message + number + '</div>';
  947. $('body').html(
  948. '<div id="remainingtime" role="alert" class="hidden ' +
  949. 'alert alert-info"><span class="glyphicon ' +
  950. 'glyphicon-fire" aria-hidden="true"></span> </div>'
  951. );
  952. $.PrivateBin.Alert.init();
  953. $.PrivateBin.Alert.showRemaining(['%s' + message + '%d', string, number]);
  954. var result = $('body').html();
  955. return expected === result;
  956. }
  957. );
  958. });
  959. describe('showLoading', function () {
  960. before(function () {
  961. cleanup();
  962. });
  963. jsc.property(
  964. 'shows a loading message',
  965. jsc.array(jsc.elements(alnumString)),
  966. jsc.array(jsc.elements(alnumString)),
  967. 'integer',
  968. function (icon, message, number) {
  969. icon = icon.join('');
  970. message = message.join('');
  971. var default_message = 'Loading…';
  972. if (message.length == 0) {
  973. message = default_message;
  974. }
  975. var expected = '<ul class="nav navbar-nav"><li ' +
  976. 'id="loadingindicator" class="navbar-text"><span ' +
  977. 'class="glyphicon glyphicon-' + icon +
  978. '" aria-hidden="true"></span> ' + message + '</li></ul>';
  979. $('body').html(
  980. '<ul class="nav navbar-nav"><li id="loadingindicator" ' +
  981. 'class="navbar-text hidden"><span class="glyphicon ' +
  982. 'glyphicon-time" aria-hidden="true"></span> ' +
  983. default_message + '</li></ul>'
  984. );
  985. $.PrivateBin.Alert.init();
  986. $.PrivateBin.Alert.showLoading(message, number, icon);
  987. var result = $('body').html();
  988. return expected === result;
  989. }
  990. );
  991. });
  992. describe('hideLoading', function () {
  993. before(function () {
  994. cleanup();
  995. });
  996. it(
  997. 'hides the loading message',
  998. function() {
  999. $('body').html(
  1000. '<ul class="nav navbar-nav"><li id="loadingindicator" ' +
  1001. 'class="navbar-text"><span class="glyphicon ' +
  1002. 'glyphicon-time" aria-hidden="true"></span> ' +
  1003. 'Loading…</li></ul>'
  1004. );
  1005. $('body').addClass('loading');
  1006. $.PrivateBin.Alert.init();
  1007. $.PrivateBin.Alert.hideLoading();
  1008. return !$('body').hasClass('loading') &&
  1009. $('#loadingindicator').hasClass('hidden');
  1010. }
  1011. );
  1012. });
  1013. describe('hideMessages', function () {
  1014. before(function () {
  1015. cleanup();
  1016. });
  1017. it(
  1018. 'hides all messages',
  1019. function() {
  1020. $('body').html(
  1021. '<div id="status" role="alert" class="statusmessage ' +
  1022. 'alert alert-info"><span class="glyphicon ' +
  1023. 'glyphicon-info-sign" aria-hidden="true"></span> </div>' +
  1024. '<div id="errormessage" role="alert" class="statusmessage ' +
  1025. 'alert alert-danger"><span class="glyphicon ' +
  1026. 'glyphicon-alert" aria-hidden="true"></span> </div>'
  1027. );
  1028. $.PrivateBin.Alert.init();
  1029. $.PrivateBin.Alert.hideMessages();
  1030. return $('#statusmessage').hasClass('hidden') &&
  1031. $('#errormessage').hasClass('hidden');
  1032. }
  1033. );
  1034. });
  1035. describe('setCustomHandler', function () {
  1036. before(function () {
  1037. cleanup();
  1038. });
  1039. jsc.property(
  1040. 'calls a given handler function',
  1041. 'nat 3',
  1042. jsc.array(jsc.elements(alnumString)),
  1043. function (trigger, message) {
  1044. message = message.join('');
  1045. var handlerCalled = false,
  1046. default_message = 'Loading…',
  1047. functions = [
  1048. $.PrivateBin.Alert.showStatus,
  1049. $.PrivateBin.Alert.showError,
  1050. $.PrivateBin.Alert.showRemaining,
  1051. $.PrivateBin.Alert.showLoading
  1052. ];
  1053. if (message.length == 0) {
  1054. message = default_message;
  1055. }
  1056. $('body').html(
  1057. '<ul class="nav navbar-nav"><li id="loadingindicator" ' +
  1058. 'class="navbar-text hidden"><span class="glyphicon ' +
  1059. 'glyphicon-time" aria-hidden="true"></span> ' +
  1060. default_message + '</li></ul>' +
  1061. '<div id="remainingtime" role="alert" class="hidden ' +
  1062. 'alert alert-info"><span class="glyphicon ' +
  1063. 'glyphicon-fire" aria-hidden="true"></span> </div>' +
  1064. '<div id="status" role="alert" class="statusmessage ' +
  1065. 'alert alert-info"><span class="glyphicon ' +
  1066. 'glyphicon-info-sign" aria-hidden="true"></span> </div>' +
  1067. '<div id="errormessage" role="alert" class="statusmessage ' +
  1068. 'alert alert-danger"><span class="glyphicon ' +
  1069. 'glyphicon-alert" aria-hidden="true"></span> </div>'
  1070. );
  1071. $.PrivateBin.Alert.init();
  1072. $.PrivateBin.Alert.setCustomHandler(function(id, $element) {
  1073. handlerCalled = true;
  1074. return jsc.random(0, 1) ? true : $element;
  1075. });
  1076. functions[trigger](message);
  1077. return handlerCalled;
  1078. }
  1079. );
  1080. });
  1081. });