PasteStatus.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. 'use strict';
  2. var common = require('../common');
  3. function urlStrings(schema, longUrl, shortUrl) {
  4. longUrl.schema = schema;
  5. shortUrl.schema = schema;
  6. let longUrlString = common.urlToString(longUrl),
  7. shortUrlString = common.urlToString(shortUrl);
  8. // ensure the two random URLs actually are sorted as expected
  9. if (longUrlString.length <= shortUrlString.length) {
  10. if (longUrlString.length === shortUrlString.length) {
  11. longUrl.address.unshift('a');
  12. longUrlString = common.urlToString(longUrl);
  13. } else {
  14. [longUrlString, shortUrlString] = [shortUrlString, longUrlString];
  15. }
  16. }
  17. return [longUrlString, shortUrlString];
  18. }
  19. describe('PasteStatus', function () {
  20. describe('createPasteNotification', function () {
  21. this.timeout(30000);
  22. jsc.property(
  23. 'creates a notification after a successful document upload',
  24. common.jscUrl(),
  25. common.jscUrl(false),
  26. function (url1, url2) {
  27. // sometimes the generator returns incomplete objects, bail out
  28. if (!url1 || !url1.address || !url2 || !url2.address) {
  29. return true;
  30. }
  31. const expected1 = common.urlToString(url1).replace(/&(gt|lt)$/, '&$1a'),
  32. expected2 = common.urlToString(url2).replace(/&(gt|lt)$/, '&$1a'),
  33. clean = jsdom();
  34. $('body').html('<a href="#" id="deletelink"><span></span></a><div id="pastelink"></div>');
  35. PrivateBin.PasteStatus.init();
  36. PrivateBin.PasteStatus.createPasteNotification(expected1, expected2);
  37. const result1 = $('#pasteurl')[0].href,
  38. result2 = $('#deletelink')[0].href;
  39. clean();
  40. return result1 === expected1 && result2 === expected2;
  41. }
  42. );
  43. });
  44. describe('extractUrl', function () {
  45. this.timeout(30000);
  46. jsc.property(
  47. 'extracts and updates IDN URLs found in given response',
  48. common.jscSchemas(false),
  49. 'nestring',
  50. common.jscUrl(),
  51. function (schema, domain, url) {
  52. domain = domain.replace(/\P{Letter}|[\u{AA}-\u{BA}]/gu, '').toLowerCase();
  53. if (domain.length === 0) {
  54. domain = 'a';
  55. }
  56. url.schema = schema;
  57. url.address.unshift('.');
  58. url.address = domain.split('').concat(url.address);
  59. const urlString = common.urlToString(url),
  60. expected = urlString.substring((schema + '://' + domain).length),
  61. clean = jsdom();
  62. $('body').html('<div><div id="pastelink"></div></div>');
  63. PrivateBin.PasteStatus.init();
  64. PrivateBin.PasteStatus.createPasteNotification('', '');
  65. PrivateBin.PasteStatus.extractUrl(urlString);
  66. const result = $('#pasteurl')[0].href;
  67. clean();
  68. return result.endsWith(expected) && (
  69. result.startsWith(schema + '://xn--') ||
  70. result.startsWith(schema + '://' + domain)
  71. );
  72. }
  73. );
  74. // YOURLS API samples from: https://yourls.org/readme.html#API;apireturn
  75. jsc.property(
  76. 'extracts and updates URLs found in YOURLS API JSON response',
  77. common.jscSchemas(false),
  78. common.jscUrl(),
  79. common.jscUrl(false),
  80. function (schema, longUrl, shortUrl) {
  81. const [longUrlString, shortUrlString] = urlStrings(schema, longUrl, shortUrl),
  82. yourlsResponse = {
  83. url: {
  84. keyword: longUrl.address.join(''),
  85. url: longUrlString,
  86. title: 'example title',
  87. date: '2014-10-24 16:01:39',
  88. ip: '127.0.0.1'
  89. },
  90. status: 'success',
  91. message: longUrlString + ' added to database',
  92. title: 'example title',
  93. shorturl: shortUrlString,
  94. statusCode: 200
  95. },
  96. clean = jsdom();
  97. $('body').html('<div><div id="pastelink"></div></div>');
  98. PrivateBin.PasteStatus.init();
  99. PrivateBin.PasteStatus.createPasteNotification('', '');
  100. PrivateBin.PasteStatus.extractUrl(JSON.stringify(yourlsResponse, undefined, 4));
  101. const result = $('#pasteurl')[0].href;
  102. clean();
  103. return result === shortUrlString;
  104. }
  105. );
  106. jsc.property(
  107. 'extracts and updates URLs found in YOURLS API XML response',
  108. common.jscSchemas(false),
  109. common.jscUrl(),
  110. common.jscUrl(false),
  111. function (schema, longUrl, shortUrl) {
  112. const [longUrlString, shortUrlString] = urlStrings(schema, longUrl, shortUrl),
  113. yourlsResponse = '<result>\n' +
  114. ' <keyword>' + longUrl.address.join('') + '</keyword>\n' +
  115. ' <shorturl>' + shortUrlString + '</shorturl>\n' +
  116. ' <longurl>' + longUrlString + '</longurl>\n' +
  117. ' <message>success</message>\n' +
  118. ' <statusCode>200</statusCode>\n' +
  119. '</result>',
  120. clean = jsdom();
  121. $('body').html('<div><div id="pastelink"></div></div>');
  122. PrivateBin.PasteStatus.init();
  123. PrivateBin.PasteStatus.createPasteNotification('', '');
  124. PrivateBin.PasteStatus.extractUrl(yourlsResponse);
  125. const result = $('#pasteurl')[0].href;
  126. clean();
  127. return result === shortUrlString;
  128. }
  129. );
  130. jsc.property(
  131. 'extracts and updates URLs found in YOURLS proxy HTML response',
  132. common.jscSchemas(false),
  133. common.jscUrl(),
  134. common.jscUrl(false),
  135. function (schema, longUrl, shortUrl) {
  136. const [_, shortUrlString] = urlStrings(schema, longUrl, shortUrl),
  137. yourlsResponse = '<!DOCTYPE html>\n' +
  138. '<html lang="en">\n' +
  139. '\t<head>\n' +
  140. '\t\t<meta charset="utf-8" />\n' +
  141. '\t\t<meta name="robots" content="noindex" />\n' +
  142. '\t\t<meta name="google" content="notranslate">\n' +
  143. '\t\t<title>PrivateBin</title>\n' +
  144. '\t</head>\n' +
  145. '\t<body>\n' +
  146. '\t\t<p>Your document is <a id="pasteurl" href="' + shortUrlString + '">' + shortUrlString + '</a> <span id="copyhint">(Hit <kbd>Ctrl</kbd>+<kbd>c</kbd> to copy)</span></p>\n' +
  147. '\t</body>\n' +
  148. '</html>',
  149. clean = jsdom();
  150. $('body').html('<div><div id="pastelink"></div></div>');
  151. PrivateBin.PasteStatus.init();
  152. PrivateBin.PasteStatus.createPasteNotification('', '');
  153. PrivateBin.PasteStatus.extractUrl(yourlsResponse);
  154. const result = $('#pasteurl')[0].href;
  155. clean();
  156. return result === shortUrlString;
  157. }
  158. );
  159. });
  160. describe('showRemainingTime', function () {
  161. this.timeout(30000);
  162. jsc.property(
  163. 'shows burn after reading message or remaining time',
  164. 'bool',
  165. 'nat',
  166. common.jscUrl(),
  167. function (burnafterreading, remainingTime, url) {
  168. let clean = jsdom('', {url: common.urlToString(url)}),
  169. result;
  170. $('body').html('<div id="remainingtime" class="hidden"></div>');
  171. PrivateBin.PasteStatus.init();
  172. PrivateBin.PasteStatus.showRemainingTime(PrivateBin.Helper.PasteFactory({
  173. 'adata': [null, null, null, burnafterreading],
  174. 'v': 2,
  175. 'meta': {
  176. 'time_to_live': remainingTime
  177. }
  178. }));
  179. if (burnafterreading) {
  180. result = $('#remainingtime').hasClass('foryoureyesonly') &&
  181. !$('#remainingtime').hasClass('hidden');
  182. } else if (remainingTime) {
  183. result =!$('#remainingtime').hasClass('foryoureyesonly') &&
  184. !$('#remainingtime').hasClass('hidden');
  185. } else {
  186. result = $('#remainingtime').hasClass('hidden') &&
  187. !$('#remainingtime').hasClass('foryoureyesonly');
  188. }
  189. clean();
  190. return result;
  191. }
  192. );
  193. });
  194. describe('hideMessages', function () {
  195. it(
  196. 'hides all messages',
  197. function() {
  198. $('body').html(
  199. '<div id="remainingtime"></div><div id="pastesuccess"></div>'
  200. );
  201. PrivateBin.PasteStatus.init();
  202. PrivateBin.PasteStatus.hideMessages();
  203. assert.ok($('#remainingtime').hasClass('hidden'));
  204. assert.ok($('#pastesuccess').hasClass('hidden'));
  205. }
  206. );
  207. });
  208. });