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

Fix unit tests for existing angular services

This commit is contained in:
Tankred Hase 2014-11-21 15:06:29 +01:00
parent 881afbff40
commit f2fee61d3b
21 changed files with 133 additions and 115 deletions

View File

@ -155,23 +155,29 @@ module.exports = function(grunt) {
unitTest: {
files: {
'test/unit/index.browserified.js': [
'test/unit/oauth-test.js',
'test/unit/auth-test.js',
'test/unit/email-dao-test.js',
'test/unit/app-controller-test.js',
'test/unit/pgp-test.js',
'test/unit/crypto-test.js',
'test/unit/backbutton-handler-test.js',
'test/unit/rest-dao-test.js',
'test/unit/admin-dao-test.js',
'test/unit/publickey-dao-test.js',
'test/unit/privatekey-dao-test.js',
'test/unit/lawnchair-dao-test.js',
'test/unit/keychain-dao-test.js',
'test/unit/devicestorage-dao-test.js',
'test/unit/newsletter-service-test.js',
'test/unit/mail-config-service-test.js',
'test/unit/dialog-ctrl-test.js',
'test/main.js',
'test/unit/util/dialog-test.js',
'test/unit/util/connection-doctor-test.js',
'test/unit/util/update-handler-test.js',
'test/unit/util/backbutton-handler-test.js',
'test/unit/util/status-display-test.js',
'test/unit/crypto/pgp-test.js',
'test/unit/crypto/crypto-test.js',
'test/unit/service/rest-dao-test.js',
'test/unit/service/admin-dao-test.js',
'test/unit/service/auth-test.js',
'test/unit/service/oauth-test.js',
'test/unit/service/publickey-dao-test.js',
'test/unit/service/privatekey-dao-test.js',
'test/unit/service/lawnchair-dao-test.js',
'test/unit/service/keychain-dao-test.js',
'test/unit/service/devicestorage-dao-test.js',
'test/unit/service/newsletter-service-test.js',
'test/unit/service/mail-config-service-test.js',
'test/unit/service/invitation-dao-test.js',
'test/unit/email/outbox-bo-test.js',
'test/unit/email/email-dao-test.js',
/*'test/unit/dialog-ctrl-test.js',
'test/unit/add-account-ctrl-test.js',
'test/unit/create-account-ctrl-test.js',
'test/unit/validate-phone-ctrl-test.js',
@ -189,12 +195,7 @@ module.exports = function(grunt) {
'test/unit/navigation-ctrl-test.js',
'test/unit/mail-list-ctrl-test.js',
'test/unit/write-ctrl-test.js',
'test/unit/action-bar-ctrl-test.js',
'test/unit/outbox-bo-test.js',
'test/unit/invitation-dao-test.js',
'test/unit/update-handler-test.js',
'test/unit/connection-doctor-test.js',
'test/main.js'
'test/unit/action-bar-ctrl-test.js',*/
]
},
options: browserifyOpt
@ -202,8 +203,8 @@ module.exports = function(grunt) {
integrationTest: {
files: {
'test/integration/index.browserified.js': [
'test/integration/email-dao-test.js',
'test/main.js'
'test/main.js',
/*'test/integration/email-dao-test.js'*/
]
},
options: browserifyOpt
@ -446,11 +447,11 @@ module.exports = function(grunt) {
tasks: ['dist-js-app']
},
jsUnitTest: {
files: ['test/unit/*-test.js'],
files: ['test/unit/**/*-test.js', 'test/*.js'],
tasks: ['dist-js-unitTest']
},
jsIntegrationTest: {
files: ['test/integration/*-test.js'],
files: ['test/integration/*-test.js', 'test/*.js'],
tasks: ['dist-js-integrationTest']
},
icons: {

View File

@ -57,9 +57,7 @@ function Email(keychain, pgp, accountStore, pgpbuilder, mailreader, dialog) {
this._pgpbuilder = pgpbuilder;
this._mailreader = mailreader;
this.onError = function(err) {
dialog.error(err);
};
this.onError = dialog.error.bind(dialog);
}

View File

@ -6,6 +6,10 @@ ngModule.factory('deviceStorage', function(lawnchairDAO) {
});
module.exports = DeviceStorage;
//
// Export special device storage singletons
//
// expose an instance with the static dbName 'app-config' to store configuration data
ngModule.factory('appConfigStore', function(deviceStorage) {
deviceStorage.init('app-config');
@ -15,6 +19,10 @@ ngModule.factory('appConfigStore', function(deviceStorage) {
// expose a singleton instance of DeviceStorage called 'accountStore' to persist user data
ngModule.service('accountStore', DeviceStorage);
//
// Implementation
//
/**
* High level storage api that handles all persistence of a user's data on the device.
*/
@ -22,6 +30,10 @@ function DeviceStorage(lawnchairDAO) {
this._lawnchairDAO = lawnchairDAO;
}
/**
* Initialize the lawnchair database
* @param {String} dbName The name of the database
*/
DeviceStorage.prototype.init = function(dbName) {
this._lawnchairDAO.init(dbName);
};

View File

@ -4,8 +4,7 @@ var ngModule = angular.module('woServices');
ngModule.service('keychain', Keychain);
module.exports = Keychain;
var util = require('crypto-lib').util,
config = require('../app-config').config;
var util = require('crypto-lib').util;
var DB_PUBLICKEY = 'publickey',
DB_PRIVATEKEY = 'privatekey',
@ -327,7 +326,8 @@ Keychain.prototype.getDeviceName = function(callback) {
* @param {Function} callback(error, deviceSecret:[base64 encoded string])
*/
Keychain.prototype.getDeviceSecret = function(callback) {
var self = this;
var self = this,
config = self._appConfig.config;
// generate random deviceSecret or get from storage
self._lawnchairDAO.read(DB_DEVICE_SECRET, function(err, storedDevSecret) {
@ -363,7 +363,8 @@ Keychain.prototype.getDeviceSecret = function(callback) {
*/
Keychain.prototype.registerDevice = function(options, callback) {
var self = this,
devName;
devName,
config = self._appConfig.config;
// check if deviceName is already persisted in storage
self.getDeviceName(function(err, deviceName) {
@ -462,7 +463,8 @@ Keychain.prototype.registerDevice = function(options, callback) {
*/
Keychain.prototype._authenticateToPrivateKeyServer = function(userId, callback) {
var self = this,
sessionId;
sessionId,
config = self._appConfig.config;
// request auth session key required for upload
self._privateKeyDao.requestAuthSessionKey({
@ -579,6 +581,7 @@ Keychain.prototype._authenticateToPrivateKeyServer = function(userId, callback)
*/
Keychain.prototype.uploadPrivateKey = function(options, callback) {
var self = this,
config = self._appConfig.config,
keySize = config.symKeySize,
salt;
@ -710,6 +713,7 @@ Keychain.prototype.decryptAndStorePrivateKeyLocally = function(options, callback
var self = this,
code = options.code,
salt = options.salt,
config = self._appConfig.config,
keySize = config.symKeySize;
if (!options._id || !options.userId || !options.code || !options.salt || !options.encryptedPrivateKey || !options.iv) {

View File

@ -20,7 +20,7 @@ MailConfig.prototype.get = function(emailAddress) {
});
}
var url = this._appConfig.settingsUrl + emailAddress.split('@')[1];
var url = this._appConfig.config.settingsUrl + emailAddress.split('@')[1];
return this._http.get(url).then(function(res) {
return res.data;
});

View File

@ -1,7 +1,7 @@
'use strict';
var Crypto = require('../../src/js/crypto/crypto'),
config = require('../../src/js/app-config').config,
var Crypto = require('../../../src/js/crypto/crypto'),
config = require('../../../src/js/app-config').config,
util = require('crypto-lib').util;
describe('Crypto unit tests', function() {

View File

@ -1,6 +1,6 @@
'use strict';
var PGP = require('../../src/js/crypto/pgp');
var PGP = require('../../../src/js/crypto/pgp');
describe('PGP Crypto Api unit tests', function() {
this.timeout(20000);

View File

@ -4,11 +4,12 @@ var mailreader = require('mailreader'),
ImapClient = require('imap-client'),
PgpMailer = require('pgpmailer'),
PgpBuilder = require('pgpbuilder'),
cfg = require('../../src/js/app-config').config,
EmailDAO = require('../../src/js/dao/email-dao'),
KeychainDAO = require('../../src/js/dao/keychain-dao'),
PGP = require('../../src/js/crypto/pgp'),
DeviceStorageDAO = require('../../src/js/dao/devicestorage-dao');
cfg = require('../../../src/js/app-config').config,
EmailDAO = require('../../../src/js/email/email'),
KeychainDAO = require('../../../src/js/service/keychain'),
PGP = require('../../../src/js/crypto/pgp'),
DeviceStorageDAO = require('../../../src/js/service/devicestorage'),
Dialog = require('../../../src/js/util/dialog');
describe('Email DAO unit tests', function() {
@ -19,7 +20,7 @@ describe('Email DAO unit tests', function() {
var dao;
// mocks
var keychainStub, imapClientStub, pgpMailerStub, pgpBuilderStub, pgpStub, devicestorageStub, parseStub;
var keychainStub, imapClientStub, pgpMailerStub, pgpBuilderStub, pgpStub, devicestorageStub, parseStub, dialogStub;
// config
var emailAddress, passphrase, asymKeySize, account;
@ -116,11 +117,12 @@ describe('Email DAO unit tests', function() {
pgpStub = sinon.createStubInstance(PGP);
parseStub = sinon.stub(mailreader, 'parse');
devicestorageStub = sinon.createStubInstance(DeviceStorageDAO);
dialogStub = sinon.createStubInstance(Dialog);
//
// setup the SUT
//
dao = new EmailDAO(keychainStub, pgpStub, devicestorageStub, pgpBuilderStub, mailreader);
dao = new EmailDAO(keychainStub, pgpStub, devicestorageStub, pgpBuilderStub, mailreader, dialogStub);
dao._account = account;
dao._pgpMailer = pgpMailerStub;
dao._imapClient = imapClientStub;

View File

@ -1,9 +1,9 @@
'use strict';
var OutboxBO = require('../../src/js/bo/outbox'),
KeychainDAO = require('../../src/js/dao/keychain-dao'),
EmailDAO = require('../../src/js/dao/email-dao'),
DeviceStorageDAO = require('../../src/js/dao/devicestorage-dao');
var OutboxBO = require('../../../src/js/email/outbox'),
KeychainDAO = require('../../../src/js/service/keychain'),
EmailDAO = require('../../../src/js/email/email'),
DeviceStorageDAO = require('../../../src/js/service/devicestorage');
describe('Outbox Business Object unit test', function() {
var outbox, emailDaoStub, devicestorageStub, keychainStub,

View File

@ -1,7 +1,8 @@
'use strict';
var RestDAO = require('../../src/js/dao/rest-dao'),
AdminDAO = require('../../src/js/dao/admin-dao');
var RestDAO = require('../../../src/js/service/rest'),
AdminDAO = require('../../../src/js/service/admin'),
appConfig = require('../../../src/js/app-config');
describe('Admin DAO unit tests', function() {
@ -11,7 +12,7 @@ describe('Admin DAO unit tests', function() {
beforeEach(function() {
restDaoStub = sinon.createStubInstance(RestDAO);
adminDao = new AdminDAO(restDaoStub);
adminDao = new AdminDAO(restDaoStub, appConfig);
});
afterEach(function() {});

View File

@ -1,9 +1,9 @@
'use strict';
var Auth = require('../../src/js/bo/auth'),
OAuth = require('../../src/js/util/oauth'),
PGP = require('../../src/js/crypto/pgp'),
DeviceStorageDAO = require('../../src/js/dao/devicestorage-dao');
var Auth = require('../../../src/js/service/auth'),
OAuth = require('../../../src/js/service/oauth'),
PGP = require('../../../src/js/crypto/pgp'),
DeviceStorageDAO = require('../../../src/js/service/devicestorage');
describe('Auth unit tests', function() {
// Constancts

View File

@ -1,7 +1,7 @@
'use strict';
var LawnchairDAO = require('../../src/js/dao/lawnchair-dao'),
DeviceStorageDAO = require('../../src/js/dao/devicestorage-dao');
var LawnchairDAO = require('../../../src/js/service/lawnchair'),
DeviceStorageDAO = require('../../../src/js/service/devicestorage');
var testUser = 'test@example.com';
@ -17,14 +17,9 @@ describe('Device Storage DAO unit tests', function() {
afterEach(function() {});
describe('init', function() {
it('should work', function(done) {
lawnchairDaoStub.init.yields();
storageDao.init(testUser, function(err) {
expect(err).to.not.exist;
expect(lawnchairDaoStub.init.calledOnce).to.be.true;
done();
});
it('should work', function() {
storageDao.init(testUser);
expect(lawnchairDaoStub.init.calledOnce).to.be.true;
});
});

View File

@ -1,7 +1,8 @@
'use strict';
var RestDAO = require('../../src/js/dao/rest-dao'),
InvitationDAO = require('../../src/js/dao/invitation-dao');
var RestDAO = require('../../../src/js/service/rest'),
InvitationDAO = require('../../../src/js/service/invitation'),
appConfig = require('../../../src/js/app-config');
describe('Invitation DAO unit tests', function() {
var restDaoStub, invitationDao,
@ -11,7 +12,7 @@ describe('Invitation DAO unit tests', function() {
beforeEach(function() {
restDaoStub = sinon.createStubInstance(RestDAO);
invitationDao = new InvitationDAO(restDaoStub);
invitationDao = new InvitationDAO(restDaoStub, appConfig);
});
describe('initialization', function() {

View File

@ -1,17 +1,19 @@
'use strict';
var LawnchairDAO = require('../../src/js/dao/lawnchair-dao'),
PublicKeyDAO = require('../../src/js/dao/publickey-dao'),
KeychainDAO = require('../../src/js/dao/keychain-dao'),
PrivateKeyDAO = require('../../src/js/dao/privatekey-dao'),
Crypto = require('../../src/js/crypto/crypto'),
PGP = require('../../src/js/crypto/pgp');
var LawnchairDAO = require('../../../src/js/service/lawnchair'),
PublicKeyDAO = require('../../../src/js/service/publickey'),
KeychainDAO = require('../../../src/js/service/keychain'),
PrivateKeyDAO = require('../../../src/js/service/privatekey'),
Crypto = require('../../../src/js/crypto/crypto'),
PGP = require('../../../src/js/crypto/pgp'),
Dialog = require('../../../src/js/util/dialog'),
appConfig = require('../../../src/js/app-config');
var testUser = 'test@example.com';
describe('Keychain DAO unit tests', function() {
var keychainDao, lawnchairDaoStub, pubkeyDaoStub, privkeyDaoStub, cryptoStub, pgpStub;
var keychainDao, lawnchairDaoStub, pubkeyDaoStub, privkeyDaoStub, cryptoStub, pgpStub, dialogStub;
beforeEach(function() {
lawnchairDaoStub = sinon.createStubInstance(LawnchairDAO);
@ -19,11 +21,25 @@ describe('Keychain DAO unit tests', function() {
privkeyDaoStub = sinon.createStubInstance(PrivateKeyDAO);
cryptoStub = sinon.createStubInstance(Crypto);
pgpStub = sinon.createStubInstance(PGP);
keychainDao = new KeychainDAO(lawnchairDaoStub, pubkeyDaoStub, privkeyDaoStub, cryptoStub, pgpStub);
dialogStub = sinon.createStubInstance(Dialog);
keychainDao = new KeychainDAO(lawnchairDaoStub, pubkeyDaoStub, privkeyDaoStub, cryptoStub, pgpStub, dialogStub, appConfig);
});
afterEach(function() {});
describe('requestPermissionForKeyUpdate', function() {
it('should work', function() {
var opt = {
newKey: {},
userId: 'asdf@example.com'
};
keychainDao.requestPermissionForKeyUpdate(opt, function() {
expect(dialogStub.confirm.calledOnce).to.be.true;
});
});
});
describe('verify public key', function() {
it('should verify public key', function(done) {
var uuid = 'asdfasdfasdfasdf';

View File

@ -1,6 +1,6 @@
'use strict';
var LawnchairDAO = require('../../src/js/dao/lawnchair-dao');
var LawnchairDAO = require('../../../src/js/service/lawnchair');
var dbName = 'lawnchair@test.com';
@ -20,13 +20,10 @@ var data2 = {
describe('Lawnchair DAO unit tests', function() {
var lawnchairDao;
beforeEach(function(done) {
beforeEach(function() {
lawnchairDao = new LawnchairDAO();
lawnchairDao.init(dbName, function(err) {
expect(err).to.not.exist;
expect(lawnchairDao._db).to.exist;
done();
});
lawnchairDao.init(dbName);
expect(lawnchairDao._db).to.exist;
});
afterEach(function(done) {

View File

@ -1,16 +1,14 @@
'use strict';
var mocks = angular.mock,
cfg = require('../../src/js/app-config').config;
require('../../src/js/service/mail-config');
var cfg = require('../../../src/js/app-config').config;
describe('Mail Config Service unit test', function() {
var mailConfig, httpBackend;
beforeEach(function() {
angular.module('mail-config-test', ['woServices']);
mocks.module('mail-config-test');
mocks.inject(function($injector, $httpBackend) {
angular.mock.module('mail-config-test');
angular.mock.inject(function($injector, $httpBackend) {
httpBackend = $httpBackend;
mailConfig = $injector.get('mailConfig');
});

View File

@ -1,15 +1,12 @@
'use strict';
var mocks = angular.mock;
require('../../src/js/service/newsletter');
describe('Newsletter Service unit test', function() {
var newsletter;
beforeEach(function() {
angular.module('newsletter-test', ['woServices']);
mocks.module('newsletter-test');
mocks.inject(function($injector) {
angular.mock.module('newsletter-test');
angular.mock.inject(function($injector) {
newsletter = $injector.get('newsletter');
});
});

View File

@ -1,7 +1,7 @@
'use strict';
var OAuth = require('../../src/js/util/oauth'),
RestDAO = require('../../src/js/dao/rest-dao');
var OAuth = require('../../../src/js/service/oauth'),
RestDAO = require('../../../src/js/service/rest');
describe('OAuth unit tests', function() {
var oauth, googleApiStub, identityStub, getPlatformInfoStub, removeCachedStub,

View File

@ -1,7 +1,8 @@
'use strict';
var RestDAO = require('../../src/js/dao/rest-dao'),
PrivateKeyDAO = require('../../src/js/dao/privatekey-dao');
var RestDAO = require('../../../src/js/service/rest'),
PrivateKeyDAO = require('../../../src/js/service/privatekey'),
appConfig = require('../../../src/js/app-config');
describe('Private Key DAO unit tests', function() {
@ -11,7 +12,7 @@ describe('Private Key DAO unit tests', function() {
beforeEach(function() {
restDaoStub = sinon.createStubInstance(RestDAO);
privkeyDao = new PrivateKeyDAO(restDaoStub);
privkeyDao = new PrivateKeyDAO(restDaoStub, appConfig);
});
afterEach(function() {});

View File

@ -1,7 +1,8 @@
'use strict';
var RestDAO = require('../../src/js/dao/rest-dao'),
PublicKeyDAO = require('../../src/js/dao/publickey-dao');
var RestDAO = require('../../../src/js/service/rest'),
PublicKeyDAO = require('../../../src/js/service/publickey'),
appConfig = require('../../../src/js/app-config');
describe('Public Key DAO unit tests', function() {
@ -9,7 +10,7 @@ describe('Public Key DAO unit tests', function() {
beforeEach(function() {
restDaoStub = sinon.createStubInstance(RestDAO);
pubkeyDao = new PublicKeyDAO(restDaoStub);
pubkeyDao = new PublicKeyDAO(restDaoStub, appConfig);
});
afterEach(function() {});

View File

@ -1,6 +1,6 @@
'use strict';
var RestDAO = require('../../src/js/dao/rest-dao');
var RestDAO = require('../../../src/js/service/rest');
describe('Rest DAO unit tests', function() {
@ -20,18 +20,12 @@ describe('Rest DAO unit tests', function() {
xhrMock.restore();
});
describe('contructor', function() {
it('should set default base uri', function() {
restDao = new RestDAO();
expect(restDao).to.exist;
expect(restDao._baseUri).to.exist;
});
it('should accept default base uri', function() {
describe('setBaseUri', function() {
it('should accept base uri', function() {
var baseUri = 'http://custom.com';
restDao = new RestDAO(baseUri);
expect(restDao).to.exist;
restDao = new RestDAO();
expect(restDao._baseUri).to.not.exist;
restDao.setBaseUri(baseUri);
expect(restDao._baseUri).to.equal(baseUri);
});
});