2014-11-21 11:25:55 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Account = require('../../../src/js/email/account'),
|
|
|
|
appConfig = require('../../../src/js/app-config'),
|
|
|
|
Auth = require('../../../src/js/service/auth'),
|
|
|
|
DeviceStorageDAO = require('../../../src/js/service/devicestorage'),
|
|
|
|
Email = require('../../../src/js/email/email'),
|
|
|
|
Outbox = require('../../../src/js/email/outbox'),
|
|
|
|
Keychain = require('../../../src/js/service/keychain'),
|
2014-11-25 06:07:26 -05:00
|
|
|
UpdateHandler = require('../../../src/js/util/update/update-handler'),
|
|
|
|
Dialog = require('../../../src/js/util/dialog');
|
2014-11-21 11:25:55 -05:00
|
|
|
|
|
|
|
describe('Account Service unit test', function() {
|
2015-01-22 06:07:14 -05:00
|
|
|
var account, authStub, outboxStub, emailStub, devicestorageStub, keychainStub, updateHandlerStub, dialogStub,
|
2014-11-24 12:07:09 -05:00
|
|
|
realname = 'John Doe',
|
2014-11-21 11:25:55 -05:00
|
|
|
dummyUser = 'spiderpig@springfield.com';
|
|
|
|
|
|
|
|
chai.config.includeStack = true;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
authStub = sinon.createStubInstance(Auth);
|
|
|
|
outboxStub = sinon.createStubInstance(Outbox);
|
|
|
|
devicestorageStub = sinon.createStubInstance(DeviceStorageDAO);
|
|
|
|
emailStub = sinon.createStubInstance(Email);
|
|
|
|
outboxStub = sinon.createStubInstance(Outbox);
|
|
|
|
keychainStub = sinon.createStubInstance(Keychain);
|
|
|
|
updateHandlerStub = sinon.createStubInstance(UpdateHandler);
|
2014-11-25 06:07:26 -05:00
|
|
|
dialogStub = sinon.createStubInstance(Dialog);
|
2015-01-22 06:07:14 -05:00
|
|
|
account = new Account(appConfig, authStub, devicestorageStub, emailStub, outboxStub, keychainStub, updateHandlerStub, dialogStub);
|
2014-11-21 11:25:55 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {});
|
|
|
|
|
|
|
|
describe('isLoggedIn', function() {
|
|
|
|
it('should be logged in', function() {
|
|
|
|
account._accounts = [{}];
|
|
|
|
expect(account.isLoggedIn()).to.be.true;
|
|
|
|
});
|
|
|
|
it('should not be logged in', function() {
|
|
|
|
account._accounts = [];
|
|
|
|
expect(account.isLoggedIn()).to.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('list', function() {
|
|
|
|
it('should work', function() {
|
|
|
|
var testAccounts = [{
|
|
|
|
foo: 'bar'
|
|
|
|
}];
|
|
|
|
account._accounts = testAccounts;
|
|
|
|
expect(account.list()).to.deep.equal(testAccounts);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-24 12:07:09 -05:00
|
|
|
describe('init', function() {
|
|
|
|
it('should fail for invalid email address', function() {
|
|
|
|
account.init({
|
|
|
|
emailAddress: dummyUser.replace('@'),
|
|
|
|
realname: realname
|
2014-12-15 13:31:34 -05:00
|
|
|
}).catch(function onInit(err) {
|
2014-11-24 12:07:09 -05:00
|
|
|
expect(err).to.exist;
|
2014-12-15 13:31:34 -05:00
|
|
|
});
|
2014-11-24 12:07:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail for _accountStore.init', function() {
|
2014-12-15 13:31:34 -05:00
|
|
|
devicestorageStub.init.returns(rejects(new Error('asdf')));
|
2014-11-24 12:07:09 -05:00
|
|
|
|
|
|
|
account.init({
|
|
|
|
emailAddress: dummyUser,
|
|
|
|
realname: realname
|
2014-12-15 13:31:34 -05:00
|
|
|
}).catch(function onInit(err) {
|
2014-11-24 12:07:09 -05:00
|
|
|
expect(err.message).to.match(/asdf/);
|
2014-12-15 13:31:34 -05:00
|
|
|
});
|
2014-11-24 12:07:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail for _updateHandler.update', function() {
|
2014-12-15 13:31:34 -05:00
|
|
|
devicestorageStub.init.returns(resolves());
|
|
|
|
updateHandlerStub.update.returns(rejects(new Error('asdf')));
|
2014-11-24 12:07:09 -05:00
|
|
|
|
|
|
|
account.init({
|
|
|
|
emailAddress: dummyUser,
|
|
|
|
realname: realname
|
2014-12-15 13:31:34 -05:00
|
|
|
}).catch(function onInit(err) {
|
2014-11-24 12:07:09 -05:00
|
|
|
expect(err.message).to.match(/Updating/);
|
2014-12-15 13:31:34 -05:00
|
|
|
});
|
2014-11-24 12:07:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail for _keychain.getUserKeyPair', function() {
|
2014-12-15 13:31:34 -05:00
|
|
|
devicestorageStub.init.returns(resolves());
|
|
|
|
updateHandlerStub.update.returns(resolves());
|
|
|
|
keychainStub.getUserKeyPair.returns(rejects(new Error('asdf')));
|
2014-11-24 12:07:09 -05:00
|
|
|
|
|
|
|
account.init({
|
|
|
|
emailAddress: dummyUser,
|
|
|
|
realname: realname
|
2014-12-15 13:31:34 -05:00
|
|
|
}).catch(function(err) {
|
2014-11-24 12:07:09 -05:00
|
|
|
expect(err.message).to.match(/asdf/);
|
2014-12-15 13:31:34 -05:00
|
|
|
});
|
2014-11-24 12:07:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail for _keychain.refreshKeyForUserId', function() {
|
|
|
|
var storedKeys = {
|
|
|
|
publicKey: 'publicKey'
|
|
|
|
};
|
|
|
|
|
2014-12-15 13:31:34 -05:00
|
|
|
devicestorageStub.init.returns(resolves());
|
|
|
|
updateHandlerStub.update.returns(resolves());
|
|
|
|
keychainStub.getUserKeyPair.returns(resolves(storedKeys));
|
|
|
|
keychainStub.refreshKeyForUserId.returns(rejects(new Error('asdf')));
|
2014-11-24 12:07:09 -05:00
|
|
|
|
|
|
|
account.init({
|
|
|
|
emailAddress: dummyUser,
|
|
|
|
realname: realname
|
2014-12-15 13:31:34 -05:00
|
|
|
}).catch(function(err) {
|
2014-11-24 12:07:09 -05:00
|
|
|
expect(err.message).to.match(/asdf/);
|
2014-12-15 13:31:34 -05:00
|
|
|
});
|
2014-11-24 12:07:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail for _emailDao.init after _keychain.refreshKeyForUserId', function() {
|
|
|
|
var storedKeys = {
|
|
|
|
publicKey: 'publicKey'
|
|
|
|
};
|
|
|
|
|
2014-12-15 13:31:34 -05:00
|
|
|
devicestorageStub.init.returns(resolves());
|
|
|
|
updateHandlerStub.update.returns(resolves());
|
|
|
|
keychainStub.getUserKeyPair.returns(resolves(storedKeys));
|
|
|
|
keychainStub.refreshKeyForUserId.returns(resolves(storedKeys));
|
|
|
|
emailStub.init.returns(rejects(new Error('asdf')));
|
2014-11-24 12:07:09 -05:00
|
|
|
|
|
|
|
account.init({
|
|
|
|
emailAddress: dummyUser,
|
|
|
|
realname: realname
|
2014-12-15 13:31:34 -05:00
|
|
|
}).catch(function(err) {
|
2014-11-24 12:07:09 -05:00
|
|
|
expect(err.message).to.match(/asdf/);
|
2014-12-15 13:31:34 -05:00
|
|
|
});
|
2014-11-24 12:07:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail for _emailDao.init', function() {
|
|
|
|
var storedKeys = {
|
|
|
|
publicKey: 'publicKey',
|
|
|
|
privateKey: 'privateKey'
|
|
|
|
};
|
|
|
|
|
2014-12-15 13:31:34 -05:00
|
|
|
devicestorageStub.init.returns(resolves());
|
|
|
|
updateHandlerStub.update.returns(resolves());
|
|
|
|
keychainStub.getUserKeyPair.returns(resolves(storedKeys));
|
|
|
|
emailStub.init.returns(rejects(new Error('asdf')));
|
2014-11-24 12:07:09 -05:00
|
|
|
|
|
|
|
account.init({
|
|
|
|
emailAddress: dummyUser,
|
|
|
|
realname: realname
|
2014-12-15 13:31:34 -05:00
|
|
|
}).catch(function(err) {
|
2014-11-24 12:07:09 -05:00
|
|
|
expect(err.message).to.match(/asdf/);
|
2014-12-15 13:31:34 -05:00
|
|
|
});
|
2014-11-24 12:07:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work after _keychain.refreshKeyForUserId', function() {
|
|
|
|
var storedKeys = {
|
|
|
|
publicKey: 'publicKey'
|
|
|
|
};
|
|
|
|
|
2014-12-15 13:31:34 -05:00
|
|
|
devicestorageStub.init.returns(resolves());
|
|
|
|
updateHandlerStub.update.returns(resolves());
|
|
|
|
keychainStub.getUserKeyPair.returns(resolves(storedKeys));
|
|
|
|
keychainStub.refreshKeyForUserId.returns(resolves('publicKey'));
|
|
|
|
emailStub.init.returns(resolves());
|
2014-11-24 12:07:09 -05:00
|
|
|
|
|
|
|
account.init({
|
|
|
|
emailAddress: dummyUser,
|
|
|
|
realname: realname
|
2014-12-15 13:31:34 -05:00
|
|
|
}, function onInit(keys) {
|
2014-11-24 12:07:09 -05:00
|
|
|
expect(keys).to.deep.equal(storedKeys);
|
2014-12-15 13:31:34 -05:00
|
|
|
expect(keychainStub.refreshKeyForUserId.calledOnce).to.be.true;
|
|
|
|
expect(emailStub.init.calledOnce).to.be.true;
|
|
|
|
});
|
2014-11-24 12:07:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', function() {
|
|
|
|
var storedKeys = {
|
|
|
|
publicKey: 'publicKey',
|
|
|
|
privateKey: 'privateKey'
|
|
|
|
};
|
|
|
|
|
2014-12-15 13:31:34 -05:00
|
|
|
devicestorageStub.init.returns(resolves());
|
|
|
|
updateHandlerStub.update.returns(resolves());
|
|
|
|
keychainStub.getUserKeyPair.returns(resolves(storedKeys));
|
|
|
|
emailStub.init.returns(resolves());
|
2014-11-24 12:07:09 -05:00
|
|
|
|
|
|
|
account.init({
|
|
|
|
emailAddress: dummyUser,
|
|
|
|
realname: realname
|
2014-12-15 13:31:34 -05:00
|
|
|
}, function onInit(keys) {
|
2014-11-24 12:07:09 -05:00
|
|
|
expect(keys).to.equal(storedKeys);
|
2014-12-15 13:31:34 -05:00
|
|
|
expect(keychainStub.refreshKeyForUserId.called).to.be.false;
|
|
|
|
expect(emailStub.init.calledOnce).to.be.true;
|
|
|
|
expect(account._accounts.length).to.equal(1);
|
|
|
|
});
|
2014-11-24 12:07:09 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-25 06:07:26 -05:00
|
|
|
describe('onConnect', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
emailStub._account = {};
|
2014-12-15 13:31:34 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', function(done) {
|
|
|
|
emailStub.onConnect.returns(resolves());
|
2014-11-25 06:07:26 -05:00
|
|
|
|
2014-12-15 13:31:34 -05:00
|
|
|
account.onConnect(function(err) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(emailStub.onConnect.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
2014-11-25 06:07:26 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('onDisconnect', function() {
|
2014-12-15 13:31:34 -05:00
|
|
|
it('should work', function(done) {
|
|
|
|
emailStub.onDisconnect.returns(resolves());
|
|
|
|
account.onDisconnect().then(done);
|
2014-11-25 06:07:26 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('logout', function() {
|
2014-12-15 13:31:34 -05:00
|
|
|
it('should fail due to _auth.logout', function(done) {
|
|
|
|
authStub.logout.returns(rejects(new Error('asdf')));
|
2014-11-25 06:07:26 -05:00
|
|
|
|
2014-12-15 13:31:34 -05:00
|
|
|
account.logout().catch(function(err) {
|
|
|
|
expect(err.message).to.match(/asdf/);
|
|
|
|
done();
|
|
|
|
});
|
2014-11-25 06:07:26 -05:00
|
|
|
});
|
|
|
|
|
2014-12-15 13:31:34 -05:00
|
|
|
it('should fail due to _emailDao.onDisconnect', function(done) {
|
|
|
|
authStub.logout.returns(resolves());
|
|
|
|
emailStub.onDisconnect.returns(rejects(new Error('asdf')));
|
2014-11-25 06:07:26 -05:00
|
|
|
|
2014-12-15 13:31:34 -05:00
|
|
|
account.logout().catch(function(err) {
|
|
|
|
expect(err.message).to.match(/asdf/);
|
|
|
|
done();
|
|
|
|
});
|
2014-11-25 06:07:26 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 11:25:55 -05:00
|
|
|
});
|