2014-10-07 14:32:23 -04:00
|
|
|
'use strict';
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-11-21 09:06:29 -05: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);
|
2014-11-21 09:06:29 -05:00
|
|
|
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) {
|
|
|
|
privkeyDao.requestDeviceRegistration({}, function(err, sessionKey) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
expect(sessionKey).to.not.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) {
|
|
|
|
restDaoStub.post.yields(null, {
|
|
|
|
encryptedRegSessionKey: 'asdf'
|
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
privkeyDao.requestDeviceRegistration({
|
|
|
|
userId: emailAddress,
|
|
|
|
deviceName: deviceName
|
|
|
|
}, function(err, sessionKey) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
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) {
|
|
|
|
privkeyDao.uploadDeviceSecret({}, function(err) {
|
|
|
|
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) {
|
|
|
|
restDaoStub.put.yields();
|
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'
|
|
|
|
}, function(err) {
|
|
|
|
expect(err).to.not.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('requestAuthSessionKey', function() {
|
|
|
|
it('should fail due to invalid args', function(done) {
|
|
|
|
privkeyDao.requestAuthSessionKey({}, function(err) {
|
|
|
|
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) {
|
|
|
|
restDaoStub.post.withArgs(undefined, '/auth/user/' + emailAddress).yields();
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
privkeyDao.requestAuthSessionKey({
|
|
|
|
userId: emailAddress
|
|
|
|
}, function(err) {
|
|
|
|
expect(err).to.not.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('verifyAuthentication', function() {
|
|
|
|
it('should fail due to invalid args', function(done) {
|
|
|
|
privkeyDao.verifyAuthentication({}, function(err) {
|
|
|
|
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-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
restDaoStub.put.withArgs(options, '/auth/user/' + emailAddress + '/session/' + sessionId).yields();
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
privkeyDao.verifyAuthentication(options, function(err) {
|
|
|
|
expect(err).to.not.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('upload', function() {
|
|
|
|
it('should fail due to invalid args', function(done) {
|
|
|
|
privkeyDao.upload({}, function(err) {
|
|
|
|
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-10-07 14:32:23 -04:00
|
|
|
restDaoStub.post.withArgs(options, '/privatekey/user/' + emailAddress + '/session/' + options.sessionId).yields();
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
privkeyDao.upload(options, function(err) {
|
|
|
|
expect(err).to.not.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('requestDownload', function() {
|
|
|
|
it('should fail due to invalid args', function(done) {
|
|
|
|
privkeyDao.requestDownload({}, function(err) {
|
|
|
|
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 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
|
|
|
|
}).yields();
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
privkeyDao.requestDownload({
|
|
|
|
userId: emailAddress,
|
|
|
|
keyId: keyId
|
|
|
|
}, function(err, found) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
expect(found).to.be.true;
|
|
|
|
done();
|
2014-08-28 12:17:34 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-08-28 12:17:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('hasPrivateKey', function() {
|
|
|
|
it('should fail due to invalid args', function(done) {
|
|
|
|
privkeyDao.hasPrivateKey({}, function(err) {
|
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2014-08-28 12:17:34 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-08-28 12:17:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
|
|
|
var keyId = '12345';
|
2014-08-28 12:17:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
restDaoStub.get.withArgs({
|
|
|
|
uri: '/privatekey/user/' + emailAddress + '/key/' + keyId + '?ignoreRecovery=true'
|
|
|
|
}).yields();
|
2014-08-28 12:17:34 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
privkeyDao.hasPrivateKey({
|
|
|
|
userId: emailAddress,
|
|
|
|
keyId: keyId
|
|
|
|
}, function(err, found) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
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) {
|
|
|
|
privkeyDao.download({}, function(err) {
|
|
|
|
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'
|
|
|
|
}).yields();
|
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'
|
|
|
|
}, function(err) {
|
|
|
|
expect(err).to.not.exist;
|
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|