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