From b791157717b29e6ce83b0d2d2fcdacdd083981fb Mon Sep 17 00:00:00 2001
From: El RIDO <elrido@gmx.net>
Date: Sun, 2 Sep 2018 11:33:27 +0200
Subject: [PATCH] fix unit tests after merge from master, issues due to newly
 async tests that cause environment changes across test scripts

---
 js/common.js         |  2 +-
 js/test/CryptTool.js | 21 ++++++++++++++++-----
 js/test/Helper.js    |  9 +++------
 3 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/js/common.js b/js/common.js
index 95cfe815..6aa43bac 100644
--- a/js/common.js
+++ b/js/common.js
@@ -56,7 +56,7 @@ var a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
     mimeLine = '';
 
 // redirect console messages to log file
-console.info = console.warn = function () {
+console.info = console.warn = console.error = function () {
     logFile.write(Array.prototype.slice.call(arguments).join('') + '\n');
 };
 
diff --git a/js/test/CryptTool.js b/js/test/CryptTool.js
index 0cc81950..7c55692f 100644
--- a/js/test/CryptTool.js
+++ b/js/test/CryptTool.js
@@ -1,7 +1,5 @@
 'use strict';
 require('../common');
-jsdom();
-window.crypto = new WebCrypto();
 
 describe('CryptTool', function () {
     describe('cipher & decipher', function () {
@@ -12,6 +10,8 @@ describe('CryptTool', function () {
                 'string',
                 'string',
                 function (key, password, message) {
+                    var clean = jsdom();
+                    window.crypto = new WebCrypto();
                     message = message.trim();
                     return $.PrivateBin.CryptTool.cipher(
                         key, password, message
@@ -19,7 +19,7 @@ describe('CryptTool', function () {
                         return $.PrivateBin.CryptTool.decipher(
                             key, password, ciphertext
                         ).then(function(plaintext) {
-                            if (message !== plaintext) console.log([message, plaintext]);
+                            clean();
                             return message === plaintext;
                         });
                     });
@@ -33,8 +33,8 @@ describe('CryptTool', function () {
             'supports PrivateBin v1 ciphertext (SJCL & browser atob)',
             async function () {
                 delete global.Base64;
-                // make btoa available
-                global.btoa = window.btoa;
+                var clean = jsdom();
+                window.crypto = new WebCrypto();
 
                 // Of course you can easily decipher the following texts, if you like.
                 // Bonus points for finding their sources and hidden meanings.
@@ -96,6 +96,7 @@ describe('CryptTool', function () {
                     '99gjHX7wphJ6umKMM+fn6PcbYJkhDh2GlJL5COXjXfm/5aj/vuyaRRWZ' +
                     'MZtmnYpGAtAPg7AUG"}'
                 );
+                clean();
 
                 assert.ok(
                     paste1.includes('securely packed in iron') &&
@@ -108,6 +109,8 @@ describe('CryptTool', function () {
             'supports ZeroBin ciphertext (SJCL & Base64 1.7)',
             async function () {
                 global.Base64 = require('../base64-1.7').Base64;
+                var clean = jsdom();
+                window.crypto = new WebCrypto();
 
                 // Of course you can easily decipher the following texts, if you like.
                 // Bonus points for finding their sources and hidden meanings.
@@ -154,6 +157,7 @@ describe('CryptTool', function () {
                     '7NnhqVk5A6vIBbu4AC5PZf76l6yep4xsoy/QtdDxCMocCXeAML9MQ9uP' +
                     'QbuspOKrBvMfN5igA1kBqasnxI472KBNXsdZnaDddSVUuvhTcETM="}'
                 );
+                clean();
 
                 delete global.Base64;
                 assert.ok(
@@ -165,6 +169,7 @@ describe('CryptTool', function () {
     });
 
     describe('getSymmetricKey', function () {
+        this.timeout(30000);
         var keys = [];
 
         // the parameter is used to ensure the test is run more then one time
@@ -172,25 +177,31 @@ describe('CryptTool', function () {
             'returns random, non-empty keys',
             'integer',
             function(counter) {
+                var clean = jsdom();
+                window.crypto = new WebCrypto();
                 var key = $.PrivateBin.CryptTool.getSymmetricKey(),
                     result = (key !== '' && keys.indexOf(key) === -1);
                 keys.push(key);
+                clean();
                 return result;
             }
         );
     });
 
     describe('SJCL.js vs abab.js', function () {
+        this.timeout(30000);
         jsc.property(
             'these all return the same base64 string',
             'string',
             function(string) {
+                var clean = jsdom();
                 // not comparing Base64.js v1.7 encode/decode, that has known issues
                 var Base64 = require('../base64-1.7').Base64,
                     sjcl = global.sjcl.codec.base64.fromBits(global.sjcl.codec.utf8String.toBits(string)),
                     abab = window.btoa(Base64.utob(string)),
                     lcjs = global.sjcl.codec.utf8String.fromBits(global.sjcl.codec.base64.toBits(abab)),
                     baba = Base64.btou(window.atob(sjcl));
+                clean();
                 return sjcl === abab && string === lcjs && lcjs === baba;
             }
         );
diff --git a/js/test/Helper.js b/js/test/Helper.js
index 056268b2..5621c322 100644
--- a/js/test/Helper.js
+++ b/js/test/Helper.js
@@ -230,6 +230,7 @@ describe('Helper', function () {
                 });
                 var clean = jsdom('', {cookie: cookieArray}),
                     result = $.PrivateBin.Helper.getCookie(selectedKey);
+                $.PrivateBin.Helper.reset();
                 clean();
                 return result === selectedValue;
             }
@@ -238,21 +239,17 @@ describe('Helper', function () {
 
     describe('baseUri', function () {
         this.timeout(30000);
-        before(function () {
-            $.PrivateBin.Helper.reset();
-        });
-
         jsc.property(
             'returns the URL without query & fragment',
-            common.jscSchemas(),
+            jsc.elements(['http', 'https']),
             jsc.nearray(common.jscA2zString()),
             jsc.array(common.jscQueryString()),
             'string',
             function (schema, address, query, fragment) {
+                $.PrivateBin.Helper.reset();
                 var expected = schema + '://' + address.join('') + '/',
                     clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
                     result = $.PrivateBin.Helper.baseUri();
-                $.PrivateBin.Helper.reset();
                 clean();
                 return expected === result;
             }