Model.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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('hasCipherData', function () {
  69. before(function () {
  70. $.PrivateBin.Model.reset();
  71. cleanup();
  72. });
  73. jsc.property(
  74. 'checks if the element with id "cipherdata" contains any data',
  75. 'asciistring',
  76. function (value) {
  77. value = common.htmlEntities(value).trim();
  78. $('body').html('<div id="cipherdata">' + value + '</div>');
  79. $.PrivateBin.Model.init();
  80. var result = $.PrivateBin.Model.hasCipherData();
  81. $.PrivateBin.Model.reset();
  82. return (value.length > 0) === result;
  83. }
  84. );
  85. });
  86. describe('getCipherData', function () {
  87. before(function () {
  88. $.PrivateBin.Model.reset();
  89. cleanup();
  90. });
  91. jsc.property(
  92. 'returns the contents of the element with id "cipherdata"',
  93. 'asciistring',
  94. function (value) {
  95. value = common.htmlEntities(value).trim();
  96. $('body').html('<div id="cipherdata">' + value + '</div>');
  97. $.PrivateBin.Model.init();
  98. var result = common.htmlEntities(
  99. $.PrivateBin.Model.getCipherData()
  100. );
  101. $.PrivateBin.Model.reset();
  102. return value === result;
  103. }
  104. );
  105. });
  106. describe('getPasteId', function () {
  107. this.timeout(30000);
  108. before(function () {
  109. $.PrivateBin.Model.reset();
  110. cleanup();
  111. });
  112. jsc.property(
  113. 'returns the query string without separator, if any',
  114. jsc.nearray(common.jscA2zString()),
  115. jsc.nearray(common.jscA2zString()),
  116. jsc.nearray(common.jscQueryString()),
  117. 'string',
  118. function (schema, address, query, fragment) {
  119. var queryString = query.join(''),
  120. clean = jsdom('', {
  121. url: schema.join('') + '://' + address.join('') +
  122. '/?' + queryString + '#' + fragment
  123. }),
  124. result = $.PrivateBin.Model.getPasteId();
  125. $.PrivateBin.Model.reset();
  126. clean();
  127. return queryString === result;
  128. }
  129. );
  130. jsc.property(
  131. 'throws exception on empty query string',
  132. jsc.nearray(common.jscA2zString()),
  133. jsc.nearray(common.jscA2zString()),
  134. 'string',
  135. function (schema, address, fragment) {
  136. var clean = jsdom('', {
  137. url: schema.join('') + '://' + address.join('') +
  138. '/#' + fragment
  139. }),
  140. result = false;
  141. try {
  142. $.PrivateBin.Model.getPasteId();
  143. }
  144. catch(err) {
  145. result = true;
  146. }
  147. $.PrivateBin.Model.reset();
  148. clean();
  149. return result;
  150. }
  151. );
  152. });
  153. describe('getPasteKey', function () {
  154. this.timeout(30000);
  155. jsc.property(
  156. 'returns the fragment of the URL',
  157. jsc.nearray(common.jscA2zString()),
  158. jsc.nearray(common.jscA2zString()),
  159. jsc.array(common.jscQueryString()),
  160. jsc.nearray(common.jscBase64String()),
  161. function (schema, address, query, fragment) {
  162. var fragmentString = fragment.join(''),
  163. clean = jsdom('', {
  164. url: schema.join('') + '://' + address.join('') +
  165. '/?' + query.join('') + '#' + fragmentString
  166. }),
  167. result = $.PrivateBin.Model.getPasteKey();
  168. $.PrivateBin.Model.reset();
  169. clean();
  170. return fragmentString === result;
  171. }
  172. );
  173. jsc.property(
  174. 'returns the fragment stripped of trailing query parts',
  175. jsc.nearray(common.jscA2zString()),
  176. jsc.nearray(common.jscA2zString()),
  177. jsc.array(common.jscQueryString()),
  178. jsc.nearray(common.jscBase64String()),
  179. jsc.array(common.jscQueryString()),
  180. function (schema, address, query, fragment, trail) {
  181. var fragmentString = fragment.join(''),
  182. clean = jsdom('', {
  183. url: schema.join('') + '://' + address.join('') + '/?' +
  184. query.join('') + '#' + fragmentString + '&' + trail.join('')
  185. }),
  186. result = $.PrivateBin.Model.getPasteKey();
  187. $.PrivateBin.Model.reset();
  188. clean();
  189. return fragmentString === result;
  190. }
  191. );
  192. jsc.property(
  193. 'throws exception on empty fragment of the URL',
  194. jsc.nearray(common.jscA2zString()),
  195. jsc.nearray(common.jscA2zString()),
  196. jsc.array(common.jscQueryString()),
  197. function (schema, address, query) {
  198. var clean = jsdom('', {
  199. url: schema.join('') + '://' + address.join('') +
  200. '/?' + query.join('')
  201. }),
  202. result = false;
  203. try {
  204. $.PrivateBin.Model.getPasteKey();
  205. }
  206. catch(err) {
  207. result = true;
  208. }
  209. $.PrivateBin.Model.reset();
  210. clean();
  211. return result;
  212. }
  213. );
  214. });
  215. describe('getTemplate', function () {
  216. before(function () {
  217. $.PrivateBin.Model.reset();
  218. cleanup();
  219. });
  220. jsc.property(
  221. 'returns the contents of the element with id "[name]template"',
  222. jsc.nearray(common.jscAlnumString()),
  223. jsc.nearray(common.jscA2zString()),
  224. jsc.nearray(common.jscAlnumString()),
  225. function (id, element, value) {
  226. id = id.join('');
  227. element = element.join('');
  228. value = value.join('').trim();
  229. // <br>, <hr>, <img> and <wbr> tags can't contain strings,
  230. // table tags can't be alone, so test with a <p> instead
  231. if (['br', 'col', 'hr', 'img', 'tr', 'td', 'th', 'wbr'].indexOf(element) >= 0) {
  232. element = 'p';
  233. }
  234. $('body').html(
  235. '<div id="templates"><' + element + ' id="' + id +
  236. 'template">' + value + '</' + element + '></div>'
  237. );
  238. $.PrivateBin.Model.init();
  239. var template = '<' + element + ' id="' + id + '">' + value +
  240. '</' + element + '>',
  241. result = $.PrivateBin.Model.getTemplate(id).wrap('<p/>').parent().html();
  242. $.PrivateBin.Model.reset();
  243. return template === result;
  244. }
  245. );
  246. });
  247. });