Check.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict';
  2. var common = require('../common');
  3. /* global Legacy, WebCrypto */
  4. describe('Check', function () {
  5. describe('init', function () {
  6. this.timeout(30000);
  7. before(function () {
  8. cleanup();
  9. });
  10. it('returns false and shows error, if a bot UA is detected', function () {
  11. jsc.assert(jsc.forall(
  12. 'string',
  13. jsc.elements(['Bot', 'bot']),
  14. 'string',
  15. function (prefix, botBit, suffix) {
  16. const clean = jsdom(
  17. '<html><body><div id="errormessage" class="hidden"></div>' +
  18. '</body></html>', {
  19. 'userAgent': prefix + botBit + suffix
  20. }
  21. );
  22. Legacy.Check.init();
  23. const result1 = Legacy.Check.getInit() && !Legacy.Check.getStatus(),
  24. result2 = (document.getElementById('errormessage').className !== 'hidden');
  25. clean();
  26. return result1 && result2;
  27. }
  28. ),
  29. {tests: 10});
  30. });
  31. jsc.property(
  32. 'shows error, if no webcrypto is detected',
  33. 'bool',
  34. jsc.elements(['localhost', '127.0.0.1', '[::1]', '']),
  35. jsc.nearray(common.jscA2zString()),
  36. jsc.elements(['.onion', '.i2p', '']),
  37. function (secureProtocol, localhost, domain, tld) {
  38. const isDomain = localhost === '',
  39. isSecureContext = secureProtocol || !isDomain || tld.length > 0,
  40. clean = jsdom(
  41. '<html><body><div id="errormessage" class="hidden"></div>' +
  42. '<div id="oldnotice" class="hidden"></div></body></html>',
  43. {
  44. 'url': (secureProtocol ? 'https' : 'http' ) + '://' +
  45. (isDomain ? domain.join('') + tld : localhost) + '/'
  46. }
  47. );
  48. Legacy.Check.init();
  49. const result1 = Legacy.Check.getInit() && !Legacy.Check.getStatus(),
  50. result2 = isSecureContext === (document.getElementById('errormessage').className === 'hidden'),
  51. result3 = (document.getElementById('oldnotice').className !== 'hidden');
  52. clean();
  53. return result1 && result2 && result3;
  54. }
  55. );
  56. jsc.property(
  57. 'shows error, if HTTP only site is detected',
  58. 'bool',
  59. jsc.nearray(common.jscA2zString()),
  60. function (secureProtocol, domain) {
  61. const clean = jsdom(
  62. '<html><body><div id="httpnotice" class="hidden"></div>' +
  63. '</body></html>',
  64. {
  65. 'url': (secureProtocol ? 'https' : 'http' ) + '://' + domain.join('') + '/'
  66. }
  67. );
  68. window.crypto = new WebCrypto();
  69. Legacy.Check.init();
  70. const result1 = Legacy.Check.getInit() && Legacy.Check.getStatus(),
  71. result2 = secureProtocol === (document.getElementById('httpnotice').className === 'hidden');
  72. clean();
  73. return result1 && result2;
  74. }
  75. );
  76. });
  77. });