test.js 7.1 KB

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