1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-26 02:42:17 -05:00
mail/test/unit/controller/app/contacts-ctrl-test.js

185 lines
5.3 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
2014-10-09 09:56:35 -04:00
var mocks = angular.mock,
2014-10-07 14:32:23 -04:00
ContactsCtrl = require('../../src/js/controller/contacts'),
appController = require('../../src/js/app-controller'),
KeychainDAO = require('../../src/js/dao/keychain-dao'),
PGP = require('../../src/js/crypto/pgp');
describe('Contacts Controller unit test', function() {
var scope, contactsCtrl,
origKeychain, keychainMock,
origPgp, pgpMock;
beforeEach(function() {
origPgp = appController._pgp;
appController._pgp = pgpMock = sinon.createStubInstance(PGP);
origKeychain = appController._keychain;
appController._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
angular.module('contactstest', []);
mocks.module('contactstest');
mocks.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
scope.state = {};
contactsCtrl = $controller(ContactsCtrl, {
$scope: scope
});
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
afterEach(function() {
// restore the module
appController._pgp = origPgp;
appController._keychain = origKeychain;
});
2014-10-07 14:32:23 -04:00
describe('scope variables', function() {
it('should be set correctly', function() {
expect(scope.state.contacts.toggle).to.exist;
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('listKeys', function() {
it('should fail due to error in keychain.listLocalPublicKeys', function(done) {
keychainMock.listLocalPublicKeys.yields(42);
2014-10-07 14:32:23 -04:00
scope.onError = function(err) {
expect(err).to.equal(42);
done();
};
2014-10-07 14:32:23 -04:00
scope.listKeys();
});
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
keychainMock.listLocalPublicKeys.yields(null, [{
_id: '12345'
}]);
pgpMock.getKeyParams.returns({
fingerprint: 'asdf'
});
2014-10-07 14:32:23 -04:00
scope.$apply = function() {
expect(scope.keys.length).to.equal(1);
expect(scope.keys[0]._id).to.equal('12345');
expect(scope.keys[0].fingerprint).to.equal('asdf');
done();
};
expect(scope.keys).to.not.exist;
scope.listKeys();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('getFingerprint', function() {
it('should work', function() {
var key = {
fingerprint: 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
};
2014-10-07 14:32:23 -04:00
scope.getFingerprint(key);
2014-10-07 14:32:23 -04:00
expect(scope.fingerprint).to.equal('YYYY YYYY YYYY YYYY YYYY ... YYYY YYYY YYYY YYYY YYYY');
});
2014-10-07 14:32:23 -04:00
});
describe('importKey', function() {
it('should work', function(done) {
var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
2014-10-07 14:32:23 -04:00
pgpMock.getKeyParams.returns({
_id: '12345',
userId: 'max@example.com',
userIds: []
});
2014-10-07 14:32:23 -04:00
keychainMock.saveLocalPublicKey.withArgs({
_id: '12345',
userId: 'max@example.com',
userIds: [],
publicKey: '-----BEGIN PGP PUBLIC KEY BLOCK-----',
imported: true
}).yields();
2014-10-07 14:32:23 -04:00
scope.listKeys = function() {
done();
};
2014-10-07 14:32:23 -04:00
scope.importKey(keyArmored);
});
2014-10-07 14:32:23 -04:00
it('should fail due to invalid armored key', function(done) {
var keyArmored = '-----BEGIN PGP PRIVATE KEY BLOCK-----';
2014-10-07 14:32:23 -04:00
scope.onError = function(err) {
expect(err).to.exist;
done();
};
2014-10-07 14:32:23 -04:00
scope.importKey(keyArmored);
});
2014-10-07 14:32:23 -04:00
it('should fail due to error in pgp.getKeyParams', function(done) {
var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
2014-10-07 14:32:23 -04:00
pgpMock.getKeyParams.throws(new Error('WAT'));
2014-10-07 14:32:23 -04:00
scope.onError = function(err) {
expect(err).to.exist;
done();
};
2014-10-07 14:32:23 -04:00
scope.importKey(keyArmored);
});
2014-10-07 14:32:23 -04:00
it('should fail due to error in keychain.saveLocalPublicKey', function(done) {
var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
2014-10-07 14:32:23 -04:00
pgpMock.getKeyParams.returns({
_id: '12345',
userId: 'max@example.com'
});
2014-10-07 14:32:23 -04:00
keychainMock.saveLocalPublicKey.yields(42);
scope.onError = function(err) {
expect(err).to.equal(42);
done();
};
scope.importKey(keyArmored);
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('removeKey', function() {
it('should work', function(done) {
var key = {
_id: '12345'
};
2014-10-07 14:32:23 -04:00
keychainMock.removeLocalPublicKey.withArgs('12345').yields();
2014-10-07 14:32:23 -04:00
scope.listKeys = function() {
done();
};
2014-10-07 14:32:23 -04:00
scope.removeKey(key);
});
2014-10-07 14:32:23 -04:00
it('should fail due to error in keychain.removeLocalPublicKey', function(done) {
var key = {
_id: '12345'
};
2014-10-07 14:32:23 -04:00
keychainMock.removeLocalPublicKey.withArgs('12345').yields(42);
2014-10-07 14:32:23 -04:00
scope.onError = function(err) {
expect(err).to.equal(42);
done();
};
2014-10-07 14:32:23 -04:00
scope.removeKey(key);
});
});
});