From 7f6410e1f8830c9c88df144cd926267738a04dd7 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Mon, 24 Nov 2014 18:07:09 +0100 Subject: [PATCH] Write account.init unit test --- test/unit/email/account-test.js | 162 ++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) diff --git a/test/unit/email/account-test.js b/test/unit/email/account-test.js index 815498b..d34d960 100644 --- a/test/unit/email/account-test.js +++ b/test/unit/email/account-test.js @@ -11,6 +11,7 @@ var Account = require('../../../src/js/email/account'), describe('Account Service unit test', function() { var account, authStub, outboxStub, emailStub, devicestorageStub, keychainStub, updateHandlerStub, pgpbuilderStub, + realname = 'John Doe', dummyUser = 'spiderpig@springfield.com'; chai.config.includeStack = true; @@ -50,4 +51,165 @@ describe('Account Service unit test', function() { }); }); + describe('init', function() { + it('should fail for invalid email address', function() { + account.init({ + emailAddress: dummyUser.replace('@'), + realname: realname + }, onInit); + + function onInit(err, keys) { + expect(err).to.exist; + expect(keys).to.not.exist; + } + }); + + it('should fail for _accountStore.init', function() { + devicestorageStub.init.throws(new Error('asdf')); + + account.init({ + emailAddress: dummyUser, + realname: realname + }, onInit); + + function onInit(err, keys) { + expect(err.message).to.match(/asdf/); + expect(keys).to.not.exist; + } + }); + + it('should fail for _updateHandler.update', function() { + updateHandlerStub.update.yields(new Error('asdf')); + + account.init({ + emailAddress: dummyUser, + realname: realname + }, onInit); + + function onInit(err, keys) { + expect(err.message).to.match(/Updating/); + expect(keys).to.not.exist; + } + }); + + it('should fail for _keychain.getUserKeyPair', function() { + updateHandlerStub.update.yields(); + keychainStub.getUserKeyPair.yields(new Error('asdf')); + + account.init({ + emailAddress: dummyUser, + realname: realname + }, onInit); + + function onInit(err, keys) { + expect(err.message).to.match(/asdf/); + expect(keys).to.not.exist; + } + }); + + it('should fail for _keychain.refreshKeyForUserId', function() { + var storedKeys = { + publicKey: 'publicKey' + }; + + updateHandlerStub.update.yields(); + keychainStub.getUserKeyPair.yields(null, storedKeys); + keychainStub.refreshKeyForUserId.yields(new Error('asdf')); + + account.init({ + emailAddress: dummyUser, + realname: realname + }, onInit); + + function onInit(err, keys) { + expect(err.message).to.match(/asdf/); + expect(keys).to.not.exist; + } + }); + + it('should fail for _emailDao.init after _keychain.refreshKeyForUserId', function() { + var storedKeys = { + publicKey: 'publicKey' + }; + + updateHandlerStub.update.yields(); + keychainStub.getUserKeyPair.yields(null, storedKeys); + keychainStub.refreshKeyForUserId.yields(null, storedKeys); + emailStub.init.yields(new Error('asdf')); + + account.init({ + emailAddress: dummyUser, + realname: realname + }, onInit); + + function onInit(err, keys) { + expect(err.message).to.match(/asdf/); + expect(keys).to.not.exist; + } + }); + + it('should fail for _emailDao.init', function() { + var storedKeys = { + publicKey: 'publicKey', + privateKey: 'privateKey' + }; + + updateHandlerStub.update.yields(); + keychainStub.getUserKeyPair.yields(null, storedKeys); + emailStub.init.yields(new Error('asdf')); + + account.init({ + emailAddress: dummyUser, + realname: realname + }, onInit); + + function onInit(err, keys) { + expect(err.message).to.match(/asdf/); + expect(keys).to.not.exist; + } + }); + + it('should work after _keychain.refreshKeyForUserId', function() { + var storedKeys = { + publicKey: 'publicKey' + }; + + updateHandlerStub.update.yields(); + keychainStub.getUserKeyPair.yields(null, storedKeys); + keychainStub.refreshKeyForUserId.yields(null, 'publicKey'); + emailStub.init.yields(); + + account.init({ + emailAddress: dummyUser, + realname: realname + }, onInit); + + function onInit(err, keys) { + expect(err).to.not.exist; + expect(keys).to.deep.equal(storedKeys); + } + }); + + it('should work', function() { + var storedKeys = { + publicKey: 'publicKey', + privateKey: 'privateKey' + }; + + updateHandlerStub.update.yields(); + keychainStub.getUserKeyPair.yields(null, storedKeys); + emailStub.init.yields(); + + account.init({ + emailAddress: dummyUser, + realname: realname + }, onInit); + + function onInit(err, keys) { + expect(err).to.not.exist; + expect(keys).to.equal(storedKeys); + } + }); + }); + }); \ No newline at end of file