From ba05870a67dccd0161b92454bc86dc777a65df4f Mon Sep 17 00:00:00 2001 From: Brian Clifton Date: Thu, 22 Mar 2018 11:01:48 -0700 Subject: [PATCH] Ensure state gets passed into `run` method Fixes https://github.com/brave/browser-laptop/issues/13535 Auditors: @NejcZdovc --- app/browser/api/ledger.js | 4 +- test/unit/app/browser/api/ledgerTest.js | 50 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/app/browser/api/ledger.js b/app/browser/api/ledger.js index 7057b8196dd..407e39f68da 100644 --- a/app/browser/api/ledger.js +++ b/app/browser/api/ledger.js @@ -240,7 +240,7 @@ const onBootStateFile = (state) => { } if (client.sync(callback) === true) { - run(random.randomInt({min: ledgerUtil.milliseconds.minute, max: 10 * ledgerUtil.milliseconds.minute})) + run(state, random.randomInt({min: ledgerUtil.milliseconds.minute, max: 10 * ledgerUtil.milliseconds.minute})) } module.exports.getBalance(state) @@ -2321,7 +2321,7 @@ const run = (state, delayTime) => { }) } - if (typeof delayTime === 'undefined' || !client) { + if (state == null || typeof delayTime === 'undefined' || !client) { return } diff --git a/test/unit/app/browser/api/ledgerTest.js b/test/unit/app/browser/api/ledgerTest.js index 0a1514609b4..d715bec9e2f 100644 --- a/test/unit/app/browser/api/ledgerTest.js +++ b/test/unit/app/browser/api/ledgerTest.js @@ -103,6 +103,9 @@ describe('ledger api unit tests', function () { // ledger client stubbing ledgerClient = sinon.stub() ledgerClientObject = { + ballots: function () { + return 1 + }, sync: function (callback) { return false }, getBraveryProperties: function () { return { @@ -2686,4 +2689,51 @@ describe('ledger api unit tests', function () { assert(fetchPublisherInfoSpy.withArgs('test.com', sinon.match.any, sinon.match.any)) }) }) + + describe('BSCrun', function () { + let clientBallotsSpy + + before(() => { + ledgerApi.setSynopsis({ + toJSON: () => { + return { + publishers: { + 'clifton.io': { + visits: 1 + } + } + } + }, + winners: () => { + return [] + } + }) + ledgerApi.setClient(ledgerClientObject) + clientBallotsSpy = sinon.spy(ledgerClientObject, 'ballots') + }) + + afterEach(() => { + clientBallotsSpy.reset() + }) + + after(() => { + clientBallotsSpy.restore() + ledgerApi.setSynopsis(undefined) + }) + + it('exits if state is undefined', function () { + ledgerApi.run(undefined, 10) + assert.equal(clientBallotsSpy.notCalled, true) + }) + + it('exits if delayTime is undefined', function () { + ledgerApi.run(defaultAppState) + assert.equal(clientBallotsSpy.notCalled, true) + }) + + it('gets balance count from client', function () { + ledgerApi.run(defaultAppState, 10) + assert.equal(clientBallotsSpy.calledOnce, true) + }) + }) })