Model.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. jsc.nearray(common.jscA2zString()),
  78. jsc.nearray(common.jscA2zString()),
  79. jsc.tuple(new Array(16).fill(common.jscHexString)),
  80. jsc.array(common.jscQueryString()),
  81. jsc.array(common.jscQueryString()),
  82. 'string',
  83. function (schema, address, pasteId, queryStart, queryEnd, fragment) {
  84. var pasteIdString = pasteId.join(''),
  85. queryStartString = queryStart.join('') + (queryStart.length > 0 ? '&' : ''),
  86. queryEndString = (queryEnd.length > 0 ? '&' : '') + queryEnd.join(''),
  87. queryString = queryStartString + pasteIdString + queryEndString,
  88. clean = jsdom('', {
  89. url: schema.join('') + '://' + address.join('') +
  90. '/?' + queryString + '#' + fragment
  91. });
  92. global.URL = require('jsdom-url').URL;
  93. var result = $.PrivateBin.Model.getPasteId();
  94. $.PrivateBin.Model.reset();
  95. clean();
  96. return pasteIdString === result;
  97. }
  98. );
  99. jsc.property(
  100. 'throws exception on empty query string',
  101. jsc.nearray(common.jscA2zString()),
  102. jsc.nearray(common.jscA2zString()),
  103. 'string',
  104. function (schema, address, fragment) {
  105. var clean = jsdom('', {
  106. url: schema.join('') + '://' + address.join('') +
  107. '/#' + fragment
  108. }),
  109. result = false;
  110. global.URL = require('jsdom-url').URL;
  111. try {
  112. $.PrivateBin.Model.getPasteId();
  113. }
  114. catch(err) {
  115. result = true;
  116. }
  117. $.PrivateBin.Model.reset();
  118. clean();
  119. return result;
  120. }
  121. );
  122. });
  123. describe('getPasteKey', function () {
  124. this.timeout(30000);
  125. beforeEach(function () {
  126. $.PrivateBin.Model.reset();
  127. });
  128. jsc.property(
  129. 'returns the fragment of a v1 URL',
  130. jsc.nearray(common.jscA2zString()),
  131. jsc.nearray(common.jscA2zString()),
  132. jsc.array(common.jscQueryString()),
  133. 'nestring',
  134. function (schema, address, query, fragment) {
  135. const fragmentString = common.btoa(fragment.padStart(32, '\u0000'));
  136. let clean = jsdom('', {
  137. url: schema.join('') + '://' + address.join('') +
  138. '/?' + query.join('') + '#' + fragmentString
  139. }),
  140. result = $.PrivateBin.Model.getPasteKey();
  141. $.PrivateBin.Model.reset();
  142. clean();
  143. return fragmentString === result;
  144. }
  145. );
  146. jsc.property(
  147. 'returns the v1 fragment stripped of trailing query parts',
  148. jsc.nearray(common.jscA2zString()),
  149. jsc.nearray(common.jscA2zString()),
  150. jsc.array(common.jscQueryString()),
  151. 'nestring',
  152. jsc.array(common.jscHashString()),
  153. function (schema, address, query, fragment, trail) {
  154. const fragmentString = common.btoa(fragment.padStart(32, '\u0000'));
  155. let clean = jsdom('', {
  156. url: schema.join('') + '://' + address.join('') + '/?' +
  157. query.join('') + '#' + fragmentString + '&' + trail.join('')
  158. }),
  159. result = $.PrivateBin.Model.getPasteKey();
  160. $.PrivateBin.Model.reset();
  161. clean();
  162. return fragmentString === result;
  163. }
  164. );
  165. jsc.property(
  166. 'returns the fragment of a v2 URL',
  167. jsc.nearray(common.jscA2zString()),
  168. jsc.nearray(common.jscA2zString()),
  169. jsc.array(common.jscQueryString()),
  170. 'nestring',
  171. function (schema, address, query, fragment) {
  172. // base58 strips leading NULL bytes, so the string is padded with these if not found
  173. fragment = fragment.padStart(32, '\u0000');
  174. let fragmentString = $.PrivateBin.CryptTool.base58encode(fragment),
  175. clean = jsdom('', {
  176. url: schema.join('') + '://' + address.join('') +
  177. '/?' + query.join('') + '#' + fragmentString
  178. }),
  179. result = $.PrivateBin.Model.getPasteKey();
  180. $.PrivateBin.Model.reset();
  181. clean();
  182. return fragment === result;
  183. }
  184. );
  185. jsc.property(
  186. 'returns the v2 fragment stripped of trailing query parts',
  187. jsc.nearray(common.jscA2zString()),
  188. jsc.nearray(common.jscA2zString()),
  189. jsc.array(common.jscQueryString()),
  190. 'nestring',
  191. jsc.array(common.jscHashString()),
  192. function (schema, address, query, fragment, trail) {
  193. // base58 strips leading NULL bytes, so the string is padded with these if not found
  194. fragment = fragment.padStart(32, '\u0000');
  195. let fragmentString = $.PrivateBin.CryptTool.base58encode(fragment),
  196. clean = jsdom('', {
  197. url: schema.join('') + '://' + address.join('') + '/?' +
  198. query.join('') + '#' + fragmentString + '&' + trail.join('')
  199. }),
  200. result = $.PrivateBin.Model.getPasteKey();
  201. $.PrivateBin.Model.reset();
  202. clean();
  203. return fragment === result;
  204. }
  205. );
  206. jsc.property(
  207. 'throws exception on empty fragment of the URL',
  208. jsc.nearray(common.jscA2zString()),
  209. jsc.nearray(common.jscA2zString()),
  210. jsc.array(common.jscQueryString()),
  211. function (schema, address, query) {
  212. var clean = jsdom('', {
  213. url: schema.join('') + '://' + address.join('') +
  214. '/?' + query.join('')
  215. }),
  216. result = false;
  217. try {
  218. $.PrivateBin.Model.getPasteKey();
  219. }
  220. catch(err) {
  221. result = true;
  222. }
  223. $.PrivateBin.Model.reset();
  224. clean();
  225. return result;
  226. }
  227. );
  228. });
  229. describe('getTemplate', function () {
  230. beforeEach(function () {
  231. $.PrivateBin.Model.reset();
  232. });
  233. jsc.property(
  234. 'returns the contents of the element with id "[name]template"',
  235. jsc.nearray(common.jscAlnumString()),
  236. jsc.nearray(common.jscA2zString()),
  237. jsc.nearray(common.jscAlnumString()),
  238. function (id, element, value) {
  239. id = id.join('');
  240. element = element.join('');
  241. value = value.join('').trim();
  242. // <br>, <hr>, <img> and <wbr> tags can't contain strings,
  243. // table tags can't be alone, so test with a <p> instead
  244. if (['br', 'col', 'hr', 'img', 'tr', 'td', 'th', 'wbr'].indexOf(element) >= 0) {
  245. element = 'p';
  246. }
  247. $('body').html(
  248. '<div id="templates"><' + element + ' id="' + id +
  249. 'template">' + value + '</' + element + '></div>'
  250. );
  251. $.PrivateBin.Model.init();
  252. var template = '<' + element + ' id="' + id + '">' + value +
  253. '</' + element + '>',
  254. result = $.PrivateBin.Model.getTemplate(id).wrap('<p/>').parent().html();
  255. $.PrivateBin.Model.reset();
  256. return template === result;
  257. }
  258. );
  259. });
  260. });