copay/test/old/controllersSpec.js

1113 lines
33 KiB
JavaScript
Raw Normal View History

2014-04-23 09:21:33 -07:00
//
// test/unit/controllers/controllersSpec.js
//
2014-06-16 13:37:33 -07:00
2014-06-24 11:46:07 -07:00
var sinon = require('sinon');
2014-06-16 13:37:33 -07:00
// Replace saveAs plugin
2014-09-12 06:40:47 -07:00
saveAs = function(blob, filename) {
saveAsLastCall = {
blob: blob,
filename: filename
};
2014-06-16 13:37:33 -07:00
};
describe("Unit: Controllers", function() {
2014-10-29 14:34:59 -07:00
config.plugins.LocalStorage = true;
config.plugins.GoogleDrive = null;
2014-11-30 18:08:16 -08:00
config.plugins.InsightStorage = null;
2014-12-09 14:29:56 -08:00
config.plugins.EncryptedInsightStorage = null;
2014-09-18 15:34:27 -07:00
2014-11-25 08:21:00 -08:00
var anAddr = 'mkfTyEk7tfgV611Z4ESwDDSZwhsZdbMpVy';
var anAmount = 1000;
var aComment = 'hola';
2014-06-26 14:37:30 -07:00
var invalidForm = {
$invalid: true
};
2014-04-23 09:21:33 -07:00
var scope;
var server;
beforeEach(module('copayApp'));
beforeEach(module('copayApp.controllers'));
beforeEach(module(function($provide) {
$provide.value('request', {
'get': function(_, cb) {
cb(null, null, [{
2014-10-29 14:34:59 -07:00
name: 'USD Dollars',
code: 'USD',
rate: 2
}]);
}
});
}));
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
2014-11-03 12:28:36 -08:00
$rootScope.safeUnspentCount = 1;
2014-10-29 14:34:59 -07:00
2014-12-09 14:29:56 -08:00
//
// TODO Use the REAL wallet, and stub only networking and DB components!
//
2015-01-15 13:51:08 -08:00
2014-10-29 14:34:59 -07:00
var w = {};
w.id = 1234;
2014-11-29 19:31:17 -08:00
w.isComplete = sinon.stub().returns(true);
2014-12-04 05:15:29 -08:00
w.isShared = sinon.stub().returns(true);
2014-10-29 14:34:59 -07:00
w.privateKey = {};
w.settings = {
unitToSatoshi: 100,
unitDecimals: 2,
alternativeName: 'US Dollar',
alternativeIsoCode: 'USD',
};
w.addressBook = {
'juan': '1',
};
w.totalCopayers = 2;
w.getMyCopayerNickname = sinon.stub().returns('nickname');
w.getMyCopayerId = sinon.stub().returns('id');
w.privateKey.toObj = sinon.stub().returns({
wallet: 'mock'
});
w.getSecret = sinon.stub().returns('secret');
w.getName = sinon.stub().returns('fakeWallet');
w.exportEncrypted = sinon.stub().returns('1234567');
2014-10-30 12:20:25 -07:00
w.getTransactionHistory = sinon.stub().yields(null);
2014-10-29 14:34:59 -07:00
w.getNetworkName = sinon.stub().returns('testnet');
2014-11-25 08:21:00 -08:00
w.spend = sinon.stub().yields(null);
w.sendTxProposal = sinon.stub();
w.broadcastTx = sinon.stub().yields(null);
2014-10-29 14:34:59 -07:00
w.requiresMultipleSignatures = sinon.stub().returns(true);
w.getTxProposals = sinon.stub().returns([1, 2, 3]);
2014-12-10 13:42:32 -08:00
w.getPendingTxProposals = sinon.stub().returns(
2014-12-10 13:18:28 -08:00
[{
2014-11-25 08:21:00 -08:00
isPending: true
2014-12-10 13:18:28 -08:00
}]
2014-12-10 13:42:32 -08:00
);
w.getId = sinon.stub().returns(1234);
2014-11-25 08:21:00 -08:00
w.on = sinon.stub().yields({
'e': 'errmsg',
'loading': false
});
2014-12-09 14:29:56 -08:00
w.sizes = sinon.stub().returns({
tota: 1234
});
w.getBalance = sinon.stub().returns(10000);
w.publicKeyRing = sinon.stub().yields(null);
w.publicKeyRing.nicknameForCopayer = sinon.stub().returns('nickcopayer');
w.updateFocusedTimestamp = sinon.stub().returns(1415804323);
2014-11-25 08:21:00 -08:00
w.getAddressesInfo = sinon.stub().returns([{
addressStr: "2MxvwvfshZxw4SkkaJZ8NDKLyepa9HLMKtu",
isChange: false
}]);
2015-02-12 05:20:14 -08:00
var iden = {};
iden.getLastFocusedWallet = sinon.stub().returns(null);
2014-12-17 19:38:00 -08:00
iden.getWallets = sinon.stub().returns([w]);
iden.getWalletById = sinon.stub().returns(w);
2014-11-13 03:44:29 -08:00
iden.getName = sinon.stub().returns('name');
2014-12-09 14:29:56 -08:00
iden.deleteWallet = sinon.stub();
2015-02-10 12:33:16 -08:00
iden.close = sinon.stub().returns(null);
2014-10-29 14:34:59 -07:00
$rootScope.wallet = w;
$rootScope.iden = iden;
2014-10-29 14:34:59 -07:00
}));
2014-09-18 15:34:27 -07:00
2014-08-29 09:20:47 -07:00
describe('Create Controller', function() {
var c;
2014-06-23 15:45:04 -07:00
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
2014-08-29 09:20:47 -07:00
c = $controller('CreateController', {
2014-06-23 15:45:04 -07:00
$scope: scope,
});
}));
describe('#getNumber', function() {
it('should return an array of n undefined elements', function() {
var n = 5;
var array = scope.getNumber(n);
expect(array.length).equal(n);
});
2014-06-23 15:45:04 -07:00
});
2014-06-26 14:22:32 -07:00
describe('#create', function() {
it('should work with invalid form', function() {
2014-06-26 14:37:30 -07:00
scope.create(invalidForm);
2014-06-26 14:22:32 -07:00
});
});
2015-02-12 05:20:14 -08:00
});
describe('Create Profile Controller', function() {
2015-02-12 10:08:39 -08:00
var c, confService, idenService;
beforeEach(inject(function($controller, $rootScope, configService, identityService) {
2015-02-12 05:20:14 -08:00
scope = $rootScope.$new();
2015-02-12 10:08:39 -08:00
confService = configService;
idenService = identityService;
2015-02-12 05:20:14 -08:00
c = $controller('CreateProfileController', {
$scope: scope,
});
}));
it('should exist', function() {
should.exist(c);
});
it('#init', function() {
scope.init();
});
it('#clear', function() {
scope.clear();
});
2014-06-23 15:45:04 -07:00
2015-02-12 10:08:39 -08:00
it('#saveSettings', function() {
var old = confService.set;
confService.set = sinon.stub().returns(null);
scope.saveSettings();
confService.set.calledOnce.should.be.true;
confService.set = old;
});
it('#createProfile', function() {
var old = scope.saveSettings;
scope.saveSettings = sinon.stub().returns(null);
scope.createProfile();
scope.saveSettings.calledOnce.should.be.true;
scope.saveSettings = old;
});
it('#_doCreateProfile', function() {
var old = idenService.create;
idenService.create = sinon.stub().returns(null);
scope._doCreateProfile('[email protected]', 'password');
idenService.create.calledOnce.should.be.true;
idenService.create = old;
});
it('#createDefaultWallet', function() {
var old = idenService.createDefaultWallet;
idenService.createDefaultWallet = sinon.stub().returns(null);
scope.createDefaultWallet();
idenService.createDefaultWallet.calledOnce.should.be.true;
idenService.createDefaultWallet = old;
});
2014-06-23 15:45:04 -07:00
});
2014-10-30 10:13:40 -07:00
describe('Receive Controller', function() {
var c;
2015-02-12 05:20:14 -08:00
var rootScope;
2014-06-03 13:42:36 -07:00
beforeEach(inject(function($controller, $rootScope) {
2015-02-12 05:20:14 -08:00
rootScope = $rootScope;
2014-06-03 13:42:36 -07:00
scope = $rootScope.$new();
2014-10-30 10:13:40 -07:00
c = $controller('ReceiveController', {
2014-06-03 13:42:36 -07:00
$scope: scope,
});
2015-01-15 13:51:08 -08:00
var createW = function(N, conf) {
var c = JSON.parse(JSON.stringify(conf || walletConfig));
if (!N) N = c.totalCopayers;
var mainPrivateKey = new copay.PrivateKey({
networkName: walletConfig.networkName
});
var mainCopayerEPK = mainPrivateKey.deriveBIP45Branch().extendedPublicKeyString();
c.privateKey = mainPrivateKey;
c.publicKeyRing = new copay.PublicKeyRing({
networkName: c.networkName,
requiredCopayers: Math.min(N, c.requiredCopayers),
totalCopayers: N,
});
c.publicKeyRing.addCopayer(mainCopayerEPK);
c.publicKeyRing.getAddressesOrdered = sinon.stub().returns(null);
c.txProposals = new copay.TxProposals({
networkName: c.networkName,
});
c.blockchain = new Blockchain(walletConfig.blockchain);
c.network = sinon.stub();
c.network.setHexNonce = sinon.stub();
c.network.setHexNonces = sinon.stub();
c.network.getHexNonce = sinon.stub();
c.network.getHexNonces = sinon.stub();
c.network.peerFromCopayer = sinon.stub().returns('xxxx');
c.network.send = sinon.stub();
c.addressBook = {
'2NFR2kzH9NUdp8vsXTB4wWQtTtzhpKxsyoJ': {
label: 'John',
copayerId: '026a55261b7c898fff760ebe14fd22a71892295f3b49e0ca66727bc0a0d7f94d03',
createdTs: 1403102115,
hidden: false
},
'2MtP8WyiwG7ZdVWM96CVsk2M1N8zyfiVQsY': {
label: 'Jennifer',
copayerId: '032991f836543a492bd6d0bb112552bfc7c5f3b7d5388fcbcbf2fbb893b44770d7',
createdTs: 1403103115,
hidden: false
}
};
c.networkName = walletConfig.networkName;
c.version = '0.0.1';
2015-02-12 05:20:14 -08:00
c.generateAddress = sinon.stub().returns({});
2015-01-15 13:51:08 -08:00
c.balanceInfo = {};
return new Wallet(c);
};
$rootScope.wallet = createW();
$rootScope.wallet.balanceInfo = {};
2014-06-03 13:42:36 -07:00
}));
2015-01-15 13:51:08 -08:00
it('should exist', function() {
should.exist(c);
});
2015-02-12 05:20:14 -08:00
it('#init', function() {
scope.init();
rootScope.title.should.be.equal('Receive');
});
2015-01-15 13:51:08 -08:00
it('should call setAddressList', function() {
scope.setAddressList();
expect(scope.addresses).to.be.empty;
scope.toggleShowAll();
scope.setAddressList();
expect(scope.addresses).to.be.empty;
});
2015-02-12 05:20:14 -08:00
it('#newAddr', function() {
rootScope.wallet.generateAddress = sinon.stub().returns({});
scope.newAddr();
rootScope.wallet.generateAddress.calledOnce.should.be.true;
});
});
2014-06-03 13:42:36 -07:00
2014-10-30 10:13:40 -07:00
describe('History Controller', function() {
2014-10-30 12:20:25 -07:00
var ctrl;
beforeEach(inject(function($controller, $rootScope) {
2014-10-30 12:20:25 -07:00
scope = $rootScope.$new();
2014-10-30 12:20:25 -07:00
scope.wallet = null;
scope.getTransactions = sinon.stub();
ctrl = $controller('HistoryController', {
$scope: scope,
});
}));
2014-09-25 12:50:15 -07:00
it('should exist', function() {
2014-10-30 12:20:25 -07:00
should.exist(ctrl);
2014-09-25 12:50:15 -07:00
});
2014-10-30 12:20:25 -07:00
it('should have a HistoryController controller', function() {
expect(scope.loading).equal(false);
});
2014-10-29 14:34:59 -07:00
// this tests has no sense: getTransaction is async
it.skip('should return an empty array of tx from insight', function() {
scope.getTransactions();
expect(scope.blockchain_txs).to.be.empty;
});
});
2015-02-12 12:26:30 -08:00
describe('Profile Controller', function() {
var ctrl, bkpService, idenService;
beforeEach(inject(function($controller, $rootScope, backupService, identityService) {
scope = $rootScope.$new();
bkpService = backupService;
idenService = identityService;
ctrl = $controller('ProfileController', {
$scope: scope,
});
}));
it('should exist', function() {
should.exist(ctrl);
});
it('#downloadProfileBackup', function() {
var old = bkpService.profileDownload;
bkpService.profileDownload = sinon.stub().returns(null);
scope.downloadProfileBackup();
bkpService.profileDownload.calledOnce.should.be.true;
bkpService.profileDownload = old;
});
it('#viewProfileBackup', function() {
var old = bkpService.profileEncrypted;
bkpService.profileEncrypted = sinon.stub().returns(null);
scope.viewProfileBackup();
//bkpService.profileEncrypted.calledOnce.should.be.true;
bkpService.profileEncrypted = old;
});
it('#copyProfileBackup', function() {
var old = bkpService.profileEncrypted;
bkpService.profileEncrypted = sinon.stub().returns(null);
window.cordova = {
plugins: {
clipboard: {
copy: function(e) {
return e;
}
}
}
};
window.plugins = {
toast: {
showShortCenter: function(e) {
return e;
}
}
};
scope.copyProfileBackup();
bkpService.profileEncrypted.calledOnce.should.be.true;
bkpService.profileEncrypted = old;
});
it('#sendProfileBackup', function() {
var old = bkpService.profileEncrypted;
bkpService.profileEncrypted = sinon.stub().returns(null);
window.plugin = {
email: {
open: function(e) {
return e;
}
}
};
window.plugins = {
toast: {
showShortCenter: function(e) {
return e;
}
}
};
scope.sendProfileBackup();
bkpService.profileEncrypted.calledOnce.should.be.true;
bkpService.profileEncrypted = old;
});
it('#deleteProfile', function() {
var old = idenService.deleteProfile;
idenService.deleteProfile = sinon.stub().returns(null);
scope.deleteProfile();
idenService.deleteProfile.calledOnce.should.be.true;
idenService.deleteProfile = old;
});
});
describe('Send Controller', function() {
2015-02-12 05:20:14 -08:00
var scope, form, sendForm, sendCtrl, rootScope;
2014-10-29 14:34:59 -07:00
beforeEach(angular.mock.inject(function($compile, $rootScope, $controller, rateService, notification) {
scope = $rootScope.$new();
2015-02-12 05:20:14 -08:00
rootScope = $rootScope;
scope.rateService = rateService;
var element = angular.element(
'<form name="form">' +
'<input type="text" id="newaddress" name="newaddress" ng-disabled="loading" placeholder="Address" ng-model="newaddress" valid-address required>' +
'<input type="text" id="newlabel" name="newlabel" ng-disabled="loading" placeholder="Label" ng-model="newlabel" required>' +
'</form>'
);
scope.model = {
newaddress: null,
2014-06-23 05:31:41 -07:00
newlabel: null,
2014-12-09 11:55:32 -08:00
_address: null,
_amount: null
};
$compile(element)(scope);
2014-06-24 11:46:07 -07:00
var element2 = angular.element(
'<form name="form2">' +
2014-12-09 11:55:32 -08:00
'<input type="text" id="address" name="address" ng-model="_address" valid-address required>' +
'<input type="number" id="amount" name="amount" ng-model="_amount" min="0.00000001" max="10000000000" valid-amount required>' +
2014-12-09 11:55:32 -08:00
'<input type="number" id="alternative" name="alternative" ng-model="_alternative">' +
2014-06-24 11:46:07 -07:00
'<textarea id="comment" name="comment" ng-model="commentText" ng-maxlength="100"></textarea>' +
'</form>'
);
$compile(element2)(scope);
2014-08-28 17:06:49 -07:00
sendCtrl = $controller('SendController', {
$scope: scope,
2014-06-23 05:31:41 -07:00
$modal: {},
});
2014-12-09 11:55:32 -08:00
scope.init();
scope.$digest();
form = scope.form;
2014-06-24 11:46:07 -07:00
sendForm = scope.form2;
2014-08-11 16:29:44 -07:00
scope.sendForm = sendForm;
}));
it('should have a SendController controller', function() {
2014-12-09 11:55:32 -08:00
should.exist(scope.submitForm);
});
it('should have a title', function() {
2014-12-09 11:55:32 -08:00
expect(scope.title);
});
2015-02-12 05:20:14 -08:00
it('#setError', function() {
scope.setError('my error');
expect(scope.error);
});
it('#setFromPayPro', function() {
var old = rootScope.wallet.fetchPaymentRequest
rootScope.wallet.fetchPaymentRequest = sinon.stub().returns(null);
scope.setFromPayPro('newURL');
rootScope.wallet.fetchPaymentRequest.calledOnce.should.be.true;
rootScope.wallet.fetchPaymentRequest = old;
});
2014-07-04 08:51:27 -07:00
it('should validate address with network', function() {
2014-08-19 13:08:06 -07:00
form.newaddress.$setViewValue('mkfTyEk7tfgV611Z4ESwDDSZwhsZdbMpVy');
expect(form.newaddress.$invalid).to.equal(false);
});
2014-07-04 08:51:27 -07:00
it('should not validate address with other network', function() {
2014-08-19 13:08:06 -07:00
form.newaddress.$setViewValue('1JqniWpWNA6Yvdivg3y9izLidETnurxRQm');
2014-07-04 08:51:27 -07:00
expect(form.newaddress.$invalid).to.equal(true);
});
it('should not validate random address', function() {
form.newaddress.$setViewValue('thisisaninvalidaddress');
expect(form.newaddress.$invalid).to.equal(true);
});
it('should validate label', function() {
form.newlabel.$setViewValue('John');
expect(form.newlabel.$invalid).to.equal(false);
});
it('should not validate label', function() {
expect(form.newlabel.$invalid).to.equal(true);
});
2015-01-04 07:39:08 -08:00
it('should create a transaction proposal with given values', inject(function($timeout) {
2014-11-25 08:21:00 -08:00
sendForm.address.$setViewValue(anAddr);
sendForm.amount.$setViewValue(anAmount);
sendForm.comment.$setViewValue(aComment);
2014-06-24 11:46:07 -07:00
2014-10-29 14:34:59 -07:00
var w = scope.wallet;
2014-06-24 11:46:07 -07:00
scope.submitForm(sendForm);
2015-01-04 07:39:08 -08:00
$timeout.flush();
2014-11-25 08:21:00 -08:00
sinon.assert.callCount(w.spend, 1);
sinon.assert.callCount(w.broadcastTx, 0);
var spendArgs = w.spend.getCall(0).args[0];
spendArgs.toAddress.should.equal(anAddr);
spendArgs.amountSat.should.equal(anAmount * scope.wallet.settings.unitToSatoshi);
spendArgs.comment.should.equal(aComment);
2015-01-04 07:39:08 -08:00
}));
2014-06-24 11:46:07 -07:00
2014-08-22 19:05:08 -07:00
2015-01-04 07:39:08 -08:00
it('should handle big values in 100 BTC', inject(function($timeout) {
2014-09-05 16:09:20 -07:00
var old = scope.wallet.settings.unitToSatoshi;
2014-12-01 06:03:35 -08:00
scope.wallet.settings.unitToSatoshi = 100000000;
2014-11-25 08:21:00 -08:00
sendForm.address.$setViewValue(anAddr);
2014-08-22 19:05:08 -07:00
sendForm.amount.$setViewValue(100);
2014-11-25 08:21:00 -08:00
sendForm.address.$setViewValue(anAddr);
2014-11-30 18:08:16 -08:00
scope.updateTxs = sinon.spy();
2014-08-22 19:05:08 -07:00
scope.submitForm(sendForm);
2014-10-29 14:34:59 -07:00
var w = scope.wallet;
2015-01-04 07:39:08 -08:00
$timeout.flush();
2014-11-25 08:21:00 -08:00
w.spend.getCall(0).args[0].amountSat.should.equal(100 * scope.wallet.settings.unitToSatoshi);
2014-09-05 16:09:20 -07:00
scope.wallet.settings.unitToSatoshi = old;
2015-01-04 07:39:08 -08:00
}));
2014-08-22 19:05:08 -07:00
2015-01-04 07:39:08 -08:00
it('should handle big values in 5000 BTC', inject(function($rootScope, $timeout) {
2014-10-29 14:34:59 -07:00
var w = scope.wallet;
w.requiresMultipleSignatures = sinon.stub().returns(true);
2014-09-05 16:09:20 -07:00
var old = $rootScope.wallet.settings.unitToSatoshi;
2014-12-01 06:03:35 -08:00
$rootScope.wallet.settings.unitToSatoshi = 100000000;
2014-11-25 08:21:00 -08:00
sendForm.address.$setViewValue(anAddr);
2014-08-22 19:05:08 -07:00
sendForm.amount.$setViewValue(5000);
scope.submitForm(sendForm);
2015-01-04 07:39:08 -08:00
$timeout.flush();
2014-10-29 14:34:59 -07:00
2014-11-25 08:21:00 -08:00
w.spend.getCall(0).args[0].amountSat.should.equal(5000 * $rootScope.wallet.settings.unitToSatoshi);
2014-09-05 16:09:20 -07:00
$rootScope.wallet.settings.unitToSatoshi = old;
}));
2014-08-22 19:05:08 -07:00
2014-08-28 17:06:49 -07:00
it('should convert bits amount to fiat', function(done) {
scope.rateService.whenAvailable(function() {
2014-08-28 17:06:49 -07:00
sendForm.amount.$setViewValue(1e6);
scope.$digest();
2014-12-09 11:55:32 -08:00
expect(scope._amount).to.equal(1e6);
expect(scope.__alternative).to.equal(2);
2014-08-28 17:06:49 -07:00
done();
});
});
it('should convert fiat to bits amount', function(done) {
scope.rateService.whenAvailable(function() {
2014-08-28 17:06:49 -07:00
sendForm.alternative.$setViewValue(2);
scope.$digest();
2014-12-09 11:55:32 -08:00
expect(scope.__alternative).to.equal(2);
expect(scope._amount).to.equal(1e6);
2014-08-28 17:06:49 -07:00
done();
});
});
it('receive from uri using bits', inject(function() {
sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=1.018085');
expect(sendForm.amount.$modelValue).to.equal(1018085);
sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=1.01808500');
expect(sendForm.amount.$modelValue).to.equal(1018085);
sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=0.29133585');
expect(sendForm.amount.$modelValue).to.equal(291335.85);
}));
it('receive from uri using BTC', inject(function($rootScope) {
var old = $rootScope.wallet.settings.unitToSatoshi;
2015-01-30 07:25:54 -08:00
var old_decimals = $rootScope.wallet.settings.unitDecimals;
$rootScope.wallet.settings.unitToSatoshi = 100000000;
2015-01-30 07:25:54 -08:00
$rootScope.wallet.settings.unitDecimals = 8;
sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=1.018085');
expect(sendForm.amount.$modelValue).to.equal(1.018085);
sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=1.01808500');
expect(sendForm.amount.$modelValue).to.equal(1.018085);
sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=0.29133585');
expect(sendForm.amount.$modelValue).to.equal(0.29133585);
2015-01-30 07:25:54 -08:00
sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=0.1');
expect(sendForm.amount.$modelValue).to.equal(0.1);
$rootScope.wallet.settings.unitToSatoshi = old;
2015-01-30 07:25:54 -08:00
$rootScope.wallet.settings.unitDecimals = old_decimals;
}));
});
2014-08-05 07:53:58 -07:00
describe("Unit: Version Controller", function() {
var scope, $httpBackendOut;
var GH = 'https://api.github.com/repos/bitpay/copay/tags';
beforeEach(inject(function($controller, $injector) {
$httpBackend = $injector.get('$httpBackend');
$httpBackend.when('GET', GH)
2014-09-05 16:19:49 -07:00
.respond([{
name: "v100.1.6",
zipball_url: "https://api.github.com/repos/bitpay/copay/zipball/v0.0.6",
tarball_url: "https://api.github.com/repos/bitpay/copay/tarball/v0.0.6",
commit: {
sha: "ead7352bf2eca705de58d8b2f46650691f2bc2c7",
url: "https://api.github.com/repos/bitpay/copay/commits/ead7352bf2eca705de58d8b2f46650691f2bc2c7"
}
}]);
}));
var rootScope;
beforeEach(inject(function($controller, $rootScope) {
rootScope = $rootScope;
scope = $rootScope.$new();
2014-08-05 07:53:58 -07:00
headerCtrl = $controller('VersionController', {
$scope: scope,
});
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
2014-07-08 04:58:24 -07:00
2014-06-16 13:37:33 -07:00
it('should hit github for version', function() {
$httpBackend.expectGET(GH);
scope.$apply();
$httpBackend.flush();
});
2014-06-27 11:42:08 -07:00
it('should check version ', inject(function($injector) {
notification = $injector.get('notification');
var spy = sinon.spy(notification, 'version');
2014-06-16 13:37:33 -07:00
$httpBackend.expectGET(GH);
scope.$apply();
$httpBackend.flush();
2014-06-27 11:42:08 -07:00
spy.calledOnce.should.equal(true);
}));
2014-06-16 13:37:33 -07:00
it('should check blockChainStatus', function() {
$httpBackend.expectGET(GH);
$httpBackend.flush();
rootScope.insightError = 1;
scope.$apply();
expect(rootScope.insightError).equal(1);
scope.$apply();
expect(rootScope.insightError).equal(1);
scope.$apply();
});
2014-08-05 07:53:58 -07:00
});
2015-02-10 12:33:16 -08:00
describe("Unit: Sidebar Controller", function() {
2014-08-05 07:53:58 -07:00
beforeEach(inject(function($controller, $rootScope) {
rootScope = $rootScope;
2014-10-29 14:34:59 -07:00
scope = $rootScope.$new();
2014-08-05 07:53:58 -07:00
headerCtrl = $controller('SidebarController', {
$scope: scope,
});
}));
2015-02-12 05:20:14 -08:00
it('should call sign out', function() {
2015-02-10 12:33:16 -08:00
scope.signout();
rootScope.iden.close.calledOnce.should.be.true;
2014-06-23 15:45:04 -07:00
});
});
2015-02-12 10:08:39 -08:00
describe("Head Controller", function() {
var scope, ctrl, rootScope, idenService, balService;
beforeEach(inject(function($controller, $rootScope, identityService, balanceService) {
rootScope = $rootScope;
idenService = identityService;
balService = balanceService;
scope = $rootScope.$new();
ctrl = $controller('HeadController', {
$scope: scope,
});
}));
it('should exist', function() {
should.exist(ctrl);
});
it('should call sign out', function() {
var old = idenService.signout;
idenService.signout = sinon.stub().returns(null);
scope.signout();
idenService.signout.calledOnce.should.be.true;
idenService.signout = old;
});
it('should call refresh', function() {
var old = rootScope.wallet.sendWalletReady;
rootScope.wallet.sendWalletReady = sinon.stub().returns(null);
balService.clearBalanceCache = sinon.stub().returns(null);
scope.refresh();
rootScope.wallet.sendWalletReady.calledOnce.should.be.true;
rootScope.wallet.sendWalletReady = old;
});
});
2014-06-19 11:07:20 -07:00
describe('Send Controller', function() {
var sendCtrl, form;
beforeEach(inject(function($compile, $rootScope, $controller) {
2014-06-19 11:07:20 -07:00
scope = $rootScope.$new();
2014-06-23 11:12:18 -07:00
$rootScope.availableBalance = 123456;
var element = angular.element(
'<form name="form">' +
'<input type="number" id="amount" name="amount" placeholder="Amount" ng-model="amount" min="0.0001" max="10000000" enough-amount required>' +
'</form>'
2014-07-01 15:49:05 -07:00
);
scope.model = {
amount: null
};
$compile(element)(scope);
scope.$digest();
form = scope.form;
2014-06-19 11:07:20 -07:00
sendCtrl = $controller('SendController', {
$scope: scope,
2014-06-23 05:53:53 -07:00
$modal: {},
2014-06-19 11:07:20 -07:00
});
}));
it('should have a SendController', function() {
expect(scope.isMobile).not.to.equal(null);
});
2014-06-23 11:12:18 -07:00
it('should autotop balance correctly', function() {
2014-11-12 16:59:58 -08:00
scope.setTopAmount(form);
form.amount.$setViewValue(123356);
2014-06-23 11:12:18 -07:00
expect(scope.amount).to.equal(123356);
expect(form.amount.$invalid).to.equal(false);
expect(form.amount.$pristine).to.equal(false);
});
2014-06-19 11:07:20 -07:00
});
2014-06-26 14:15:27 -07:00
describe('Import Controller', function() {
2015-02-12 12:26:30 -08:00
var ctrl;
2014-06-26 14:15:27 -07:00
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
2015-02-12 12:26:30 -08:00
ctrl = $controller('ImportController', {
2014-06-26 14:15:27 -07:00
$scope: scope,
});
}));
it('should exist', function() {
2015-02-12 12:26:30 -08:00
should.exist(ctrl);
2014-06-26 14:15:27 -07:00
});
it('import status', function() {
expect(scope.importStatus).equal('Importing wallet - Reading backup...');
});
2014-06-26 14:37:30 -07:00
});
2014-10-29 14:34:59 -07:00
// TODO: fix this test
describe.skip('Home Controller', function() {
2015-02-12 12:26:30 -08:00
var ctrl;
2014-06-26 14:37:30 -07:00
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
2015-02-12 12:26:30 -08:00
ctrl = $controller('HomeController', {
2014-06-26 14:37:30 -07:00
$scope: scope,
});
}));
2014-06-26 14:15:27 -07:00
2014-06-26 14:37:30 -07:00
it('should exist', function() {
2015-02-12 12:26:30 -08:00
should.exist(ctrl);
2014-06-26 14:37:30 -07:00
});
describe('#open', function() {
it('should work with invalid form', function() {
scope.open(invalidForm);
});
});
2014-07-28 09:30:41 -07:00
});
2015-02-12 05:20:14 -08:00
describe('SignOut Controller', function() {
2015-02-12 12:26:30 -08:00
var ctrl;
2015-02-10 12:33:16 -08:00
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
2015-02-12 12:26:30 -08:00
ctrl = $controller('signOutController', {
2015-02-10 12:33:16 -08:00
$scope: scope,
});
}));
it('should exist', function() {
2015-02-12 12:26:30 -08:00
should.exist(ctrl);
2015-02-10 12:33:16 -08:00
});
});
2014-08-11 13:26:48 -07:00
describe('Settings Controller', function() {
var what;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
what = $controller('SettingsController', {
$scope: scope,
});
}));
it('should exist', function() {
should.exist(what);
});
});
2014-09-09 08:59:48 -07:00
describe('Copayers Controller', function() {
var saveDownload = null;
2015-02-12 12:26:30 -08:00
var ctrl, rootScope, idenService;
beforeEach(inject(function($controller, $rootScope, identityService) {
2014-09-09 08:59:48 -07:00
scope = $rootScope.$new();
2015-02-12 12:26:30 -08:00
rootScope = $rootScope;
idenService = identityService;
2014-09-09 08:59:48 -07:00
ctrl = $controller('CopayersController', {
$scope: scope,
$modal: {},
});
}));
2014-08-11 13:26:48 -07:00
2014-09-09 08:59:48 -07:00
it('should exist', function() {
should.exist(ctrl);
});
2015-02-12 10:08:39 -08:00
it('#init', function() {
var old = scope.updateList;
scope.updateList = sinon.stub().returns(null);
scope.init();
scope.updateList.callCount.should.be.equal(3); //why 3 ??????
scope.updateList = old;
});
2015-02-12 12:26:30 -08:00
it('#updateList', function() {
var old = rootScope.wallet.getRegisteredPeerIds;
rootScope.wallet.getRegisteredPeerIds = sinon.stub().returns(null);
rootScope.wallet.removeListener = sinon.stub().returns(null);
scope.updateList();
rootScope.wallet.getRegisteredPeerIds.callCount.should.be.equal(1);
rootScope.wallet.getRegisteredPeerIds = old;
});
2015-02-20 07:27:05 -08:00
it('#deleteWallet', inject(function($timeout) {
2015-02-12 12:26:30 -08:00
var old = idenService.deleteWallet;
idenService.deleteWallet = sinon.stub().returns(null);
scope.deleteWallet();
2015-02-20 07:27:05 -08:00
$timeout.flush();
2015-02-12 12:26:30 -08:00
idenService.deleteWallet.callCount.should.be.equal(1);
idenService.deleteWallet = old;
2015-02-20 07:27:05 -08:00
}));
2015-02-12 12:26:30 -08:00
2014-09-09 08:59:48 -07:00
});
2014-08-11 13:26:48 -07:00
2014-07-28 09:30:41 -07:00
describe('Join Controller', function() {
2015-02-12 12:26:30 -08:00
var ctrl;
2014-07-28 09:30:41 -07:00
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
2015-02-12 12:26:30 -08:00
ctrl = $controller('JoinController', {
2014-07-28 09:30:41 -07:00
$scope: scope,
});
}));
it('should exist', function() {
2015-02-12 12:26:30 -08:00
should.exist(ctrl);
2014-07-28 09:30:41 -07:00
});
2014-06-26 14:37:30 -07:00
describe('#join', function() {
it('should work with invalid form', function() {
scope.join(invalidForm);
});
});
2014-06-26 14:15:27 -07:00
});
2014-12-09 11:55:32 -08:00
describe('paymentUriController Controller', function() {
2014-07-01 15:49:05 -07:00
var what;
2014-10-09 06:19:13 -07:00
beforeEach(inject(function($controller, $rootScope, $location) {
2014-07-01 15:49:05 -07:00
scope = $rootScope.$new();
2014-07-04 08:51:27 -07:00
var routeParams = {
2014-10-09 06:19:13 -07:00
data: 'bitcoin:19mP9FKrXqL46Si58pHdhGKow88SUPy1V8'
2014-07-01 15:49:05 -07:00
};
2014-10-29 14:34:59 -07:00
var query = {
amount: 0.1,
message: "a bitcoin donation"
};
2014-12-09 11:55:32 -08:00
what = $controller('paymentUriController', {
2014-07-01 15:49:05 -07:00
$scope: scope,
2014-10-09 06:19:13 -07:00
$routeParams: routeParams,
$location: {
2014-10-29 14:34:59 -07:00
search: function() {
return query;
}
2014-10-09 06:19:13 -07:00
}
2014-07-01 15:49:05 -07:00
});
}));
it('should exist', function() {
should.exist(what);
});
it('should parse url correctly', function() {
should.exist(what);
2014-08-13 11:15:53 -07:00
should.exist(scope.pendingPayment);
2014-12-09 11:55:32 -08:00
scope.pendingPayment.should.equal('bitcoin:19mP9FKrXqL46Si58pHdhGKow88SUPy1V8?amount=0.1&message=a bitcoin donation');
2014-07-01 15:49:05 -07:00
});
});
2014-08-12 12:26:15 -07:00
describe('Warning Controller', function() {
2015-02-12 10:08:39 -08:00
var ctrl, idenService;
beforeEach(inject(function($controller, $rootScope, identityService) {
2014-08-12 12:26:15 -07:00
scope = $rootScope.$new();
2015-02-12 10:08:39 -08:00
idenService = identityService;
ctrl = $controller('WarningController', {
2014-08-12 12:26:15 -07:00
$scope: scope,
});
}));
it('should exist', function() {
2015-02-12 10:08:39 -08:00
should.exist(ctrl);
2014-08-12 12:26:15 -07:00
});
2015-02-12 10:08:39 -08:00
it('#signout', function() {
var old = idenService.signout;
idenService.signout = sinon.stub().returns(null);
scope.signout();
idenService.signout.calledOnce.should.be.true;
idenService.signout = old;
});
2014-08-12 12:26:15 -07:00
});
2015-01-14 12:06:13 -08:00
describe('More Controller', function() {
2015-02-12 10:08:39 -08:00
var ctrl, modalCtrl, rootScope, idenService, bkpService;
beforeEach(inject(function($controller, $rootScope, backupService, identityService) {
scope = $rootScope.$new();
2015-02-12 05:20:14 -08:00
rootScope = $rootScope;
2015-02-12 10:08:39 -08:00
idenService = identityService;
bkpService = backupService;
2015-01-14 12:06:13 -08:00
ctrl = $controller('MoreController', {
$scope: scope
});
saveAsLastCall = null;
2014-12-09 14:29:56 -08:00
}));
it('Backup Wallet controller #download', function() {
var w = scope.wallet;
expect(saveAsLastCall).equal(null);
2015-01-14 12:06:13 -08:00
scope.downloadWalletBackup();
2014-12-09 14:29:56 -08:00
expect(saveAsLastCall.blob.size).equal(7);
expect(saveAsLastCall.blob.type).equal('text/plain;charset=utf-8');
});
it('Backup Wallet controller should name backup correctly for multiple copayers', function() {
var w = scope.wallet;
expect(saveAsLastCall).equal(null);
2015-01-14 12:06:13 -08:00
scope.downloadWalletBackup();
expect(saveAsLastCall.filename).equal('nickname-fakeWallet-keybackup.json.aes');
});
it('Backup Wallet controller should name backup correctly for 1-1 wallet', function() {
var w = scope.wallet;
expect(saveAsLastCall).equal(null);
scope.wallet.totalCopayers = 1;
2015-01-14 12:06:13 -08:00
scope.downloadWalletBackup();
expect(saveAsLastCall.filename).equal('fakeWallet-keybackup.json.aes');
});
2015-02-20 07:27:05 -08:00
it('Delete a wallet', inject(function($timeout) {
var w = scope.wallet;
2014-12-09 14:29:56 -08:00
2015-01-14 12:06:13 -08:00
scope.deleteWallet();
2015-02-20 07:27:05 -08:00
$timeout.flush();
2014-12-09 14:29:56 -08:00
scope.$digest();
scope.iden.deleteWallet.calledOnce.should.equal(true);
scope.iden.deleteWallet.getCall(0).args[0].should.equal(w.getId());
2015-02-20 07:27:05 -08:00
}));
2015-02-12 05:20:14 -08:00
it('#save', function() {
var old = rootScope.wallet.changeSettings;
rootScope.wallet.changeSettings = sinon.stub().returns(null);
scope.selectedUnit = {};
scope.save();
rootScope.wallet.changeSettings.calledOnce.should.equal.true;
rootScope.wallet.changeSettings = old;
});
it('#purge checking balance', function() {
var old = rootScope.wallet.purgeTxProposals;
rootScope.wallet.purgeTxProposals = sinon.stub().returns(true);
scope.purge();
rootScope.wallet.purgeTxProposals.calledOnce.should.equal.true;
rootScope.wallet.purgeTxProposals = old;
});
it('#purge without checking balance', function() {
var old = rootScope.wallet.purgeTxProposals;
rootScope.wallet.purgeTxProposals = sinon.stub().returns(false);
scope.purge();
rootScope.wallet.purgeTxProposals.calledOnce.should.equal.true;
rootScope.wallet.purgeTxProposals = old;
});
it('#updateIndexes', function() {
var old = rootScope.wallet.purgeTxProposals;
rootScope.wallet.updateIndexes = sinon.stub().yields();
scope.updateIndexes();
rootScope.wallet.updateIndexes.calledOnce.should.equal.true;
rootScope.wallet.updateIndexes = old;
});
it('#updateIndexes return error', function() {
var old = rootScope.wallet.purgeTxProposals;
rootScope.wallet.updateIndexes = sinon.stub().yields('error');
scope.updateIndexes();
rootScope.wallet.updateIndexes.calledOnce.should.equal.true;
rootScope.wallet.updateIndexes = old;
});
2015-02-20 07:27:05 -08:00
it('#deleteWallet', inject(function($timeout) {
2015-02-12 10:08:39 -08:00
var old = idenService.deleteWallet;
idenService.deleteWallet = sinon.stub().yields(null);
scope.deleteWallet();
2015-02-20 07:27:05 -08:00
$timeout.flush();
2015-02-12 10:08:39 -08:00
idenService.deleteWallet.calledOnce.should.equal.true;
scope.loading.should.be.false;
idenService.deleteWallet = old;
2015-02-20 07:27:05 -08:00
}));
2015-02-12 10:08:39 -08:00
2015-02-20 07:27:05 -08:00
it('#deleteWallet with error', inject(function($timeout) {
2015-02-12 10:08:39 -08:00
var old = idenService.deleteWallet;
idenService.deleteWallet = sinon.stub().yields('error');
scope.deleteWallet();
2015-02-20 07:27:05 -08:00
$timeout.flush();
2015-02-12 10:08:39 -08:00
idenService.deleteWallet.calledOnce.should.equal.true;
scope.error.should.be.equal('error');
idenService.deleteWallet = old;
2015-02-20 07:27:05 -08:00
}));
2015-02-12 10:08:39 -08:00
it('#viewWalletBackup', function() {
var old = bkpService.walletEncrypted;
bkpService.walletEncrypted = sinon.stub().returns('backup0001');
scope.viewWalletBackup();
bkpService.walletEncrypted.calledOnce.should.equal.true;
bkpService.walletEncrypted = old;
});
it('#copyWalletBackup', function() {
var old = bkpService.walletEncrypted;
bkpService.walletEncrypted = sinon.stub().returns('backup0001');
window.cordova = {
plugins: {
clipboard: {
copy: function(e) {
return e;
}
}
}
};
window.plugins = {
toast: {
showShortCenter: function(e) {
return e;
}
}
};
scope.copyWalletBackup();
bkpService.walletEncrypted.calledOnce.should.equal.true;
bkpService.walletEncrypted = old;
});
it('#sendWalletBackup', function() {
var old = bkpService.walletEncrypted;
bkpService.walletEncrypted = sinon.stub().returns('backup0001');
window.plugins = {
toast: {
showShortCenter: function(e) {
return e;
}
}
};
window.plugin = {
email: {
open: function(e) {
return e;
}
}
};
scope.sendWalletBackup();
bkpService.walletEncrypted.calledOnce.should.equal.true;
bkpService.walletEncrypted = old;
});
});
2014-04-23 09:21:33 -07:00
});