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

[SDK-1649] Fix issue where cache was missed when scope parameter was provided #461

Merged
merged 2 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions __tests__/Auth0Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,33 @@ describe('Auth0Client', () => {
value: originalUserAgent
});
});

it('uses the cache for multiple token requests with audience and scope', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a general question; do we want to migrate tests out of index.test.ts into this file? I'm conscious that we're starting to develop a set of tests across two places for the same functions.

Perhaps we should dedicate some effort to unifying the tests somewhere to make them more discoverable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

const auth0 = setup();
await login(auth0);
jest.spyOn(<any>utils, 'runIframe').mockResolvedValue({
access_token: 'my_access_token',
state: 'MTIz'
});
mockFetch.mockResolvedValue(
fetchResponse(true, {
id_token: 'my_id_token',
refresh_token: 'my_refresh_token',
access_token: 'my_access_token',
expires_in: 86400
})
);
let access_token = await auth0.getTokenSilently({
audience: 'foo',
scope: 'bar'
});
expect(access_token).toEqual('my_access_token');
expect(utils.runIframe).toHaveBeenCalledTimes(1);
access_token = await auth0.getTokenSilently({
audience: 'foo',
scope: 'bar'
});
expect(access_token).toEqual('my_access_token');
expect(utils.runIframe).toHaveBeenCalledTimes(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would better here to reset the utils.runIframe mock for this second call, then use .not.toHaveBeenCalled for the assertion.

The way the test reads right now at first glance is that we're expecting runIframe to be called once after each call, whereas we're actually only expecting it to be called once in total.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah 👍 good idea

});
});
4 changes: 2 additions & 2 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,9 @@ export default class Auth0Client {
public async getTokenSilently(options: GetTokenSilentlyOptions = {}) {
const { ignoreCache, ...getTokenOptions } = {
audience: this.options.audience,
scope: getUniqueScopes(this.defaultScope, this.scope, options.scope),
ignoreCache: false,
...options
...options,
scope: getUniqueScopes(this.defaultScope, this.scope, options.scope)
};

try {
Expand Down