Skip to content

Commit

Permalink
Allow viewer to overide paywall as well as auth (ampproject#22933)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpettitt committed Jun 19, 2019
1 parent 3d15231 commit 23986bc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
22 changes: 20 additions & 2 deletions extensions/amp-subscriptions/0.1/amp-subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,20 @@ export class SubscriptionService {
/** @const @private {!../../../src/service/timer-impl.Timer} */
this.timer_ = Services.timerFor(ampdoc.win);

/** @private @const {boolean} */
/**
* @private @const {boolean}
* View substitutes for other services and handles auth. Usually an app
* with a webview.
*/
this.doesViewerProvideAuth_ = this.viewer_.hasCapability('auth');

/**
* @private @const {boolean}
* Viewer also does paywall/metering so on page "local" paywall is not shown.
*/
this.doesViewerProvidePaywall_ =
this.doesViewerProvideAuth_ && this.viewer_.hasCapability('paywall');

/** @private @const {!Promise<!../../../src/service/cid-impl.Cid>} */
this.cid_ = Services.cidForDoc(ampdoc);

Expand Down Expand Up @@ -224,7 +235,14 @@ export class SubscriptionService {
*/
processGrantState_(grantState) {
this.renderer_.toggleLoading(false);
this.renderer_.setGrantState(grantState);
/*
* If the viewer is providing a paywall we don't want the publisher
* paywall to render in the case of no grant so we leave the page
* in the original "unknown" state.
*/
if (grantState || !this.doesViewerProvidePaywall_) {
this.renderer_.setGrantState(grantState);
}
this.viewTrackerPromise_ = this.viewerTracker_.scheduleView(2000);
if (grantState === false) {
// TODO(@prateekbh): Show UI that no eligible entitlement found
Expand Down
28 changes: 28 additions & 0 deletions extensions/amp-subscriptions/0.1/test/test-amp-subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,34 @@ describes.fakeWin('AmpSubscriptions', {amp: true}, env => {
}
);

it('not unlock page if no entitlments and viewer provides paywall', () => {
subscriptionService.doesViewerProvidePaywall_ = true;
subscriptionService.doesViewerProvideAuth_ = true;
subscriptionService.platformStore_ = new PlatformStore(products);
const getGrantStatusStub = sandbox
.stub(subscriptionService.platformStore_, 'getGrantStatus')
.callsFake(() => Promise.resolve());
const selectAndActivateStub = sandbox.stub(
subscriptionService,
'selectAndActivatePlatform_'
);
const performPingbackStub = sandbox.stub(
subscriptionService,
'performPingback_'
);
const setGrantStateStub = sandbox.stub(
subscriptionService.renderer_,
'setGrantState'
);
subscriptionService.startAuthorizationFlow_();
expect(getGrantStatusStub).to.be.calledOnce;
expect(selectAndActivateStub).to.be.calledOnce;
return subscriptionService.platformStore_.getGrantStatus().then(() => {
expect(performPingbackStub).to.be.called;
expect(setGrantStateStub).to.not.be.called;
});
});

it('should fallback if viewer provides auth but fails', function*() {
// Make sendMessageAwaitResponse() return a pending promise so we have
// a chance to stub the platform store.
Expand Down

0 comments on commit 23986bc

Please sign in to comment.