1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-22 08:52:15 -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/account-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/create-account-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-set-credentials-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/account-ctrl-test.js',
'test/unit/controller/app/set-passphrase-ctrl-test.js',
@ -206,7 +206,7 @@ module.exports = function(grunt) {
files: {
'test/integration/index.browserified.js': [
'test/main.js',
/*'test/integration/email-dao-test.js'*/
'test/integration/email-dao-test.js'
]
},
options: browserifyOpt
@ -281,7 +281,6 @@ module.exports = function(grunt) {
'node_modules/jquery/dist/jquery.min.js',
'src/lib/angular/angular.js',
'src/lib/angular/angular-route.js',
'src/lib/angular/angular-animate.js',
'src/lib/angular/angular-mocks.js',
'src/lib/lawnchair/lawnchair-git.js',
'src/lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js',
@ -296,6 +295,9 @@ module.exports = function(grunt) {
integrationTest: {
src: [
'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-adapter-webkit-sqlite-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'),
BrowserSMTP = require('browsersmtp'),
SmtpClient = require('wo-smtpclient'),
LawnchairDAO = require('../../src/js/dao/lawnchair-dao'),
DeviceStorageDAO = require('../../src/js/dao/devicestorage-dao'),
appController = require('../../src/js/app-controller'),
LawnchairDAO = require('../../src/js/service/lawnchair'),
DeviceStorageDAO = require('../../src/js/service/devicestorage'),
mailreader = require('mailreader'),
PgpMailer = require('pgpmailer'),
config = require('../../src/js/app-config').config,
@ -14,9 +13,8 @@ var ImapClient = require('imap-client'),
describe('Email DAO integration tests', function() {
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;
var testAccount = {
@ -25,7 +23,11 @@ describe('Email DAO integration tests', function() {
xoauth2: 'testtoken'
};
before(function(done) {
beforeEach(function(done) {
//
// Test data
//
imapMessages = [{
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 = {};
serverUsers[testAccount.user] = {
password: testAccount.pass,
@ -180,10 +186,27 @@ describe('Email DAO integration tests', function() {
}
};
//
// Test client setup
//
// don't multithread, Function.prototype.bind() is broken in phantomjs in web workers
window.Worker = undefined;
navigator.online = true;
sinon.stub(mailreader, 'startWorker', function() {});
sinon.stub(openpgp, 'initWorker', function() {});
// build and inject angular services
angular.module('email-integration-test', ['woEmail']);
angular.mock.module('email-integration-test');
angular.mock.inject(function($injector) {
accountService = $injector.get('account');
initAccountService();
});
function initAccountService() {
// create imap/smtp clients with stubbed tcp sockets
imapClient = new ImapClient({
auth: {
user: testAccount.user,
@ -196,17 +219,8 @@ describe('Email DAO integration tests', function() {
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.');
});
console.error('IMAP error.', err);
throw err;
};
smtpClient = new SmtpClient('localhost', 25, {
@ -224,42 +238,36 @@ describe('Email DAO integration tests', function() {
// 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(openpgp, 'initWorker', function() {});
sinon.stub(appController, 'onConnect', function(cb) {
appController._emailDao.onConnect({
// 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'
}
}, appController._pgpbuilder)
}, accountService._pgpbuilder)
}, cb);
});
// clear the local database before each test
var cleanup = new DeviceStorageDAO(new LawnchairDAO());
cleanup.init(testAccount.user, function(err) {
expect(err).to.not.exist;
cleanup.init(testAccount.user);
cleanup.clear(function(err) {
expect(err).to.not.exist;
appController.start({
onError: function() {}
}, function(err) {
expect(err).to.not.exist;
// stub rest request to key server
sinon.stub(appController._emailDao._keychain._publicKeyDao, 'get').yields(null, mockKeyPair.publicKey);
sinon.stub(appController._emailDao._keychain._publicKeyDao, 'getByUserId').yields(null, mockKeyPair.publicKey);
userStorage = accountService._accountStore;
userStorage = appController._userStorage;
appController.init({
accountService.init({
emailAddress: testAccount.user
}, function(err) {
expect(err).to.not.exist;
emailDao = appController._emailDao;
emailDao = accountService._emailDao;
// stub rest request to key server
sinon.stub(emailDao._keychain._publicKeyDao, 'get').yields(null, mockKeyPair.publicKey);
sinon.stub(emailDao._keychain._publicKeyDao, 'getByUserId').yields(null, mockKeyPair.publicKey);
emailDao.onIncomingMessage = function(messages) {
expect(messages.length).to.equal(imapMessages.length);
@ -284,20 +292,19 @@ describe('Email DAO integration tests', function() {
}, function(err) {
expect(err).to.not.exist;
appController.onConnect(function(err) {
accountService.onConnect(function(err) {
expect(err).to.not.exist;
});
});
});
});
});
});
}
});
after(function(done) {
afterEach(function(done) {
openpgp.initWorker.restore();
mailreader.startWorker.restore();
appController.onConnect.restore();
accountService.onConnect.restore();
imapClient._client.close();
imapClient._listeningClient.close();

View File

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