test.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. global.$ = global.jQuery = require('./jquery-3.1.1');
  10. global.sjcl = require('./sjcl-1.0.6');
  11. global.Base64 = require('./base64-2.1.9');
  12. global.RawDeflate = require('./rawdeflate-0.5');
  13. require('./rawinflate-0.3');
  14. require('./privatebin');
  15. describe('helper', function () {
  16. describe('secondsToHuman', function () {
  17. after(function () {
  18. cleanup();
  19. });
  20. jsc.property('returns an array with a number and a word', 'integer', function (number) {
  21. var result = $.PrivateBin.helper.secondsToHuman(number);
  22. return Array.isArray(result) &&
  23. result.length === 2 &&
  24. result[0] === parseInt(result[0], 10) &&
  25. typeof result[1] === 'string';
  26. });
  27. jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
  28. return $.PrivateBin.helper.secondsToHuman(number)[0] === number;
  29. });
  30. jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
  31. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'second';
  32. });
  33. jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
  34. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  35. });
  36. jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
  37. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'minute';
  38. });
  39. jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
  40. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  41. });
  42. jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
  43. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'hour';
  44. });
  45. jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
  46. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  47. });
  48. jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
  49. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'day';
  50. });
  51. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  52. jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
  53. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  54. });
  55. jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
  56. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'month';
  57. });
  58. });
  59. describe('scriptLocation', function () {
  60. jsc.property(
  61. 'returns the URL without query & fragment',
  62. jsc.nearray(jsc.elements(a2zString)),
  63. jsc.nearray(jsc.elements(a2zString)),
  64. jsc.array(jsc.elements(queryString)),
  65. 'string',
  66. function (schema, address, query, fragment) {
  67. var expected = schema.join('') + '://' + address.join('') + '/',
  68. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  69. result = $.PrivateBin.helper.scriptLocation();
  70. clean();
  71. return expected === result;
  72. }
  73. );
  74. });
  75. describe('pasteId', function () {
  76. jsc.property(
  77. 'returns the query string without separator, if any',
  78. jsc.nearray(jsc.elements(a2zString)),
  79. jsc.nearray(jsc.elements(a2zString)),
  80. jsc.array(jsc.elements(queryString)),
  81. 'string',
  82. function (schema, address, query, fragment) {
  83. var query = query.join(''),
  84. clean = jsdom('', {
  85. url: schema.join('') + '://' + address.join('') +
  86. '/?' + query + '#' + fragment
  87. }),
  88. result = $.PrivateBin.helper.pasteId();
  89. clean();
  90. return query === result;
  91. }
  92. );
  93. });
  94. });