1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-26 10:52:17 -05:00

started testing app controller

This commit is contained in:
Tankred Hase 2013-08-20 15:19:13 +02:00
parent 39c0215657
commit 4d861d7f81
2 changed files with 96 additions and 23 deletions

View File

@ -1,13 +1,19 @@
/** /**
* The main application controller * The main application controller
*/ */
define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keychain-dao', define(function(require) {
'js/dao/cloudstorage-dao', 'js/app-config', 'cordova'
], function($, ImapClient, SmtpClient, EmailDAO, KeychainDAO, cloudstorage, app) {
'use strict'; 'use strict';
var self = {}, var $ = require('jquery'),
emailDao; ImapClient = require('ImapClient'),
SmtpClient = require('SmtpClient'),
EmailDAO = require('js/dao/email-dao'),
KeychainDAO = require('js/dao/keychain-dao'),
cloudstorage = require('js/dao/cloudstorage-dao'),
app = require('js/app-config');
require('cordova');
var self = {};
/** /**
* Start the application by loading the view templates * Start the application by loading the view templates
@ -48,7 +54,7 @@ define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keycha
} else if (cmd === 'syncEmails') { } else if (cmd === 'syncEmails') {
// list emails from folder // list emails from folder
emailDao.syncFromCloud(args.folder, function(err) { self._emailDao.syncFromCloud(args.folder, function(err) {
callback({ callback({
err: err err: err
}); });
@ -56,7 +62,7 @@ define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keycha
} else if (cmd === 'listEmails') { } else if (cmd === 'listEmails') {
// list emails from folder // list emails from folder
emailDao.listItems(args.folder, args.offset, args.num, function(err, emails) { self._emailDao.listItems(args.folder, args.offset, args.num, function(err, emails) {
callback({ callback({
err: err, err: err,
emails: emails emails: emails
@ -65,7 +71,7 @@ define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keycha
} else if (cmd === 'getEmail') { } else if (cmd === 'getEmail') {
// list emails from folder // list emails from folder
var mail = emailDao.getItem(args.folder, args.messageId); var mail = self._emailDao.getItem(args.folder, args.messageId);
callback({ callback({
err: null, err: null,
email: mail email: mail
@ -73,7 +79,7 @@ define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keycha
} else if (cmd === 'sendEmail') { } else if (cmd === 'sendEmail') {
// list emails from folder // list emails from folder
emailDao.smtpSend(args.email, function(err) { self._emailDao.smtpSend(args.email, function(err) {
callback({ callback({
err: err err: err
}); });
@ -106,7 +112,7 @@ define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keycha
dataType: 'json', dataType: 'json',
success: function(info) { success: function(info) {
// login using the received email address // login using the received email address
login(info.email, password, token, function(err) { self.login(info.email, password, token, function(err) {
// send email address to sandbox // send email address to sandbox
callback(err, info.email); callback(err, info.email);
}); });
@ -123,7 +129,7 @@ define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keycha
); );
} }
function login(userId, password, token, callback) { self.login = function(userId, password, token, callback) {
var auth, imapOptions, smtpOptions, var auth, imapOptions, smtpOptions,
keychain, imapClient, smtpClient; keychain, imapClient, smtpClient;
@ -152,7 +158,7 @@ define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keycha
keychain = new KeychainDAO(cloudstorage); keychain = new KeychainDAO(cloudstorage);
imapClient = new ImapClient(imapOptions); imapClient = new ImapClient(imapOptions);
smtpClient = new SmtpClient(smtpOptions); smtpClient = new SmtpClient(smtpOptions);
emailDao = new EmailDAO(keychain, imapClient, smtpClient); self._emailDao = new EmailDAO(keychain, imapClient, smtpClient);
// init email dao // init email dao
var account = { var account = {
@ -161,8 +167,8 @@ define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keycha
symIvSize: app.config.symIvSize, symIvSize: app.config.symIvSize,
asymKeySize: app.config.asymKeySize asymKeySize: app.config.asymKeySize
}; };
emailDao.init(account, password, callback); self._emailDao.init(account, password, callback);
} };
function loadTemplates(names, callback) { function loadTemplates(names, callback) {
var loadTemplate = function(index) { var loadTemplate = function(index) {

View File

@ -1,20 +1,87 @@
define(function() { define(function(require) {
'use strict'; 'use strict';
var expect = chai.expect; var controller = require('js/app-controller'),
EmailDAO = require('js/dao/email-dao'),
$ = require('jquery'),
expect = chai.expect;
var appControllerTest = {
user: 'test@exmaple.com',
passphrase: 'asdf'
};
describe('App Controller unit tests', function() { describe('App Controller unit tests', function() {
beforeEach(function() {}); beforeEach(function() {
sinon.stub(controller, 'login', function(userId, password, token, callback) {
controller._emailDao = sinon.createStubInstance(EmailDAO);
callback();
});
afterEach(function() {}); sinon.stub($, 'get');
sinon.stub($, 'ajax').yieldsTo('success', {
email: appControllerTest.user
});
describe('init', function() { window.chrome = window.chrome || {};
it('should not explode', function() { window.chrome.identity = window.chrome.identity || {};
expect(true).to.be.ok; if (typeof window.chrome.identity.getAuthToken !== 'function') {
window.chrome.identity.getAuthToken = function() {};
}
sinon.stub(window.chrome.identity, 'getAuthToken');
window.chrome.identity.getAuthToken.yields('token42');
});
afterEach(function() {
controller.login.restore();
$.get.restore();
$.ajax.restore();
window.chrome.identity.getAuthToken.restore();
});
describe('start', function() {
it('should not explode', function(done) {
$.get.yields('<div></div>');
controller.start(function(err) {
expect($.get.called).to.be.true;
expect(err).to.not.exist;
done();
});
});
});
describe('execute', function() {
describe('login', function() {
it('should work', function(done) {
controller.execute('login', {
password: appControllerTest.passphrase
}, function(resArgs) {
expect(resArgs.err).to.not.exist;
expect(resArgs.userId).to.equal(appControllerTest.user);
expect($.ajax.called).to.be.true;
expect(window.chrome.identity.getAuthToken.called).to.be.true;
done();
});
});
});
describe('sendEmail', function() {
it('should work', function(done) {
controller._emailDao.smtpSend.yields();
controller.execute('sendEmail', {
password: appControllerTest.passphrase
}, function(resArgs) {
expect(resArgs.err).to.not.exist;
expect(controller._emailDao.smtpSend.called).to.be.true;
done();
});
}); });
}); });
}); });
}); });
});