Write account.init unit test

This commit is contained in:
Tankred Hase 2014-11-24 18:07:09 +01:00
parent c8980f6f51
commit 7f6410e1f8
1 changed files with 162 additions and 0 deletions

View File

@ -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);
}
});
});
});