diff --git a/copay.js b/copay.js index ee4473480..511a3dafd 100644 --- a/copay.js +++ b/copay.js @@ -5,6 +5,7 @@ module.exports.TxProposals = require('./js/models/TxProposals'); module.exports.PrivateKey = require('./js/models/PrivateKey'); module.exports.HDPath = require('./js/models/HDPath'); module.exports.HDParams = require('./js/models/HDParams'); +module.exports.crypto = require('./js/util/crypto'); // components diff --git a/js/controllers/send.js b/js/controllers/send.js index 457993c32..4759efedf 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -59,6 +59,7 @@ angular.module('copayApp.controllers').controller('SendController', set: function(newValue) { this._amount = newValue; if (typeof(newValue) === 'number' && $scope.isRateAvailable) { + this._alternative = parseFloat( (rateService.toFiat(newValue * w.settings.unitToSatoshi, w.settings.alternativeIsoCode)).toFixed(2), 10); } else { diff --git a/karma.conf.js b/karma.conf.js index c8df66d07..8fd203312 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -53,7 +53,6 @@ module.exports = function(config) { 'js/translations.js', 'js/init.js', - 'test/mocks/FakeWallet.js', 'test/mocks/FakeBlockchainSocket.js', 'test/mocks/FakePayProServer.js', diff --git a/test/Identity.js b/test/Identity.js index ca14e2b39..718a68e21 100644 --- a/test/Identity.js +++ b/test/Identity.js @@ -94,7 +94,6 @@ describe('Identity model', function() { var walletClass = function(args) { - console.log('[Identity.js.96:args:]', args); //TODO return getNewWallet(args); }; @@ -180,7 +179,6 @@ describe('Identity model', function() { args.storage.setItem.onFirstCall().callsArg(2); args.storage.setItem.onSecondCall().callsArg(2); should.exist(walletClass, 'check walletClass stub'); - console.log('[Identity.js.184:walletClass:]', walletClass); //TODO iden.createWallet({ privateKeyHex: priv, walletClass: walletClass, diff --git a/test/mocks/FakeWallet.js b/test/mocks/FakeWallet.js deleted file mode 100644 index 74ec1b9ff..000000000 --- a/test/mocks/FakeWallet.js +++ /dev/null @@ -1,146 +0,0 @@ -var is_browser = typeof process == 'undefined' || typeof process.versions === 'undefined'; -if (is_browser) { - var copay = require('copay'); //browser -} else { - var copay = require('../copay'); //node -} -var Wallet = copay.Wallet; - -var FakePrivateKey = function() {}; - -FakePrivateKey.prototype.toObj = function() { - return extendedPublicKeyString = 'privHex'; -}; - -var FakeWallet = function() { - this.id = 'testID'; - this.balance = 10000; - this.safeBalance = 1000; - this.totalCopayers = 2; - this.requiredCopayers = 2; - this.isLocked = false; - this.balanceByAddr = { - '1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC': 1000 - }; - this.name = 'myTESTwullet'; - this.nickname = 'myNickname'; - this.addressBook = { - '2NFR2kzH9NUdp8vsXTB4wWQtTtzhpKxsyoJ': { - label: 'John', - copayerId: '026a55261b7c898fff760ebe14fd22a71892295f3b49e0ca66727bc0a0d7f94d03', - createdTs: 1403102115, - } - }; - this.publicKeyRing = { - isComplete: function() { - return true; - } - }; - this.blockchain = { - getSubscriptions: function() { - return []; - }, - subscribe: function() {}, - getTransactions: function() {} - }; - - this.privateKey = new FakePrivateKey(); - this.settings = { - unitName: 'bits', - unitToSatoshi: 100, - unitDecimals: 2, - alternativeName: 'US Dollar', - alternativeIsoCode: 'USD', - }; -}; - -FakeWallet.prototype.createTx = function(toAddress, amountSatStr, comment, opts, cb) { - var callback = cb || opts; - callback(null, {}); -} - - -FakeWallet.prototype.getSecret = function() { - return 'xxx'; -}; - -FakeWallet.prototype.sendTx = function(ntxid, cb) { - cb(8); -} -FakeWallet.prototype.getAddressesStr = function() { - return ['2Mw2YXxyMD7fhtPhHYY39X6BVWiBRaez5Zn']; -}; - -FakeWallet.prototype.set = function(balance, safeBalance, balanceByAddr) { - this.balance = balance; - this.safeBalance = safeBalance; - this.balanceByAddr = balanceByAddr; -}; - -FakeWallet.prototype.getAddressesInfo = function() { - var ret = []; - - for (var ii in this.balanceByAddr) { - ret.push({ - address: ii, - addressStr: ii, - isChange: false, - }); - } - return ret; -}; - -FakeWallet.prototype.subscribeToAddresses = function() {}; - -FakeWallet.prototype.getMyCopayerNickname = function() { - return this.nickname; -}; - -FakeWallet.prototype.isShared = function() { - return this.totalCopayers > 1; -} - -FakeWallet.prototype.requiresMultipleSignatures = function() { - return this.requiredCopayers > 1; -}; - -FakeWallet.prototype.isReady = function() { - return true; -}; - -FakeWallet.prototype.fetchPaymentTx = function(opts, cb) { - cb(null, { - pr: { - pd: { - expires: 12 - } - } - }); -}; - - -FakeWallet.prototype.createPaymentTx = Wallet.prototype.createPaymentTx; - - -FakeWallet.prototype.getBalance = function(cb) { - return cb(null, this.balance, this.balanceByAddr, this.safeBalance); -}; - -FakeWallet.prototype.removeTxWithSpentInputs = function(cb) {}; - -FakeWallet.prototype.setEnc = function(enc) { - this.enc = enc; -}; - -FakeWallet.prototype.export = function() { - return this.enc; -}; - -FakeWallet.prototype.close = function() {}; - -FakeWallet.prototype.getNetworkName = function() { - return 'testnet'; -}; - -// TODO a try catch was here -module.exports = FakeWallet; diff --git a/test/unit/controllers/controllersSpec.js b/test/unit/controllers/controllersSpec.js index 47e4d7a8f..3b3a010b5 100644 --- a/test/unit/controllers/controllersSpec.js +++ b/test/unit/controllers/controllersSpec.js @@ -15,8 +15,8 @@ saveAs = function(blob, filename) { var startServer = require('../../mocks/FakePayProServer'); describe("Unit: Controllers", function() { - config.plugins.LocalStorage=true; - config.plugins.GoogleDrive=null; + config.plugins.LocalStorage = true; + config.plugins.GoogleDrive = null; var invalidForm = { $invalid: true @@ -28,24 +28,58 @@ describe("Unit: Controllers", function() { beforeEach(module('copayApp.services')); beforeEach(module('copayApp.controllers')); beforeEach(angular.mock.module('copayApp')); + beforeEach(module(function($provide) { + $provide.value('request', { + 'get': function(_, cb) { + cb(null, null, [{ + name: 'USD Dollars', + code: 'USD', + rate: 2 + }]); + } + }); + })); - var walletConfig = { - requiredCopayers: 3, - totalCopayers: 5, - spendUnconfirmed: 1, - reconnectDelay: 100, - networkName: 'testnet', - alternativeName: 'lol currency', - alternativeIsoCode: 'LOL' - }; + beforeEach(inject(function($controller, $rootScope) { + scope = $rootScope.$new(); + $rootScope.iden = sinon.stub(); + var w = {}; + w.isReady = sinon.stub().returns(true); + 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'); + w.getTransactionHistory = sinon.stub().yields({}); + w.getNetworkName = sinon.stub().returns('testnet'); + + w.createTx = sinon.stub().yields(null); + w.sendTx = sinon.stub().yields(null); + w.requiresMultipleSignatures = sinon.stub().returns(true); + w.getTxProposals = sinon.stub().returns([1,2,3]); + + + $rootScope.wallet = w; + })); describe('More Controller', function() { var ctrl; beforeEach(inject(function($controller, $rootScope) { - scope = $rootScope.$new(); - - $rootScope.wallet = new FakeWallet(walletConfig); ctrl = $controller('MoreController', { $scope: scope, $modal: {}, @@ -54,7 +88,6 @@ describe("Unit: Controllers", function() { })); it('Backup controller #download', function() { - scope.wallet.setEnc('1234567'); expect(saveAsLastCall).equal(null); scope.downloadBackup(); expect(saveAsLastCall.blob.size).equal(7); @@ -62,18 +95,16 @@ describe("Unit: Controllers", function() { }); it('Backup controller should name backup correctly for multiple copayers', function() { - scope.wallet.setEnc('1234567'); expect(saveAsLastCall).equal(null); scope.downloadBackup(); - expect(saveAsLastCall.filename).equal('myNickname-myTESTwullet-testID-keybackup.json.aes'); + expect(saveAsLastCall.filename).equal('nickname-fakeWallet-keybackup.json.aes'); }); 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'); + expect(saveAsLastCall.filename).equal('fakeWallet-keybackup.json.aes'); }); }); @@ -104,7 +135,6 @@ describe("Unit: Controllers", function() { describe('Address Controller', function() { var addressCtrl; - beforeEach(angular.mock.module('copayApp')); beforeEach(inject(function($controller, $rootScope) { scope = $rootScope.$new(); addressCtrl = $controller('AddressesController', { @@ -121,7 +151,6 @@ describe("Unit: Controllers", function() { var transactionsCtrl; beforeEach(inject(function($controller, $rootScope) { scope = $rootScope.$new(); - $rootScope.wallet = new FakeWallet(walletConfig); transactionsCtrl = $controller('TransactionsController', { $scope: scope, }); @@ -135,7 +164,8 @@ describe("Unit: Controllers", function() { expect(scope.loading).equal(false); }); - it('should return an empty array of tx from insight', function() { + // 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; }); @@ -154,24 +184,9 @@ describe("Unit: Controllers", function() { describe('Send Controller', function() { var scope, form, sendForm, sendCtrl; - beforeEach(angular.mock.module('copayApp')); - beforeEach(module(function($provide) { - $provide.value('request', { - 'get': function(_, cb) { - cb(null, null, [{ - name: 'lol currency', - code: 'LOL', - rate: 2 - }]); - } - }); - })); - beforeEach(angular.mock.inject(function($compile, $rootScope, $controller, rateService) { + beforeEach(angular.mock.inject(function($compile, $rootScope, $controller, rateService, notification) { scope = $rootScope.$new(); scope.rateService = rateService; - $rootScope.wallet = new FakeWallet(walletConfig); - $rootScope.wallet.settings.alternativeName = 'lol currency'; - $rootScope.wallet.settings.alternativeIsoCode = 'LOL'; var element = angular.element( '