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

finished refactoring unit tests with email dao

This commit is contained in:
Tankred Hase 2013-06-10 23:50:26 +02:00
parent deb47fd4df
commit f1fd936141
7 changed files with 286 additions and 280 deletions

View File

@ -2,14 +2,19 @@
* A high-level Data-Access Api for handling Email synchronization
* between the cloud service and the device's local storage
*/
app.dao.EmailDAO = function(jsonDB, crypto, devicestorage, cloudstorage, util, keychain) {
define(['underscore', 'cryptoLib/util', 'js/crypto/crypto', 'js/dao/lawnchair-dao',
'js/dao/devicestorage-dao', 'js/model/account-model'
], function(_, util, crypto, jsonDB, devicestorage) {
'use strict';
var EmailDAO = function(cloudstorage, keychain) {
var self = this;
/**
* Inits all dependencies
*/
this.init = function(account, password, callback) {
this.account = account;
self.init = function(account, password, callback) {
self.account = account;
// validate email address
var emailAddress = account.get('emailAddress');
@ -59,8 +64,8 @@ app.dao.EmailDAO = function(jsonDB, crypto, devicestorage, cloudstorage, util, k
/**
* Fetch an email with the following id
*/
this.getItem = function(folderName, itemId) {
var folder = this.account.get('folders').where({
self.getItem = function(folderName, itemId) {
var folder = self.account.get('folders').where({
name: folderName
})[0];
var mail = _.find(folder.get('items'), function(email) {
@ -74,12 +79,11 @@ app.dao.EmailDAO = function(jsonDB, crypto, devicestorage, cloudstorage, util, k
* @param offset [Number] The offset of items to fetch (0 is the last stored item)
* @param num [Number] The number of items to fetch (null means fetch all)
*/
this.listItems = function(folderName, offset, num, callback) {
var collection, folder, already, pubkeyIds = [],
self = this;
self.listItems = function(folderName, offset, num, callback) {
var collection, folder, already, pubkeyIds = [];
// check if items are in memory already (account.folders model)
folder = this.account.get('folders').where({
folder = self.account.get('folders').where({
name: folderName
})[0];
@ -148,10 +152,10 @@ app.dao.EmailDAO = function(jsonDB, crypto, devicestorage, cloudstorage, util, k
* Synchronize a folder's items from the cloud to the device-storage
* @param folderName [String] The name of the folder e.g. 'inbox'
*/
this.syncFromCloud = function(folderName, callback) {
var folder, self = this;
self.syncFromCloud = function(folderName, callback) {
var folder;
cloudstorage.listEncryptedItems('email', this.account.get('emailAddress'), folderName, function(err, data) {
cloudstorage.listEncryptedItems('email', self.account.get('emailAddress'), folderName, function(err, data) {
// return if an error occured
if (err) {
callback({
@ -180,8 +184,8 @@ app.dao.EmailDAO = function(jsonDB, crypto, devicestorage, cloudstorage, util, k
/**
* Send a plaintext Email to the user's outbox in the cloud
*/
this.sendEmail = function(email, callback) {
var userId = this.account.get('emailAddress');
self.sendEmail = function(email, callback) {
var userId = self.account.get('emailAddress');
// validate email addresses
var invalidRecipient;
@ -211,10 +215,16 @@ app.dao.EmailDAO = function(jsonDB, crypto, devicestorage, cloudstorage, util, k
callback(err);
});
};
};
//
// helper functions
//
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
};
return EmailDAO;
});

View File

@ -1,4 +1,4 @@
(function() {
define(['backbone', 'js/model/folder-model'], function(Backbone) {
'use strict';
app.model.Account = Backbone.Model.extend({
@ -23,4 +23,4 @@
});
}());
});

View File

@ -1,4 +1,4 @@
(function() {
define(['backbone'], function(Backbone) {
'use strict';
app.model.Folder = Backbone.Model.extend({
@ -18,4 +18,4 @@
});
}());
});

View File

@ -1,4 +1,6 @@
define(['underscore', 'cryptoLib/util', 'js/crypto/crypto', 'js/dao/lawnchair-dao', 'js/dao/devicestorage-dao', 'test/test-data'], function(_, util, crypto, jsonDao, storage, testData) {
define(['underscore', 'cryptoLib/util', 'js/crypto/crypto', 'js/dao/lawnchair-dao',
'js/dao/devicestorage-dao', 'test/test-data'
], function(_, util, crypto, jsonDao, storage, testData) {
'use strict';
module("DeviceStorage");

View File

@ -1,8 +1,11 @@
define(['js/dao/email-dao', 'js/dao/keychain-dao', 'js/dao/lawnchair-dao',
'js/crypto/crypto', 'js/dao/devicestorage-dao', 'test/test-data'
], function(EmailDAO, KeychainDAO, jsonDao, crypto, storage, testData) {
'use strict';
module("Email DAO");
var emaildao_test = {
var emaildaoTest = {
user: 'test@atlasdev.onmicrosoft.com',
password: 'Xoza76645',
keySize: 128,
@ -12,11 +15,7 @@ var emaildao_test = {
asyncTest("Init", 3, function() {
// init dependencies
var util = new cryptoLib.Util(window, uuid);
var jsonDao = new app.dao.LawnchairDAO(Lawnchair);
jsonDao.init(emaildao_test.user);
emaildao_test.crypto = new app.crypto.Crypto(window, util);
emaildao_test.storage = new app.dao.DeviceStorage(util, emaildao_test.crypto, jsonDao, null);
jsonDao.init(emaildaoTest.user);
// cloud storage stub
var cloudstorageStub = {
putPublicKey: function(pk, callback) {
@ -29,26 +28,26 @@ asyncTest("Init", 3, function() {
callback();
}
};
emaildao_test.keychain = new app.dao.KeychainDAO(jsonDao, cloudstorageStub);
emaildao_test.emailDao = new app.dao.EmailDAO(jsonDao, emaildao_test.crypto, emaildao_test.storage, cloudstorageStub, util, emaildao_test.keychain);
emaildaoTest.keychain = new KeychainDAO(cloudstorageStub);
emaildaoTest.emailDao = new EmailDAO(cloudstorageStub, emaildaoTest.keychain);
// generate test data
emaildao_test.list = new TestData().getEmailCollection(100);
emaildaoTest.list = testData.getEmailCollection(100);
var account = new app.model.Account({
emailAddress: emaildao_test.user,
symKeySize: emaildao_test.keySize,
symIvSize: emaildao_test.ivSize,
asymKeySize: emaildao_test.rsaKeySize
emailAddress: emaildaoTest.user,
symKeySize: emaildaoTest.keySize,
symIvSize: emaildaoTest.ivSize,
asymKeySize: emaildaoTest.rsaKeySize
});
// clear db before tests
jsonDao.clear(function(err) {
ok(!err, 'DB cleared. Error status: ' + err);
emaildao_test.emailDao.init(account, emaildao_test.password, function(err) {
emaildaoTest.emailDao.init(account, emaildaoTest.password, function(err) {
ok(!err);
equal(emaildao_test.emailDao.account.get('emailAddress'), emaildao_test.user, 'Email DAO Account');
equal(emaildaoTest.emailDao.account.get('emailAddress'), emaildaoTest.user, 'Email DAO Account');
start();
});
@ -56,21 +55,21 @@ asyncTest("Init", 3, function() {
});
asyncTest("Persist test emails", 4, function() {
emaildao_test.keychain.getUserKeyPair(emaildao_test.user, function(err, keypair) {
emaildaoTest.keychain.getUserKeyPair(emaildaoTest.user, function(err, keypair) {
ok(!err && keypair, 'Fetch keypair from keychain');
var receiverPubkeys = [keypair.publicKey];
emaildao_test.crypto.encryptListForUser(emaildao_test.list.toJSON(), receiverPubkeys, function(err, encryptedList) {
crypto.encryptListForUser(emaildaoTest.list.toJSON(), receiverPubkeys, function(err, encryptedList) {
ok(!err);
equal(encryptedList.length, emaildao_test.list.length, 'Encrypt list');
equal(encryptedList.length, emaildaoTest.list.length, 'Encrypt list');
// add sent date to encrypted items
for (var i = 0; i < encryptedList.length; i++) {
encryptedList[i].sentDate = emaildao_test.list.at(i).get('sentDate');
encryptedList[i].sentDate = emaildaoTest.list.at(i).get('sentDate');
}
emaildao_test.storage.storeEcryptedList(encryptedList, 'email_inbox', function() {
storage.storeEcryptedList(encryptedList, 'email_inbox', function() {
ok(true, 'Store encrypted list');
start();
@ -80,10 +79,10 @@ asyncTest("Persist test emails", 4, function() {
});
asyncTest("List Email models", 2, function() {
emaildao_test.emailDao.listItems('inbox', 0, emaildao_test.list.length, function(err, gotten) {
emaildaoTest.emailDao.listItems('inbox', 0, emaildaoTest.list.length, function(err, gotten) {
ok(!err);
var reference = emaildao_test.list.toJSON();
var reference = emaildaoTest.list.toJSON();
deepEqual(gotten, reference, 'Compare collection');
@ -92,8 +91,10 @@ asyncTest("List Email models", 2, function() {
});
asyncTest("Get item", 1, function() {
var item = emaildao_test.list.toJSON()[0];
var mail = emaildao_test.emailDao.getItem('inbox', item.id);
var item = emaildaoTest.list.toJSON()[0];
var mail = emaildaoTest.emailDao.getItem('inbox', item.id);
deepEqual(mail, item, 'Item correct');
start();
});
});

View File

@ -13,13 +13,5 @@
<script>QUnit.config.autostart = false;</script>
<script data-main="main.js" src="../../src/lib/require.js"></script>
<!--<script src="aes-test.js"></script>
<script src="rsa-test.js"></script>
<script src="lawnchair-dao-test.js"></script>
<script src="keychain-dao-test.js"></script>
<script src="crypto-test.js"></script>
<script src="devicestorage-test.js"></script>
<script src="email-dao-test.js"></script>-->
</body>
</html>

View File

@ -27,7 +27,8 @@ function startTests() {
'test/unit/lawnchair-dao-test',
'test/unit/keychain-dao-test',
'test/unit/crypto-test',
'test/unit/devicestorage-dao-test'
'test/unit/devicestorage-dao-test',
'test/unit/email-dao-test'
], function() {
//Tests loaded, run tests
QUnit.start();