1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-29 20:32:15 -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 * A high-level Data-Access Api for handling Email synchronization
* between the cloud service and the device's local storage * 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'; 'use strict';
var EmailDAO = function(cloudstorage, keychain) {
var self = this;
/** /**
* Inits all dependencies * Inits all dependencies
*/ */
this.init = function(account, password, callback) { self.init = function(account, password, callback) {
this.account = account; self.account = account;
// validate email address // validate email address
var emailAddress = account.get('emailAddress'); 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 * Fetch an email with the following id
*/ */
this.getItem = function(folderName, itemId) { self.getItem = function(folderName, itemId) {
var folder = this.account.get('folders').where({ var folder = self.account.get('folders').where({
name: folderName name: folderName
})[0]; })[0];
var mail = _.find(folder.get('items'), function(email) { 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 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) * @param num [Number] The number of items to fetch (null means fetch all)
*/ */
this.listItems = function(folderName, offset, num, callback) { self.listItems = function(folderName, offset, num, callback) {
var collection, folder, already, pubkeyIds = [], var collection, folder, already, pubkeyIds = [];
self = this;
// check if items are in memory already (account.folders model) // check if items are in memory already (account.folders model)
folder = this.account.get('folders').where({ folder = self.account.get('folders').where({
name: folderName name: folderName
})[0]; })[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 * Synchronize a folder's items from the cloud to the device-storage
* @param folderName [String] The name of the folder e.g. 'inbox' * @param folderName [String] The name of the folder e.g. 'inbox'
*/ */
this.syncFromCloud = function(folderName, callback) { self.syncFromCloud = function(folderName, callback) {
var folder, self = this; 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 // return if an error occured
if (err) { if (err) {
callback({ 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 * Send a plaintext Email to the user's outbox in the cloud
*/ */
this.sendEmail = function(email, callback) { self.sendEmail = function(email, callback) {
var userId = this.account.get('emailAddress'); var userId = self.account.get('emailAddress');
// validate email addresses // validate email addresses
var invalidRecipient; var invalidRecipient;
@ -211,10 +215,16 @@ app.dao.EmailDAO = function(jsonDB, crypto, devicestorage, cloudstorage, util, k
callback(err); callback(err);
}); });
}; };
};
//
// helper functions
//
function validateEmail(email) { 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,}))$/; 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 re.test(email);
} }
}; return EmailDAO;
});

View File

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

View File

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

View File

@ -13,13 +13,5 @@
<script>QUnit.config.autostart = false;</script> <script>QUnit.config.autostart = false;</script>
<script data-main="main.js" src="../../src/lib/require.js"></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> </body>
</html> </html>

View File

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