From f8e0c90b5bd0a030eead3041f7fe8308ca2ddd6b Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Mon, 31 Mar 2014 21:35:40 +0200 Subject: [PATCH] fix tests --- src/js/controller/login-initial.js | 3 +- test/new-unit/app-controller-test.js | 22 ------ test/new-unit/email-dao-test.js | 22 ++---- test/new-unit/login-ctrl-test.js | 50 ++++++++++++- test/new-unit/login-initial-ctrl-test.js | 83 +++------------------ test/new-unit/login-new-device-ctrl-test.js | 8 -- 6 files changed, 61 insertions(+), 127 deletions(-) diff --git a/src/js/controller/login-initial.js b/src/js/controller/login-initial.js index 12245f0..72564dd 100644 --- a/src/js/controller/login-initial.js +++ b/src/js/controller/login-initial.js @@ -2,8 +2,7 @@ define(function(require) { 'use strict'; var appController = require('js/app-controller'), - errorUtil = require('js/util/error'), - dl = require('js/util/download'); + errorUtil = require('js/util/error'); var LoginInitialCtrl = function($scope, $location) { var emailDao = appController._emailDao, diff --git a/test/new-unit/app-controller-test.js b/test/new-unit/app-controller-test.js index 5a4f05f..b3944cb 100644 --- a/test/new-unit/app-controller-test.js +++ b/test/new-unit/app-controller-test.js @@ -291,32 +291,11 @@ define(function(require) { }); }); - it('should fail due to error in onConnect', function(done) { - devicestorageStub.init.yields(); - updateHandlerStub.update.yields(); - emailDaoStub.init.yields(); - - onConnectStub.yields({}); - - controller.init({ - emailAddress: emailAddress - }, function(err) { - expect(err).to.exist; - expect(updateHandlerStub.update.calledOnce).to.be.true; - expect(emailDaoStub.init.calledOnce).to.be.true; - expect(devicestorageStub.init.calledOnce).to.be.true; - expect(onConnectStub.calledOnce).to.be.true; - done(); - }); - }); - it('should work and return a keypair', function(done) { devicestorageStub.init.withArgs(emailAddress).yields(); emailDaoStub.init.yields(null, {}); updateHandlerStub.update.yields(); - onConnectStub.yields(); - controller.init({ emailAddress: emailAddress }, function(err, keypair) { @@ -325,7 +304,6 @@ define(function(require) { expect(updateHandlerStub.update.calledOnce).to.be.true; expect(emailDaoStub.init.calledOnce).to.be.true; expect(devicestorageStub.init.calledOnce).to.be.true; - expect(onConnectStub.calledOnce).to.be.true; done(); }); }); diff --git a/test/new-unit/email-dao-test.js b/test/new-unit/email-dao-test.js index a426ba4..56eeca8 100644 --- a/test/new-unit/email-dao-test.js +++ b/test/new-unit/email-dao-test.js @@ -133,14 +133,18 @@ define(function(require) { dao._account.folders = []; imapClientStub.login.yields(); + var listFolderStub = sinon.stub(dao, '_imapListFolders').yields(null, []); + dao.onConnect({ imapClient: imapClientStub, pgpMailer: pgpMailerStub }, function(err) { expect(err).to.not.exist; expect(dao._account.online).to.be.true; + expect(dao._account.folders).to.deep.equal([]); expect(dao._imapClient).to.equal(dao._imapClient); expect(dao._smtpClient).to.equal(dao._smtpClient); + listFolderStub.restore(); done(); }); }); @@ -306,23 +310,7 @@ define(function(require) { }); }); - it('should work when folder already initiated', function(done) { - dao._account.folders = []; - imapLoginStub.yields(); - - dao.onConnect({ - imapClient: imapClientStub, - pgpMailer: pgpMailerStub - }, function(err) { - expect(err).to.not.exist; - expect(dao._account.online).to.be.true; - expect(dao._imapClient).to.equal(dao._imapClient); - expect(dao._smtpClient).to.equal(dao._smtpClient); - done(); - }); - }); - - it('should work when folder not yet initiated', function(done) { + it('should work', function(done) { var folders = []; imapLoginStub.yields(); imapListFoldersStub.yields(null, folders); diff --git a/test/new-unit/login-ctrl-test.js b/test/new-unit/login-ctrl-test.js index 357993e..7c858f7 100644 --- a/test/new-unit/login-ctrl-test.js +++ b/test/new-unit/login-ctrl-test.js @@ -60,13 +60,55 @@ define(function(require) { initStub.restore(); }); - it('should forward to existing user login', function(done) { - startAppStub.yields(); - getEmailAddressStub.yields(null, emailAddress); - initStub.yields(null, { + it('should forward directly to desktop for empty passphrase', function(done) { + var testKeys = { privateKey: 'a', publicKey: 'b' + }; + + startAppStub.yields(); + getEmailAddressStub.yields(null, emailAddress); + initStub.yields(null, testKeys); + + emailDaoMock.unlock.withArgs({ + keypair: testKeys, + passphrase: undefined + }).yields(); + + angular.module('logintest', []); + mocks.module('logintest'); + mocks.inject(function($controller, $rootScope, $location) { + location = $location; + sinon.stub(location, 'path', function(path) { + expect(path).to.equal('/desktop'); + expect(startAppStub.calledOnce).to.be.true; + expect(checkForUpdateStub.calledOnce).to.be.true; + expect(getEmailAddressStub.calledOnce).to.be.true; + done(); + }); + scope = $rootScope.$new(); + scope.state = {}; + ctrl = $controller(LoginCtrl, { + $location: location, + $scope: scope + }); }); + }); + + it('should forward to existing user login', function(done) { + var testKeys = { + privateKey: 'a', + publicKey: 'b' + }; + + startAppStub.yields(); + getEmailAddressStub.yields(null, emailAddress); + initStub.yields(null, testKeys); + + emailDaoMock.unlock.withArgs({ + keypair: testKeys, + passphrase: undefined + }).yields({}); angular.module('logintest', []); mocks.module('logintest'); diff --git a/test/new-unit/login-initial-ctrl-test.js b/test/new-unit/login-initial-ctrl-test.js index a424376..47b807b 100644 --- a/test/new-unit/login-initial-ctrl-test.js +++ b/test/new-unit/login-initial-ctrl-test.js @@ -5,7 +5,6 @@ define(function(require) { angular = require('angular'), mocks = require('angularMocks'), LoginInitialCtrl = require('js/controller/login-initial'), - dl = require('js/util/download'), PGP = require('js/crypto/pgp'), EmailDAO = require('js/dao/email-dao'), appController = require('js/app-controller'); @@ -54,8 +53,6 @@ define(function(require) { describe('initial state', function() { it('should be well defined', function() { - expect(scope.proceed).to.exist; - expect(scope.exportKeypair).to.exist; expect(scope.confirmPassphrase).to.exist; expect(scope.state.ui).to.equal(1); }); @@ -63,10 +60,10 @@ define(function(require) { describe('check passphrase quality', function() { it('should be too short', function() { - scope.state.passphrase = '&§DG36abc'; + scope.state.passphrase = '&§DG36'; scope.checkPassphraseQuality(); - expect(scope.passphraseMsg).to.equal('Too short'); + expect(scope.passphraseMsg).to.equal('Very weak'); expect(scope.passphraseRating).to.equal(0); }); @@ -112,16 +109,12 @@ define(function(require) { emailDaoMock.unlock.withArgs({ passphrase: passphrase }).yields(); - setStateStub = sinon.stub(scope, 'setState', function(state) { - if (setStateStub.calledOnce) { - expect(state).to.equal(2); - } else if (setStateStub.calledTwice) { - expect(state).to.equal(4); - expect(emailDaoMock.unlock.calledOnce).to.be.true; - scope.setState.restore(); - done(); - } - }); + + scope.$apply = function() { + expect(location.$$path).to.equal('/desktop'); + expect(emailDaoMock.unlock.calledOnce).to.be.true; + done(); + }; scope.confirmPassphrase(); }); @@ -139,6 +132,7 @@ define(function(require) { emailDaoMock.unlock.withArgs({ passphrase: passphrase }).yields(new Error('asd')); + setStateStub = sinon.stub(scope, 'setState', function(state) { if (setStateStub.calledOnce) { expect(state).to.equal(2); @@ -154,64 +148,5 @@ define(function(require) { }); }); - describe('proceed', function() { - it('should forward', function() { - var locationSpy = sinon.spy(location, 'path'); - - scope.proceed(); - - expect(locationSpy.calledWith('/desktop')).to.be.true; - }); - }); - - describe('export keypair', function() { - it('should work', function() { - var locationSpy, createDownloadMock; - - createDownloadMock = sinon.stub(dl, 'createDownload'); - cryptoMock.exportKeys.yields(null, { - publicKeyArmored: 'a', - privateKeyArmored: 'b', - keyId: keyId - }); - createDownloadMock.withArgs(sinon.match(function(arg) { - return arg.content === 'ab' && arg.filename === 'whiteout_mail_' + emailAddress + '_' + expectedKeyId + '.asc' && arg.contentType === 'text/plain'; - })).yields(); - - locationSpy = sinon.spy(location, 'path'); - - scope.exportKeypair(); - - expect(cryptoMock.exportKeys.calledOnce).to.be.true; - expect(createDownloadMock.calledOnce).to.be.true; - expect(locationSpy.calledWith('/desktop')).to.be.true; - dl.createDownload.restore(); - }); - - it('should not work when download fails', function() { - var createDownloadMock = sinon.stub(dl, 'createDownload'); - cryptoMock.exportKeys.yields(null, { - publicKeyArmored: 'a', - privateKeyArmored: 'b', - keyId: keyId - }); - createDownloadMock.yields({ - errMsg: 'snafu.' - }); - scope.exportKeypair(); - - expect(cryptoMock.exportKeys.calledOnce).to.be.true; - expect(createDownloadMock.calledOnce).to.be.true; - dl.createDownload.restore(); - }); - - it('should not work when export fails', function() { - cryptoMock.exportKeys.yields(new Error('snafu.')); - - scope.exportKeypair(); - - expect(cryptoMock.exportKeys.calledOnce).to.be.true; - }); - }); }); }); \ No newline at end of file diff --git a/test/new-unit/login-new-device-ctrl-test.js b/test/new-unit/login-new-device-ctrl-test.js index 52f2672..81985dd 100644 --- a/test/new-unit/login-new-device-ctrl-test.js +++ b/test/new-unit/login-new-device-ctrl-test.js @@ -98,14 +98,6 @@ define(function(require) { expect(keychainMock.getUserKeyPair.calledOnce).to.be.true; }); - it('should not do anything without passphrase', function() { - scope.state.passphrase = ''; - - scope.confirmPassphrase(); - - expect(scope.incorrect).to.be.true; - }); - it('should not work when keypair upload fails', function() { scope.passphrase = passphrase; scope.key = {