Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #14357 from NejcZdovc/fix/#14356-block
Browse files Browse the repository at this point in the history
Adds captcha block overlay
  • Loading branch information
NejcZdovc committed Jun 8, 2018
1 parent d287db8 commit f5d7d50
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 19 deletions.
31 changes: 21 additions & 10 deletions app/browser/api/ledger.js
Expand Up @@ -197,7 +197,11 @@ const paymentPresent = (state, tabId, present) => {

if (!present) {
const status = ledgerState.getPromotionProp(state, 'promotionStatus')
if (status === promotionStatuses.CAPTCHA_CHECK || status === promotionStatuses.CAPTCHA_ERROR) {
if (
status === promotionStatuses.CAPTCHA_CHECK ||
status === promotionStatuses.CAPTCHA_ERROR ||
status === promotionStatuses.CAPTCHA_BLOCK
) {
state = ledgerState.setPromotionProp(state, 'promotionStatus', null)
}
}
Expand Down Expand Up @@ -1827,9 +1831,8 @@ const generatePaymentData = (state) => {

const getPaymentInfo = (state) => {
let amount, currency
const inProgress = ledgerState.getAboutProp(state, 'status') === ledgerStatuses.IN_PROGRESS

if (!client || inProgress) {
if (!client) {
return state
}

Expand Down Expand Up @@ -3030,20 +3033,24 @@ const getCaptcha = (state) => {
return
}

client.getPromotionCaptcha(promotion.get('promotionId'), (err, body) => {
client.getPromotionCaptcha(promotion.get('promotionId'), (err, body, response) => {
if (err) {
console.error(`Problem getting promotion captcha ${err.toString()}`)
appActions.onCaptchaResponse(null)
appActions.onCaptchaResponse(response, null)
return
}

appActions.onCaptchaResponse(body)
appActions.onCaptchaResponse(null, body)
})
}

const onCaptchaResponse = (state, body) => {
const onCaptchaResponse = (state, response, body) => {
if (body == null) {
state = ledgerState.setPromotionProp(state, 'promotionStatus', promotionStatuses.CAPTCHA_ERROR)
return state
if (response && response.get('statusCode') === 429) {
return ledgerState.setPromotionProp(state, 'promotionStatus', promotionStatuses.CAPTCHA_BLOCK)
}

return ledgerState.setPromotionProp(state, 'promotionStatus', promotionStatuses.CAPTCHA_ERROR)
}

const image = `data:image/jpeg;base64,${Buffer.from(body).toString('base64')}`
Expand Down Expand Up @@ -3088,6 +3095,9 @@ const onPromotionResponse = (state, status) => {
// captcha verification failed
state = ledgerState.setPromotionProp(state, 'promotionStatus', promotionStatuses.CAPTCHA_ERROR)
module.exports.getCaptcha(state)
} else if (status.get('statusCode') === 429) {
// too many attempts
state = ledgerState.setPromotionProp(state, 'promotionStatus', promotionStatuses.CAPTCHA_BLOCK)
} else {
// general error
state = ledgerState.setPromotionProp(state, 'promotionStatus', promotionStatuses.GENERAL_ERROR)
Expand All @@ -3099,7 +3109,8 @@ const onPromotionResponse = (state, status) => {

if (
currentStatus === promotionStatuses.CAPTCHA_ERROR ||
currentStatus === promotionStatuses.CAPTCHA_CHECK
currentStatus === promotionStatuses.CAPTCHA_CHECK ||
currentStatus === promotionStatuses.CAPTCHA_BLOCK
) {
state = ledgerState.setPromotionProp(state, 'promotionStatus', null)
}
Expand Down
2 changes: 1 addition & 1 deletion app/browser/reducers/ledgerReducer.js
Expand Up @@ -441,7 +441,7 @@ const ledgerReducer = (state, action, immutableAction) => {
}
case appConstants.APP_ON_CAPTCHA_RESPONSE:
{
state = ledgerApi.onCaptchaResponse(state, action.get('body'))
state = ledgerApi.onCaptchaResponse(state, action.get('response'), action.get('body'))
break
}
case appConstants.APP_ON_CAPTCHA_CLOSE:
Expand Down
1 change: 1 addition & 0 deletions app/common/constants/promotionStatuses.js
Expand Up @@ -6,6 +6,7 @@ const statuses = {
GENERAL_ERROR: 'generalError',
PROMO_EXPIRED: 'expiredError',
CAPTCHA_CHECK: 'captchaCheck',
CAPTCHA_BLOCK: 'captchaBlock',
CAPTCHA_ERROR: 'captchaError'
}

Expand Down
2 changes: 2 additions & 0 deletions app/extensions/brave/locales/en-US/preferences.properties
Expand Up @@ -264,6 +264,8 @@ printKeys=Print key
privacy=Privacy
privateData=Private Data
privateDataMessage=Clear the following data types when I close Brave
promotionCaptchaBlockTitle=Uh-oh.
promotionCaptchaBlockMessage=We’ve detected too many attempts to claim tokens from your network. Please try again in a few hours.
promotionCaptchaTitle=Almost there!
promotionCaptchaErrorTitle=Hmmm…not quite.
promotionCaptchaErrorText=Please try again.
Expand Down
2 changes: 2 additions & 0 deletions app/locale.js
Expand Up @@ -285,6 +285,8 @@ var rendererIdentifiers = function () {
'promotionClaimedErrorMessage',
'promotionClaimedErrorText',
'promotionClaimedErrorTitle',
'promotionCaptchaBlockTitle',
'promotionCaptchaBlockMessage',
'corruptedOverlayTitle',
'corruptedOverlayMessage',
'corruptedOverlayText',
Expand Down
7 changes: 7 additions & 0 deletions app/renderer/components/preferences/payment/enabledContent.js
Expand Up @@ -310,6 +310,13 @@ class EnabledContent extends ImmutableComponent {
text = locale.translation('promotionClaimedErrorText')
break
}
case promotionStatuses.CAPTCHA_BLOCK:
{
title = locale.translation('promotionCaptchaBlockTitle')
message = locale.translation('promotionCaptchaBlockMessage')
text = ' '
break
}
case promotionStatuses.CAPTCHA_CHECK:
case promotionStatuses.CAPTCHA_ERROR:
{
Expand Down
6 changes: 5 additions & 1 deletion app/sessionStore.js
Expand Up @@ -416,7 +416,11 @@ module.exports.cleanAppData = (immutableData, isShutdown) => {

if (isShutdown) {
const status = ledgerState.getPromotionProp(immutableData, 'promotionStatus')
if (status === promotionStatuses.CAPTCHA_CHECK || status === promotionStatuses.CAPTCHA_ERROR) {
if (
status === promotionStatuses.CAPTCHA_CHECK ||
status === promotionStatuses.CAPTCHA_BLOCK ||
status === promotionStatuses.CAPTCHA_ERROR
) {
immutableData = ledgerState.setPromotionProp(immutableData, 'promotionStatus', null)
}
}
Expand Down
5 changes: 3 additions & 2 deletions js/actions/appActions.js
Expand Up @@ -1859,10 +1859,11 @@ const appActions = {
})
},

onCaptchaResponse: function (body) {
onCaptchaResponse: function (response, body) {
dispatch({
actionType: appConstants.APP_ON_CAPTCHA_RESPONSE,
body
body,
response
})
},

Expand Down
28 changes: 26 additions & 2 deletions test/unit/app/browser/api/ledgerTest.js
Expand Up @@ -1957,6 +1957,16 @@ describe('ledger api unit tests', function () {
assert.deepEqual(result.toJS(), expectedSate.toJS())
assert(getCaptchaSpy.notCalled)
})

it('block error', function () {
const result = ledgerApi.onPromotionResponse(defaultAppState, Immutable.fromJS({
statusCode: 429
}))
const expectedSate = defaultAppState
.setIn(['ledger', 'promotion', 'promotionStatus'], promotionStatuses.CAPTCHA_BLOCK)
assert.deepEqual(result.toJS(), expectedSate.toJS())
assert(getCaptchaSpy.notCalled)
})
})
})

Expand Down Expand Up @@ -3786,11 +3796,25 @@ describe('ledger api unit tests', function () {
assert.deepEqual(result.toJS(), expectedState.toJS())
})

it('responose too many', function () {
const expectedState = defaultAppState
.setIn(['ledger', 'promotion', 'promotionStatus'], promotionStatuses.CAPTCHA_BLOCK)
const result = ledgerApi.onCaptchaResponse(defaultAppState, Immutable.fromJS({statusCode: 429}))
assert.deepEqual(result.toJS(), expectedState.toJS())
})

it('responose not found', function () {
const expectedState = defaultAppState
.setIn(['ledger', 'promotion', 'promotionStatus'], promotionStatuses.CAPTCHA_ERROR)
const result = ledgerApi.onCaptchaResponse(defaultAppState, Immutable.fromJS({statusCode: 404}))
assert.deepEqual(result.toJS(), expectedState.toJS())
})

it('new captcha', function () {
const expectedState = defaultAppState
.setIn(['ledger', 'promotion', 'promotionStatus'], promotionStatuses.CAPTCHA_CHECK)
.setIn(['ledger', 'promotion', 'captcha'], 'data:image/jpeg;base64,/9j/2wA=')
const result = ledgerApi.onCaptchaResponse(defaultAppState, body)
const result = ledgerApi.onCaptchaResponse(defaultAppState, null, body)
assert.deepEqual(result.toJS(), expectedState.toJS())
})

Expand All @@ -3800,7 +3824,7 @@ describe('ledger api unit tests', function () {
const expectedState = defaultAppState
.setIn(['ledger', 'promotion', 'promotionStatus'], promotionStatuses.CAPTCHA_ERROR)
.setIn(['ledger', 'promotion', 'captcha'], 'data:image/jpeg;base64,/9j/2wA=')
const result = ledgerApi.onCaptchaResponse(state, body)
const result = ledgerApi.onCaptchaResponse(state, null, body)
assert.deepEqual(result.toJS(), expectedState.toJS())
})
})
Expand Down
20 changes: 17 additions & 3 deletions test/unit/app/browser/reducers/ledgerReducerTest.js
Expand Up @@ -1019,12 +1019,26 @@ describe('ledgerReducer unit tests', function () {
onCaptchaResponseSpy.restore()
})

it('execute', function () {
it('execute error', function () {
ledgerReducer(appState, Immutable.fromJS({
actionType: appConstants.APP_ON_CAPTCHA_RESPONSE,
body: null,
response: {
statusCode: 429
}
}))
assert(onCaptchaResponseSpy.withArgs(sinon.match.any, Immutable.fromJS({
statusCode: 429
}), null).calledOnce)
})

it('execute correct', function () {
ledgerReducer(appState, Immutable.fromJS({
actionType: appConstants.APP_ON_CAPTCHA_RESPONSE,
body: 1
body: 1,
response: null
}))
assert(onCaptchaResponseSpy.withArgs(sinon.match.any, 1).calledOnce)
assert(onCaptchaResponseSpy.withArgs(sinon.match.any, null, 1).calledOnce)
})
})

Expand Down

0 comments on commit f5d7d50

Please sign in to comment.