Harden Settings using mapped types (#28775)
* Harden Settings using mapped types Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Fix issues found during hardening Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Remove oidc native flow stale key Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
committed by
GitHub
parent
4e1bd69e4d
commit
1e42f28a69
@@ -13,10 +13,11 @@ import SdkConfig from "../../../src/SdkConfig";
|
||||
import { SettingLevel } from "../../../src/settings/SettingLevel";
|
||||
import SettingsStore from "../../../src/settings/SettingsStore";
|
||||
import { mkStubRoom, mockPlatformPeg, stubClient } from "../../test-utils";
|
||||
import { SettingKey } from "../../../src/settings/Settings.tsx";
|
||||
|
||||
const TEST_DATA = [
|
||||
{
|
||||
name: "Electron.showTrayIcon",
|
||||
name: "Electron.showTrayIcon" as SettingKey,
|
||||
level: SettingLevel.PLATFORM,
|
||||
value: true,
|
||||
},
|
||||
|
||||
@@ -9,6 +9,7 @@ Please see LICENSE files in the repository root for full details.
|
||||
import IncompatibleController from "../../../../src/settings/controllers/IncompatibleController";
|
||||
import { SettingLevel } from "../../../../src/settings/SettingLevel";
|
||||
import SettingsStore from "../../../../src/settings/SettingsStore";
|
||||
import { FeatureSettingKey } from "../../../../src/settings/Settings.tsx";
|
||||
|
||||
describe("IncompatibleController", () => {
|
||||
const settingsGetValueSpy = jest.spyOn(SettingsStore, "getValue");
|
||||
@@ -20,7 +21,7 @@ describe("IncompatibleController", () => {
|
||||
describe("when incompatibleValue is not set", () => {
|
||||
it("returns true when setting value is true", () => {
|
||||
// no incompatible value set, defaulted to true
|
||||
const controller = new IncompatibleController("feature_spotlight", { key: null });
|
||||
const controller = new IncompatibleController("feature_spotlight" as FeatureSettingKey, { key: null });
|
||||
settingsGetValueSpy.mockReturnValue(true);
|
||||
// true === true
|
||||
expect(controller.incompatibleSetting).toBe(true);
|
||||
@@ -30,7 +31,7 @@ describe("IncompatibleController", () => {
|
||||
|
||||
it("returns false when setting value is not true", () => {
|
||||
// no incompatible value set, defaulted to true
|
||||
const controller = new IncompatibleController("feature_spotlight", { key: null });
|
||||
const controller = new IncompatibleController("feature_spotlight" as FeatureSettingKey, { key: null });
|
||||
settingsGetValueSpy.mockReturnValue("test");
|
||||
expect(controller.incompatibleSetting).toBe(false);
|
||||
});
|
||||
@@ -38,13 +39,21 @@ describe("IncompatibleController", () => {
|
||||
|
||||
describe("when incompatibleValue is set to a value", () => {
|
||||
it("returns true when setting value matches incompatible value", () => {
|
||||
const controller = new IncompatibleController("feature_spotlight", { key: null }, "test");
|
||||
const controller = new IncompatibleController(
|
||||
"feature_spotlight" as FeatureSettingKey,
|
||||
{ key: null },
|
||||
"test",
|
||||
);
|
||||
settingsGetValueSpy.mockReturnValue("test");
|
||||
expect(controller.incompatibleSetting).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false when setting value is not true", () => {
|
||||
const controller = new IncompatibleController("feature_spotlight", { key: null }, "test");
|
||||
const controller = new IncompatibleController(
|
||||
"feature_spotlight" as FeatureSettingKey,
|
||||
{ key: null },
|
||||
"test",
|
||||
);
|
||||
settingsGetValueSpy.mockReturnValue("not test");
|
||||
expect(controller.incompatibleSetting).toBe(false);
|
||||
});
|
||||
@@ -53,7 +62,11 @@ describe("IncompatibleController", () => {
|
||||
describe("when incompatibleValue is set to a function", () => {
|
||||
it("returns result from incompatibleValue function", () => {
|
||||
const incompatibleValueFn = jest.fn().mockReturnValue(false);
|
||||
const controller = new IncompatibleController("feature_spotlight", { key: null }, incompatibleValueFn);
|
||||
const controller = new IncompatibleController(
|
||||
"feature_spotlight" as FeatureSettingKey,
|
||||
{ key: null },
|
||||
incompatibleValueFn,
|
||||
);
|
||||
settingsGetValueSpy.mockReturnValue("test");
|
||||
expect(controller.incompatibleSetting).toBe(false);
|
||||
expect(incompatibleValueFn).toHaveBeenCalledWith("test");
|
||||
@@ -64,7 +77,7 @@ describe("IncompatibleController", () => {
|
||||
describe("getValueOverride()", () => {
|
||||
it("returns forced value when setting is incompatible", () => {
|
||||
settingsGetValueSpy.mockReturnValue(true);
|
||||
const controller = new IncompatibleController("feature_spotlight", { key: null });
|
||||
const controller = new IncompatibleController("feature_spotlight" as FeatureSettingKey, { key: null });
|
||||
expect(
|
||||
controller.getValueOverride(SettingLevel.ACCOUNT, "$room:server", true, SettingLevel.ACCOUNT),
|
||||
).toEqual({ key: null });
|
||||
@@ -72,7 +85,7 @@ describe("IncompatibleController", () => {
|
||||
|
||||
it("returns null when setting is not incompatible", () => {
|
||||
settingsGetValueSpy.mockReturnValue(false);
|
||||
const controller = new IncompatibleController("feature_spotlight", { key: null });
|
||||
const controller = new IncompatibleController("feature_spotlight" as FeatureSettingKey, { key: null });
|
||||
expect(
|
||||
controller.getValueOverride(SettingLevel.ACCOUNT, "$room:server", true, SettingLevel.ACCOUNT),
|
||||
).toEqual(null);
|
||||
|
||||
@@ -11,7 +11,7 @@ import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import ServerSupportUnstableFeatureController from "../../../../src/settings/controllers/ServerSupportUnstableFeatureController";
|
||||
import { SettingLevel } from "../../../../src/settings/SettingLevel";
|
||||
import { LabGroup, SETTINGS } from "../../../../src/settings/Settings";
|
||||
import { FeatureSettingKey, LabGroup, SETTINGS } from "../../../../src/settings/Settings";
|
||||
import { stubClient } from "../../../test-utils";
|
||||
import { WatchManager } from "../../../../src/settings/WatchManager";
|
||||
import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController";
|
||||
@@ -19,7 +19,7 @@ import { TranslationKey } from "../../../../src/languageHandler";
|
||||
|
||||
describe("ServerSupportUnstableFeatureController", () => {
|
||||
const watchers = new WatchManager();
|
||||
const setting = "setting_name";
|
||||
const setting = "setting_name" as FeatureSettingKey;
|
||||
|
||||
async function prepareSetting(
|
||||
cli: MatrixClient,
|
||||
|
||||
@@ -17,7 +17,7 @@ describe("SystemFontController", () => {
|
||||
it("dispatches a system font update action on change", () => {
|
||||
const controller = new SystemFontController();
|
||||
|
||||
const getValueSpy = jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName) => {
|
||||
const getValueSpy = jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName): any => {
|
||||
if (settingName === "useBundledEmojiFont") return false;
|
||||
if (settingName === "useSystemFont") return true;
|
||||
if (settingName === "systemFont") return "Comic Sans MS";
|
||||
|
||||
@@ -9,6 +9,7 @@ Please see LICENSE files in the repository root for full details.
|
||||
import SettingsStore from "../../../../src/settings/SettingsStore";
|
||||
import ThemeWatcher from "../../../../src/settings/watchers/ThemeWatcher";
|
||||
import { SettingLevel } from "../../../../src/settings/SettingLevel";
|
||||
import { SettingKey, Settings } from "../../../../src/settings/Settings.tsx";
|
||||
|
||||
function makeMatchMedia(values: any) {
|
||||
class FakeMediaQueryList {
|
||||
@@ -33,8 +34,12 @@ function makeMatchMedia(values: any) {
|
||||
};
|
||||
}
|
||||
|
||||
function makeGetValue(values: any) {
|
||||
return function getValue<T = any>(settingName: string, _roomId: string | null = null, _excludeDefault = false): T {
|
||||
function makeGetValue(values: any): any {
|
||||
return function getValue<S extends SettingKey>(
|
||||
settingName: S,
|
||||
_roomId: string | null = null,
|
||||
_excludeDefault = false,
|
||||
): Settings[S] {
|
||||
return values[settingName];
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user