2014-10-07 14:32:23 -04:00
|
|
|
'use strict';
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
var AccountCtrl = require('../../../../src/js/controller/app/account'),
|
|
|
|
PGP = require('../../../../src/js/crypto/pgp'),
|
|
|
|
Download = require('../../../../src/js/util/download'),
|
|
|
|
Keychain = require('../../../../src/js/service/keychain'),
|
|
|
|
Auth = require('../../../../src/js/service/auth'),
|
|
|
|
Dialog = require('../../../../src/js/util/dialog');
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('Account Controller unit test', function() {
|
|
|
|
var scope, accountCtrl,
|
|
|
|
dummyFingerprint, expectedFingerprint,
|
|
|
|
dummyKeyId, expectedKeyId,
|
2014-11-25 12:19:40 -05:00
|
|
|
emailAddress, keySize, pgpStub, keychainStub, authStub, dialogStub, downloadStub;
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
beforeEach(function() {
|
2014-11-25 12:19:40 -05:00
|
|
|
pgpStub = sinon.createStubInstance(PGP);
|
|
|
|
authStub = sinon.createStubInstance(Auth);
|
|
|
|
keychainStub = sinon.createStubInstance(Keychain);
|
|
|
|
dialogStub = sinon.createStubInstance(Dialog);
|
|
|
|
downloadStub = sinon.createStubInstance(Download);
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
dummyFingerprint = '3A2D39B4E1404190B8B949DE7D7E99036E712926';
|
|
|
|
expectedFingerprint = '3A2D 39B4 E140 4190 B8B9 49DE 7D7E 9903 6E71 2926';
|
|
|
|
dummyKeyId = '9FEB47936E712926';
|
|
|
|
expectedKeyId = '6E712926';
|
2014-11-25 12:19:40 -05:00
|
|
|
pgpStub.getFingerprint.returns(dummyFingerprint);
|
|
|
|
pgpStub.getKeyId.returns(dummyKeyId);
|
2014-10-07 14:32:23 -04:00
|
|
|
emailAddress = 'fred@foo.com';
|
|
|
|
keySize = 1234;
|
2014-11-25 12:19:40 -05:00
|
|
|
authStub.emailAddress = emailAddress;
|
|
|
|
pgpStub.getKeyParams.returns({
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: dummyKeyId,
|
|
|
|
fingerprint: dummyFingerprint,
|
|
|
|
userId: emailAddress,
|
|
|
|
bitSize: keySize
|
|
|
|
});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
angular.module('accounttest', ['woServices']);
|
|
|
|
angular.mock.module('accounttest');
|
|
|
|
angular.mock.inject(function($rootScope, $controller) {
|
2014-10-07 14:32:23 -04:00
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.state = {};
|
|
|
|
accountCtrl = $controller(AccountCtrl, {
|
2014-11-25 12:19:40 -05:00
|
|
|
$scope: scope,
|
|
|
|
auth: authStub,
|
|
|
|
keychain: keychainStub,
|
|
|
|
pgp: pgpStub,
|
|
|
|
download: downloadStub,
|
|
|
|
dialog: dialogStub
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
afterEach(function() {});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04: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);
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
describe('export to key file', function() {
|
|
|
|
it('should work', function() {
|
2014-11-25 12:19:40 -05:00
|
|
|
keychainStub.getUserKeyPair.withArgs(emailAddress).yields(null, {
|
2014-10-07 14:32:23 -04:00
|
|
|
publicKey: {
|
|
|
|
_id: dummyKeyId,
|
|
|
|
publicKey: 'a'
|
|
|
|
},
|
|
|
|
privateKey: {
|
|
|
|
encryptedKey: 'b'
|
|
|
|
}
|
|
|
|
});
|
2014-11-25 12:19:40 -05:00
|
|
|
downloadStub.createDownload.withArgs(sinon.match(function(arg) {
|
2014-10-07 14:32:23 -04:00
|
|
|
return arg.content === 'a\r\nb' && arg.filename === 'whiteout_mail_' + emailAddress + '_' + expectedKeyId + '.asc' && arg.contentType === 'text/plain';
|
|
|
|
})).returns();
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.exportKeyFile();
|
2014-09-24 06:46:19 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(scope.state.lightbox).to.equal(undefined);
|
2014-11-25 12:19:40 -05:00
|
|
|
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
|
|
|
|
expect(downloadStub.createDownload.calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
it('should not work when key export failed', function() {
|
|
|
|
keychainStub.getUserKeyPair.yields(new Error());
|
2013-11-11 11:56:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.exportKeyFile();
|
2014-11-25 12:19:40 -05:00
|
|
|
|
|
|
|
expect(dialogStub.error.calledOnce).to.be.true;
|
2013-11-11 11:56:51 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|