Skip to content

Commit

Permalink
Migrates the contribution amounts from old USD setting to new BAT set…
Browse files Browse the repository at this point in the history
…ting

Fixes brave#11719

Auditors: @NejcZdovc, @evq, @petemill

Test Plan:
- Use an older version of Brave (0.18.36 for example)
- Enable payments and set contribution amount as one of the original amounts (like $10 for example)
- Run a build with this fix in it
- In preferences#payments, you should notice it should be 25 BAT if USD was $5, 50 if $10, 75 if $15, 100 if $20
- Exit Brave
- Open the ledger-state.json file and inspect, looking for properties.fee.amount
- This should have the new value (25, 50, 75, or 100) not the old value (5, 10, 15, 20)
  • Loading branch information
bsclifton authored and syuan100 committed Nov 9, 2017
1 parent c08f46e commit 64607e8
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 16 deletions.
23 changes: 21 additions & 2 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -1867,7 +1867,7 @@ const onCallback = (state, result, delayTime) => {

if (client && result.getIn(['properties', 'wallet'])) {
if (!ledgerState.getInfoProp(state, 'created')) {
setPaymentInfo(getSetting(settings.PAYMENTS_CONTRIBUTION_AMOUNT))
module.exports.setPaymentInfo(getSetting(settings.PAYMENTS_CONTRIBUTION_AMOUNT))
}

state = getStateInfo(state, regularResults) // TODO optimize if possible
Expand Down Expand Up @@ -2014,6 +2014,24 @@ const initialize = (state, paymentsEnabled) => {
}
}

const getContributionAmount = () => {
const amount = getSetting(settings.PAYMENTS_CONTRIBUTION_AMOUNT)
// if amount is 5, 10, 15, or 20... the amount wasn't updated when changing
// from BTC to BAT (see https://github.com/brave/browser-laptop/issues/11719)
let updatedAmount
switch (amount) {
case 5: updatedAmount = 25; break
case 10: updatedAmount = 50; break
case 15: updatedAmount = 75; break
case 20: updatedAmount = 100; break
}
if (updatedAmount) {
appActions.changeSetting(settings.PAYMENTS_CONTRIBUTION_AMOUNT, updatedAmount)
return updatedAmount
}
return amount
}

const onInitRead = (state, parsedData) => {
state = getStateInfo(state, parsedData)

Expand Down Expand Up @@ -2071,7 +2089,8 @@ const onInitRead = (state, parsedData) => {
state = ledgerState.setInfoProp(state, 'address', client.getWalletAddress())
}

setPaymentInfo(getSetting(settings.PAYMENTS_CONTRIBUTION_AMOUNT))
const contributionAmount = getContributionAmount()
module.exports.setPaymentInfo(contributionAmount)
getBalance(state)

// Show relevant browser notifications on launch
Expand Down
128 changes: 114 additions & 14 deletions test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ describe('ledger api unit tests', function () {
let paymentsNotifications
let isBusy = false
let ledgerClient
let contributionAmount = 25

// spies
let ledgerTransitionSpy
let ledgerTransitionedSpy
let onBitcoinToBatTransitionedSpy
let onLedgerCallbackSpy
let onBitcoinToBatBeginTransitionSpy
let onChangeSettingSpy

before(function () {
this.clock = sinon.useFakeTimers()
Expand All @@ -44,11 +46,13 @@ describe('ledger api unit tests', function () {
mockery.registerMock('ad-block', fakeAdBlock)
mockery.registerMock('../../../js/settings', {
getSetting: (settingKey, settingsCollection, value) => {
if (settingKey === settings.PAYMENTS_ENABLED) {
return paymentsEnabled
}
if (settingKey === settings.PAYMENTS_NOTIFICATIONS) {
return paymentsNotifications
switch (settingKey) {
case settings.PAYMENTS_ENABLED:
return paymentsEnabled
case settings.PAYMENTS_NOTIFICATIONS:
return paymentsNotifications
case settings.PAYMENTS_CONTRIBUTION_AMOUNT:
return contributionAmount
}
return false
}
Expand All @@ -57,6 +61,7 @@ describe('ledger api unit tests', function () {
onBitcoinToBatTransitionedSpy = sinon.spy(appActions, 'onBitcoinToBatTransitioned')
onLedgerCallbackSpy = sinon.spy(appActions, 'onLedgerCallback')
onBitcoinToBatBeginTransitionSpy = sinon.spy(appActions, 'onBitcoinToBatBeginTransition')
onChangeSettingSpy = sinon.spy(appActions, 'changeSetting')

// ledger client stubbing
ledgerClient = sinon.stub()
Expand All @@ -70,6 +75,15 @@ describe('ledger api unit tests', function () {
}
}
},
getWalletAddresses: function () {
return {
'BAT': '0xADDRESS_HERE',
'BTC': 'ADDRESS_HERE',
'CARD_ID': 'ADDRESS-GUID-GOES-IN-HERE',
'ETH': '0xADDRESS_HERE',
'LTC': 'ADDRESS_HERE'
}
},
getWalletProperties: function (amount, currency, callback) {
callback(null, {})
},
Expand All @@ -87,6 +101,13 @@ describe('ledger api unit tests', function () {
transitioned: function () {
return {}
},
setBraveryProperties: function (clientProperties, callback) {
if (typeof callback === 'function') {
const err = undefined
const result = {}
callback(err, result)
}
},
state: {
transactions: []
},
Expand All @@ -106,6 +127,9 @@ describe('ledger api unit tests', function () {
})
after(function () {
onBitcoinToBatTransitionedSpy.restore()
onLedgerCallbackSpy.restore()
onBitcoinToBatBeginTransitionSpy.restore()
onChangeSettingSpy.restore()
this.clock.restore()
mockery.deregisterAll()
mockery.disable()
Expand All @@ -126,25 +150,101 @@ describe('ledger api unit tests', function () {
})

describe('onInitRead', function () {
let parsedLedgerData
let onLaunchSpy
beforeEach(function () {
onLaunchSpy = sinon.spy(ledgerApi.notifications, 'onLaunch')
})
afterEach(function () {
onLaunchSpy.restore()
})
it('calls notifications.onLaunch', function () {
ledgerApi.onInitRead(defaultAppState, {
let setPaymentInfoSpy
before(function () {
parsedLedgerData = {
paymentInfo: {
},
properties: {
wallet: {
paymentId: 12345
}
}
})
}
contributionAmount = 25
})
before(function () {
onLaunchSpy = sinon.spy(ledgerApi.notifications, 'onLaunch')
setPaymentInfoSpy = sinon.spy(ledgerApi, 'setPaymentInfo')
})
after(function () {
onLaunchSpy.restore()
setPaymentInfoSpy.restore()
})
it('calls notifications.onLaunch', function () {
onLaunchSpy.reset()
ledgerApi.onInitRead(defaultAppState, parsedLedgerData)
assert(onLaunchSpy.calledOnce)
})
it('calls setPaymentInfo with contribution amount', function () {
setPaymentInfoSpy.reset()
ledgerApi.onInitRead(defaultAppState, parsedLedgerData)
assert(setPaymentInfoSpy.withArgs(25).calledOnce)
})

describe('when contribution amount is still set to the USD amount (before BAT Mercury)', function () {
after(function () {
contributionAmount = 25
})
describe('when set to 5 USD', function () {
before(function () {
setPaymentInfoSpy.reset()
onChangeSettingSpy.reset()
contributionAmount = 5
ledgerApi.onInitRead(defaultAppState, parsedLedgerData)
})
it('converts to 25 BAT', function () {
assert(setPaymentInfoSpy.withArgs(25).calledOnce)
})
it('updates the setting', function () {
assert(onChangeSettingSpy.withArgs(settings.PAYMENTS_CONTRIBUTION_AMOUNT, 25).calledOnce)
})
})
describe('when set to 10 USD', function () {
before(function () {
setPaymentInfoSpy.reset()
onChangeSettingSpy.reset()
contributionAmount = 10
ledgerApi.onInitRead(defaultAppState, parsedLedgerData)
})
it('converts to 50 BAT', function () {
assert(setPaymentInfoSpy.withArgs(50).calledOnce)
})
it('updates the setting', function () {
assert(onChangeSettingSpy.withArgs(settings.PAYMENTS_CONTRIBUTION_AMOUNT, 50).calledOnce)
})
})
describe('when set to 15 USD', function () {
before(function () {
setPaymentInfoSpy.reset()
onChangeSettingSpy.reset()
contributionAmount = 15
ledgerApi.onInitRead(defaultAppState, parsedLedgerData)
})
it('converts to 75 BAT', function () {
assert(setPaymentInfoSpy.withArgs(75).calledOnce)
})
it('updates the setting', function () {
assert(onChangeSettingSpy.withArgs(settings.PAYMENTS_CONTRIBUTION_AMOUNT, 75).calledOnce)
})
})
describe('when set to 20 USD', function () {
before(function () {
setPaymentInfoSpy.reset()
onChangeSettingSpy.reset()
contributionAmount = 20
ledgerApi.onInitRead(defaultAppState, parsedLedgerData)
})
it('converts to 100 BAT', function () {
assert(setPaymentInfoSpy.withArgs(100).calledOnce)
})
it('updates the setting', function () {
assert(onChangeSettingSpy.withArgs(settings.PAYMENTS_CONTRIBUTION_AMOUNT, 100).calledOnce)
})
})
})
})

describe('transitionWalletToBat', function () {
Expand Down

0 comments on commit 64607e8

Please sign in to comment.