1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-14 05:05:10 -05:00
mail/test/unit/controller/app/account-ctrl-test.js
2014-11-25 18:19:40 +01:00

95 lines
3.5 KiB
JavaScript

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