2022-10-19 23:00:53 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-10-19 23:00:53 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 16:57:16 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-10-19 23:00:53 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
2023-07-10 18:06:58 +03:00
|
|
|
import { MatrixWidgetType, Widget, WidgetKind } from "matrix-widget-api";
|
2022-10-19 23:00:53 +03:00
|
|
|
|
|
|
|
import { OIDCState, WidgetPermissionStore } from "../../../src/stores/widgets/WidgetPermissionStore";
|
|
|
|
import SettingsStore from "../../../src/settings/SettingsStore";
|
|
|
|
import { TestSdkContext } from "../../TestSdkContext";
|
|
|
|
import { SettingLevel } from "../../../src/settings/SettingLevel";
|
|
|
|
import { SdkContextClass } from "../../../src/contexts/SDKContext";
|
|
|
|
import { stubClient } from "../../test-utils";
|
2023-07-10 18:06:58 +03:00
|
|
|
import { StopGapWidgetDriver } from "../../../src/stores/widgets/StopGapWidgetDriver";
|
2022-10-19 23:00:53 +03:00
|
|
|
|
|
|
|
jest.mock("../../../src/settings/SettingsStore");
|
|
|
|
|
|
|
|
describe("WidgetPermissionStore", () => {
|
|
|
|
let widgetPermissionStore: WidgetPermissionStore;
|
|
|
|
let mockClient: MatrixClient;
|
|
|
|
const userId = "@alice:localhost";
|
|
|
|
const roomId = "!room:localhost";
|
|
|
|
const w = new Widget({
|
|
|
|
id: "wid",
|
|
|
|
creatorUserId: userId,
|
|
|
|
type: "m.custom",
|
|
|
|
url: "https://invalid.address.here",
|
|
|
|
});
|
2023-07-10 18:06:58 +03:00
|
|
|
const elementCallWidget = new Widget({
|
|
|
|
id: "group_call",
|
|
|
|
creatorUserId: "@alice:example.org",
|
|
|
|
type: MatrixWidgetType.Custom,
|
|
|
|
url: "https://call.element.io",
|
|
|
|
});
|
2023-02-13 14:39:16 +03:00
|
|
|
let settings: Record<string, any> = {}; // key value store
|
2022-10-19 23:00:53 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
settings = {}; // clear settings
|
|
|
|
mocked(SettingsStore.getValue).mockImplementation((setting: string) => {
|
|
|
|
return settings[setting];
|
|
|
|
});
|
|
|
|
mocked(SettingsStore.setValue).mockImplementation(
|
|
|
|
(settingName: string, roomId: string | null, level: SettingLevel, value: any): Promise<void> => {
|
|
|
|
// the store doesn't use any specific level or room ID (room IDs are packed into keys in `value`)
|
|
|
|
settings[settingName] = value;
|
|
|
|
return Promise.resolve();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
mockClient = stubClient();
|
|
|
|
const context = new TestSdkContext();
|
|
|
|
context.client = mockClient;
|
|
|
|
widgetPermissionStore = new WidgetPermissionStore(context);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should persist OIDCState.Allowed for a widget", () => {
|
2023-02-16 20:21:44 +03:00
|
|
|
widgetPermissionStore.setOIDCState(w, WidgetKind.Account, roomId, OIDCState.Allowed);
|
2022-10-19 23:00:53 +03:00
|
|
|
// check it remembered the value
|
2023-02-16 20:21:44 +03:00
|
|
|
expect(widgetPermissionStore.getOIDCState(w, WidgetKind.Account, roomId)).toEqual(OIDCState.Allowed);
|
2022-10-19 23:00:53 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should persist OIDCState.Denied for a widget", () => {
|
2023-02-16 20:21:44 +03:00
|
|
|
widgetPermissionStore.setOIDCState(w, WidgetKind.Account, roomId, OIDCState.Denied);
|
2022-10-19 23:00:53 +03:00
|
|
|
// check it remembered the value
|
2023-02-16 20:21:44 +03:00
|
|
|
expect(widgetPermissionStore.getOIDCState(w, WidgetKind.Account, roomId)).toEqual(OIDCState.Denied);
|
2022-10-19 23:00:53 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should update OIDCState for a widget", () => {
|
2023-02-16 20:21:44 +03:00
|
|
|
widgetPermissionStore.setOIDCState(w, WidgetKind.Account, roomId, OIDCState.Allowed);
|
|
|
|
widgetPermissionStore.setOIDCState(w, WidgetKind.Account, roomId, OIDCState.Denied);
|
2022-10-19 23:00:53 +03:00
|
|
|
// check it remembered the latest value
|
2023-02-16 20:21:44 +03:00
|
|
|
expect(widgetPermissionStore.getOIDCState(w, WidgetKind.Account, roomId)).toEqual(OIDCState.Denied);
|
2022-10-19 23:00:53 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should scope the location for a widget when setting OIDC state", () => {
|
|
|
|
// allow this widget for this room
|
|
|
|
widgetPermissionStore.setOIDCState(w, WidgetKind.Room, roomId, OIDCState.Allowed);
|
|
|
|
// check it remembered the value
|
|
|
|
expect(widgetPermissionStore.getOIDCState(w, WidgetKind.Room, roomId)).toEqual(OIDCState.Allowed);
|
|
|
|
// check this is not the case for the entire account
|
|
|
|
expect(widgetPermissionStore.getOIDCState(w, WidgetKind.Account, roomId)).toEqual(OIDCState.Unknown);
|
|
|
|
});
|
|
|
|
it("is created once in SdkContextClass", () => {
|
|
|
|
const context = new SdkContextClass();
|
|
|
|
const store = context.widgetPermissionStore;
|
|
|
|
expect(store).toBeDefined();
|
|
|
|
const store2 = context.widgetPermissionStore;
|
|
|
|
expect(store2).toStrictEqual(store);
|
|
|
|
});
|
2023-07-10 18:06:58 +03:00
|
|
|
it("auto-approves OIDC requests for element-call", async () => {
|
|
|
|
new StopGapWidgetDriver([], elementCallWidget, WidgetKind.Room, true, roomId);
|
|
|
|
expect(widgetPermissionStore.getOIDCState(elementCallWidget, WidgetKind.Room, roomId)).toEqual(
|
|
|
|
OIDCState.Allowed,
|
|
|
|
);
|
|
|
|
});
|
2022-10-19 23:00:53 +03:00
|
|
|
});
|