Use vitest for some EW unit tests (#33816)

* Use vitest for some EW unit tests

* Ensure library builds are done before unit tests

* Stabilise jest tests

* Move more tests over

* Make sonar happier

* Update types/node for happy-dom compat again

* Decrease max-workers to stabilise jest tests

* Split jest over 3 runners to alleviate memory woes

* Switch jest to runInBand

* Attempt to deflake jest tests

* tweak coverage

* tweak coverage
This commit is contained in:
Michael Telatynski
2026-06-15 13:24:33 +00:00
committed by GitHub
parent 7949980a7e
commit 2f4e6a4ec4
37 changed files with 475 additions and 326 deletions
@@ -1,114 +0,0 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2023 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import { type IdTokenClaims } from "oidc-client-ts";
import { decodeIdToken } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";
import {
getStoredOidcClientId,
getStoredOidcIdToken,
getStoredOidcIdTokenClaims,
getStoredOidcTokenIssuer,
persistOidcAuthenticatedSettings,
} from "../../../../src/utils/oidc/persistOidcSettings";
jest.mock("matrix-js-sdk/src/matrix");
describe("persist OIDC settings", () => {
jest.spyOn(Storage.prototype, "getItem");
jest.spyOn(Storage.prototype, "setItem");
beforeEach(() => {
localStorage.clear();
});
const clientId = "test-client-id";
const issuer = "https://auth.org/";
const idToken = "test-id-token";
const idTokenClaims: IdTokenClaims = {
// audience is this client
aud: "123",
// issuer matches
iss: issuer,
sub: "123",
exp: 123,
iat: 456,
};
describe("persistOidcAuthenticatedSettings", () => {
it("should set clientId and issuer in localStorage", () => {
persistOidcAuthenticatedSettings(clientId, issuer, idToken);
expect(localStorage.setItem).toHaveBeenCalledWith("mx_oidc_client_id", clientId);
expect(localStorage.setItem).toHaveBeenCalledWith("mx_oidc_token_issuer", issuer);
expect(localStorage.setItem).toHaveBeenCalledWith("mx_oidc_id_token", idToken);
});
it("should not set idToken in localStorage when idToken is undefined", () => {
persistOidcAuthenticatedSettings(clientId, issuer, undefined);
expect(localStorage.setItem).toHaveBeenCalledWith("mx_oidc_client_id", clientId);
expect(localStorage.setItem).toHaveBeenCalledWith("mx_oidc_token_issuer", issuer);
expect(localStorage.getItem("mx_oidc_id_token")).toBeFalsy();
});
});
describe("getStoredOidcTokenIssuer()", () => {
it("should return issuer from localStorage", () => {
localStorage.setItem("mx_oidc_token_issuer", issuer);
expect(getStoredOidcTokenIssuer()).toEqual(issuer);
expect(localStorage.getItem).toHaveBeenCalledWith("mx_oidc_token_issuer");
});
it("should return undefined when no issuer in localStorage", () => {
expect(getStoredOidcTokenIssuer()).toBeUndefined();
});
});
describe("getStoredOidcClientId()", () => {
it("should return clientId from localStorage", () => {
localStorage.setItem("mx_oidc_client_id", clientId);
expect(getStoredOidcClientId()).toEqual(clientId);
expect(localStorage.getItem).toHaveBeenCalledWith("mx_oidc_client_id");
});
it("should throw when no clientId in localStorage", () => {
expect(() => getStoredOidcClientId()).toThrow("Oidc client id not found in storage");
});
});
describe("getStoredOidcIdToken()", () => {
it("should return token from localStorage", () => {
localStorage.setItem("mx_oidc_id_token", idToken);
expect(getStoredOidcIdToken()).toEqual(idToken);
expect(localStorage.getItem).toHaveBeenCalledWith("mx_oidc_id_token");
});
it("should return undefined when no token in localStorage", () => {
expect(getStoredOidcIdToken()).toBeUndefined();
});
});
describe("getStoredOidcIdTokenClaims()", () => {
it("should return claims from localStorage", () => {
localStorage.setItem("mx_oidc_id_token_claims", JSON.stringify(idTokenClaims));
expect(getStoredOidcIdTokenClaims()).toEqual(idTokenClaims);
expect(localStorage.getItem).toHaveBeenCalledWith("mx_oidc_id_token_claims");
});
it("should return claims extracted from id_token in localStorage", () => {
localStorage.setItem("mx_oidc_id_token", idToken);
mocked(decodeIdToken).mockReturnValue(idTokenClaims);
expect(getStoredOidcIdTokenClaims()).toEqual(idTokenClaims);
expect(decodeIdToken).toHaveBeenCalledWith(idToken);
expect(localStorage.getItem).toHaveBeenCalledWith("mx_oidc_id_token_claims");
});
it("should return undefined when no claims in localStorage", () => {
expect(getStoredOidcIdTokenClaims()).toBeUndefined();
});
});
});
@@ -1,48 +0,0 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import { getManageDeviceUrl } from "../../../../src/utils/oidc/urls";
describe("OIDC urls", () => {
const accountManagementEndpoint = "https://auth.com/manage";
const deviceId = "DEVICEID1234";
describe("getManageDeviceUrl()", () => {
it("prefers stable action", async () => {
expect(
getManageDeviceUrl(
accountManagementEndpoint,
["org.matrix.session_view", "session_view", "org.matrix.device_view"],
deviceId,
),
).toEqual("https://auth.com/manage?action=org.matrix.device_view&device_id=DEVICEID1234");
});
it("defaults to stable action when no known action is supported", async () => {
expect(getManageDeviceUrl(accountManagementEndpoint, [], deviceId)).toEqual(
"https://auth.com/manage?action=org.matrix.device_view&device_id=DEVICEID1234",
);
expect(getManageDeviceUrl(accountManagementEndpoint, ["foo"], deviceId)).toEqual(
"https://auth.com/manage?action=org.matrix.device_view&device_id=DEVICEID1234",
);
});
it("defaults to backwards compatible action when no supported actions are provided", async () => {
expect(getManageDeviceUrl(accountManagementEndpoint, undefined, deviceId)).toEqual(
"https://auth.com/manage?action=org.matrix.session_view&device_id=DEVICEID1234",
);
});
it("uses unstable org.matrix.session_view", async () => {
expect(getManageDeviceUrl(accountManagementEndpoint, ["org.matrix.session_view"], deviceId)).toEqual(
"https://auth.com/manage?action=org.matrix.session_view&device_id=DEVICEID1234",
);
});
it("uses unstable session_view", async () => {
expect(getManageDeviceUrl(accountManagementEndpoint, ["session_view"], deviceId)).toEqual(
"https://auth.com/manage?action=session_view&device_id=DEVICEID1234",
);
});
});
});