2013-06-10 17:50:26 -04:00
|
|
|
define(['js/dao/email-dao', 'js/dao/keychain-dao', 'js/dao/lawnchair-dao',
|
2013-09-04 13:18:28 -04:00
|
|
|
'js/crypto/crypto', 'js/dao/devicestorage-dao', 'test/test-data', 'js/app-config'
|
2013-06-10 21:14:57 -04:00
|
|
|
], function(EmailDAO, KeychainDAO, jsonDao, crypto, storage, testData, app) {
|
2013-06-10 17:50:26 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
module("Email DAO");
|
|
|
|
|
|
|
|
var emaildaoTest = {
|
|
|
|
user: 'test@atlasdev.onmicrosoft.com',
|
|
|
|
password: 'Xoza76645',
|
|
|
|
keySize: 128,
|
|
|
|
ivSize: 128,
|
|
|
|
rsaKeySize: 1024
|
2013-03-13 11:58:46 -04:00
|
|
|
};
|
2013-04-01 17:23:25 -04:00
|
|
|
|
2013-06-10 17:50:26 -04:00
|
|
|
asyncTest("Init", 3, function() {
|
|
|
|
// init dependencies
|
|
|
|
jsonDao.init(emaildaoTest.user);
|
|
|
|
// cloud storage stub
|
2013-06-26 11:37:21 -04:00
|
|
|
emaildaoTest.cloudstorageStub = {
|
2013-06-10 17:50:26 -04:00
|
|
|
putPublicKey: function(pk, callback) {
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
putPrivateKey: function(prk, callback) {
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
getPublicKeyByUserId: function(userId, callback) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
2013-06-26 11:37:21 -04:00
|
|
|
emaildaoTest.keychain = new KeychainDAO(emaildaoTest.cloudstorageStub);
|
|
|
|
emaildaoTest.emailDao = new EmailDAO(emaildaoTest.cloudstorageStub, emaildaoTest.keychain);
|
2013-06-10 17:50:26 -04:00
|
|
|
|
|
|
|
// generate test data
|
|
|
|
emaildaoTest.list = testData.getEmailCollection(100);
|
|
|
|
|
|
|
|
var account = new app.model.Account({
|
|
|
|
emailAddress: emaildaoTest.user,
|
|
|
|
symKeySize: emaildaoTest.keySize,
|
|
|
|
symIvSize: emaildaoTest.ivSize,
|
|
|
|
asymKeySize: emaildaoTest.rsaKeySize
|
|
|
|
});
|
2013-04-01 17:23:25 -04:00
|
|
|
|
2013-06-10 17:50:26 -04:00
|
|
|
// clear db before tests
|
|
|
|
jsonDao.clear(function(err) {
|
|
|
|
ok(!err, 'DB cleared. Error status: ' + err);
|
2013-04-01 17:23:25 -04:00
|
|
|
|
2013-06-10 17:50:26 -04:00
|
|
|
emaildaoTest.emailDao.init(account, emaildaoTest.password, function(err) {
|
|
|
|
ok(!err);
|
|
|
|
equal(emaildaoTest.emailDao.account.get('emailAddress'), emaildaoTest.user, 'Email DAO Account');
|
2013-04-01 17:23:25 -04:00
|
|
|
|
2013-06-10 17:50:26 -04:00
|
|
|
start();
|
|
|
|
});
|
2013-03-13 11:58:46 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-06-26 11:37:21 -04:00
|
|
|
asyncTest("Persist test emails (stubbed sync from cloud)", 4, function() {
|
2013-06-10 17:50:26 -04:00
|
|
|
emaildaoTest.keychain.getUserKeyPair(emaildaoTest.user, function(err, keypair) {
|
|
|
|
ok(!err && keypair, 'Fetch keypair from keychain');
|
2013-04-01 17:23:25 -04:00
|
|
|
|
2013-06-10 17:50:26 -04:00
|
|
|
var receiverPubkeys = [keypair.publicKey];
|
2013-04-01 17:23:25 -04:00
|
|
|
|
2013-09-04 13:18:28 -04:00
|
|
|
crypto.encryptListForUser(emaildaoTest.list, receiverPubkeys, function(err, encryptedList) {
|
2013-06-10 17:50:26 -04:00
|
|
|
ok(!err);
|
|
|
|
equal(encryptedList.length, emaildaoTest.list.length, 'Encrypt list');
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-06-10 17:50:26 -04:00
|
|
|
// add sent date to encrypted items
|
|
|
|
for (var i = 0; i < encryptedList.length; i++) {
|
2013-09-04 13:18:28 -04:00
|
|
|
encryptedList[i].sentDate = emaildaoTest.list[i].sentDate;
|
2013-06-10 17:50:26 -04:00
|
|
|
}
|
2013-05-31 09:51:34 -04:00
|
|
|
|
2013-06-26 11:37:21 -04:00
|
|
|
// set encrypted test list as return value for cloud storage stub
|
|
|
|
emaildaoTest.cloudstorageStub.listEncryptedItems = function(type, emailAddress, folderName, callback) {
|
|
|
|
callback(null, encryptedList);
|
|
|
|
};
|
|
|
|
|
|
|
|
emaildaoTest.emailDao.syncFromCloud('inbox', function() {
|
|
|
|
ok(true, 'Stored encrypted list');
|
2013-05-31 09:51:34 -04:00
|
|
|
|
2013-06-10 17:50:26 -04:00
|
|
|
start();
|
|
|
|
});
|
2013-05-31 09:51:34 -04:00
|
|
|
});
|
2013-03-13 11:58:46 -04:00
|
|
|
});
|
2013-04-01 17:23:25 -04:00
|
|
|
});
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-06-10 17:50:26 -04:00
|
|
|
asyncTest("List Email models", 2, function() {
|
|
|
|
emaildaoTest.emailDao.listItems('inbox', 0, emaildaoTest.list.length, function(err, gotten) {
|
|
|
|
ok(!err);
|
|
|
|
|
2013-09-04 13:18:28 -04:00
|
|
|
var reference = emaildaoTest.list;
|
2013-05-18 16:33:10 -04:00
|
|
|
|
2013-06-10 17:50:26 -04:00
|
|
|
deepEqual(gotten, reference, 'Compare collection');
|
2013-04-01 17:23:25 -04:00
|
|
|
|
2013-06-10 17:50:26 -04:00
|
|
|
start();
|
|
|
|
});
|
|
|
|
});
|
2013-04-01 17:23:25 -04:00
|
|
|
|
2013-06-10 17:50:26 -04:00
|
|
|
asyncTest("Get item", 1, function() {
|
2013-09-04 13:18:28 -04:00
|
|
|
var item = emaildaoTest.list[0];
|
2013-06-10 17:50:26 -04:00
|
|
|
var mail = emaildaoTest.emailDao.getItem('inbox', item.id);
|
|
|
|
deepEqual(mail, item, 'Item correct');
|
2013-03-13 11:58:46 -04:00
|
|
|
start();
|
|
|
|
});
|
|
|
|
|
2013-04-01 17:23:25 -04:00
|
|
|
});
|