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

fix(instantsearch.js): prevent authenticated token being set as the userToken #6443

Merged
merged 10 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ describe('insights', () => {

describe('authenticatedUserToken', () => {
describe('before `init`', () => {
it('uses the `authenticatedUserToken` as the `userToken` when defined', () => {
it('does not use `authenticatedUserToken` as the `userToken` when defined', () => {
const { insightsClient, instantSearchInstance, getUserToken } =
createTestEnvironment();

Expand All @@ -937,10 +937,10 @@ describe('insights', () => {
createInsightsMiddleware({ insightsClient })
);

expect(getUserToken()).toEqual('abc');
expect(getUserToken()).not.toEqual('abc');
shaejaz marked this conversation as resolved.
Show resolved Hide resolved
});

it('uses the `authenticatedUserToken` as the `userToken` when both are defined', () => {
it('does not use `authenticatedUserToken` as the `userToken` when both are defined', () => {
const { insightsClient, instantSearchInstance, getUserToken } =
createTestEnvironment();

Expand All @@ -951,60 +951,60 @@ describe('insights', () => {
createInsightsMiddleware({ insightsClient })
);

expect(getUserToken()).toEqual('def');
expect(getUserToken()).toEqual('abc');
});

it('reverts to the `userToken` when unsetting the `authenticatedUserToken`', () => {
it('does not use `authenticatedUserToken` when a `userToken` is set after', () => {
const { insightsClient, instantSearchInstance, getUserToken } =
createTestEnvironment();

insightsClient('setUserToken', 'abc');
insightsClient('setAuthenticatedUserToken', 'def');
insightsClient('setAuthenticatedUserToken', undefined);

instantSearchInstance.use(
createInsightsMiddleware({ insightsClient })
);

insightsClient('setUserToken', 'abc');

expect(getUserToken()).toEqual('abc');
});
});

it('uses the `authenticatedUserToken` when a `userToken` is set after', () => {
describe('from `init` props', () => {
it('does not use `authenticatedUserToken` as the `userToken` when defined', () => {
const { insightsClient, instantSearchInstance, getUserToken } =
createTestEnvironment();

insightsClient('setAuthenticatedUserToken', 'def');

instantSearchInstance.use(
createInsightsMiddleware({ insightsClient })
createInsightsMiddleware({
insightsClient,
insightsInitParams: { authenticatedUserToken: 'abc' },
})
);

insightsClient('setUserToken', 'abc');

expect(getUserToken()).toEqual('def');
expect(getUserToken()).not.toEqual('abc');
});

it('resets the token to the `userToken` when `authenticatedUserToken` is set as undefined', () => {
it('does not use `authenticatedUserToken` as the `userToken` when both are defined', () => {
const { insightsClient, instantSearchInstance, getUserToken } =
createTestEnvironment();

insightsClient('setUserToken', 'abc');
insightsClient('setAuthenticatedUserToken', 'def');

instantSearchInstance.use(
createInsightsMiddleware({ insightsClient })
createInsightsMiddleware({
insightsClient,
insightsInitParams: {
authenticatedUserToken: 'abc',
userToken: 'def',
},
})
);

expect(getUserToken()).toEqual('def');

insightsClient('setAuthenticatedUserToken', undefined);

expect(getUserToken()).toEqual('abc');
});
});

describe('after `init`', () => {
it('uses the `authenticatedUserToken` as the `userToken` when defined', async () => {
it('does not use `authenticatedUserToken` as the `userToken` when defined', async () => {
const { insightsClient, instantSearchInstance, getUserToken } =
createTestEnvironment();
instantSearchInstance.use(
Expand All @@ -1015,25 +1015,10 @@ describe('insights', () => {

await wait(0);

expect(getUserToken()).toEqual('abc');
});

it('uses the `authenticatedUserToken` as the `userToken` when both are defined', async () => {
const { insightsClient, instantSearchInstance, getUserToken } =
createTestEnvironment();
instantSearchInstance.use(
createInsightsMiddleware({ insightsClient })
);

insightsClient('setUserToken', 'abc');
insightsClient('setAuthenticatedUserToken', 'def');

await wait(0);

expect(getUserToken()).toEqual('def');
expect(getUserToken()).not.toEqual('abc');
});

it('reverts to the `userToken` when unsetting the `authenticatedUserToken`', async () => {
it('does not use `authenticatedUserToken` as the `userToken` when both are defined', async () => {
const { insightsClient, instantSearchInstance, getUserToken } =
createTestEnvironment();
instantSearchInstance.use(
Expand All @@ -1042,7 +1027,6 @@ describe('insights', () => {

insightsClient('setUserToken', 'abc');
insightsClient('setAuthenticatedUserToken', 'def');
insightsClient('setAuthenticatedUserToken', undefined);

await wait(0);

Expand All @@ -1051,7 +1035,7 @@ describe('insights', () => {
});

describe('from queue', () => {
it('uses the `authenticatedUserToken` as the `userToken` when defined', () => {
it('does not use `authenticatedUserToken` as the `userToken` when defined', () => {
const {
insightsClient,
libraryLoadedAndProcessQueue,
Expand All @@ -1069,10 +1053,10 @@ describe('insights', () => {
);
libraryLoadedAndProcessQueue();

expect(getUserToken()).toEqual('abc');
expect(getUserToken()).not.toEqual('abc');
});

it('uses the `authenticatedUserToken` as the `userToken` when both are defined', () => {
it('does not use `authenticatedUserToken` as the `userToken` when both are defined', () => {
const {
insightsClient,
libraryLoadedAndProcessQueue,
Expand All @@ -1091,28 +1075,6 @@ describe('insights', () => {
);
libraryLoadedAndProcessQueue();

expect(getUserToken()).toEqual('def');
});

it('reverts to the `userToken` when unsetting the `authenticatedUserToken`', () => {
const {
insightsClient,
libraryLoadedAndProcessQueue,
instantSearchInstance,
getUserToken,
} = createUmdTestEnvironment();

insightsClient('setUserToken', 'abc');
insightsClient('setAuthenticatedUserToken', 'def');
insightsClient('setAuthenticatedUserToken', undefined);

instantSearchInstance.use(
createInsightsMiddleware({
insightsClient,
})
);
libraryLoadedAndProcessQueue();

expect(getUserToken()).toEqual('abc');
});
});
Expand Down
Loading