Model.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. before(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.nearray(common.jscHashString()),
  80. 'string',
  81. function (schema, address, query, fragment) {
  82. var queryString = query.join(''),
  83. clean = jsdom('', {
  84. url: schema.join('') + '://' + address.join('') +
  85. '/?' + queryString + '#' + fragment
  86. }),
  87. result = $.PrivateBin.Model.getPasteId();
  88. $.PrivateBin.Model.reset();
  89. clean();
  90. return queryString === result;
  91. }
  92. );
  93. jsc.property(
  94. 'throws exception on empty query string',
  95. jsc.nearray(common.jscA2zString()),
  96. jsc.nearray(common.jscA2zString()),
  97. 'string',
  98. function (schema, address, fragment) {
  99. var clean = jsdom('', {
  100. url: schema.join('') + '://' + address.join('') +
  101. '/#' + fragment
  102. }),
  103. result = false;
  104. try {
  105. $.PrivateBin.Model.getPasteId();
  106. }
  107. catch(err) {
  108. result = true;
  109. }
  110. $.PrivateBin.Model.reset();
  111. clean();
  112. return result;
  113. }
  114. );
  115. });
  116. describe('getPasteKey', function () {
  117. this.timeout(30000);
  118. jsc.property(
  119. 'returns the fragment of the URL',
  120. jsc.nearray(common.jscA2zString()),
  121. jsc.nearray(common.jscA2zString()),
  122. jsc.array(common.jscQueryString()),
  123. jsc.nearray(common.jscBase64String()),
  124. function (schema, address, query, fragment) {
  125. var fragmentString = fragment.join(''),
  126. clean = jsdom('', {
  127. url: schema.join('') + '://' + address.join('') +
  128. '/?' + query.join('') + '#' + fragmentString
  129. }),
  130. result = $.PrivateBin.Model.getPasteKey();
  131. $.PrivateBin.Model.reset();
  132. clean();
  133. return fragmentString === result;
  134. }
  135. );
  136. jsc.property(
  137. 'returns the fragment stripped of trailing query parts',
  138. jsc.nearray(common.jscA2zString()),
  139. jsc.nearray(common.jscA2zString()),
  140. jsc.array(common.jscQueryString()),
  141. jsc.nearray(common.jscBase64String()),
  142. jsc.array(common.jscHashString()),
  143. function (schema, address, query, fragment, trail) {
  144. var fragmentString = fragment.join(''),
  145. clean = jsdom('', {
  146. url: schema.join('') + '://' + address.join('') + '/?' +
  147. query.join('') + '#' + fragmentString + '&' + trail.join('')
  148. }),
  149. result = $.PrivateBin.Model.getPasteKey();
  150. $.PrivateBin.Model.reset();
  151. clean();
  152. return fragmentString === result;
  153. }
  154. );
  155. jsc.property(
  156. 'throws exception on empty fragment of the URL',
  157. jsc.nearray(common.jscA2zString()),
  158. jsc.nearray(common.jscA2zString()),
  159. jsc.array(common.jscQueryString()),
  160. function (schema, address, query) {
  161. var clean = jsdom('', {
  162. url: schema.join('') + '://' + address.join('') +
  163. '/?' + query.join('')
  164. }),
  165. result = false;
  166. try {
  167. $.PrivateBin.Model.getPasteKey();
  168. }
  169. catch(err) {
  170. result = true;
  171. }
  172. $.PrivateBin.Model.reset();
  173. clean();
  174. return result;
  175. }
  176. );
  177. });
  178. describe('getTemplate', function () {
  179. before(function () {
  180. $.PrivateBin.Model.reset();
  181. });
  182. jsc.property(
  183. 'returns the contents of the element with id "[name]template"',
  184. jsc.nearray(common.jscAlnumString()),
  185. jsc.nearray(common.jscA2zString()),
  186. jsc.nearray(common.jscAlnumString()),
  187. function (id, element, value) {
  188. id = id.join('');
  189. element = element.join('');
  190. value = value.join('').trim();
  191. // <br>, <hr>, <img> and <wbr> tags can't contain strings,
  192. // table tags can't be alone, so test with a <p> instead
  193. if (['br', 'col', 'hr', 'img', 'tr', 'td', 'th', 'wbr'].indexOf(element) >= 0) {
  194. element = 'p';
  195. }
  196. $('body').html(
  197. '<div id="templates"><' + element + ' id="' + id +
  198. 'template">' + value + '</' + element + '></div>'
  199. );
  200. $.PrivateBin.Model.init();
  201. var template = '<' + element + ' id="' + id + '">' + value +
  202. '</' + element + '>',
  203. result = $.PrivateBin.Model.getTemplate(id).wrap('<p/>').parent().html();
  204. $.PrivateBin.Model.reset();
  205. return template === result;
  206. }
  207. );
  208. });
  209. });