From d400bd3b3f08b3181c1176dfb9476880cf937066 Mon Sep 17 00:00:00 2001 From: justinchou-google Date: Fri, 9 Aug 2024 19:53:26 +0000 Subject: [PATCH 1/4] save point --- src/runtime/entitlements-manager-test.js | 14 ++++++++++++++ src/runtime/entitlements-manager.ts | 20 ++++++++++++++++++++ src/runtime/experiment-flags.ts | 5 +++++ 3 files changed, 39 insertions(+) diff --git a/src/runtime/entitlements-manager-test.js b/src/runtime/entitlements-manager-test.js index 529407b27c..277581bbd9 100644 --- a/src/runtime/entitlements-manager-test.js +++ b/src/runtime/entitlements-manager-test.js @@ -437,6 +437,20 @@ describes.realWin('EntitlementsManager', (env) => { }); }); + it('adds content_type to query params when experiment is enabled', async () => { + it('should log error messages from entitlements server', async () => { + fetcherMock + .expects('fetch') + .withExactArgs( + 'https://news.google.com/swg/_/api/v1/publication/pub1/entitlements', + { + method: 'GET', + headers: {'Accept': 'text/plain, application/json'}, + credentials: 'include', + } + ) + }) + it('should accept encrypted document key', async () => { fetcherMock .expects('fetch') diff --git a/src/runtime/entitlements-manager.ts b/src/runtime/entitlements-manager.ts index 1cc2a0fa8c..e022e228e5 100644 --- a/src/runtime/entitlements-manager.ts +++ b/src/runtime/entitlements-manager.ts @@ -41,6 +41,7 @@ import { GOOGLE_METERING_SOURCE, PRIVILEGED_SOURCE, } from '../api/entitlements'; +import {ArticleExperimentFlags} from './experiment-flags'; import {Fetcher} from './fetcher'; import {Intervention} from './intervention'; import {InterventionType} from '../api/intervention-type'; @@ -823,6 +824,18 @@ export class EntitlementsManager { if (this.useArticleEndpoint_) { url = addQueryParam(url, 'locked', String(this.pageConfig_.isLocked())); } + const experimentFlags = await this.getExperimentConfigFlags(); + if ( + experimentFlags.includes( + ArticleExperimentFlags.CONTENT_TYPE_ARTICLE_PARAM_ENABLED + ) + ) { + url = addQueryParam( + url, + 'content_type', + getContentTypeParam(this.pageConfig_.isLocked()) + ); + } const hashedCanonicalUrl = await this.getHashedCanonicalUrl_(); let encodableParams: GetEntitlementsParamsInternalDef | undefined = this @@ -1019,3 +1032,10 @@ function irtpStringToBoolean(value: string | null): boolean | undefined { return undefined; } } + +/** + * Returns ContentType Enum string from isLocked page config status. + */ +function getContentTypeParam(isLocked: boolean) { + return isLocked ? 'CLOSED' : 'OPEN'; +} diff --git a/src/runtime/experiment-flags.ts b/src/runtime/experiment-flags.ts index d0a3ee34a7..f9f500373a 100644 --- a/src/runtime/experiment-flags.ts +++ b/src/runtime/experiment-flags.ts @@ -54,4 +54,9 @@ export enum ArticleExperimentFlags { * Experiment flag to enable onsite preview. */ ONSITE_PREVIEW_ENABLED = 'onsite_preview_enabled', + + /** + * Experiment flag to enable the content type article param. + */ + CONTENT_TYPE_ARTICLE_PARAM_ENABLED = 'content_type_article_param_enabled' } From c6510605949c57fda0b38526f2dddb8d4ae425d6 Mon Sep 17 00:00:00 2001 From: justinchou-google Date: Fri, 9 Aug 2024 20:13:04 +0000 Subject: [PATCH 2/4] remove extra text --- src/runtime/entitlements-manager-test.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/runtime/entitlements-manager-test.js b/src/runtime/entitlements-manager-test.js index 277581bbd9..f3894934cc 100644 --- a/src/runtime/entitlements-manager-test.js +++ b/src/runtime/entitlements-manager-test.js @@ -438,18 +438,17 @@ describes.realWin('EntitlementsManager', (env) => { }); it('adds content_type to query params when experiment is enabled', async () => { - it('should log error messages from entitlements server', async () => { - fetcherMock - .expects('fetch') - .withExactArgs( - 'https://news.google.com/swg/_/api/v1/publication/pub1/entitlements', - { - method: 'GET', - headers: {'Accept': 'text/plain, application/json'}, - credentials: 'include', - } - ) - }) + fetcherMock + .expects('fetch') + .withExactArgs( + 'https://news.google.com/swg/_/api/v1/publication/pub1/entitlements', + { + method: 'GET', + headers: {'Accept': 'text/plain, application/json'}, + credentials: 'include', + } + ); + }); it('should accept encrypted document key', async () => { fetcherMock From 443e37c4851b754bc066c184e753ceef3f21eb6c Mon Sep 17 00:00:00 2001 From: justinchou-google Date: Fri, 9 Aug 2024 21:27:28 +0000 Subject: [PATCH 3/4] update tests, add test for unlocked content --- src/runtime/entitlements-manager-test.js | 90 +++++++++++++++++++----- src/runtime/entitlements-manager.ts | 10 +-- 2 files changed, 75 insertions(+), 25 deletions(-) diff --git a/src/runtime/entitlements-manager-test.js b/src/runtime/entitlements-manager-test.js index f3894934cc..ac69cb2bdd 100644 --- a/src/runtime/entitlements-manager-test.js +++ b/src/runtime/entitlements-manager-test.js @@ -437,19 +437,6 @@ describes.realWin('EntitlementsManager', (env) => { }); }); - it('adds content_type to query params when experiment is enabled', async () => { - fetcherMock - .expects('fetch') - .withExactArgs( - 'https://news.google.com/swg/_/api/v1/publication/pub1/entitlements', - { - method: 'GET', - headers: {'Accept': 'text/plain, application/json'}, - credentials: 'include', - } - ); - }); - it('should accept encrypted document key', async () => { fetcherMock .expects('fetch') @@ -1745,7 +1732,7 @@ describes.realWin('EntitlementsManager', (env) => { fetcherMock .expects('fetch') .withExactArgs( - `https://news.google.com/swg/_/api/v1/publication/pub1/article?locked=true&encodedEntitlementsParams=${encodedParams}`, + `https://news.google.com/swg/_/api/v1/publication/pub1/article?locked=true&contentType=CLOSED&encodedEntitlementsParams=${encodedParams}`, { method: 'GET', headers: {'Accept': 'text/plain, application/json'}, @@ -1784,6 +1771,77 @@ describes.realWin('EntitlementsManager', (env) => { ); }); + it('should pass OPEN contentType for unlocked content to the article endpoint', async () => { + pageConfig = new PageConfig('pub1:label1', false); + manager = new EntitlementsManager( + win, + pageConfig, + fetcher, + deps, + /* useArticleEndpoint */ true + ); + jwtHelperMock = sandbox.mock(manager.jwtHelper_); + jwtHelperMock + .expects('decode') + .withExactArgs('SIGNED_DATA') + .returns({ + entitlements: { + products: ['pub1:label1'], + subscriptionToken: 'token1', + source: 'google:metering', + }, + }); + const testSubscriptionTokenContents = { + metering: { + ownerId: 'scenic-2017.appspot.com', + action: 'READ', + clientUserAttribute: 'standard_registered_user', + }, + }; + jwtHelperMock + .expects('decode') + .withExactArgs('token1') + .returns(testSubscriptionTokenContents); + const article = { + entitlements: { + signedEntitlements: 'SIGNED_DATA', + }, + clientConfig: { + id: 'foo', + }, + }; + const encodedParams = base64UrlEncodeFromBytes( + utf8EncodeSync( + `{"metering":{"clientTypes":[1],"owner":"pub1","resource":{"hashedCanonicalUrl":"${HASHED_CANONICAL_URL}"},"state":{"id":"u1","attributes":[]},"token":"token"}}` + ) + ); + fetcherMock + .expects('fetch') + .withExactArgs( + `https://news.google.com/swg/_/api/v1/publication/pub1/article?locked=false&contentType=OPEN&encodedEntitlementsParams=${encodedParams}`, + { + method: 'GET', + headers: {'Accept': 'text/plain, application/json'}, + credentials: 'include', + } + ) + .returns( + Promise.resolve({ + text: () => Promise.resolve(JSON.stringify(article)), + }) + ); + expectGetSwgUserTokenToBeCalled(); + + await manager.getEntitlements({ + metering: { + state: { + id: 'u1', + }, + }, + }); + await manager.getArticle(); + }); + it('should use the article endpoint with preview config id param', async () => { manager = new EntitlementsManager( win, @@ -1809,7 +1867,7 @@ describes.realWin('EntitlementsManager', (env) => { fetcherMock .expects('fetch') .withExactArgs( - `https://news.google.com/swg/_/api/v1/publication/pub1/article?previewConfigId=${configId}&locked=true&encodedEntitlementsParams=${encodedParams}`, + `https://news.google.com/swg/_/api/v1/publication/pub1/article?previewConfigId=${configId}&locked=true&contentType=CLOSED&encodedEntitlementsParams=${encodedParams}`, { method: 'GET', headers: {'Accept': 'text/plain, application/json'}, @@ -1864,7 +1922,7 @@ describes.realWin('EntitlementsManager', (env) => { fetcherMock .expects('fetch') .withExactArgs( - `https://news.google.com/swg/_/api/v1/publication/pub1/article?previewConfigId=${configId}&previewKey=${previewKey}&locked=true&encodedEntitlementsParams=${encodedParams}`, + `https://news.google.com/swg/_/api/v1/publication/pub1/article?previewConfigId=${configId}&previewKey=${previewKey}&locked=true&contentType=CLOSED&encodedEntitlementsParams=${encodedParams}`, { method: 'GET', headers: {'Accept': 'text/plain, application/json'}, diff --git a/src/runtime/entitlements-manager.ts b/src/runtime/entitlements-manager.ts index e022e228e5..2c9a183717 100644 --- a/src/runtime/entitlements-manager.ts +++ b/src/runtime/entitlements-manager.ts @@ -41,7 +41,6 @@ import { GOOGLE_METERING_SOURCE, PRIVILEGED_SOURCE, } from '../api/entitlements'; -import {ArticleExperimentFlags} from './experiment-flags'; import {Fetcher} from './fetcher'; import {Intervention} from './intervention'; import {InterventionType} from '../api/intervention-type'; @@ -823,16 +822,9 @@ export class EntitlementsManager { // Add locked param. if (this.useArticleEndpoint_) { url = addQueryParam(url, 'locked', String(this.pageConfig_.isLocked())); - } - const experimentFlags = await this.getExperimentConfigFlags(); - if ( - experimentFlags.includes( - ArticleExperimentFlags.CONTENT_TYPE_ARTICLE_PARAM_ENABLED - ) - ) { url = addQueryParam( url, - 'content_type', + 'contentType', getContentTypeParam(this.pageConfig_.isLocked()) ); } From ab9f753883a8e9196003eecd4db14d7084a7387c Mon Sep 17 00:00:00 2001 From: justinchou-google Date: Fri, 9 Aug 2024 21:28:21 +0000 Subject: [PATCH 4/4] remove unused flag --- src/runtime/experiment-flags.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/runtime/experiment-flags.ts b/src/runtime/experiment-flags.ts index f9f500373a..d0a3ee34a7 100644 --- a/src/runtime/experiment-flags.ts +++ b/src/runtime/experiment-flags.ts @@ -54,9 +54,4 @@ export enum ArticleExperimentFlags { * Experiment flag to enable onsite preview. */ ONSITE_PREVIEW_ENABLED = 'onsite_preview_enabled', - - /** - * Experiment flag to enable the content type article param. - */ - CONTENT_TYPE_ARTICLE_PARAM_ENABLED = 'content_type_article_param_enabled' }