Model.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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();
  8. });
  9. jsc.property(
  10. 'returns the contents of the element with id "pasteExpiration"',
  11. 'array asciinestring',
  12. 'string',
  13. 'small nat',
  14. function (keys, value, key) {
  15. keys = keys.map(common.htmlEntities);
  16. value = common.htmlEntities(value);
  17. var content = keys.length > key ? keys[key] : (keys.length > 0 ? keys[0] : 'null'),
  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 = common.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. cleanup();
  40. });
  41. jsc.property(
  42. 'returns the contents of the element with id "pasteFormatter"',
  43. 'array asciinestring',
  44. 'string',
  45. 'small nat',
  46. function (keys, value, key) {
  47. keys = keys.map(common.htmlEntities);
  48. value = common.htmlEntities(value);
  49. var content = keys.length > key ? keys[key] : (keys.length > 0 ? keys[0] : 'null'),
  50. contents = '<select id="pasteFormatter" name="pasteFormatter">';
  51. keys.forEach(function(item) {
  52. contents += '<option value="' + item + '"';
  53. if (item === content) {
  54. contents += ' selected="selected"';
  55. }
  56. contents += '>' + value + '</option>';
  57. });
  58. contents += '</select>';
  59. $('body').html(contents);
  60. var result = common.htmlEntities(
  61. $.PrivateBin.Model.getFormatDefault()
  62. );
  63. $.PrivateBin.Model.reset();
  64. return content === result;
  65. }
  66. );
  67. });
  68. describe('getPasteId', function () {
  69. this.timeout(30000);
  70. beforeEach(function () {
  71. $.PrivateBin.Model.reset();
  72. cleanup();
  73. });
  74. jsc.property(
  75. 'returns the query string without separator, if any',
  76. jsc.nearray(common.jscA2zString()),
  77. jsc.nearray(common.jscA2zString()),
  78. jsc.tuple(new Array(16).fill(common.jscHexString)),
  79. jsc.array(common.jscQueryString()),
  80. jsc.array(common.jscQueryString()),
  81. 'string',
  82. function (schema, address, pasteId, queryStart, queryEnd, fragment) {
  83. var pasteIdString = pasteId.join(''),
  84. queryStartString = queryStart.join('') + (queryStart.length > 0 ? '&' : ''),
  85. queryEndString = (queryEnd.length > 0 ? '&' : '') + queryEnd.join(''),
  86. queryString = queryStartString + pasteIdString + queryEndString,
  87. clean = jsdom('', {
  88. url: schema.join('') + '://' + address.join('') +
  89. '/?' + queryString + '#' + fragment
  90. }),
  91. 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. jsc.nearray(common.jscA2zString()),
  100. jsc.nearray(common.jscA2zString()),
  101. 'string',
  102. function (schema, address, fragment) {
  103. var clean = jsdom('', {
  104. url: schema.join('') + '://' + address.join('') +
  105. '/#' + fragment
  106. }),
  107. result = false;
  108. try {
  109. $.PrivateBin.Model.getPasteId();
  110. }
  111. catch(err) {
  112. result = true;
  113. }
  114. $.PrivateBin.Model.reset();
  115. clean();
  116. return result;
  117. }
  118. );
  119. });
  120. describe('getPasteKey', function () {
  121. this.timeout(30000);
  122. beforeEach(function () {
  123. $.PrivateBin.Model.reset();
  124. cleanup();
  125. });
  126. jsc.property(
  127. 'returns the fragment of a v1 URL',
  128. jsc.nearray(common.jscA2zString()),
  129. jsc.nearray(common.jscA2zString()),
  130. jsc.array(common.jscQueryString()),
  131. 'nestring',
  132. function (schema, address, query, fragment) {
  133. fragment = fragment.padStart(32, String.fromCharCode(0));
  134. let fragmentString = common.btoa(fragment),
  135. clean = jsdom('', {
  136. url: schema.join('') + '://' + address.join('') +
  137. '/?' + query.join('') + '#' + fragmentString
  138. }),
  139. result = $.PrivateBin.Model.getPasteKey();
  140. $.PrivateBin.Model.reset();
  141. clean();
  142. return fragment === result;
  143. }
  144. );
  145. jsc.property(
  146. 'returns the v1 fragment stripped of trailing query parts',
  147. jsc.nearray(common.jscA2zString()),
  148. jsc.nearray(common.jscA2zString()),
  149. jsc.array(common.jscQueryString()),
  150. 'nestring',
  151. jsc.array(common.jscHashString()),
  152. function (schema, address, query, fragment, trail) {
  153. fragment = fragment.padStart(32, String.fromCharCode(0));
  154. let fragmentString = common.btoa(fragment),
  155. 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 fragment === 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. let fragmentString = $.PrivateBin.CryptTool.base58encode(fragment),
  173. clean = jsdom('', {
  174. url: schema.join('') + '://' + address.join('') +
  175. '/?' + query.join('') + '#' + fragmentString
  176. }),
  177. result = $.PrivateBin.Model.getPasteKey();
  178. $.PrivateBin.Model.reset();
  179. clean();
  180. return fragment === result;
  181. }
  182. );
  183. jsc.property(
  184. 'returns the v2 fragment stripped of trailing query parts',
  185. jsc.nearray(common.jscA2zString()),
  186. jsc.nearray(common.jscA2zString()),
  187. jsc.array(common.jscQueryString()),
  188. 'nestring',
  189. jsc.array(common.jscHashString()),
  190. function (schema, address, query, fragment, trail) {
  191. let fragmentString = $.PrivateBin.CryptTool.base58encode(fragment),
  192. clean = jsdom('', {
  193. url: schema.join('') + '://' + address.join('') + '/?' +
  194. query.join('') + '#' + fragmentString + '&' + trail.join('')
  195. }),
  196. result = $.PrivateBin.Model.getPasteKey();
  197. $.PrivateBin.Model.reset();
  198. clean();
  199. return fragment === result;
  200. }
  201. );
  202. jsc.property(
  203. 'throws exception on empty fragment of the URL',
  204. jsc.nearray(common.jscA2zString()),
  205. jsc.nearray(common.jscA2zString()),
  206. jsc.array(common.jscQueryString()),
  207. function (schema, address, query) {
  208. var clean = jsdom('', {
  209. url: schema.join('') + '://' + address.join('') +
  210. '/?' + query.join('')
  211. }),
  212. result = false;
  213. try {
  214. $.PrivateBin.Model.getPasteKey();
  215. }
  216. catch(err) {
  217. result = true;
  218. }
  219. $.PrivateBin.Model.reset();
  220. clean();
  221. return result;
  222. }
  223. );
  224. });
  225. describe('getTemplate', function () {
  226. beforeEach(function () {
  227. $.PrivateBin.Model.reset();
  228. cleanup();
  229. });
  230. jsc.property(
  231. 'returns the contents of the element with id "[name]template"',
  232. jsc.nearray(common.jscAlnumString()),
  233. jsc.nearray(common.jscA2zString()),
  234. jsc.nearray(common.jscAlnumString()),
  235. function (id, element, value) {
  236. id = id.join('');
  237. element = element.join('');
  238. value = value.join('').trim();
  239. // <br>, <hr>, <img> and <wbr> tags can't contain strings,
  240. // table tags can't be alone, so test with a <p> instead
  241. if (['br', 'col', 'hr', 'img', 'tr', 'td', 'th', 'wbr'].indexOf(element) >= 0) {
  242. element = 'p';
  243. }
  244. $('body').html(
  245. '<div id="templates"><' + element + ' id="' + id +
  246. 'template">' + value + '</' + element + '></div>'
  247. );
  248. $.PrivateBin.Model.init();
  249. var template = '<' + element + ' id="' + id + '">' + value +
  250. '</' + element + '>',
  251. result = $.PrivateBin.Model.getTemplate(id).wrap('<p/>').parent().html();
  252. $.PrivateBin.Model.reset();
  253. return template === result;
  254. }
  255. );
  256. });
  257. });