From c8f13511c14aaafc3d408b9d3f80ceccf935cdaa Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Wed, 1 Apr 2015 14:24:46 +0200 Subject: [PATCH] Review imap key-sync --- src/js/controller/app/navigation.js | 9 ++++++--- src/js/controller/login/login-privatekey-download.js | 3 ++- src/js/controller/login/login-privatekey-upload.js | 11 +++++------ src/js/service/privatekey.js | 8 +++++--- src/tpl/login-privatekey-upload.html | 5 ++++- .../login/login-privatekey-download-ctrl-test.js | 3 +-- .../login/login-privatekey-upload-ctrl-test.js | 5 +++-- test/unit/service/privatekey-dao-test.js | 2 +- 8 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/js/controller/app/navigation.js b/src/js/controller/app/navigation.js index c80dc7e..45dfcb8 100644 --- a/src/js/controller/app/navigation.js +++ b/src/js/controller/app/navigation.js @@ -204,9 +204,12 @@ var NavigationCtrl = function($scope, $location, $q, $timeout, account, email, o showNegativeBtn: true, callback: function(granted) { if (granted) { - // send to key upload screen - $timeout(function() { - $location.path('/login-privatekey-upload'); + // logout of the current session + email.onDisconnect().then(function() { + // send to key upload screen + $timeout(function() { + $location.path('/login-privatekey-upload'); + }); }); } } diff --git a/src/js/controller/login/login-privatekey-download.js b/src/js/controller/login/login-privatekey-download.js index 52224a8..e7db1e4 100644 --- a/src/js/controller/login/login-privatekey-download.js +++ b/src/js/controller/login/login-privatekey-download.js @@ -54,9 +54,10 @@ var LoginPrivateKeyDownloadCtrl = function($scope, $location, $routeParams, $q, return email.unlock({ keypair: cachedKeypair, passphrase: undefined - }).catch(function() { + }).catch(function(err) { // passphrase incorrct ... go to passphrase login screen $scope.goTo('/login-existing'); + throw err; }); }).then(function() { diff --git a/src/js/controller/login/login-privatekey-upload.js b/src/js/controller/login/login-privatekey-upload.js index b645600..59c522a 100644 --- a/src/js/controller/login/login-privatekey-upload.js +++ b/src/js/controller/login/login-privatekey-upload.js @@ -22,18 +22,17 @@ var LoginPrivateKeyUploadCtrl = function($scope, $location, $routeParams, $q, au // $scope.encryptAndUploadKey = function() { - if ($scope.inputCode.toUpperCase() !== $scope.code) { - $scope.errMsg = 'The code does not match. Please go back and check the generated code.'; - return; - } - - // register device to keychain service return $q(function(resolve) { $scope.busy = true; $scope.errMsg = undefined; $scope.incorrect = false; resolve(); + }).then(function() { + if ($scope.inputCode.toUpperCase() !== $scope.code) { + throw new Error('The code does not match. Please go back and check the generated code.'); + } + }).then(function() { // login to imap return privateKey.init(); diff --git a/src/js/service/privatekey.js b/src/js/service/privatekey.js index c01d9fa..3470aaa 100644 --- a/src/js/service/privatekey.js +++ b/src/js/service/privatekey.js @@ -41,7 +41,7 @@ PrivateKey.prototype.init = function() { * Cleanup by logging out of the imap client. */ PrivateKey.prototype.destroy = function() { - this._imap.login(); + this._imap.logout(); // don't wait for logout to complete return new Promise(function(resolve) { resolve(); @@ -111,8 +111,10 @@ PrivateKey.prototype.upload = function(options) { path: IMAP_KEYS_FOLDER }).then(function() { self._axe.debug('Successfully created imap folder ' + IMAP_KEYS_FOLDER); - }).catch(function() { - self._axe.debug('Creating imap folder ' + IMAP_KEYS_FOLDER + ' failed. Probably already available.'); + }).catch(function(err) { + var prettyErr = new Error('Creating imap folder ' + IMAP_KEYS_FOLDER + ' failed: ' + err.message); + self._axe.error(prettyErr); + throw prettyErr; }); }).then(createMessage).then(function(message) { // upload to imap folder diff --git a/src/tpl/login-privatekey-upload.html b/src/tpl/login-privatekey-upload.html index e59eaa4..864610b 100644 --- a/src/tpl/login-privatekey-upload.html +++ b/src/tpl/login-privatekey-upload.html @@ -31,8 +31,11 @@

Please confirm the backup code you have written down.

+

{{errMsg}}

+
-
diff --git a/test/unit/controller/login/login-privatekey-download-ctrl-test.js b/test/unit/controller/login/login-privatekey-download-ctrl-test.js index 86ac591..b99e9bf 100644 --- a/test/unit/controller/login/login-privatekey-download-ctrl-test.js +++ b/test/unit/controller/login/login-privatekey-download-ctrl-test.js @@ -103,12 +103,11 @@ describe('Login Private Key Download Controller unit test', function() { keyId: cachedKeypair.publicKey._id }).returns(resolves(encryptedPrivateKey)); privateKeyStub.decrypt.returns(resolves(privkey)); - emailDaoMock.unlock.returns(rejects()); + emailDaoMock.unlock.returns(rejects(new Error())); authMock.storeCredentials.returns(resolves()); privateKeyStub.destroy.returns(resolves()); scope.checkCode().then(function() { - expect(scope.errMsg).to.not.exist; expect(scope.goTo.withArgs('/login-existing').calledOnce).to.be.true; done(); }); diff --git a/test/unit/controller/login/login-privatekey-upload-ctrl-test.js b/test/unit/controller/login/login-privatekey-upload-ctrl-test.js index 8cf367f..9183841 100644 --- a/test/unit/controller/login/login-privatekey-upload-ctrl-test.js +++ b/test/unit/controller/login/login-privatekey-upload-ctrl-test.js @@ -57,8 +57,9 @@ describe('Login Private Key Upload Controller unit test', function() { it('should fail for invalid code', function() { scope.inputCode = 'asdf'; - scope.encryptAndUploadKey(); - expect(scope.errMsg).to.match(/go back and check/); + scope.encryptAndUploadKey().then(function() { + expect(scope.errMsg).to.match(/go back and check/); + }); }); it('should work', function(done) { diff --git a/test/unit/service/privatekey-dao-test.js b/test/unit/service/privatekey-dao-test.js index dfa135a..3ddaff6 100644 --- a/test/unit/service/privatekey-dao-test.js +++ b/test/unit/service/privatekey-dao-test.js @@ -35,7 +35,7 @@ describe('Private Key DAO unit tests', function() { describe('destroy', function() { it('should work', function(done) { privkeyDao.destroy().then(function() { - expect(imapClientStub.login.calledOnce).to.be.true; + expect(imapClientStub.logout.calledOnce).to.be.true; done(); }); });