You've already forked matrix-react-sdk
mirror of
https://github.com/matrix-org/matrix-react-sdk.git
synced 2025-07-28 15:22:05 +03:00
ModuleAPI: overwrite_login
action was not stopping the existing client resulting in the action failing with rust-sdk (#12272)
* Fix overwrite login action not stopping client * remove unneeded fixture for overwrite login test * Fix playwrite bad import of app sources * revert uneeded change on fore OnLoggedIn causing side effects * Add unit test for overwrite login action * remove un needed ts-ignore
This commit is contained in:
@ -32,6 +32,7 @@ import ToastStore from "../src/stores/ToastStore";
|
||||
import { OidcClientStore } from "../src/stores/oidc/OidcClientStore";
|
||||
import { makeDelegatedAuthConfig } from "./test-utils/oidc";
|
||||
import { persistOidcAuthenticatedSettings } from "../src/utils/oidc/persistOidcSettings";
|
||||
import { Action } from "../src/dispatcher/actions";
|
||||
|
||||
const webCrypto = new Crypto();
|
||||
|
||||
@ -823,4 +824,75 @@ describe("Lifecycle", () => {
|
||||
expect(oidcClientStore.revokeTokens).toHaveBeenCalledWith(accessToken, refreshToken);
|
||||
});
|
||||
});
|
||||
|
||||
describe("overwritelogin", () => {
|
||||
beforeEach(async () => {
|
||||
jest.spyOn(MatrixJs, "createClient").mockReturnValue(mockClient);
|
||||
});
|
||||
|
||||
it("should replace the current login with a new one", async () => {
|
||||
const stopSpy = jest.spyOn(mockClient, "stopClient").mockReturnValue(undefined);
|
||||
const dis = window.mxDispatcher;
|
||||
|
||||
const firstLoginEvent: Promise<void> = new Promise((resolve) => {
|
||||
dis.register(({ action }) => {
|
||||
if (action === Action.OnLoggedIn) {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
// set a logged in state
|
||||
await setLoggedIn(credentials);
|
||||
|
||||
await firstLoginEvent;
|
||||
|
||||
expect(stopSpy).toHaveBeenCalledTimes(1);
|
||||
// important the overwrite action should not call unset before replacing.
|
||||
// So spy on it and make sure it's not called.
|
||||
jest.spyOn(MatrixClientPeg, "unset").mockReturnValue(undefined);
|
||||
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId,
|
||||
}),
|
||||
undefined,
|
||||
);
|
||||
|
||||
const otherCredentials = {
|
||||
...credentials,
|
||||
userId: "@bob:server.org",
|
||||
deviceId: "def456",
|
||||
};
|
||||
|
||||
const secondLoginEvent: Promise<void> = new Promise((resolve) => {
|
||||
dis.register(({ action }) => {
|
||||
if (action === Action.OnLoggedIn) {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Trigger the overwrite login action
|
||||
dis.dispatch(
|
||||
{
|
||||
action: "overwrite_login",
|
||||
credentials: otherCredentials,
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
await secondLoginEvent;
|
||||
// the client should have been stopped
|
||||
expect(stopSpy).toHaveBeenCalledTimes(2);
|
||||
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId: otherCredentials.userId,
|
||||
}),
|
||||
undefined,
|
||||
);
|
||||
|
||||
expect(MatrixClientPeg.unset).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user