copay/test/unit/controllers/controllersSpec.js

580 lines
18 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
};
var startServer = require('../../mocks/FakePayProServer');
2014-06-16 13:37:33 -07:00
describe("Unit: Controllers", function() {
2014-06-26 14:37:30 -07:00
var invalidForm = {
$invalid: true
};
2014-04-23 09:21:33 -07:00
var scope;
2014-06-03 13:42:36 -07:00
var server;
beforeEach(module('copayApp.services'));
beforeEach(module('copayApp.controllers'));
2014-08-22 19:05:08 -07:00
var walletConfig = {
2014-06-16 13:37:33 -07:00
requiredCopayers: 3,
totalCopayers: 5,
spendUnconfirmed: 1,
reconnectDelay: 100,
2014-08-28 17:06:49 -07:00
networkName: 'testnet',
alternativeName: 'lol currency',
alternativeIsoCode: 'LOL'
2014-06-16 13:37:33 -07:00
};
2014-09-09 08:59:48 -07:00
describe('More Controller', function() {
2014-06-16 13:37:33 -07:00
var ctrl;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
2014-08-22 19:05:08 -07:00
$rootScope.wallet = new FakeWallet(walletConfig);
2014-08-21 08:04:19 -07:00
ctrl = $controller('MoreController', {
2014-06-16 13:37:33 -07:00
$scope: scope,
$modal: {},
});
2014-09-12 06:40:47 -07:00
saveAsLastCall = null;
2014-06-16 13:37:33 -07:00
}));
2014-06-17 19:16:15 -07:00
it('Backup controller #download', function() {
2014-06-16 13:37:33 -07:00
scope.wallet.setEnc('1234567');
expect(saveAsLastCall).equal(null);
2014-07-16 15:00:34 -07:00
scope.downloadBackup();
2014-09-12 06:40:47 -07:00
expect(saveAsLastCall.blob.size).equal(7);
expect(saveAsLastCall.blob.type).equal('text/plain;charset=utf-8');
});
2014-09-12 07:10:45 -07:00
it('Backup controller should name backup correctly for multiple copayers', function() {
2014-09-12 06:40:47 -07:00
scope.wallet.setEnc('1234567');
expect(saveAsLastCall).equal(null);
scope.downloadBackup();
expect(saveAsLastCall.filename).equal('myNickname-myTESTwullet-testID-keybackup.json.aes');
2014-06-16 13:37:33 -07:00
});
2014-09-12 07:10:45 -07:00
it('Backup controller should name backup correctly for 1-1 wallet', function() {
scope.wallet.setEnc('1234567');
expect(saveAsLastCall).equal(null);
scope.wallet.totalCopayers = 1;
scope.downloadBackup();
expect(saveAsLastCall.filename).equal('myTESTwullet-testID-keybackup.json.aes');
});
2014-06-17 19:16:15 -07:00
it('Backup controller #delete', function() {
2014-06-16 13:37:33 -07:00
expect(scope.wallet).not.equal(undefined);
scope.deleteWallet();
expect(scope.wallet).equal(undefined);
});
});
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
});
});
2014-06-23 15:45:04 -07:00
});
describe('Address Controller', function() {
var addressCtrl;
2014-07-28 09:30:41 -07:00
beforeEach(angular.mock.module('copayApp'));
2014-06-03 13:42:36 -07:00
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
addressCtrl = $controller('AddressesController', {
$scope: scope,
});
}));
it('should have a AddressesController controller', function() {
expect(scope.loading).equal(false);
});
});
2014-06-03 13:42:36 -07:00
describe('Transactions Controller', function() {
var transactionsCtrl;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
2014-09-05 16:19:49 -07:00
$rootScope.wallet = new FakeWallet(walletConfig);
transactionsCtrl = $controller('TransactionsController', {
$scope: scope,
});
}));
it('should have a TransactionController controller', function() {
expect(scope.loading).equal(false);
});
2014-06-04 07:25:58 -07:00
it('should return an empty array of tx from insight', function() {
scope.getTransactions();
expect(scope.blockchain_txs).to.be.empty;
});
});
describe('Send Controller', function() {
2014-08-28 17:06:49 -07:00
var scope, form, sendForm, sendCtrl;
beforeEach(angular.mock.module('copayApp'));
2014-08-28 17:06:49 -07:00
beforeEach(module(function($provide) {
$provide.value('request', {
'get': function(_, cb) {
2014-09-05 16:19:49 -07:00
cb(null, null, [{
name: 'lol currency',
code: 'LOL',
rate: 2
}]);
2014-08-28 17:06:49 -07:00
}
});
}));
beforeEach(angular.mock.inject(function($compile, $rootScope, $controller, rateService) {
scope = $rootScope.$new();
scope.rateService = rateService;
2014-08-22 19:05:08 -07:00
$rootScope.wallet = new FakeWallet(walletConfig);
$rootScope.wallet.settings.alternativeName = 'lol currency';
$rootScope.wallet.settings.alternativeIsoCode = 'LOL';
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-06-24 11:46:07 -07:00
address: null,
amount: null
};
$compile(element)(scope);
2014-06-24 11:46:07 -07:00
var element2 = angular.element(
'<form name="form2">' +
'<input type="text" id="address" name="address" ng-model="address" valid-address required>' +
'<input type="number" id="amount" name="amount" ng-model="amount" min="1" max="10000000000" required>' +
2014-08-28 17:06:49 -07: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-06-24 11:46:07 -07:00
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() {
expect(scope.loading).equal(false);
});
it('should have a title', function() {
expect(scope.title).equal('Send');
});
it('should return true if wallet has addressBook', function() {
expect(scope.showAddressBook()).equal(true);
});
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);
});
2014-08-22 19:05:08 -07:00
it('should create a transaction proposal with given values', function() {
2014-08-19 13:08:06 -07:00
sendForm.address.$setViewValue('mkfTyEk7tfgV611Z4ESwDDSZwhsZdbMpVy');
2014-06-24 11:46:07 -07:00
sendForm.amount.$setViewValue(1000);
var spy = sinon.spy(scope.wallet, 'createTx');
var spy2 = sinon.spy(scope.wallet, 'sendTx');
scope.loadTxs = sinon.spy();
2014-06-24 11:46:07 -07:00
scope.submitForm(sendForm);
sinon.assert.callCount(spy, 1);
sinon.assert.callCount(spy2, 0);
sinon.assert.callCount(scope.loadTxs, 1);
2014-08-25 09:49:26 -07:00
spy.getCall(0).args[0].should.equal('mkfTyEk7tfgV611Z4ESwDDSZwhsZdbMpVy');
2014-09-05 16:09:20 -07:00
spy.getCall(0).args[1].should.equal(1000 * scope.wallet.settings.unitToSatoshi);
2014-08-22 19:05:08 -07:00
(typeof spy.getCall(0).args[2]).should.equal('undefined');
2014-06-24 11:46:07 -07:00
});
2014-08-22 19:05:08 -07:00
it('should handle big values in 100 BTC', function() {
2014-09-05 16:09:20 -07:00
var old = scope.wallet.settings.unitToSatoshi;
scope.wallet.settings.unitToSatoshi = 100000000;;
2014-08-25 09:49:26 -07:00
sendForm.address.$setViewValue('mkfTyEk7tfgV611Z4ESwDDSZwhsZdbMpVy');
2014-08-22 19:05:08 -07:00
sendForm.amount.$setViewValue(100);
var spy = sinon.spy(scope.wallet, 'createTx');
scope.loadTxs = sinon.spy();
scope.submitForm(sendForm);
2014-09-05 16:09:20 -07:00
spy.getCall(0).args[1].should.equal(100 * scope.wallet.settings.unitToSatoshi);
scope.wallet.settings.unitToSatoshi = old;
2014-08-22 19:05:08 -07:00
});
2014-09-05 16:09:20 -07:00
it('should handle big values in 5000 BTC', inject(function($rootScope) {
var old = $rootScope.wallet.settings.unitToSatoshi;
$rootScope.wallet.settings.unitToSatoshi = 100000000;;
2014-08-25 09:49:26 -07:00
sendForm.address.$setViewValue('mkfTyEk7tfgV611Z4ESwDDSZwhsZdbMpVy');
2014-08-22 19:05:08 -07:00
sendForm.amount.$setViewValue(5000);
var spy = sinon.spy(scope.wallet, 'createTx');
scope.loadTxs = sinon.spy();
scope.submitForm(sendForm);
2014-09-05 16:09:20 -07:00
spy.getCall(0).args[1].should.equal(5000 * $rootScope.wallet.settings.unitToSatoshi);
$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();
expect(scope.alternative).to.equal(2);
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();
expect(scope.amount).to.equal(1e6);
done();
});
});
2014-08-22 19:05:08 -07:00
2014-06-24 11:46:07 -07:00
it('should create and send a transaction proposal', function() {
2014-08-19 13:08:06 -07:00
sendForm.address.$setViewValue('mkfTyEk7tfgV611Z4ESwDDSZwhsZdbMpVy');
2014-06-24 11:46:07 -07:00
sendForm.amount.$setViewValue(1000);
scope.wallet.totalCopayers = scope.wallet.requiredCopayers = 1;
var spy = sinon.spy(scope.wallet, 'createTx');
var spy2 = sinon.spy(scope.wallet, 'sendTx');
scope.loadTxs = sinon.spy();
2014-06-24 11:46:07 -07:00
scope.submitForm(sendForm);
sinon.assert.callCount(spy, 1);
sinon.assert.callCount(spy2, 1);
2014-08-07 06:20:24 -07:00
sinon.assert.callCount(scope.loadTxs, 1);
2014-06-24 11:46:07 -07:00
});
2014-09-08 12:37:33 -07:00
it('should not send txp when there is an error at creation', function() {
sendForm.address.$setViewValue('mkfTyEk7tfgV611Z4ESwDDSZwhsZdbMpVy');
sendForm.amount.$setViewValue(1000);
scope.wallet.totalCopayers = scope.wallet.requiredCopayers = 1;
sinon.stub(scope.wallet, 'createTx').yields('error');
var spySendTx = sinon.spy(scope.wallet, 'sendTx');
scope.loadTxs = sinon.spy();
scope.submitForm(sendForm);
sinon.assert.callCount(spySendTx, 0);
sinon.assert.callCount(scope.loadTxs, 1);
});
});
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-06-12 13:42:26 -07:00
2014-08-05 07:53:58 -07:00
});
describe("Unit: Sidebar Controller", function() {
var rootScope;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
rootScope = $rootScope;
2014-08-22 19:05:08 -07:00
rootScope.wallet = new FakeWallet(walletConfig);
2014-08-05 07:53:58 -07:00
headerCtrl = $controller('SidebarController', {
$scope: scope,
});
}));
it('should return an array of n undefined elements', function() {
2014-06-23 15:45:04 -07:00
var n = 5;
var array = scope.getNumber(n);
expect(array.length).equal(n);
});
});
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;
2014-09-05 16:16:06 -07:00
$rootScope.wallet = new FakeWallet(walletConfig);
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() {
scope.topAmount(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);
});
it('should return available amount', function() {
var amount = scope.getAvailableAmount();
expect(amount).to.equal(123356);
2014-06-23 11:12:18 -07:00
});
it('should return 0 if available amount below minimum fee', function() {
inject(function($compile, $rootScope, $controller) {
$rootScope.availableBalance = 1;
});
var amount = scope.getAvailableAmount();
expect(amount).to.equal(0);
});
2014-06-19 11:07:20 -07:00
});
2014-06-26 14:15:27 -07:00
describe('Import Controller', function() {
var what;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
what = $controller('ImportController', {
$scope: scope,
});
}));
it('should exist', function() {
should.exist(what);
});
2014-06-26 14:37:30 -07:00
});
2014-07-28 09:30:41 -07:00
describe('Open Controller', function() {
2014-06-26 14:37:30 -07:00
var what;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
2014-07-28 09:30:41 -07:00
what = $controller('OpenController', {
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() {
should.exist(what);
});
describe('#open', function() {
it('should work with invalid form', function() {
scope.open(invalidForm);
});
});
2014-07-28 09:30:41 -07: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;
var ctrl;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
$rootScope.wallet = new FakeWallet(walletConfig);
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);
});
it('Delete Wallet', function() {
expect(scope.wallet).not.equal(undefined);
scope.deleteWallet();
expect(scope.wallet).equal(undefined);
});
});
2014-08-11 13:26:48 -07:00
2014-07-28 09:30:41 -07:00
describe('Join Controller', function() {
var what;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
what = $controller('JoinController', {
$scope: scope,
});
}));
it('should exist', function() {
should.exist(what);
});
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-07-01 15:49:05 -07:00
describe('UriPayment Controller', function() {
var what;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
2014-07-04 08:51:27 -07:00
var routeParams = {
2014-08-13 11:15:53 -07:00
data: 'bitcoin:19mP9FKrXqL46Si58pHdhGKow88SUPy1V8?amount=0.1&message=a%20bitcoin%20donation'
2014-07-01 15:49:05 -07:00
};
what = $controller('UriPaymentController', {
$scope: scope,
$routeParams: routeParams
});
}));
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);
scope.pendingPayment.address.data.should.equal('19mP9FKrXqL46Si58pHdhGKow88SUPy1V8');
scope.pendingPayment.data.amount.should.equal(0.1);
scope.pendingPayment.data.message.should.equal('a bitcoin donation');
2014-07-01 15:49:05 -07:00
});
});
2014-08-12 12:26:15 -07:00
describe('Warning Controller', function() {
var what;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
what = $controller('WarningController', {
$scope: scope,
});
}));
it('should exist', function() {
should.exist(what);
});
});
2014-04-23 09:21:33 -07:00
});