Skip to content

Commit 2f1f397

Browse files
shaejazHaroenv
andauthoredDec 9, 2024··
fix(instantsearch.js): prevent authenticated token being set as the userToken (#6443)
* remove auth token * remove auth tests * clean vars * add tests * add test for init props * fix typo * remove immediate flag * fix assertion * add immediate flag --------- Co-authored-by: Haroen Viaene <hello@haroen.me>
1 parent a3f0e18 commit 2f1f397

File tree

3 files changed

+69
-200
lines changed

3 files changed

+69
-200
lines changed
 

‎packages/instantsearch.js/src/middlewares/__tests__/createInsightsMiddleware.ts

+29-67
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ describe('insights', () => {
927927

928928
describe('authenticatedUserToken', () => {
929929
describe('before `init`', () => {
930-
it('uses the `authenticatedUserToken` as the `userToken` when defined', () => {
930+
it('does not use `authenticatedUserToken` as the `userToken` when defined', () => {
931931
const { insightsClient, instantSearchInstance, getUserToken } =
932932
createTestEnvironment();
933933

@@ -937,10 +937,10 @@ describe('insights', () => {
937937
createInsightsMiddleware({ insightsClient })
938938
);
939939

940-
expect(getUserToken()).toEqual('abc');
940+
expect(getUserToken()).toEqual(expect.stringMatching(/^anonymous-/));
941941
});
942942

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

@@ -951,60 +951,60 @@ describe('insights', () => {
951951
createInsightsMiddleware({ insightsClient })
952952
);
953953

954-
expect(getUserToken()).toEqual('def');
954+
expect(getUserToken()).toEqual('abc');
955955
});
956956

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

961-
insightsClient('setUserToken', 'abc');
962961
insightsClient('setAuthenticatedUserToken', 'def');
963-
insightsClient('setAuthenticatedUserToken', undefined);
964962

965963
instantSearchInstance.use(
966964
createInsightsMiddleware({ insightsClient })
967965
);
968966

967+
insightsClient('setUserToken', 'abc');
968+
969969
expect(getUserToken()).toEqual('abc');
970970
});
971+
});
971972

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

976-
insightsClient('setAuthenticatedUserToken', 'def');
977-
978978
instantSearchInstance.use(
979-
createInsightsMiddleware({ insightsClient })
979+
createInsightsMiddleware({
980+
insightsClient,
981+
insightsInitParams: { authenticatedUserToken: 'abc' },
982+
})
980983
);
981984

982-
insightsClient('setUserToken', 'abc');
983-
984-
expect(getUserToken()).toEqual('def');
985+
expect(getUserToken()).toEqual(expect.stringMatching(/^anonymous-/));
985986
});
986987

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

991-
insightsClient('setUserToken', 'abc');
992-
insightsClient('setAuthenticatedUserToken', 'def');
993-
994992
instantSearchInstance.use(
995-
createInsightsMiddleware({ insightsClient })
993+
createInsightsMiddleware({
994+
insightsClient,
995+
insightsInitParams: {
996+
authenticatedUserToken: 'abc',
997+
userToken: 'def',
998+
},
999+
})
9961000
);
9971001

9981002
expect(getUserToken()).toEqual('def');
999-
1000-
insightsClient('setAuthenticatedUserToken', undefined);
1001-
1002-
expect(getUserToken()).toEqual('abc');
10031003
});
10041004
});
10051005

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

10161016
await wait(0);
10171017

1018-
expect(getUserToken()).toEqual('abc');
1019-
});
1020-
1021-
it('uses the `authenticatedUserToken` as the `userToken` when both are defined', async () => {
1022-
const { insightsClient, instantSearchInstance, getUserToken } =
1023-
createTestEnvironment();
1024-
instantSearchInstance.use(
1025-
createInsightsMiddleware({ insightsClient })
1026-
);
1027-
1028-
insightsClient('setUserToken', 'abc');
1029-
insightsClient('setAuthenticatedUserToken', 'def');
1030-
1031-
await wait(0);
1032-
1033-
expect(getUserToken()).toEqual('def');
1018+
expect(getUserToken()).toEqual(expect.stringMatching(/^anonymous-/));
10341019
});
10351020

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

10431028
insightsClient('setUserToken', 'abc');
10441029
insightsClient('setAuthenticatedUserToken', 'def');
1045-
insightsClient('setAuthenticatedUserToken', undefined);
10461030

10471031
await wait(0);
10481032

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

10531037
describe('from queue', () => {
1054-
it('uses the `authenticatedUserToken` as the `userToken` when defined', () => {
1038+
it('does not use `authenticatedUserToken` as the `userToken` when defined', () => {
10551039
const {
10561040
insightsClient,
10571041
libraryLoadedAndProcessQueue,
@@ -1069,10 +1053,10 @@ describe('insights', () => {
10691053
);
10701054
libraryLoadedAndProcessQueue();
10711055

1072-
expect(getUserToken()).toEqual('abc');
1056+
expect(getUserToken()).toEqual(expect.stringMatching(/^anonymous-/));
10731057
});
10741058

1075-
it('uses the `authenticatedUserToken` as the `userToken` when both are defined', () => {
1059+
it('does not use `authenticatedUserToken` as the `userToken` when both are defined', () => {
10761060
const {
10771061
insightsClient,
10781062
libraryLoadedAndProcessQueue,
@@ -1091,28 +1075,6 @@ describe('insights', () => {
10911075
);
10921076
libraryLoadedAndProcessQueue();
10931077

1094-
expect(getUserToken()).toEqual('def');
1095-
});
1096-
1097-
it('reverts to the `userToken` when unsetting the `authenticatedUserToken`', () => {
1098-
const {
1099-
insightsClient,
1100-
libraryLoadedAndProcessQueue,
1101-
instantSearchInstance,
1102-
getUserToken,
1103-
} = createUmdTestEnvironment();
1104-
1105-
insightsClient('setUserToken', 'abc');
1106-
insightsClient('setAuthenticatedUserToken', 'def');
1107-
insightsClient('setAuthenticatedUserToken', undefined);
1108-
1109-
instantSearchInstance.use(
1110-
createInsightsMiddleware({
1111-
insightsClient,
1112-
})
1113-
);
1114-
libraryLoadedAndProcessQueue();
1115-
11161078
expect(getUserToken()).toEqual('abc');
11171079
});
11181080
});

0 commit comments

Comments
 (0)
Please sign in to comment.