test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. describe('baseUri', function () {
  65. before(function () {
  66. $.PrivateBin.Helper.reset();
  67. });
  68. jsc.property(
  69. 'returns the URL without query & fragment',
  70. jsc.nearray(jsc.elements(a2zString)),
  71. jsc.nearray(jsc.elements(a2zString)),
  72. jsc.array(jsc.elements(queryString)),
  73. 'string',
  74. function (schema, address, query, fragment) {
  75. var expected = schema.join('') + '://' + address.join('') + '/',
  76. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  77. result = $.PrivateBin.Helper.baseUri();
  78. $.PrivateBin.Helper.reset();
  79. clean();
  80. return expected === result;
  81. }
  82. );
  83. });
  84. describe('htmlEntities', function () {
  85. after(function () {
  86. cleanup();
  87. });
  88. jsc.property(
  89. 'removes all HTML entities from any given string',
  90. 'string',
  91. function (string) {
  92. var result = $.PrivateBin.Helper.htmlEntities(string);
  93. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  94. }
  95. );
  96. });
  97. });
  98. describe('Model', function () {
  99. describe('getPasteId', function () {
  100. before(function () {
  101. $.PrivateBin.Model.reset();
  102. });
  103. jsc.property(
  104. 'returns the query string without separator, if any',
  105. jsc.nearray(jsc.elements(a2zString)),
  106. jsc.nearray(jsc.elements(a2zString)),
  107. jsc.nearray(jsc.elements(queryString)),
  108. 'string',
  109. function (schema, address, query, fragment) {
  110. var queryString = query.join(''),
  111. clean = jsdom('', {
  112. url: schema.join('') + '://' + address.join('') +
  113. '/?' + queryString + '#' + fragment
  114. }),
  115. result = $.PrivateBin.Model.getPasteId();
  116. $.PrivateBin.Model.reset();
  117. clean();
  118. return queryString === result;
  119. }
  120. );
  121. });
  122. describe('getPasteKey', function () {
  123. jsc.property(
  124. 'returns the fragment of the URL',
  125. jsc.nearray(jsc.elements(a2zString)),
  126. jsc.nearray(jsc.elements(a2zString)),
  127. jsc.array(jsc.elements(queryString)),
  128. jsc.nearray(jsc.elements(base64String)),
  129. function (schema, address, query, fragment) {
  130. var fragmentString = fragment.join(''),
  131. clean = jsdom('', {
  132. url: schema.join('') + '://' + address.join('') +
  133. '/?' + query.join('') + '#' + fragmentString
  134. }),
  135. result = $.PrivateBin.Model.getPasteKey();
  136. $.PrivateBin.Model.reset();
  137. clean();
  138. return fragmentString === result;
  139. }
  140. );
  141. jsc.property(
  142. 'returns the fragment stripped of trailing query parts',
  143. jsc.nearray(jsc.elements(a2zString)),
  144. jsc.nearray(jsc.elements(a2zString)),
  145. jsc.array(jsc.elements(queryString)),
  146. jsc.nearray(jsc.elements(base64String)),
  147. jsc.array(jsc.elements(queryString)),
  148. function (schema, address, query, fragment, trail) {
  149. var fragmentString = fragment.join(''),
  150. clean = jsdom('', {
  151. url: schema.join('') + '://' + address.join('') + '/?' +
  152. query.join('') + '#' + fragmentString + '&' + trail.join('')
  153. }),
  154. result = $.PrivateBin.Model.getPasteKey();
  155. $.PrivateBin.Model.reset();
  156. clean();
  157. return fragmentString === result;
  158. }
  159. );
  160. });
  161. });