1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-23 09:22:23 -05:00
mail/test/unit/service/privatekey-dao-test.js

240 lines
7.1 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
2014-06-06 12:36:23 -04:00
var RestDAO = require('../../../src/js/service/rest'),
PrivateKeyDAO = require('../../../src/js/service/privatekey'),
appConfig = require('../../../src/js/app-config');
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
describe('Private Key DAO unit tests', function() {
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
var privkeyDao, restDaoStub,
emailAddress = 'test@example.com',
deviceName = 'iPhone Work';
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
beforeEach(function() {
restDaoStub = sinon.createStubInstance(RestDAO);
privkeyDao = new PrivateKeyDAO(restDaoStub, appConfig);
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
afterEach(function() {});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
describe('requestDeviceRegistration', function() {
it('should fail due to invalid args', function(done) {
2014-12-11 13:07:04 -05:00
privkeyDao.requestDeviceRegistration({}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
done();
2014-06-06 12:36:23 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
2014-12-11 13:07:04 -05:00
restDaoStub.post.returns(resolves({
2014-10-07 14:32:23 -04:00
encryptedRegSessionKey: 'asdf'
2014-12-11 13:07:04 -05:00
}));
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
privkeyDao.requestDeviceRegistration({
userId: emailAddress,
deviceName: deviceName
2014-12-11 13:07:04 -05:00
}).then(function(sessionKey) {
2014-10-07 14:32:23 -04:00
expect(sessionKey).to.exist;
done();
2014-06-06 12:36:23 -04:00
});
});
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
describe('uploadDeviceSecret', function() {
it('should fail due to invalid args', function(done) {
2014-12-11 13:07:04 -05:00
privkeyDao.uploadDeviceSecret({}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
done();
2014-06-06 12:36:23 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
2014-12-11 13:07:04 -05:00
restDaoStub.put.returns(resolves());
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
privkeyDao.uploadDeviceSecret({
userId: emailAddress,
deviceName: deviceName,
encryptedDeviceSecret: 'asdf',
iv: 'iv'
2014-12-11 13:07:04 -05:00
}).then(done);
2014-06-06 12:36:23 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
describe('requestAuthSessionKey', function() {
it('should fail due to invalid args', function(done) {
2014-12-11 13:07:04 -05:00
privkeyDao.requestAuthSessionKey({}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
done();
2014-06-06 12:36:23 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
2014-12-11 13:07:04 -05:00
restDaoStub.post.withArgs(undefined, '/auth/user/' + emailAddress).returns(resolves());
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
privkeyDao.requestAuthSessionKey({
userId: emailAddress
2014-12-11 13:07:04 -05:00
}).then(done);
2014-06-06 12:36:23 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
describe('verifyAuthentication', function() {
it('should fail due to invalid args', function(done) {
2014-12-11 13:07:04 -05:00
privkeyDao.verifyAuthentication({}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
done();
2014-06-06 12:36:23 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
var sessionId = '1';
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
var options = {
userId: emailAddress,
sessionId: sessionId,
encryptedChallenge: 'asdf',
encryptedDeviceSecret: 'qwer',
iv: ' iv'
};
2014-12-11 13:07:04 -05:00
restDaoStub.put.withArgs(options, '/auth/user/' + emailAddress + '/session/' + sessionId).returns(resolves());
2014-12-11 13:07:04 -05:00
privkeyDao.verifyAuthentication(options).then(done);
2014-06-06 12:36:23 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
describe('upload', function() {
it('should fail due to invalid args', function(done) {
2014-12-11 13:07:04 -05:00
privkeyDao.upload({}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
done();
2014-06-06 12:36:23 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
var options = {
_id: '12345',
userId: emailAddress,
encryptedPrivateKey: 'asdf',
sessionId: '1',
salt: 'salt',
iv: 'iv'
};
2014-06-06 12:36:23 -04:00
2014-12-11 13:07:04 -05:00
restDaoStub.post.withArgs(options, '/privatekey/user/' + emailAddress + '/session/' + options.sessionId).returns(resolves());
2014-06-06 12:36:23 -04:00
2014-12-11 13:07:04 -05:00
privkeyDao.upload(options).then(done);
2014-06-06 12:36:23 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
describe('requestDownload', function() {
it('should fail due to invalid args', function(done) {
2014-12-11 13:07:04 -05:00
privkeyDao.requestDownload({}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
done();
2014-06-06 12:36:23 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-12-11 13:07:04 -05:00
it('should not find a key', function(done) {
var keyId = '12345';
restDaoStub.get.withArgs({
uri: '/privatekey/user/' + emailAddress + '/key/' + keyId
}).returns(rejects({
code: 404
}));
privkeyDao.requestDownload({
userId: emailAddress,
keyId: keyId
}).then(function(found) {
expect(found).to.be.false;
done();
});
});
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
var keyId = '12345';
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
restDaoStub.get.withArgs({
uri: '/privatekey/user/' + emailAddress + '/key/' + keyId
2014-12-11 13:07:04 -05:00
}).returns(resolves());
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
privkeyDao.requestDownload({
userId: emailAddress,
keyId: keyId
2014-12-11 13:07:04 -05:00
}).then(function(found) {
2014-10-07 14:32:23 -04:00
expect(found).to.be.true;
done();
});
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('hasPrivateKey', function() {
it('should fail due to invalid args', function(done) {
2014-12-11 13:07:04 -05:00
privkeyDao.hasPrivateKey({}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
done();
});
2014-10-07 14:32:23 -04:00
});
2014-12-11 13:07:04 -05:00
it('should not find a key', function(done) {
var keyId = '12345';
restDaoStub.get.withArgs({
uri: '/privatekey/user/' + emailAddress + '/key/' + keyId + '?ignoreRecovery=true'
}).returns(rejects({
code: 404
}));
privkeyDao.hasPrivateKey({
userId: emailAddress,
keyId: keyId
}).then(function(found) {
expect(found).to.be.false;
done();
});
});
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
var keyId = '12345';
2014-10-07 14:32:23 -04:00
restDaoStub.get.withArgs({
uri: '/privatekey/user/' + emailAddress + '/key/' + keyId + '?ignoreRecovery=true'
2014-12-11 13:07:04 -05:00
}).returns(resolves());
2014-10-07 14:32:23 -04:00
privkeyDao.hasPrivateKey({
userId: emailAddress,
keyId: keyId
2014-12-11 13:07:04 -05:00
}).then(function(found) {
2014-10-07 14:32:23 -04:00
expect(found).to.be.true;
done();
2014-06-06 12:36:23 -04:00
});
});
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
describe('download', function() {
it('should fail due to invalid args', function(done) {
2014-12-11 13:07:04 -05:00
privkeyDao.download({}).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
done();
2014-06-06 12:36:23 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
var key = {
_id: '12345'
};
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
restDaoStub.get.withArgs({
uri: '/privatekey/user/' + emailAddress + '/key/' + key._id + '/recovery/token'
2014-12-11 13:07:04 -05:00
}).returns(resolves());
2014-06-06 12:36:23 -04:00
2014-10-07 14:32:23 -04:00
privkeyDao.download({
userId: emailAddress,
keyId: key._id,
recoveryToken: 'token'
2014-12-11 13:07:04 -05:00
}).then(done);
2014-06-06 12:36:23 -04:00
});
});
});