1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-14 05:05:10 -05:00
mail/test/unit/service/auth-test.js

396 lines
14 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
var Auth = require('../../../src/js/service/auth'),
OAuth = require('../../../src/js/service/oauth'),
PGP = require('../../../src/js/crypto/pgp'),
DeviceStorageDAO = require('../../../src/js/service/devicestorage');
2014-10-07 14:32:23 -04:00
describe('Auth unit tests', function() {
// Constancts
var EMAIL_ADDR_DB_KEY = 'emailaddress';
var USERNAME_DB_KEY = 'username';
var REALNAME_DB_KEY = 'realname';
var PASSWD_DB_KEY = 'password';
var IMAP_DB_KEY = 'imap';
var SMTP_DB_KEY = 'smtp';
var APP_CONFIG_DB_NAME = 'app-config';
2014-10-07 14:32:23 -04:00
// SUT
var auth;
// Dependencies
var storageStub, oauthStub, pgpStub;
// test data
var emailAddress = 'bla@blubb.com';
var password = 'passwordpasswordpassword';
var encryptedPassword = 'pgppasswordpgppassword';
var oauthToken = 'tokentokentokentoken';
var realname = 'Bla Blubb';
var username = 'bla';
var imap = {
host: 'mail.blablubb.com',
port: 123,
secure: true,
ca: 'PEMPEMPEMPEMPEMPEMPEMPEMPEMPEM'
};
var smtp = {
host: 'mail.blablubb.com',
port: 456,
secure: true,
ca: 'PEMPEMPEMPEMPEMPEMPEMPEMPEMPEM'
};
beforeEach(function() {
storageStub = sinon.createStubInstance(DeviceStorageDAO);
oauthStub = sinon.createStubInstance(OAuth);
pgpStub = sinon.createStubInstance(PGP);
auth = new Auth(storageStub, oauthStub, pgpStub);
});
describe('#init', function() {
it('should initialize a user db', function(done) {
2014-12-11 14:27:55 -05:00
storageStub.init.withArgs(APP_CONFIG_DB_NAME).returns(resolves());
auth.init().then(function() {
expect(auth._initialized).to.be.true;
done();
});
});
it('should initialize a user db', function(done) {
2014-12-11 14:27:55 -05:00
storageStub.init.withArgs(APP_CONFIG_DB_NAME).returns(rejects(new Error()));
auth.init().catch(function(err) {
expect(err).to.exist;
expect(auth._initialized).to.be.false;
done();
});
});
});
2014-10-07 14:32:23 -04:00
describe('#getCredentials', function() {
it('should load credentials and retrieve credentials from cfg', function(done) {
2014-12-11 14:27:55 -05:00
storageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY, 0, null).returns(resolves([emailAddress]));
storageStub.listItems.withArgs(PASSWD_DB_KEY, 0, null).returns(resolves([encryptedPassword]));
storageStub.listItems.withArgs(USERNAME_DB_KEY, 0, null).returns(resolves([username]));
storageStub.listItems.withArgs(REALNAME_DB_KEY, 0, null).returns(resolves([realname]));
storageStub.listItems.withArgs(IMAP_DB_KEY, 0, null).returns(resolves([imap]));
storageStub.listItems.withArgs(SMTP_DB_KEY, 0, null).returns(resolves([smtp]));
pgpStub.decrypt.withArgs(encryptedPassword, undefined).returns(resolves(password));
auth.getCredentials().then(function(cred) {
2014-10-07 14:32:23 -04:00
expect(auth.emailAddress).to.equal(emailAddress);
expect(auth.password).to.equal(password);
2014-10-07 14:32:23 -04:00
expect(cred.imap.host).to.equal(imap.host);
expect(cred.imap.port).to.equal(imap.port);
expect(cred.imap.secure).to.equal(imap.secure);
expect(cred.imap.ca).to.equal(imap.ca);
expect(cred.imap.auth.user).to.equal(username);
expect(cred.imap.auth.pass).to.equal(password);
2014-10-07 14:32:23 -04:00
expect(cred.smtp.host).to.equal(smtp.host);
expect(cred.smtp.port).to.equal(smtp.port);
expect(cred.smtp.secure).to.equal(smtp.secure);
expect(cred.smtp.ca).to.equal(smtp.ca);
expect(cred.smtp.auth.user).to.equal(username);
expect(cred.smtp.auth.pass).to.equal(password);
expect(storageStub.listItems.callCount).to.equal(6);
2014-10-07 14:32:23 -04:00
expect(pgpStub.decrypt.calledOnce).to.be.true;
done();
});
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('#setCredentials', function() {
it('should set the credentials', function() {
auth.setCredentials({
emailAddress: emailAddress,
username: username,
realname: realname,
password: password,
imap: imap,
smtp: smtp
});
2014-10-07 14:32:23 -04:00
expect(auth.emailAddress).to.equal(emailAddress);
expect(auth.username).to.equal(username);
expect(auth.realname).to.equal(realname);
expect(auth.password).to.equal(password);
expect(auth.smtp).to.equal(smtp);
expect(auth.imap).to.equal(imap);
expect(auth.credentialsDirty).to.be.true;
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('#storeCredentials', function() {
it('should persist ALL the things!', function(done) {
auth.credentialsDirty = true;
auth.emailAddress = emailAddress;
auth.username = username;
auth.realname = realname;
auth.password = password;
auth.smtp = smtp;
auth.imap = imap;
2014-12-11 14:27:55 -05:00
storageStub.storeList.withArgs([encryptedPassword], PASSWD_DB_KEY).returns(resolves());
storageStub.storeList.withArgs([emailAddress], EMAIL_ADDR_DB_KEY).returns(resolves());
storageStub.storeList.withArgs([username], USERNAME_DB_KEY).returns(resolves());
storageStub.storeList.withArgs([realname], REALNAME_DB_KEY).returns(resolves());
storageStub.storeList.withArgs([imap], IMAP_DB_KEY).returns(resolves());
storageStub.storeList.withArgs([smtp], SMTP_DB_KEY).returns(resolves());
pgpStub.encrypt.withArgs(password).returns(resolves(encryptedPassword));
2014-10-07 14:32:23 -04:00
2014-12-11 14:27:55 -05:00
auth.storeCredentials().then(function() {
expect(storageStub.storeList.callCount).to.equal(6);
2014-10-07 14:32:23 -04:00
expect(pgpStub.encrypt.calledOnce).to.be.true;
done();
});
});
2014-10-07 14:32:23 -04:00
});
describe('#useOAuth', function() {
it('should not recommend oauth for unsupported platorm', function() {
oauthStub.isSupported.returns(false);
var res = auth.useOAuth('imap.gmail.com');
expect(res).to.be.false;
});
it('should recommend oauth for whitelisted host', function() {
oauthStub.isSupported.returns(true);
var res = auth.useOAuth('imap.gmail.com');
expect(res).to.be.true;
});
it('should not recommend oauth for other hosts', function() {
oauthStub.isSupported.returns(true);
var res = auth.useOAuth('imap.ggmail.com');
expect(res).to.be.false;
});
});
2014-10-07 14:32:23 -04:00
describe('#getOAuthToken', function() {
it('should refresh token with known email address', function(done) {
auth.emailAddress = emailAddress;
auth.oauthToken = 'oldToken';
2014-10-07 14:32:23 -04:00
oauthStub.refreshToken.withArgs({
emailAddress: emailAddress,
oldToken: 'oldToken'
2014-12-11 14:27:55 -05:00
}).returns(resolves(oauthToken));
2014-12-11 14:27:55 -05:00
auth.getOAuthToken().then(function() {
2014-10-07 14:32:23 -04:00
expect(auth.emailAddress).to.equal(emailAddress);
expect(auth.oauthToken).to.equal(oauthToken);
expect(oauthStub.refreshToken.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fetch token with known email address', function(done) {
auth.emailAddress = emailAddress;
2014-12-11 14:27:55 -05:00
oauthStub.getOAuthToken.withArgs(emailAddress).returns(resolves(oauthToken));
2014-12-11 14:27:55 -05:00
auth.getOAuthToken().then(function() {
2014-10-07 14:32:23 -04:00
expect(auth.emailAddress).to.equal(emailAddress);
expect(auth.oauthToken).to.equal(oauthToken);
expect(oauthStub.getOAuthToken.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fetch token with unknown email address', function(done) {
2014-12-11 14:27:55 -05:00
oauthStub.getOAuthToken.withArgs(undefined).returns(resolves(oauthToken));
oauthStub.queryEmailAddress.withArgs(oauthToken).returns(resolves(emailAddress));
2014-12-11 14:27:55 -05:00
auth.getOAuthToken().then(function() {
2014-10-07 14:32:23 -04:00
expect(auth.emailAddress).to.equal(emailAddress);
expect(auth.oauthToken).to.equal(oauthToken);
expect(oauthStub.getOAuthToken.calledOnce).to.be.true;
expect(oauthStub.queryEmailAddress.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when email address fetch fails', function(done) {
2014-12-11 14:27:55 -05:00
oauthStub.getOAuthToken.returns(resolves(oauthToken));
oauthStub.queryEmailAddress.returns(rejects(new Error()));
2014-12-11 14:27:55 -05:00
auth.getOAuthToken().catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
expect(auth.emailAddress).to.not.exist;
expect(auth.oauthToken).to.not.exist;
expect(oauthStub.getOAuthToken.calledOnce).to.be.true;
expect(oauthStub.queryEmailAddress.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail when oauth fetch fails', function(done) {
2014-12-11 14:27:55 -05:00
oauthStub.getOAuthToken.returns(rejects(new Error()));
2014-12-11 14:27:55 -05:00
auth.getOAuthToken().catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
expect(auth.emailAddress).to.not.exist;
expect(auth.oauthToken).to.not.exist;
expect(oauthStub.getOAuthToken.calledOnce).to.be.true;
expect(oauthStub.queryEmailAddress.called).to.be.false;
2014-10-07 14:32:23 -04:00
done();
});
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('#_loadCredentials', function() {
it('should work', function(done) {
2014-12-11 14:27:55 -05:00
storageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY, 0, null).returns(resolves([emailAddress]));
storageStub.listItems.withArgs(PASSWD_DB_KEY, 0, null).returns(resolves([encryptedPassword]));
storageStub.listItems.withArgs(USERNAME_DB_KEY, 0, null).returns(resolves([username]));
storageStub.listItems.withArgs(REALNAME_DB_KEY, 0, null).returns(resolves([realname]));
storageStub.listItems.withArgs(IMAP_DB_KEY, 0, null).returns(resolves([imap]));
storageStub.listItems.withArgs(SMTP_DB_KEY, 0, null).returns(resolves([smtp]));
auth._loadCredentials().then(function() {
2014-10-07 14:32:23 -04:00
expect(auth.emailAddress).to.equal(emailAddress);
expect(auth.password).to.equal(encryptedPassword);
expect(auth.imap).to.equal(imap);
expect(auth.smtp).to.equal(smtp);
expect(auth.username).to.equal(username);
expect(auth.realname).to.equal(realname);
2014-10-07 14:32:23 -04:00
expect(auth.passwordNeedsDecryption).to.be.true;
expect(storageStub.listItems.callCount).to.equal(6);
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail', function(done) {
2014-12-11 14:27:55 -05:00
storageStub.listItems.returns(rejects(new Error()));
2014-12-11 14:27:55 -05:00
auth._loadCredentials().catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
expect(auth.emailAddress).to.not.exist;
expect(auth.password).to.not.exist;
expect(auth.imap).to.not.exist;
expect(auth.smtp).to.not.exist;
expect(auth.username).to.not.exist;
expect(auth.realname).to.not.exist;
2014-10-07 14:32:23 -04:00
expect(storageStub.listItems.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('#handleCertificateUpdate', function() {
var storeCredentialsStub;
var dummyCert = 'cert';
2014-10-07 14:32:23 -04:00
function onConnectDummy() {}
2014-10-07 14:32:23 -04:00
beforeEach(function() {
storeCredentialsStub = sinon.stub(auth, 'storeCredentials');
});
2014-10-07 14:32:23 -04:00
it('should work for Trust on first use', function(done) {
auth.imap = {};
2014-12-11 14:27:55 -05:00
storeCredentialsStub.returns(resolves());
2014-12-11 14:27:55 -05:00
function callback() {
2014-10-07 14:32:23 -04:00
expect(storeCredentialsStub.callCount).to.equal(1);
done();
}
2014-12-15 13:31:34 -05:00
auth.handleCertificateUpdate('imap', onConnectDummy, callback, dummyCert);
2014-10-07 14:32:23 -04:00
});
it('should work for stored cert', function() {
auth.imap = {
ca: dummyCert
};
2014-12-11 14:27:55 -05:00
storeCredentialsStub.returns(resolves());
2014-12-15 13:31:34 -05:00
function callback() {}
auth.handleCertificateUpdate('imap', onConnectDummy, callback, dummyCert);
2014-10-07 14:32:23 -04:00
expect(storeCredentialsStub.callCount).to.equal(0);
});
2014-10-07 14:32:23 -04:00
it('should work for pinned cert', function(done) {
auth.imap = {
ca: 'other',
pinned: true
};
2014-12-11 14:27:55 -05:00
storeCredentialsStub.returns(resolves());
2014-10-07 14:32:23 -04:00
function callback(err) {
expect(err).to.exist;
expect(err.message).to.exist;
expect(storeCredentialsStub.callCount).to.equal(0);
2014-10-07 14:32:23 -04:00
done();
}
2014-12-15 13:31:34 -05:00
auth.handleCertificateUpdate('imap', onConnectDummy, callback, dummyCert);
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should work for updated cert', function(done) {
auth.imap = {
ca: 'other'
};
2014-12-11 14:27:55 -05:00
storeCredentialsStub.returns(resolves());
2014-10-07 14:32:23 -04:00
function callback(err) {
if (err && err.callback) {
expect(err).to.exist;
expect(err.message).to.exist;
expect(storeCredentialsStub.callCount).to.equal(0);
2014-12-15 13:31:34 -05:00
err.callback(true);
} else {
expect(storeCredentialsStub.callCount).to.equal(1);
done();
}
2014-10-07 14:32:23 -04:00
}
2014-12-15 13:31:34 -05:00
function onConnect(cb) {
cb();
}
auth.handleCertificateUpdate('imap', onConnect, callback, dummyCert);
});
2014-10-07 14:32:23 -04:00
});
2014-09-26 13:14:53 -04:00
2014-10-07 14:32:23 -04:00
describe('#logout', function() {
it('should fail to to error in calling db clear', function(done) {
2014-12-11 14:27:55 -05:00
storageStub.clear.returns(rejects(new Error()));
2014-09-26 13:14:53 -04:00
2014-12-11 14:27:55 -05:00
auth.logout().catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
done();
2014-09-26 13:14:53 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-09-26 13:14:53 -04:00
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
2014-12-11 14:27:55 -05:00
storageStub.clear.returns(resolves());
2014-09-26 13:14:53 -04:00
2014-12-11 14:27:55 -05:00
auth.logout().then(function() {
2014-10-07 14:32:23 -04:00
expect(auth.password).to.be.undefined;
expect(auth.initialized).to.be.undefined;
expect(auth.credentialsDirty).to.be.undefined;
expect(auth.passwordNeedsDecryption).to.be.undefined;
done();
2014-09-26 13:14:53 -04:00
});
});
});
});