test.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // schemas supported by the whatwg-url library
  15. schemas = ['ftp','gopher','http','https','ws','wss'];
  16. global.$ = global.jQuery = require('./jquery-3.1.1');
  17. global.sjcl = require('./sjcl-1.0.6');
  18. global.Base64 = require('./base64-2.1.9');
  19. global.RawDeflate = require('./rawdeflate-0.5');
  20. require('./rawinflate-0.3');
  21. require('./privatebin');
  22. describe('Helper', function () {
  23. describe('secondsToHuman', function () {
  24. after(function () {
  25. cleanup();
  26. });
  27. jsc.property('returns an array with a number and a word', 'integer', function (number) {
  28. var result = $.PrivateBin.Helper.secondsToHuman(number);
  29. return Array.isArray(result) &&
  30. result.length === 2 &&
  31. result[0] === parseInt(result[0], 10) &&
  32. typeof result[1] === 'string';
  33. });
  34. jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
  35. return $.PrivateBin.Helper.secondsToHuman(number)[0] === number;
  36. });
  37. jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
  38. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'second';
  39. });
  40. jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
  41. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / 60);
  42. });
  43. jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
  44. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'minute';
  45. });
  46. jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
  47. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
  48. });
  49. jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
  50. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'hour';
  51. });
  52. jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
  53. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
  54. });
  55. jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
  56. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'day';
  57. });
  58. // max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
  59. jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
  60. return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
  61. });
  62. jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
  63. return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'month';
  64. });
  65. });
  66. describe('baseUri', function () {
  67. before(function () {
  68. $.PrivateBin.Helper.reset();
  69. });
  70. jsc.property(
  71. 'returns the URL without query & fragment',
  72. jsc.elements(schemas),
  73. jsc.nearray(jsc.elements(a2zString)),
  74. jsc.array(jsc.elements(queryString)),
  75. 'string',
  76. function (schema, address, query, fragment) {
  77. var expected = schema + '://' + address.join('') + '/',
  78. clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
  79. result = $.PrivateBin.Helper.baseUri();
  80. $.PrivateBin.Helper.reset();
  81. clean();
  82. return expected === result;
  83. }
  84. );
  85. });
  86. describe('htmlEntities', function () {
  87. after(function () {
  88. cleanup();
  89. });
  90. jsc.property(
  91. 'removes all HTML entities from any given string',
  92. 'string',
  93. function (string) {
  94. var result = $.PrivateBin.Helper.htmlEntities(string);
  95. return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&amp;/.test(result)));
  96. }
  97. );
  98. });
  99. });
  100. describe('Model', function () {
  101. describe('getPasteId', function () {
  102. before(function () {
  103. $.PrivateBin.Model.reset();
  104. });
  105. jsc.property(
  106. 'returns the query string without separator, if any',
  107. jsc.nearray(jsc.elements(a2zString)),
  108. jsc.nearray(jsc.elements(a2zString)),
  109. jsc.nearray(jsc.elements(queryString)),
  110. 'string',
  111. function (schema, address, query, fragment) {
  112. var queryString = query.join(''),
  113. clean = jsdom('', {
  114. url: schema.join('') + '://' + address.join('') +
  115. '/?' + queryString + '#' + fragment
  116. }),
  117. result = $.PrivateBin.Model.getPasteId();
  118. $.PrivateBin.Model.reset();
  119. clean();
  120. return queryString === result;
  121. }
  122. );
  123. });
  124. describe('getPasteKey', function () {
  125. jsc.property(
  126. 'returns the fragment of the URL',
  127. jsc.nearray(jsc.elements(a2zString)),
  128. jsc.nearray(jsc.elements(a2zString)),
  129. jsc.array(jsc.elements(queryString)),
  130. jsc.nearray(jsc.elements(base64String)),
  131. function (schema, address, query, fragment) {
  132. var fragmentString = fragment.join(''),
  133. clean = jsdom('', {
  134. url: schema.join('') + '://' + address.join('') +
  135. '/?' + query.join('') + '#' + fragmentString
  136. }),
  137. result = $.PrivateBin.Model.getPasteKey();
  138. $.PrivateBin.Model.reset();
  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.nearray(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.Model.getPasteKey();
  157. $.PrivateBin.Model.reset();
  158. clean();
  159. return fragmentString === result;
  160. }
  161. );
  162. });
  163. });