2013-11-11 11:56:51 -05:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var expect = chai.expect,
|
|
|
|
angular = require('angular'),
|
|
|
|
mocks = require('angularMocks'),
|
|
|
|
AccountCtrl = require('js/controller/account'),
|
|
|
|
PGP = require('js/crypto/pgp'),
|
|
|
|
dl = require('js/util/download'),
|
2014-04-11 12:39:13 -04:00
|
|
|
appController = require('js/app-controller'),
|
|
|
|
KeychainDAO = require('js/dao/keychain-dao');
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
describe('Account Controller unit test', function() {
|
2014-04-11 12:39:13 -04:00
|
|
|
var scope, accountCtrl,
|
2013-11-11 11:56:51 -05:00
|
|
|
dummyFingerprint, expectedFingerprint,
|
|
|
|
dummyKeyId, expectedKeyId,
|
2014-04-11 12:39:13 -04:00
|
|
|
emailAddress, keySize, cryptoMock, keychainMock;
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-04-11 12:39:13 -04:00
|
|
|
appController._crypto = cryptoMock = sinon.createStubInstance(PGP);
|
|
|
|
appController._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
dummyFingerprint = '3A2D39B4E1404190B8B949DE7D7E99036E712926';
|
|
|
|
expectedFingerprint = '3A2D 39B4 E140 4190 B8B9 49DE 7D7E 9903 6E71 2926';
|
|
|
|
dummyKeyId = '9FEB47936E712926';
|
|
|
|
expectedKeyId = '6E712926';
|
|
|
|
cryptoMock.getFingerprint.returns(dummyFingerprint);
|
|
|
|
cryptoMock.getKeyId.returns(dummyKeyId);
|
|
|
|
emailAddress = 'fred@foo.com';
|
|
|
|
keySize = 1234;
|
2014-04-11 12:39:13 -04:00
|
|
|
appController._emailDao = {
|
|
|
|
_account: {
|
|
|
|
emailAddress: emailAddress,
|
|
|
|
asymKeySize: keySize
|
|
|
|
}
|
2013-11-11 11:56:51 -05:00
|
|
|
};
|
2014-04-11 12:39:13 -04:00
|
|
|
cryptoMock.getKeyParams.returns({
|
|
|
|
_id: dummyKeyId,
|
|
|
|
fingerprint: dummyFingerprint,
|
|
|
|
userId: emailAddress,
|
|
|
|
bitSize: keySize
|
|
|
|
});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
angular.module('accounttest', []);
|
|
|
|
mocks.module('accounttest');
|
|
|
|
mocks.inject(function($rootScope, $controller) {
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.state = {};
|
|
|
|
accountCtrl = $controller(AccountCtrl, {
|
|
|
|
$scope: scope
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-04-11 12:39:13 -04:00
|
|
|
afterEach(function() {});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
describe('scope variables', function() {
|
|
|
|
it('should be set correctly', function() {
|
|
|
|
expect(scope.eMail).to.equal(emailAddress);
|
|
|
|
expect(scope.keyId).to.equal(expectedKeyId);
|
|
|
|
expect(scope.fingerprint).to.equal(expectedFingerprint);
|
|
|
|
expect(scope.keysize).to.equal(keySize);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('export to key file', function() {
|
2013-11-14 14:13:27 -05:00
|
|
|
it('should work', function(done) {
|
2013-11-11 11:56:51 -05:00
|
|
|
var createDownloadMock = sinon.stub(dl, 'createDownload');
|
2014-04-11 12:39:13 -04:00
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).yields(null, {
|
|
|
|
publicKey: {
|
|
|
|
_id: dummyKeyId,
|
|
|
|
publicKey: 'a'
|
|
|
|
},
|
|
|
|
privateKey: {
|
|
|
|
encryptedKey: 'b'
|
|
|
|
}
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
createDownloadMock.withArgs(sinon.match(function(arg) {
|
2014-01-07 13:23:03 -05:00
|
|
|
return arg.content === 'ab' && arg.filename === 'whiteout_mail_' + emailAddress + '_' + expectedKeyId + '.asc' && arg.contentType === 'text/plain';
|
2013-11-11 11:56:51 -05:00
|
|
|
})).yields();
|
2014-04-11 12:39:13 -04:00
|
|
|
scope.onError = function(err) {
|
|
|
|
expect(err.title).to.equal('Success');
|
|
|
|
expect(scope.state.account.open).to.be.false;
|
|
|
|
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
|
2013-11-14 14:13:27 -05:00
|
|
|
expect(dl.createDownload.calledOnce).to.be.true;
|
|
|
|
dl.createDownload.restore();
|
|
|
|
done();
|
|
|
|
};
|
2013-11-11 11:56:51 -05:00
|
|
|
|
|
|
|
scope.exportKeyFile();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not work when key export failed', function(done) {
|
2014-04-11 12:39:13 -04:00
|
|
|
keychainMock.getUserKeyPair.yields(new Error('Boom!'));
|
|
|
|
scope.onError = function(err) {
|
|
|
|
expect(err.message).to.equal('Boom!');
|
|
|
|
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
|
2013-11-11 11:56:51 -05:00
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.exportKeyFile();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not work when create download failed', function(done) {
|
|
|
|
var createDownloadMock = sinon.stub(dl, 'createDownload');
|
2014-04-11 12:39:13 -04:00
|
|
|
keychainMock.getUserKeyPair.withArgs(emailAddress).yields(null, {
|
|
|
|
publicKey: {
|
|
|
|
_id: dummyKeyId,
|
|
|
|
publicKey: 'a'
|
|
|
|
},
|
|
|
|
privateKey: {
|
|
|
|
encryptedKey: 'b'
|
|
|
|
}
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
2014-01-07 13:23:03 -05:00
|
|
|
createDownloadMock.withArgs().yields(new Error('asdasd'));
|
2014-04-11 12:39:13 -04:00
|
|
|
scope.onError = function(err) {
|
|
|
|
expect(err.message).to.equal('asdasd');
|
|
|
|
expect(keychainMock.getUserKeyPair.calledOnce).to.be.true;
|
2013-11-11 11:56:51 -05:00
|
|
|
expect(dl.createDownload.calledOnce).to.be.true;
|
|
|
|
dl.createDownload.restore();
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.exportKeyFile();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|