test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. 'use strict';
  2. var jsc = require('jsverify'),
  3. jsdom = require('jsdom-global'),
  4. cleanup = jsdom(),
  5. a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  6. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  7. alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']),
  8. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  9. base64String = alnumString.concat(['+','/','=']).concat(
  10. a2zString.map(function(c) {
  11. return c.toUpperCase();
  12. })
  13. );
  14. global.$ = global.jQuery = require('./jquery-3.1.1');
  15. global.sjcl = require('./sjcl-1.0.6');
  16. global.Base64 = require('./base64-2.1.9');
  17. global.RawDeflate = require('./rawdeflate-0.5');
  18. require('./rawinflate-0.3');
  19. require('./privatebin');
  20. describe('helper', function () {
  21. describe('secondsToHuman', function () {
  22. after(function () {
  23. cleanup();
  24. });
  25. jsc.property('returns an array with a number and a word', 'integer', function (number) {
  26. var result = $.PrivateBin.helper.secondsToHuman(number);
  27. return Array.isArray(result) &&
  28. result.length === 2 &&
  29. result[0] === parseInt(result[0], 10) &&
  30. typeof result[1] === 'string';
  31. });
  32. jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
  33. return $.PrivateBin.helper.secondsToHuman(number)[0] === number;
  34. });
  35. jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
  36. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'second';
  37. });
  38. jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
  39. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  40. });
  41. jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
  42. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'minute';
  43. });
  44. jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
  45. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  46. });
  47. jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
  48. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'hour';
  49. });
  50. jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
  51. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  52. });
  53. jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
  54. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'day';
  55. });
  56. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  57. jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
  58. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  59. });
  60. jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
  61. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'month';
  62. });
  63. });
  64. // this test is not yet meaningful using jsdom, as it does not contain getSelection support.
  65. // TODO: This needs to be tested using a browser.
  66. describe('selectText', function () {
  67. jsc.property(
  68. 'selection contains content of given ID',
  69. 'nearray string',
  70. 'nearray nestring',
  71. function (ids, contents) {
  72. //console.log(ids, contents);
  73. var html = '',
  74. result = true;
  75. ids.forEach(function(item, i) {
  76. html += '<div id="' + item + '">' + (contents[i] || contents[0]) + '</div>';
  77. });
  78. var clean = jsdom(html);
  79. ids.forEach(function(item, i) {
  80. $.PrivateBin.helper.selectText(item);
  81. // TODO: As per https://github.com/tmpvar/jsdom/issues/321 there is no getSelection in jsdom, yet.
  82. // Once there is one, uncomment the line below to actually check the result.
  83. //result *= (contents[i] || contents[0]) === window.getSelection().toString();
  84. });
  85. clean();
  86. return result;
  87. }
  88. );
  89. });
  90. describe('scriptLocation', function () {
  91. jsc.property(
  92. 'returns the URL without query & fragment',
  93. jsc.nearray(jsc.elements(a2zString)),
  94. jsc.nearray(jsc.elements(a2zString)),
  95. jsc.array(jsc.elements(queryString)),
  96. 'string',
  97. function (schema, address, query, fragment) {
  98. var expected = schema.join('') + '://' + address.join('') + '/',
  99. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  100. result = $.PrivateBin.helper.scriptLocation();
  101. clean();
  102. return expected === result;
  103. }
  104. );
  105. });
  106. describe('pasteId', function () {
  107. jsc.property(
  108. 'returns the query string without separator, if any',
  109. jsc.nearray(jsc.elements(a2zString)),
  110. jsc.nearray(jsc.elements(a2zString)),
  111. jsc.array(jsc.elements(queryString)),
  112. 'string',
  113. function (schema, address, query, fragment) {
  114. var queryString = query.join(''),
  115. clean = jsdom('', {
  116. url: schema.join('') + '://' + address.join('') +
  117. '/?' + queryString + '#' + fragment
  118. }),
  119. result = $.PrivateBin.helper.pasteId();
  120. clean();
  121. return queryString === result;
  122. }
  123. );
  124. });
  125. describe('pageKey', function () {
  126. jsc.property(
  127. 'returns the fragment of the URL',
  128. jsc.nearray(jsc.elements(a2zString)),
  129. jsc.nearray(jsc.elements(a2zString)),
  130. jsc.array(jsc.elements(queryString)),
  131. jsc.array(jsc.elements(base64String)),
  132. function (schema, address, query, fragment) {
  133. var fragmentString = fragment.join(''),
  134. clean = jsdom('', {
  135. url: schema.join('') + '://' + address.join('') +
  136. '/?' + query.join('') + '#' + fragmentString
  137. }),
  138. result = $.PrivateBin.helper.pageKey();
  139. clean();
  140. return fragmentString === result;
  141. }
  142. );
  143. jsc.property(
  144. 'returns the fragment stripped of trailing query parts',
  145. jsc.nearray(jsc.elements(a2zString)),
  146. jsc.nearray(jsc.elements(a2zString)),
  147. jsc.array(jsc.elements(queryString)),
  148. jsc.array(jsc.elements(base64String)),
  149. jsc.array(jsc.elements(queryString)),
  150. function (schema, address, query, fragment, trail) {
  151. var fragmentString = fragment.join(''),
  152. clean = jsdom('', {
  153. url: schema.join('') + '://' + address.join('') + '/?' +
  154. query.join('') + '#' + fragmentString + '&' + trail.join('')
  155. }),
  156. result = $.PrivateBin.helper.pageKey();
  157. clean();
  158. return fragmentString === result;
  159. }
  160. );
  161. });
  162. describe('htmlEntities', function () {
  163. after(function () {
  164. cleanup();
  165. });
  166. jsc.property(
  167. 'removes all HTML entities from any given string',
  168. 'string',
  169. function (string) {
  170. var result = $.PrivateBin.helper.htmlEntities(string);
  171. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  172. }
  173. );
  174. });
  175. });