Model.js 8.3 KB

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