Revert deletion of hydrateSession (#29703)

* Revert deletion of hydrateSession

* remove line break to make prettier happy :-)

* add tests for hydrateSession on soft logout

* fix coding style

---------

Co-authored-by: Florian Duros <florianduros@element.io>
This commit is contained in:
Julien CLEMENT
2025-04-11 10:40:00 +02:00
committed by GitHub
parent d4f25e8e13
commit d594441b53
3 changed files with 78 additions and 8 deletions

View File

@@ -38,6 +38,12 @@ const webCrypto = new Crypto();
const windowCrypto = window.crypto;
describe("Lifecycle", () => {
const homeserverUrl = "https://domain";
const identityServerUrl = "https://is.org";
const userId = "@alice:domain";
const deviceId = "abc123";
const accessToken = "test-access-token";
let mockPlatform: MockedObject<BasePlatform>;
const realLocalStorage = global.localStorage;
@@ -53,7 +59,7 @@ describe("Lifecycle", () => {
removeAllListeners: jest.fn(),
clearStores: jest.fn(),
getAccountData: jest.fn(),
getDeviceId: jest.fn(),
getDeviceId: jest.fn().mockReturnValue(deviceId),
isVersionSupported: jest.fn().mockResolvedValue(true),
getCrypto: jest.fn(),
getClientWellKnown: jest.fn(),
@@ -156,11 +162,6 @@ describe("Lifecycle", () => {
});
};
const homeserverUrl = "https://server.org";
const identityServerUrl = "https://is.org";
const userId = "@alice:server.org";
const deviceId = "abc123";
const accessToken = "test-access-token";
const localStorageSession = {
mx_hs_url: homeserverUrl,
mx_is_url: identityServerUrl,
@@ -605,6 +606,38 @@ describe("Lifecycle", () => {
expect(MatrixClientPeg.start).toHaveBeenCalled();
});
describe("after a soft-logout", () => {
beforeEach(async () => {
await setLoggedIn(credentials);
localStorage.setItem("mx_soft_logout", "true");
});
it("should not clear the storage if device is the same", async () => {
await Lifecycle.hydrateSession(credentials);
expect(localStorage.removeItem).toHaveBeenCalledWith("mx_soft_logout");
expect(mockClient.getUserId).toHaveReturnedWith(userId);
expect(mockClient.getDeviceId).toHaveReturnedWith(deviceId);
expect(mockClient.clearStores).toHaveBeenCalledTimes(1);
});
it("should clear the storage if device is not the same", async () => {
const fakeCredentials = {
homeserverUrl,
identityServerUrl,
userId: "@bob:domain",
deviceId,
accessToken,
};
await Lifecycle.hydrateSession(fakeCredentials);
expect(localStorage.removeItem).toHaveBeenCalledWith("mx_soft_logout");
expect(mockClient.getUserId).toHaveReturnedWith(userId);
expect(mockClient.getDeviceId).toHaveReturnedWith(deviceId);
expect(mockClient.clearStores).toHaveBeenCalledTimes(2);
});
});
describe("without a pickle key", () => {
beforeEach(() => {
jest.spyOn(mockPlatform, "createPickleKey").mockResolvedValue(null);