This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 973
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #6261 Auditors: @mrose17, @bsclifton
- Loading branch information
1 parent
f00cce0
commit ac6d8b4
Showing
12 changed files
with
252 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const React = require('react') | ||
const tldjs = require('tldjs') | ||
const ImmutableComponent = require('../../../js/components/immutableComponent') | ||
const appActions = require('../../../js/actions/appActions') | ||
const settings = require('../../../js/constants/settings') | ||
const getSetting = require('../../../js/settings').getSetting | ||
const cx = require('../../../js/lib/classSet') | ||
const Button = require('../../../js/components/button') | ||
|
||
class PublisherToggle extends ImmutableComponent { | ||
constructor () { | ||
super() | ||
this.onAuthorizePublisher = this.onAuthorizePublisher.bind(this) | ||
} | ||
|
||
get domain () { | ||
return tldjs.getDomain(this.props.url) | ||
} | ||
|
||
get hostPattern () { | ||
return `https?://${this.domain}` | ||
} | ||
|
||
get hostSettings () { | ||
// hostPattern defines it's own identifier for authorized publishers | ||
// sites that do not match criteria would populate siteSettings | ||
// with their default protocol, not hostPattern | ||
return this.props.hostSettings.get(this.hostPattern) | ||
} | ||
|
||
get validPublisherSynopsis () { | ||
// If session is clear then siteSettings is undefined and icon will never be shown, | ||
// but synopsis may not be empty. In such cases let's check if synopsis matches current domain | ||
return this.props.synopsis.map(entry => entry.get('site')).includes(this.domain) | ||
} | ||
|
||
get enabledPublisher () { | ||
// If we can't get ledgerPayments, then it's likely that we are | ||
// on a clean session. Let's then check for publisher's synopsis | ||
return this.hostSettings | ||
? this.hostSettings.get('ledgerPayments') !== false | ||
: this.validPublisherSynopsis | ||
} | ||
|
||
get visiblePublisher () { | ||
// ledgerPaymentsShown is undefined by default until user decide to permanently hide the publisher | ||
// so for icon to be shown it can be everything but false | ||
const ledgerPaymentsShown = this.hostSettings && this.hostSettings.get('ledgerPaymentsShown') | ||
return ledgerPaymentsShown === 'undefined' || ledgerPaymentsShown !== false | ||
} | ||
|
||
get shouldShowAddPublisherButton () { | ||
if ((!!this.hostSettings || !!this.validPublisherSynopsis) && this.visiblePublisher) { | ||
// Only show publisher icon if autoSuggest option is OFF | ||
return !getSetting(settings.AUTO_SUGGEST_SITES) | ||
} | ||
return false | ||
} | ||
|
||
onAuthorizePublisher () { | ||
// if payments disabled, enable it | ||
if (!getSetting(settings.AUTO_SUGGEST_SITES)) { | ||
appActions.changeSetting(settings.PAYMENTS_ENABLED, true) | ||
} | ||
|
||
this.enabledPublisher | ||
? appActions.changeSiteSetting(this.hostPattern, 'ledgerPayments', false) | ||
: appActions.changeSiteSetting(this.hostPattern, 'ledgerPayments', true) | ||
} | ||
|
||
render () { | ||
return this.shouldShowAddPublisherButton | ||
? <span className={cx({ | ||
addPublisherButtonContainer: true, | ||
authorizedPublisher: this.enabledPublisher | ||
})}> | ||
<Button iconClass='fa-btc publisherToggleBtn' | ||
l10nId='enablePublisher' | ||
onClick={this.onAuthorizePublisher} | ||
/> | ||
</span> | ||
: null | ||
} | ||
} | ||
|
||
PublisherToggle.propTypes = { | ||
url: React.PropTypes.string, | ||
hostSettings: React.PropTypes.string, | ||
synopsis: React.PropTypes.string | ||
} | ||
|
||
module.exports = PublisherToggle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* global describe, it, before, after */ | ||
const mockery = require('mockery') | ||
const {shallow} = require('enzyme') | ||
const assert = require('assert') | ||
const Immutable = require('immutable') | ||
const tldjs = require('tldjs') | ||
const fakeElectron = require('../../lib/fakeElectron') | ||
const settingsConst = require('../../../../js/constants/settings') | ||
let PublisherToggle | ||
require('../../braveUnit') | ||
|
||
describe('PublisherToggle component', function () { | ||
const getDomain = (url) => tldjs.getDomain(url) | ||
const getPattern = (pattern) => `https?://${getDomain(pattern)}` | ||
const getHostPattern = (pattern, isEnabled, shouldShow) => Immutable.fromJS({ | ||
[pattern]: { | ||
'ledgerPayments': isEnabled, | ||
'ledgerPaymentsShown': shouldShow | ||
} | ||
}) | ||
const getPublisherSynopsis = (url) => Immutable.fromJS([{ | ||
'site': getDomain(url), | ||
'publisherURL': url | ||
}]) | ||
|
||
const url1 = 'https://clifton.io/sharing-my-passion-for-mercedes-benz' | ||
const domain1 = getDomain(url1) | ||
const pattern1 = getPattern(domain1) | ||
|
||
before(function () { | ||
mockery.enable({ | ||
warnOnReplace: false, | ||
warnOnUnregistered: false, | ||
useCleanCache: true | ||
}) | ||
mockery.registerMock('../../../js/settings', { getSetting: (settingKey, settingsCollection, value) => { | ||
if (settingKey === settingsConst.AUTO_SUGGEST_SITES) { | ||
return false | ||
} | ||
return true | ||
}}) | ||
mockery.registerMock('electron', fakeElectron) | ||
window.chrome = fakeElectron | ||
PublisherToggle = require('../../../../app/renderer/components/publisherToggle') | ||
}) | ||
after(function () { | ||
mockery.disable() | ||
}) | ||
|
||
describe('criteria to be shown', function () { | ||
it('renders if domain match synopsis criteria (siteSettings is empty)', function () { | ||
const wrapper = shallow( | ||
<PublisherToggle | ||
url={url1} | ||
hostSettings={Immutable.Map()} | ||
synopsis={getPublisherSynopsis(domain1, url1)} /> | ||
) | ||
assert.equal(wrapper.find('.addPublisherButtonContainer').length, 1) | ||
}) | ||
it('render if hostPattern match siteSettings (synopsis is empty)', function () { | ||
const wrapper = shallow( | ||
<PublisherToggle | ||
url={url1} | ||
hostSettings={getHostPattern(pattern1, true, true)} | ||
synopsis={Immutable.List()} /> | ||
) | ||
assert.equal(wrapper.find('.addPublisherButtonContainer').length, 1) | ||
}) | ||
it('do not render for unauthorized publishers (no siteSettings and no synopsis)', function () { | ||
const wrapper = shallow( | ||
<PublisherToggle | ||
url={url1} | ||
hostSettings={Immutable.Map()} | ||
synopsis={Immutable.List()} /> | ||
) | ||
assert.equal(wrapper.find('.addPublisherButtonContainer').length, 0) | ||
}) | ||
it('do not render if publisher is permanently hidden', function () { | ||
const wrapper = shallow( | ||
<PublisherToggle | ||
url={url1} | ||
hostSettings={getHostPattern(pattern1, true, false)} | ||
synopsis={Immutable.List()} /> | ||
) | ||
assert.equal(wrapper.find('.addPublisherButtonContainer').length, 0) | ||
}) | ||
it('show as enabled if ledgerPayments is true for that publisher', function () { | ||
const wrapper = shallow( | ||
<PublisherToggle | ||
url={url1} | ||
hostSettings={getHostPattern(pattern1, true, true)} | ||
synopsis={Immutable.List()} /> | ||
) | ||
assert.equal(wrapper.find('.addPublisherButtonContainer').length, 1) | ||
assert.equal(wrapper.find('.authorizedPublisher').length, 1) | ||
}) | ||
it('Show as disabled if ledgerPayments is false for that publisher', function () { | ||
const wrapper = shallow( | ||
<PublisherToggle | ||
url={url1} | ||
hostSettings={getHostPattern(pattern1, false, true)} | ||
synopsis={Immutable.List()} /> | ||
) | ||
assert.equal(wrapper.find('.addPublisherButtonContainer').length, 1) | ||
assert.equal(wrapper.find('.authorizedPublisher').length, 0) | ||
}) | ||
}) | ||
}) |
"proper english" is "Show only included sites" (don't make the ST:TOS mistake!)
@cezaraugusto ^^^