2014-10-07 14:32:23 -04:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
var ContactsCtrl = require('../../../../src/js/controller/app/contacts'),
|
|
|
|
Keychain = require('../../../../src/js/service/keychain'),
|
|
|
|
PGP = require('../../../../src/js/crypto/pgp'),
|
|
|
|
Dialog = require('../../../../src/js/util/dialog');
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
describe('Contacts Controller unit test', function() {
|
2014-11-25 12:19:40 -05:00
|
|
|
var scope, contactsCtrl, keychainStub, pgpStub, dialogStub;
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-11-25 12:19:40 -05:00
|
|
|
pgpStub = sinon.createStubInstance(PGP);
|
|
|
|
keychainStub = sinon.createStubInstance(Keychain);
|
|
|
|
dialogStub = sinon.createStubInstance(Dialog);
|
|
|
|
|
|
|
|
angular.module('contactstest', ['woServices']);
|
|
|
|
angular.mock.module('contactstest');
|
|
|
|
angular.mock.inject(function($rootScope, $controller) {
|
2014-10-07 14:32:23 -04:00
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.state = {};
|
|
|
|
contactsCtrl = $controller(ContactsCtrl, {
|
2014-11-25 12:19:40 -05:00
|
|
|
$scope: scope,
|
2014-12-18 09:19:06 -05:00
|
|
|
$q: window.qMock,
|
2014-11-25 12:19:40 -05:00
|
|
|
keychain: keychainStub,
|
|
|
|
pgp: pgpStub,
|
|
|
|
dialog: dialogStub
|
2014-03-06 12:19:51 -05:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
afterEach(function() {});
|
2014-03-06 12:19:51 -05:00
|
|
|
|
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-03-06 12:19:51 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('listKeys', function() {
|
2014-12-18 09:19:06 -05:00
|
|
|
it('should fail due to error in keychain.listLocalPublicKeys', function(done) {
|
|
|
|
keychainStub.listLocalPublicKeys.returns(rejects(42));
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
scope.listKeys().then(function() {
|
|
|
|
expect(dialogStub.error.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
it('should work', function(done) {
|
|
|
|
keychainStub.listLocalPublicKeys.returns(resolves([{
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345'
|
2014-12-18 09:19:06 -05:00
|
|
|
}]));
|
2014-11-25 12:19:40 -05:00
|
|
|
pgpStub.getKeyParams.returns({
|
2014-10-07 14:32:23 -04:00
|
|
|
fingerprint: 'asdf'
|
2014-03-06 12:19:51 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
expect(scope.keys).to.not.exist;
|
2014-12-18 09:19:06 -05:00
|
|
|
scope.listKeys().then(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();
|
|
|
|
});
|
2014-03-06 12:19:51 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('getFingerprint', function() {
|
|
|
|
it('should work', function() {
|
|
|
|
var key = {
|
|
|
|
fingerprint: 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
|
|
|
|
};
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.getFingerprint(key);
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(scope.fingerprint).to.equal('YYYY YYYY YYYY YYYY YYYY ... YYYY YYYY YYYY YYYY YYYY');
|
2014-03-06 12:19:51 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('importKey', function() {
|
|
|
|
it('should work', function(done) {
|
|
|
|
var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
pgpStub.getKeyParams.returns({
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
userId: 'max@example.com',
|
|
|
|
userIds: []
|
2014-03-06 12:19:51 -05:00
|
|
|
});
|
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
keychainStub.saveLocalPublicKey.withArgs({
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
userId: 'max@example.com',
|
|
|
|
userIds: [],
|
|
|
|
publicKey: '-----BEGIN PGP PUBLIC KEY BLOCK-----',
|
|
|
|
imported: true
|
2014-12-18 09:19:06 -05:00
|
|
|
}).returns(resolves());
|
2014-03-10 13:30:06 -04:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
scope.listKeys = function() {};
|
2014-03-10 13:30:06 -04:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
scope.importKey(keyArmored).then(function() {
|
|
|
|
done();
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-03-10 13:30:06 -04:00
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
it('should fail due to invalid armored key', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
var keyArmored = '-----BEGIN PGP PRIVATE KEY BLOCK-----';
|
2014-03-10 13:30:06 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.importKey(keyArmored);
|
2014-11-25 12:19:40 -05:00
|
|
|
|
|
|
|
expect(dialogStub.error.calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-03-10 13:30:06 -04:00
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
it('should fail due to error in pgp.getKeyParams', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
|
2014-11-25 12:19:40 -05:00
|
|
|
pgpStub.getKeyParams.throws(new Error('WAT'));
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
scope.importKey(keyArmored);
|
2014-11-25 12:19:40 -05:00
|
|
|
|
|
|
|
expect(dialogStub.error.calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
it('should fail due to error in keychain.saveLocalPublicKey', function(done) {
|
2014-10-07 14:32:23 -04:00
|
|
|
var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-11-25 12:19:40 -05:00
|
|
|
pgpStub.getKeyParams.returns({
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
userId: 'max@example.com'
|
2014-03-06 12:19:51 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
keychainStub.saveLocalPublicKey.returns(rejects(42));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
scope.importKey(keyArmored).then(function() {
|
|
|
|
expect(dialogStub.error.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
2014-03-06 12:19:51 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('removeKey', function() {
|
|
|
|
it('should work', function(done) {
|
|
|
|
var key = {
|
|
|
|
_id: '12345'
|
|
|
|
};
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
keychainStub.removeLocalPublicKey.withArgs('12345').returns(resolves());
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
scope.listKeys = function() {};
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
scope.removeKey(key).then(function() {
|
|
|
|
done();
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
it('should fail due to error in keychain.removeLocalPublicKey', function(done) {
|
2014-10-07 14:32:23 -04:00
|
|
|
var key = {
|
|
|
|
_id: '12345'
|
|
|
|
};
|
2014-03-06 12:19:51 -05:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
keychainStub.removeLocalPublicKey.withArgs('12345').returns(rejects(42));
|
2014-11-25 12:19:40 -05:00
|
|
|
|
2014-12-18 09:19:06 -05:00
|
|
|
scope.removeKey(key).then(function() {
|
|
|
|
expect(dialogStub.error.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
2014-03-06 12:19:51 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|