1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-21 23:08:50 -05:00
mail/test/unit/util/connection-doctor-test.js

399 lines
14 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
var TCPSocket = require('tcp-socket'),
ImapClient = require('imap-client'),
SmtpClient = require('wo-smtpclient'),
2014-11-21 07:33:22 -05:00
ConnectionDoctor = require('../../../src/js/util/connection-doctor'),
appConfig = require('../../../src/js/app-config'),
cfg = appConfig.config;
2014-10-07 14:32:23 -04:00
describe('Connection Doctor', function() {
var doctor;
2014-10-15 13:58:26 -04:00
var socketStub, imapStub, smtpStub, credentials, workerPath;
2014-10-07 14:32:23 -04:00
beforeEach(function() {
//
// Stubs
//
// there is no socket shim for for this use case, use dummy object
socketStub = {
close: function() {
this.onclose();
}
};
2014-11-21 07:33:22 -05:00
workerPath = '../lib/tcp-socket-tls-worker.min.js';
2014-10-07 14:32:23 -04:00
imapStub = sinon.createStubInstance(ImapClient);
smtpStub = sinon.createStubInstance(SmtpClient);
//
// Fixture
//
credentials = {
imap: {
host: 'asd',
port: 1234,
secure: true,
ca: 'cert'
},
smtp: {
host: 'qwe',
port: 5678,
secure: false,
ca: 'cert'
},
username: 'username',
password: 'password'
};
sinon.stub(TCPSocket, 'open').returns(socketStub); // convenience constructors suck
//
// Setup SUT
//
2014-12-11 12:12:37 -05:00
doctor = new ConnectionDoctor(appConfig);
2014-10-07 14:32:23 -04:00
doctor.configure(credentials);
doctor._imap = imapStub;
doctor._smtp = smtpStub;
});
2014-10-07 14:32:23 -04:00
afterEach(function() {
TCPSocket.open.restore();
});
2014-10-07 14:32:23 -04:00
describe('#_checkOnline', function() {
it('should check if browser is online', function(done) {
2014-12-11 07:55:55 -05:00
if (navigator.onLine) {
doctor._checkOnline().then(done);
} else {
doctor._checkOnline().catch(function(err) {
expect(err.code).to.equal(ConnectionDoctor.OFFLINE);
done();
});
}
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('#_checkReachable', function() {
it('should be able to reach the host w/o cert', function(done) {
credentials.imap.ca = undefined;
2014-12-11 07:55:55 -05:00
doctor._checkReachable(credentials.imap).then(function() {
2014-10-07 14:32:23 -04:00
expect(TCPSocket.open.calledOnce).to.be.true;
expect(TCPSocket.open.calledWith(credentials.imap.host, credentials.imap.port, {
binaryType: 'arraybuffer',
useSecureTransport: credentials.imap.secure,
2014-10-15 13:58:26 -04:00
ca: credentials.imap.ca,
tlsWorkerPath: workerPath
2014-10-07 14:32:23 -04:00
})).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
socketStub.oncert();
socketStub.onopen();
});
2014-10-07 14:32:23 -04:00
it('should catch Mozilla TCPSocket exception', function(done) {
// Mozilla forbids extensions to the TCPSocket object
Object.defineProperty(socketStub, 'oncert', {
set: function() {
throw 'Mozilla specific behavior';
}
});
2014-12-11 07:55:55 -05:00
doctor._checkReachable(credentials.imap).then(function() {
2014-10-07 14:32:23 -04:00
expect(TCPSocket.open.calledOnce).to.be.true;
expect(TCPSocket.open.calledWith(credentials.imap.host, credentials.imap.port, {
binaryType: 'arraybuffer',
useSecureTransport: credentials.imap.secure,
2014-10-15 13:58:26 -04:00
ca: credentials.imap.ca,
tlsWorkerPath: workerPath
2014-10-07 14:32:23 -04:00
})).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
socketStub.onopen();
});
2014-10-07 14:32:23 -04:00
it('should fail w/ wrong cert', function(done) {
2014-12-11 07:55:55 -05:00
doctor._checkReachable(credentials.imap).catch(function(error) {
2014-10-07 14:32:23 -04:00
expect(error.code).to.equal(ConnectionDoctor.TLS_WRONG_CERT);
expect(TCPSocket.open.calledOnce).to.be.true;
expect(TCPSocket.open.calledWith(credentials.imap.host, credentials.imap.port, {
binaryType: 'arraybuffer',
useSecureTransport: credentials.imap.secure,
2014-10-15 13:58:26 -04:00
ca: credentials.imap.ca,
tlsWorkerPath: workerPath
2014-10-07 14:32:23 -04:00
})).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
socketStub.oncert();
socketStub.onerror();
socketStub.onclose();
});
2014-10-07 14:32:23 -04:00
it('should fail w/ host unreachable', function(done) {
2014-12-11 07:55:55 -05:00
doctor._checkReachable(credentials.imap).catch(function(error) {
2014-10-07 14:32:23 -04:00
expect(error.code).to.equal(ConnectionDoctor.HOST_UNREACHABLE);
expect(TCPSocket.open.calledOnce).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
socketStub.onerror({
data: new Error()
});
2014-10-07 14:32:23 -04:00
socketStub.onclose();
});
2014-10-07 14:32:23 -04:00
it('should fail w/ timeout', function(done) {
var origTimeout = cfg.connDocTimeout; // remember timeout from the config to reset it on done
cfg.connDocTimeout = 20; // set to 20ms for the test
2014-12-11 07:55:55 -05:00
doctor._checkReachable(credentials.imap).catch(function(error) {
2014-10-07 14:32:23 -04:00
expect(error.code).to.equal(ConnectionDoctor.HOST_TIMEOUT);
expect(TCPSocket.open.calledOnce).to.be.true;
cfg.connDocTimeout = origTimeout;
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('#_checkImap', function() {
it('should perform IMAP login, list folders, logout', function(done) {
2015-02-12 10:10:46 -05:00
imapStub.login.returns(resolves());
imapStub.listWellKnownFolders.returns(resolves({
2014-10-07 14:32:23 -04:00
Inbox: [{}]
2015-02-12 10:10:46 -05:00
}));
imapStub.logout.returns(resolves());
2014-12-11 07:55:55 -05:00
doctor._checkImap().then(function() {
2014-10-07 14:32:23 -04:00
expect(imapStub.login.calledOnce).to.be.true;
expect(imapStub.listWellKnownFolders.calledOnce).to.be.true;
expect(imapStub.logout.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
});
2014-10-07 14:32:23 -04:00
it('should fail w/ generic error on logout', function(done) {
2015-02-12 10:10:46 -05:00
imapStub.login.returns(resolves());
imapStub.listWellKnownFolders.returns(resolves({
2014-10-07 14:32:23 -04:00
Inbox: [{}]
2015-02-12 10:10:46 -05:00
}));
imapStub.logout.returns(rejects(new Error()));
2014-12-11 07:55:55 -05:00
doctor._checkImap().catch(function(error) {
2014-10-07 14:32:23 -04:00
expect(error.code).to.equal(ConnectionDoctor.GENERIC_ERROR);
expect(error.underlyingError).to.exist;
expect(imapStub.login.calledOnce).to.be.true;
expect(imapStub.listWellKnownFolders.calledOnce).to.be.true;
expect(imapStub.logout.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 w/ generic error on inbox missing', function(done) {
2015-02-12 10:10:46 -05:00
imapStub.login.returns(resolves());
imapStub.listWellKnownFolders.returns(resolves({
2014-10-07 14:32:23 -04:00
Inbox: []
2015-02-12 10:10:46 -05:00
}));
2014-12-11 07:55:55 -05:00
doctor._checkImap().catch(function(error) {
2014-10-07 14:32:23 -04:00
expect(error.code).to.equal(ConnectionDoctor.NO_INBOX);
expect(imapStub.login.calledOnce).to.be.true;
expect(imapStub.listWellKnownFolders.calledOnce).to.be.true;
expect(imapStub.logout.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
it('should fail w/ generic error on listing folders fails', function(done) {
2015-02-12 10:10:46 -05:00
imapStub.login.returns(resolves());
imapStub.listWellKnownFolders.returns(rejects(new Error()));
2014-12-11 07:55:55 -05:00
doctor._checkImap().catch(function(error) {
2014-10-07 14:32:23 -04:00
expect(error.code).to.equal(ConnectionDoctor.GENERIC_ERROR);
expect(error.underlyingError).to.exist;
expect(imapStub.login.calledOnce).to.be.true;
expect(imapStub.listWellKnownFolders.calledOnce).to.be.true;
expect(imapStub.logout.called).to.be.false;
2014-10-07 14:32:23 -04:00
done();
});
});
2014-10-07 14:32:23 -04:00
it('should fail w/ auth rejected', function(done) {
2015-02-12 10:10:46 -05:00
imapStub.login.returns(new Promise(function() {
setTimeout(function() {
imapStub.onError(new Error());
}, 0);
}));
2014-12-11 07:55:55 -05:00
doctor._checkImap().catch(function(error) {
2014-10-07 14:32:23 -04:00
expect(error.code).to.equal(ConnectionDoctor.AUTH_REJECTED);
expect(error.underlyingError).to.exist;
expect(imapStub.login.calledOnce).to.be.true;
expect(imapStub.listWellKnownFolders.called).to.be.false;
expect(imapStub.logout.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('#_checkSmtp', function() {
it('should perform SMTP login, logout', function(done) {
2014-12-11 07:55:55 -05:00
doctor._checkSmtp().then(function() {
2014-10-07 14:32:23 -04:00
expect(smtpStub.connect.calledOnce).to.be.true;
expect(smtpStub.quit.calledOnce).to.be.true;
2014-10-07 14:32:23 -04:00
done();
});
2014-10-07 14:32:23 -04:00
smtpStub.onidle();
smtpStub.onclose();
});
2014-10-07 14:32:23 -04:00
it('should fail w/ auth rejected', function(done) {
2014-12-11 07:55:55 -05:00
doctor._checkSmtp().catch(function(error) {
2014-10-07 14:32:23 -04:00
expect(error.code).to.equal(ConnectionDoctor.AUTH_REJECTED);
expect(error.underlyingError).to.exist;
expect(smtpStub.connect.calledOnce).to.be.true;
expect(smtpStub.quit.called).to.be.false;
done();
});
2014-10-07 14:32:23 -04:00
smtpStub.onerror(new Error());
});
});
describe('#check', function() {
beforeEach(function() {
sinon.stub(doctor, '_checkOnline');
sinon.stub(doctor, '_checkReachable');
sinon.stub(doctor, '_checkImap');
sinon.stub(doctor, '_checkSmtp');
});
it('should perform all tests', function(done) {
2014-12-11 07:55:55 -05:00
doctor._checkOnline.returns(resolves());
doctor._checkReachable.withArgs(credentials.imap).returns(resolves());
doctor._checkReachable.withArgs(credentials.smtp).returns(resolves());
doctor._checkImap.returns(resolves());
doctor._checkSmtp.returns(resolves());
doctor.check().then(function() {
2014-10-07 14:32:23 -04:00
expect(doctor._checkOnline.calledOnce).to.be.true;
expect(doctor._checkReachable.calledTwice).to.be.true;
expect(doctor._checkImap.calledOnce).to.be.true;
expect(doctor._checkSmtp.calledOnce).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail for smtp', function(done) {
2014-12-11 07:55:55 -05:00
doctor._checkOnline.returns(resolves());
doctor._checkReachable.withArgs(credentials.imap).returns(resolves());
doctor._checkReachable.withArgs(credentials.smtp).returns(resolves());
doctor._checkImap.returns(resolves());
doctor._checkSmtp.returns(rejects(new Error()));
2014-10-07 14:32:23 -04:00
2014-12-11 07:55:55 -05:00
doctor.check().catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
expect(doctor._checkOnline.calledOnce).to.be.true;
expect(doctor._checkReachable.calledTwice).to.be.true;
expect(doctor._checkImap.calledOnce).to.be.true;
expect(doctor._checkSmtp.calledOnce).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
});
it('should fail for imap', function(done) {
2014-12-11 07:55:55 -05:00
doctor._checkOnline.returns(resolves());
doctor._checkReachable.withArgs(credentials.imap).returns(resolves());
doctor._checkReachable.withArgs(credentials.smtp).returns(resolves());
doctor._checkImap.returns(rejects(new Error()));
2014-12-11 07:55:55 -05:00
doctor.check().catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
expect(doctor._checkOnline.calledOnce).to.be.true;
expect(doctor._checkReachable.calledTwice).to.be.true;
expect(doctor._checkImap.calledOnce).to.be.true;
expect(doctor._checkSmtp.called).to.be.false;
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail for smtp reachability', function(done) {
2014-12-11 07:55:55 -05:00
doctor._checkOnline.returns(resolves());
doctor._checkReachable.withArgs(credentials.imap).returns(resolves());
doctor._checkReachable.withArgs(credentials.smtp).returns(rejects(new Error()));
2014-12-11 07:55:55 -05:00
doctor.check().catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
expect(doctor._checkOnline.calledOnce).to.be.true;
expect(doctor._checkReachable.calledTwice).to.be.true;
expect(doctor._checkImap.called).to.be.false;
expect(doctor._checkSmtp.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
it('should fail for imap reachability', function(done) {
2014-12-11 07:55:55 -05:00
doctor._checkOnline.returns(resolves());
doctor._checkReachable.withArgs(credentials.imap).returns(rejects(new Error()));
2014-12-11 07:55:55 -05:00
doctor.check().catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
expect(doctor._checkOnline.calledOnce).to.be.true;
expect(doctor._checkReachable.calledOnce).to.be.true;
expect(doctor._checkImap.called).to.be.false;
expect(doctor._checkSmtp.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
it('should fail for offline', function(done) {
2014-12-11 07:55:55 -05:00
doctor._checkOnline.returns(rejects(new Error()));
2014-12-11 07:55:55 -05:00
doctor.check().catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
expect(doctor._checkOnline.calledOnce).to.be.true;
expect(doctor._checkReachable.called).to.be.false;
expect(doctor._checkImap.called).to.be.false;
expect(doctor._checkSmtp.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
it('should fail w/o config', function(done) {
doctor.credentials = doctor._imap = doctor._smtp = undefined;
2014-12-11 07:55:55 -05:00
doctor.check().catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
expect(doctor._checkOnline.called).to.be.false;
expect(doctor._checkReachable.called).to.be.false;
expect(doctor._checkImap.called).to.be.false;
expect(doctor._checkSmtp.called).to.be.false;
2014-10-07 14:32:23 -04:00
done();
});
});
});
2014-11-21 07:33:22 -05:00
});