1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-22 17:02:17 -05:00

Fix integration test setup and teardown... some tests stil fails

This commit is contained in:
Tankred Hase 2014-11-27 13:55:40 +01:00
parent f740b27659
commit f8f7588c93
3 changed files with 110 additions and 99 deletions

View File

@ -179,7 +179,6 @@ module.exports = function(grunt) {
'test/unit/email/email-dao-test.js', 'test/unit/email/email-dao-test.js',
'test/unit/email/account-test.js', 'test/unit/email/account-test.js',
'test/unit/email/search-test.js', 'test/unit/email/search-test.js',
'test/unit/controller/app/dialog-ctrl-test.js',
'test/unit/controller/login/add-account-ctrl-test.js', 'test/unit/controller/login/add-account-ctrl-test.js',
'test/unit/controller/login/create-account-ctrl-test.js', 'test/unit/controller/login/create-account-ctrl-test.js',
'test/unit/controller/login/validate-phone-ctrl-test.js', 'test/unit/controller/login/validate-phone-ctrl-test.js',
@ -189,6 +188,7 @@ module.exports = function(grunt) {
'test/unit/controller/login/login-privatekey-download-ctrl-test.js', 'test/unit/controller/login/login-privatekey-download-ctrl-test.js',
'test/unit/controller/login/login-set-credentials-ctrl-test.js', 'test/unit/controller/login/login-set-credentials-ctrl-test.js',
'test/unit/controller/login/login-ctrl-test.js', 'test/unit/controller/login/login-ctrl-test.js',
'test/unit/controller/app/dialog-ctrl-test.js',
'test/unit/controller/app/privatekey-upload-ctrl-test.js', 'test/unit/controller/app/privatekey-upload-ctrl-test.js',
'test/unit/controller/app/account-ctrl-test.js', 'test/unit/controller/app/account-ctrl-test.js',
'test/unit/controller/app/set-passphrase-ctrl-test.js', 'test/unit/controller/app/set-passphrase-ctrl-test.js',
@ -206,7 +206,7 @@ module.exports = function(grunt) {
files: { files: {
'test/integration/index.browserified.js': [ 'test/integration/index.browserified.js': [
'test/main.js', 'test/main.js',
/*'test/integration/email-dao-test.js'*/ 'test/integration/email-dao-test.js'
] ]
}, },
options: browserifyOpt options: browserifyOpt
@ -281,7 +281,6 @@ module.exports = function(grunt) {
'node_modules/jquery/dist/jquery.min.js', 'node_modules/jquery/dist/jquery.min.js',
'src/lib/angular/angular.js', 'src/lib/angular/angular.js',
'src/lib/angular/angular-route.js', 'src/lib/angular/angular-route.js',
'src/lib/angular/angular-animate.js',
'src/lib/angular/angular-mocks.js', 'src/lib/angular/angular-mocks.js',
'src/lib/lawnchair/lawnchair-git.js', 'src/lib/lawnchair/lawnchair-git.js',
'src/lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js', 'src/lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js',
@ -296,6 +295,9 @@ module.exports = function(grunt) {
integrationTest: { integrationTest: {
src: [ src: [
'src/lib/underscore/underscore.js', 'src/lib/underscore/underscore.js',
'node_modules/jquery/dist/jquery.min.js',
'src/lib/angular/angular.js',
'src/lib/angular/angular-mocks.js',
'src/lib/lawnchair/lawnchair-git.js', 'src/lib/lawnchair/lawnchair-git.js',
'src/lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js', 'src/lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js',
'src/lib/lawnchair/lawnchair-adapter-indexed-db-git.js', 'src/lib/lawnchair/lawnchair-adapter-indexed-db-git.js',

View File

@ -4,9 +4,8 @@ var ImapClient = require('imap-client'),
BrowserCrow = require('browsercrow'), BrowserCrow = require('browsercrow'),
BrowserSMTP = require('browsersmtp'), BrowserSMTP = require('browsersmtp'),
SmtpClient = require('wo-smtpclient'), SmtpClient = require('wo-smtpclient'),
LawnchairDAO = require('../../src/js/dao/lawnchair-dao'), LawnchairDAO = require('../../src/js/service/lawnchair'),
DeviceStorageDAO = require('../../src/js/dao/devicestorage-dao'), DeviceStorageDAO = require('../../src/js/service/devicestorage'),
appController = require('../../src/js/app-controller'),
mailreader = require('mailreader'), mailreader = require('mailreader'),
PgpMailer = require('pgpmailer'), PgpMailer = require('pgpmailer'),
config = require('../../src/js/app-config').config, config = require('../../src/js/app-config').config,
@ -14,9 +13,8 @@ var ImapClient = require('imap-client'),
describe('Email DAO integration tests', function() { describe('Email DAO integration tests', function() {
this.timeout(100000); this.timeout(100000);
chai.config.includeStack = true;
var emailDao, imapClient, imapMessages, imapFolders, imapServer, smtpServer, smtpClient, userStorage, var accountService, emailDao, imapClient, imapMessages, imapFolders, imapServer, smtpServer, smtpClient, userStorage,
mockKeyPair, inbox, spam; mockKeyPair, inbox, spam;
var testAccount = { var testAccount = {
@ -25,7 +23,11 @@ describe('Email DAO integration tests', function() {
xoauth2: 'testtoken' xoauth2: 'testtoken'
}; };
before(function(done) { beforeEach(function(done) {
//
// Test data
//
imapMessages = [{ imapMessages = [{
raw: 'Message-id: <c>\r\nSubject: hello 3\r\n\r\nWorld 3!', raw: 'Message-id: <c>\r\nSubject: hello 3\r\n\r\nWorld 3!',
@ -129,6 +131,10 @@ describe('Email DAO integration tests', function() {
} }
}; };
//
// Test server setup
//
var serverUsers = {}; var serverUsers = {};
serverUsers[testAccount.user] = { serverUsers[testAccount.user] = {
password: testAccount.pass, password: testAccount.pass,
@ -180,124 +186,125 @@ describe('Email DAO integration tests', function() {
} }
}; };
//
// Test client setup
//
// don't multithread, Function.prototype.bind() is broken in phantomjs in web workers // don't multithread, Function.prototype.bind() is broken in phantomjs in web workers
window.Worker = undefined; window.Worker = undefined;
navigator.online = true; navigator.online = true;
imapClient = new ImapClient({
auth: {
user: testAccount.user,
xoauth2: testAccount.xoauth2
},
secure: true,
ca: ['random string']
});
imapClient._client.client._TCPSocket = imapServer.createTCPSocket();
imapClient._listeningClient.client._TCPSocket = imapServer.createTCPSocket();
imapClient.onError = function(err) {
console.log('IMAP error.', err);
console.log('IMAP reconnecting...');
// re-init client modules on error
appController.onConnect(function(err) {
if (err) {
console.error('IMAP reconnect failed!', err);
return;
}
console.log('IMAP reconnect attempt complete.');
});
};
smtpClient = new SmtpClient('localhost', 25, {
auth: {
user: testAccount.user,
xoauth2: testAccount.xoauth2
},
secure: true,
ca: ['random string'],
onError: console.error
});
smtpClient._TCPSocket = smtpServer.createTCPSocket();
// phantomjs is really slow, so setting the tcp socket timeouts to 200s will effectively disarm the timeout
imapClient._client.client.TIMEOUT_SOCKET_LOWER_BOUND = smtpClient.TIMEOUT_SOCKET_LOWER_BOUND = 200000;
sinon.stub(mailreader, 'startWorker', function() {}); sinon.stub(mailreader, 'startWorker', function() {});
sinon.stub(openpgp, 'initWorker', function() {}); sinon.stub(openpgp, 'initWorker', function() {});
sinon.stub(appController, 'onConnect', function(cb) { // build and inject angular services
appController._emailDao.onConnect({ angular.module('email-integration-test', ['woEmail']);
imapClient: imapClient, angular.mock.module('email-integration-test');
pgpMailer: new PgpMailer({ angular.mock.inject(function($injector) {
tls: { accountService = $injector.get('account');
ca: 'random string' initAccountService();
}
}, appController._pgpbuilder)
}, cb);
}); });
var cleanup = new DeviceStorageDAO(new LawnchairDAO()); function initAccountService() {
cleanup.init(testAccount.user, function(err) { // create imap/smtp clients with stubbed tcp sockets
expect(err).to.not.exist; imapClient = new ImapClient({
auth: {
user: testAccount.user,
xoauth2: testAccount.xoauth2
},
secure: true,
ca: ['random string']
});
imapClient._client.client._TCPSocket = imapServer.createTCPSocket();
imapClient._listeningClient.client._TCPSocket = imapServer.createTCPSocket();
imapClient.onError = function(err) {
console.error('IMAP error.', err);
throw err;
};
smtpClient = new SmtpClient('localhost', 25, {
auth: {
user: testAccount.user,
xoauth2: testAccount.xoauth2
},
secure: true,
ca: ['random string'],
onError: console.error
});
smtpClient._TCPSocket = smtpServer.createTCPSocket();
// phantomjs is really slow, so setting the tcp socket timeouts to 200s will effectively disarm the timeout
imapClient._client.client.TIMEOUT_SOCKET_LOWER_BOUND = smtpClient.TIMEOUT_SOCKET_LOWER_BOUND = 200000;
// stub the onConnect function to inject the test imap/smtp clients
sinon.stub(accountService, 'onConnect', function(cb) {
accountService._emailDao.onConnect({
imapClient: imapClient,
pgpMailer: new PgpMailer({
tls: {
ca: 'random string'
}
}, accountService._pgpbuilder)
}, cb);
});
// clear the local database before each test
var cleanup = new DeviceStorageDAO(new LawnchairDAO());
cleanup.init(testAccount.user);
cleanup.clear(function(err) { cleanup.clear(function(err) {
expect(err).to.not.exist; expect(err).to.not.exist;
appController.start({
onError: function() {} userStorage = accountService._accountStore;
accountService.init({
emailAddress: testAccount.user
}, function(err) { }, function(err) {
expect(err).to.not.exist; expect(err).to.not.exist;
emailDao = accountService._emailDao;
// stub rest request to key server // stub rest request to key server
sinon.stub(appController._emailDao._keychain._publicKeyDao, 'get').yields(null, mockKeyPair.publicKey); sinon.stub(emailDao._keychain._publicKeyDao, 'get').yields(null, mockKeyPair.publicKey);
sinon.stub(appController._emailDao._keychain._publicKeyDao, 'getByUserId').yields(null, mockKeyPair.publicKey); sinon.stub(emailDao._keychain._publicKeyDao, 'getByUserId').yields(null, mockKeyPair.publicKey);
userStorage = appController._userStorage; emailDao.onIncomingMessage = function(messages) {
expect(messages.length).to.equal(imapMessages.length);
inbox = emailDao._account.folders.filter(function(folder) {
return folder.path === 'INBOX';
}).pop();
spam = emailDao._account.folders.filter(function(folder) {
return folder.path === '[Gmail]/Spam';
}).pop();
expect(inbox).to.exist;
expect(spam).to.exist;
appController.init({ inbox.messages.sort(function(a, b) {
emailAddress: testAccount.user return a.uid - b.uid;
});
done();
};
emailDao.unlock({
passphrase: testAccount.pass,
keypair: mockKeyPair
}, function(err) { }, function(err) {
expect(err).to.not.exist; expect(err).to.not.exist;
emailDao = appController._emailDao; accountService.onConnect(function(err) {
emailDao.onIncomingMessage = function(messages) {
expect(messages.length).to.equal(imapMessages.length);
inbox = emailDao._account.folders.filter(function(folder) {
return folder.path === 'INBOX';
}).pop();
spam = emailDao._account.folders.filter(function(folder) {
return folder.path === '[Gmail]/Spam';
}).pop();
expect(inbox).to.exist;
expect(spam).to.exist;
inbox.messages.sort(function(a, b) {
return a.uid - b.uid;
});
done();
};
emailDao.unlock({
passphrase: testAccount.pass,
keypair: mockKeyPair
}, function(err) {
expect(err).to.not.exist; expect(err).to.not.exist;
appController.onConnect(function(err) {
expect(err).to.not.exist;
});
}); });
}); });
}); });
}); });
}); }
}); });
after(function(done) { afterEach(function(done) {
openpgp.initWorker.restore(); openpgp.initWorker.restore();
mailreader.startWorker.restore(); mailreader.startWorker.restore();
appController.onConnect.restore(); accountService.onConnect.restore();
imapClient._client.close(); imapClient._client.close();
imapClient._listeningClient.close(); imapClient._listeningClient.close();

View File

@ -26,7 +26,9 @@
if (window.mochaPhantomJS) { if (window.mochaPhantomJS) {
mochaPhantomJS.run(); mochaPhantomJS.run();
} else { } else {
mocha.run(); setTimeout(function() {
mocha.run();
}, 1000)
} }
</script> </script>
</body> </body>