Model.js 9.0 KB

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