Skip to content

Commit

Permalink
Merge pull request #3532 from kristenwang/dev
Browse files Browse the repository at this point in the history
Add previewKey in article query param based on url param
  • Loading branch information
kristenwang authored Jul 3, 2024
2 parents c94f699 + f8efc78 commit b764e57
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/runtime/entitlements-manager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,61 @@ describes.realWin('EntitlementsManager', (env) => {
);
});

it('should use the article endpoint with preview key 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';
const previewKey = 'test_key';
win.location.hash = `#swg.debug=1&rrmPromptRequested=${configId}&rrmPreviewKey=${previewKey}`;
fetcherMock
.expects('fetch')
.withExactArgs(
`https://news.google.com/swg/_/api/v1/publication/pub1/article?previewConfigId=${configId}&previewKey=${previewKey}&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 @@ -778,6 +778,8 @@ export class EntitlementsManager {

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

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

// Add encryption param.
if (params?.encryption) {
url = addQueryParam(url, 'crypt', params.encryption.encryptedDocumentKey);
Expand Down Expand Up @@ -990,6 +992,19 @@ function addPreviewConfigIdToUrl(location: Location, url: string): string {
return addQueryParam(url, 'previewConfigId', previewConfigIdRequested);
}

/**
* Parses preview security key params from the given hash fragment and adds it
* to the given URL.
*/
function addPreviewKeyToUrl(location: Location, url: string): string {
const hashParams = parseQueryString(location.hash);
const previewKeyRequested = hashParams['rrmPreviewKey'];
if (previewKeyRequested === undefined) {
return url;
}
return addQueryParam(url, 'previewKey', previewKeyRequested);
}

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

0 comments on commit b764e57

Please sign in to comment.