Model.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. 'use strict';
  2. var common = require('../common');
  3. describe('Model', function () {
  4. describe('getExpirationDefault', function () {
  5. before(function () {
  6. $.PrivateBin.Model.reset();
  7. cleanup = jsdom();
  8. });
  9. jsc.property(
  10. 'returns the contents of the element with id "pasteExpiration"',
  11. 'nearray asciinestring',
  12. 'string',
  13. 'small nat',
  14. function (keys, value, key) {
  15. keys = keys.map($.PrivateBin.Helper.htmlEntities);
  16. value = $.PrivateBin.Helper.htmlEntities(value);
  17. var content = keys.length > key ? keys[key] : keys[0],
  18. contents = '<select id="pasteExpiration" name="pasteExpiration">';
  19. keys.forEach(function(item) {
  20. contents += '<option value="' + item + '"';
  21. if (item === content) {
  22. contents += ' selected="selected"';
  23. }
  24. contents += '>' + value + '</option>';
  25. });
  26. contents += '</select>';
  27. $('body').html(contents);
  28. var result = $.PrivateBin.Helper.htmlEntities(
  29. $.PrivateBin.Model.getExpirationDefault()
  30. );
  31. $.PrivateBin.Model.reset();
  32. return content === result;
  33. }
  34. );
  35. });
  36. describe('getFormatDefault', function () {
  37. before(function () {
  38. $.PrivateBin.Model.reset();
  39. });
  40. after(function () {
  41. cleanup();
  42. });
  43. jsc.property(
  44. 'returns the contents of the element with id "pasteFormatter"',
  45. 'nearray asciinestring',
  46. 'string',
  47. 'small nat',
  48. function (keys, value, key) {
  49. keys = keys.map($.PrivateBin.Helper.htmlEntities);
  50. value = $.PrivateBin.Helper.htmlEntities(value);
  51. var content = keys.length > key ? keys[key] : keys[0],
  52. contents = '<select id="pasteFormatter" name="pasteFormatter">';
  53. keys.forEach(function(item) {
  54. contents += '<option value="' + item + '"';
  55. if (item === content) {
  56. contents += ' selected="selected"';
  57. }
  58. contents += '>' + value + '</option>';
  59. });
  60. contents += '</select>';
  61. $('body').html(contents);
  62. var result = $.PrivateBin.Helper.htmlEntities(
  63. $.PrivateBin.Model.getFormatDefault()
  64. );
  65. $.PrivateBin.Model.reset();
  66. return content === result;
  67. }
  68. );
  69. });
  70. describe('getPasteId', function () {
  71. this.timeout(30000);
  72. beforeEach(function () {
  73. $.PrivateBin.Model.reset();
  74. });
  75. jsc.property(
  76. 'returns the query string without separator, if any',
  77. common.jscUrl(true, false),
  78. jsc.tuple(new Array(16).fill(common.jscHexString)),
  79. jsc.array(common.jscQueryString()),
  80. jsc.array(common.jscQueryString()),
  81. function (url, pasteId, queryStart, queryEnd) {
  82. if (queryStart.length > 0) {
  83. queryStart.push('&');
  84. }
  85. if (queryEnd.length > 0) {
  86. queryEnd.unshift('&');
  87. }
  88. url.query = queryStart.concat(pasteId, queryEnd);
  89. const pasteIdString = pasteId.join(''),
  90. clean = jsdom('', {url: common.urlToString(url)});
  91. const result = $.PrivateBin.Model.getPasteId();
  92. $.PrivateBin.Model.reset();
  93. clean();
  94. return pasteIdString === result;
  95. }
  96. );
  97. jsc.property(
  98. 'throws exception on empty query string',
  99. common.jscUrl(true, false),
  100. function (url) {
  101. let clean = jsdom('', {url: common.urlToString(url)}),
  102. result = false;
  103. try {
  104. $.PrivateBin.Model.getPasteId();
  105. }
  106. catch(err) {
  107. result = true;
  108. }
  109. $.PrivateBin.Model.reset();
  110. clean();
  111. return result;
  112. }
  113. );
  114. });
  115. describe('getPasteKey', function () {
  116. this.timeout(30000);
  117. beforeEach(function () {
  118. $.PrivateBin.Model.reset();
  119. });
  120. jsc.property(
  121. 'returns the fragment of a v1 URL',
  122. common.jscUrl(),
  123. function (url) {
  124. url.fragment = '0OIl'; // any non-base58 string
  125. const clean = jsdom('', {url: common.urlToString(url)}),
  126. result = $.PrivateBin.Model.getPasteKey();
  127. $.PrivateBin.Model.reset();
  128. clean();
  129. return url.fragment === result;
  130. }
  131. );
  132. jsc.property(
  133. 'returns the fragment stripped of trailing query parts',
  134. common.jscUrl(),
  135. jsc.array(common.jscHashString()),
  136. function (url, trail) {
  137. const fragment = url.fragment.padStart(32, '\u0000');
  138. url.fragment = $.PrivateBin.CryptTool.base58encode(fragment) + '&' + trail.join('');
  139. const clean = jsdom('', {url: common.urlToString(url)}),
  140. result = $.PrivateBin.Model.getPasteKey();
  141. $.PrivateBin.Model.reset();
  142. clean();
  143. return fragment === result;
  144. }
  145. );
  146. jsc.property(
  147. 'returns the fragment of a v2 URL',
  148. common.jscUrl(),
  149. function (url) {
  150. // base58 strips leading NULL bytes, so the string is padded with these if not found
  151. const fragment = url.fragment.padStart(32, '\u0000');
  152. url.fragment = $.PrivateBin.CryptTool.base58encode(fragment);
  153. const clean = jsdom('', {url: common.urlToString(url)}),
  154. result = $.PrivateBin.Model.getPasteKey();
  155. $.PrivateBin.Model.reset();
  156. clean();
  157. return fragment === result;
  158. }
  159. );
  160. jsc.property(
  161. 'returns the v2 fragment stripped of trailing query parts',
  162. common.jscUrl(),
  163. jsc.array(common.jscHashString()),
  164. function (url, trail) {
  165. // base58 strips leading NULL bytes, so the string is padded with these if not found
  166. const fragment = url.fragment.padStart(32, '\u0000');
  167. url.fragment = $.PrivateBin.CryptTool.base58encode(fragment) + '&' + trail.join('');
  168. const clean = jsdom('', {url: common.urlToString(url)}),
  169. result = $.PrivateBin.Model.getPasteKey();
  170. $.PrivateBin.Model.reset();
  171. clean();
  172. return fragment === result;
  173. }
  174. );
  175. jsc.property(
  176. 'throws exception on empty fragment of the URL',
  177. common.jscUrl(false),
  178. function (url) {
  179. let clean = jsdom('', {url: common.urlToString(url)}),
  180. result = false;
  181. try {
  182. $.PrivateBin.Model.getPasteKey();
  183. }
  184. catch(err) {
  185. result = true;
  186. }
  187. $.PrivateBin.Model.reset();
  188. clean();
  189. return result;
  190. }
  191. );
  192. });
  193. describe('getTemplate', function () {
  194. beforeEach(function () {
  195. $.PrivateBin.Model.reset();
  196. });
  197. jsc.property(
  198. 'returns the contents of the element with id "[name]template"',
  199. jsc.nearray(common.jscAlnumString()),
  200. jsc.nearray(common.jscA2zString()),
  201. jsc.nearray(common.jscAlnumString()),
  202. function (id, element, value) {
  203. id = id.join('');
  204. element = element.join('');
  205. value = value.join('').trim();
  206. // <br>, <hr>, <img> and <wbr> tags can't contain strings,
  207. // table tags can't be alone, so test with a <p> instead
  208. if (['br', 'col', 'hr', 'img', 'tr', 'td', 'th', 'wbr'].indexOf(element) >= 0) {
  209. element = 'p';
  210. }
  211. $('body').html(
  212. '<div id="templates"><' + element + ' id="' + id +
  213. 'template">' + value + '</' + element + '></div>'
  214. );
  215. $.PrivateBin.Model.init();
  216. var template = '<' + element + ' id="' + id + '">' + value +
  217. '</' + element + '>',
  218. result = $.PrivateBin.Model.getTemplate(id).wrap('<p/>').parent().html();
  219. $.PrivateBin.Model.reset();
  220. return template === result;
  221. }
  222. );
  223. });
  224. });