test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. var jsc = require('jsverify');
  3. before(function () {
  4. this.jsdom = require('jsdom-global')();
  5. global.$ = global.jQuery = require('./jquery-3.1.1');
  6. global.sjcl = require('./sjcl-1.0.4');
  7. global.Base64 = require('./base64-2.1.9');
  8. global.RawDeflate = require('./rawdeflate-0.5');
  9. require('./rawinflate-0.3');
  10. require('./privatebin');
  11. })
  12. after(function () {
  13. this.jsdom();
  14. })
  15. describe('helper', function () {
  16. describe('secondsToHuman', function () {
  17. jsc.property('returns an array with a number and a word', 'integer', function (number) {
  18. var result = $.PrivateBin.helper.secondsToHuman(number);
  19. return Array.isArray(result) &&
  20. result.length === 2 &&
  21. result[0] === parseInt(result[0], 10) &&
  22. typeof result[1] === 'string';
  23. });
  24. jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
  25. return $.PrivateBin.helper.secondsToHuman(number)[0] === number;
  26. });
  27. jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
  28. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'second';
  29. });
  30. jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
  31. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  32. });
  33. jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
  34. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'minute';
  35. });
  36. jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
  37. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  38. });
  39. jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
  40. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'hour';
  41. });
  42. jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
  43. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  44. });
  45. jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
  46. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'day';
  47. });
  48. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  49. jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
  50. return $.PrivateBin.helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  51. });
  52. jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
  53. return $.PrivateBin.helper.secondsToHuman(number)[1] === 'month';
  54. });
  55. });
  56. describe('hashToParameterString', function () {
  57. jsc.property('returns strings', 'dict nestring', function (dict) {
  58. return typeof $.PrivateBin.helper.hashToParameterString(dict) === 'string';
  59. });
  60. });
  61. describe('parameterStringToHash', function () {
  62. jsc.property('returns objects', 'string', function (string) {
  63. return typeof $.PrivateBin.helper.parameterStringToHash(string) === 'object';
  64. });
  65. jsc.property('decodes hashes generated with hashToParameterString', 'dict nestring', function (dict) {
  66. var result = $.PrivateBin.helper.parameterStringToHash($.PrivateBin.helper.hashToParameterString(dict));
  67. // padding for URL shorteners
  68. dict.p = 'p';
  69. return JSON.stringify(result) === JSON.stringify(dict);
  70. });
  71. });
  72. });