1
0

Model.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. const clean = jsdom('', {url: common.urlToString(url)});
  102. let 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. 'throws exception on v1 URLs',
  122. common.jscUrl(),
  123. function (url) {
  124. url.fragment = '0OIl'; // any non-base58 string
  125. const clean = jsdom('', {url: common.urlToString(url)});
  126. let result = false;
  127. try {
  128. $.PrivateBin.Model.getPasteId();
  129. }
  130. catch(err) {
  131. result = true;
  132. }
  133. $.PrivateBin.Model.reset();
  134. clean();
  135. return result;
  136. }
  137. );
  138. jsc.property(
  139. 'returns the fragment stripped of trailing query parts',
  140. common.jscUrl(),
  141. jsc.array(common.jscHashString()),
  142. function (url, trail) {
  143. const fragment = url.fragment.padStart(32, '\u0000');
  144. url.fragment = $.PrivateBin.CryptTool.base58encode(fragment) + '&' + trail.join('');
  145. const clean = jsdom('', {url: common.urlToString(url)}),
  146. result = $.PrivateBin.Model.getPasteKey();
  147. $.PrivateBin.Model.reset();
  148. clean();
  149. return fragment === result;
  150. }
  151. );
  152. jsc.property(
  153. 'returns the fragment of a v2 URL',
  154. common.jscUrl(),
  155. function (url) {
  156. // base58 strips leading NULL bytes, so the string is padded with these if not found
  157. const fragment = url.fragment.padStart(32, '\u0000');
  158. url.fragment = $.PrivateBin.CryptTool.base58encode(fragment);
  159. const clean = jsdom('', {url: common.urlToString(url)}),
  160. result = $.PrivateBin.Model.getPasteKey();
  161. $.PrivateBin.Model.reset();
  162. clean();
  163. return fragment === result;
  164. }
  165. );
  166. jsc.property(
  167. 'returns the v2 fragment stripped of trailing query parts',
  168. common.jscUrl(),
  169. jsc.array(common.jscHashString()),
  170. function (url, trail) {
  171. // base58 strips leading NULL bytes, so the string is padded with these if not found
  172. const fragment = url.fragment.padStart(32, '\u0000');
  173. url.fragment = $.PrivateBin.CryptTool.base58encode(fragment) + '&' + trail.join('');
  174. const clean = jsdom('', {url: common.urlToString(url)}),
  175. result = $.PrivateBin.Model.getPasteKey();
  176. $.PrivateBin.Model.reset();
  177. clean();
  178. return fragment === result;
  179. }
  180. );
  181. jsc.property(
  182. 'throws exception on empty fragment of the URL',
  183. common.jscUrl(false),
  184. function (url) {
  185. let clean = jsdom('', {url: common.urlToString(url)}),
  186. result = false;
  187. try {
  188. $.PrivateBin.Model.getPasteKey();
  189. }
  190. catch(err) {
  191. result = true;
  192. }
  193. $.PrivateBin.Model.reset();
  194. clean();
  195. return result;
  196. }
  197. );
  198. });
  199. describe('getTemplate', function () {
  200. beforeEach(function () {
  201. $.PrivateBin.Model.reset();
  202. });
  203. jsc.property(
  204. 'returns the contents of the element with id "[name]template"',
  205. jsc.nearray(common.jscAlnumString()),
  206. jsc.nearray(common.jscA2zString()),
  207. jsc.nearray(common.jscAlnumString()),
  208. function (id, element, value) {
  209. id = id.join('');
  210. element = element.join('');
  211. value = value.join('').trim();
  212. // <br>, <hr>, <img> and <wbr> tags can't contain strings,
  213. // table tags can't be alone, so test with a <p> instead
  214. if (['br', 'col', 'hr', 'img', 'tr', 'td', 'th', 'wbr'].indexOf(element) >= 0) {
  215. element = 'p';
  216. }
  217. $('body').html(
  218. '<div id="templates"><' + element + ' id="' + id +
  219. 'template">' + value + '</' + element + '></div>'
  220. );
  221. $.PrivateBin.Model.init();
  222. var template = '<' + element + ' id="' + id + '">' + value +
  223. '</' + element + '>',
  224. result = $.PrivateBin.Model.getTemplate(id).wrap('<p/>').parent().html();
  225. $.PrivateBin.Model.reset();
  226. return template === result;
  227. }
  228. );
  229. });
  230. });