diff --git a/src/js/email/email.js b/src/js/email/email.js index 3f5a0ec..ab3f291 100644 --- a/src/js/email/email.js +++ b/src/js/email/email.js @@ -1017,6 +1017,13 @@ Email.prototype.encrypt = function(options) { Email.prototype.onConnect = function(imap) { var self = this; + if (!self.isOnline()) { + // don't try to connect when navigator is offline + return new Promise(function(resolve) { + resolve(); + }); + } + self._account.loggingIn = true; // init imap/smtp clients diff --git a/test/integration/email-dao-test.js b/test/integration/email-dao-test.js index 07999c5..6fd5a81 100644 --- a/test/integration/email-dao-test.js +++ b/test/integration/email-dao-test.js @@ -293,6 +293,7 @@ describe('Email DAO integration tests', function() { passphrase: testAccount.pass, keypair: mockKeyPair }).then(function() { + sinon.stub(accountService._emailDao, 'isOnline').returns(true); accountService._emailDao.onConnect(imapClient); }); }); diff --git a/test/unit/email/email-dao-test.js b/test/unit/email/email-dao-test.js index 6b1f5b2..819257d 100644 --- a/test/unit/email/email-dao-test.js +++ b/test/unit/email/email-dao-test.js @@ -1956,6 +1956,7 @@ describe('Email DAO unit tests', function() { beforeEach(function() { initFoldersStub = sinon.stub(dao, '_initFoldersFromImap'); + sinon.stub(dao, 'isOnline'); delete dao._imapClient; credentials = { @@ -1968,6 +1969,7 @@ describe('Email DAO unit tests', function() { uid: 123, modseq: '123' }]; + dao.isOnline.returns(true); authStub.getCredentials.returns(resolves(credentials)); imapClientStub.login.returns(resolves()); imapClientStub.selectMailbox.returns(resolves()); @@ -1990,6 +1992,16 @@ describe('Email DAO unit tests', function() { done(); }); }); + + it('should not connect when user agent is offline', function(done) { + dao.isOnline.returns(false); + + dao.onConnect(imapClientStub).then(function() { + expect(authStub.getCredentials.called).to.be.false; + + done(); + }); + }); }); describe('#onDisconnect', function() {