Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add previewConfigId in article query param #3523

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/runtime/entitlements-manager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,60 @@ describes.realWin('EntitlementsManager', (env) => {
);
});

it('should use the article endpoint with preview config id param', async () => {
manager = new EntitlementsManager(
win,
pageConfig,
fetcher,
deps,
/* useArticleEndpoint */ true
);

const article = {
entitlements: {},
clientConfig: {
id: 'foo',
},
};
const encodedParams = base64UrlEncodeFromBytes(
utf8EncodeSync(
`{"metering":{"clientTypes":[1],"owner":"pub1","resource":{"hashedCanonicalUrl":"${HASHED_CANONICAL_URL}"},"state":{"id":"u1","attributes":[]},"token":"token"}}`
)
);
const configId = 'test_id';
win.location.hash = `#swg.debug=1&rrmPromptRequested=${configId}`;
fetcherMock
.expects('fetch')
.withExactArgs(
`https://news.google.com/swg/_/api/v1/publication/pub1/article?previewConfigId=${configId}&locked=true&encodedEntitlementsParams=${encodedParams}`,
{
method: 'GET',
headers: {'Accept': 'text/plain, application/json'},
credentials: 'include',
}
)
.returns(
Promise.resolve({
text: () => Promise.resolve(JSON.stringify(article)),
})
);
expectGetSwgUserTokenToBeCalled();

const ents = await manager.getEntitlements({
metering: {
state: {
id: 'u1',
},
},
});

expect(ents.entitlements).to.deep.equal([]);
expect(await manager.getArticle()).to.deep.equal(
article,
'getArticle should return the article endpoint response'
);
});

it('should only include METERED_BY_GOOGLE client type if explicitly enabled', async () => {
expectGetSwgUserTokenToBeCalled();
fetcherMock
Expand Down
15 changes: 15 additions & 0 deletions src/runtime/entitlements-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ export class EntitlementsManager {

url = addDevModeParamsToUrl(this.win_.location, url);

url = addPreviewConfigIdToUrl(this.win_.location, url);

// Add encryption param.
if (params?.encryption) {
url = addQueryParam(url, 'crypt', params.encryption.encryptedDocumentKey);
Expand Down Expand Up @@ -969,6 +971,19 @@ function addDevModeParamsToUrl(location: Location, url: string): string {
return addQueryParam(url, 'devEnt', devModeScenario);
}

/**
* Parses preview config id params from the given hash fragment and adds it
* to the given URL.
*/
function addPreviewConfigIdToUrl(location: Location, url: string): string {
const hashParams = parseQueryString(location.hash);
const previewConfigIdRequested = hashParams['rrmPromptRequested'];
if (previewConfigIdRequested === undefined) {
return url;
}
return addQueryParam(url, 'previewConfigId', previewConfigIdRequested);
}

/**
* Convert String value of isReadyToPay
* (from JSON or Cache) to a boolean value.
Expand Down
Loading