Skip to content

Commit

Permalink
fix more config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBrax committed Nov 15, 2023
1 parent fbcd10f commit b045400
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
18 changes: 16 additions & 2 deletions server/src/Core/Config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { debugLog } from "@/Helpers/Console";
import { GetRunningProcesses, execSimple } from "@/Helpers/Execute";
import { is_docker } from "@/Helpers/System";
import { isNumber } from "@/Helpers/Types";
import type { SettingField } from "@common/Config";
import { settingsFields } from "@common/ServerConfig";
import type { AxiosResponse } from "axios";
Expand Down Expand Up @@ -331,15 +332,28 @@ export class Config {
setting.stripslash &&
typeof newValue === "string"
) {
newValue = newValue.replace(/\/$/, "");
newValue = newValue.replace(/\/$/, "").replace(/\\$/, "");
}

if (setting.type === "number" && typeof newValue === "string") {
if (!isNumber(newValue)) {
throw new Error(
`Invalid value for setting '${key}': ${newValue}`
);
}
newValue = parseInt(newValue);
}

if (setting.type === "boolean" && typeof newValue === "string") {
newValue = newValue === "true" || newValue === "1";
if (newValue === "true" || newValue === "1") {
newValue = true;
} else if (newValue === "false" || newValue === "0") {
newValue = false;
} else {
throw new Error(
`Invalid value for setting '${key}': ${newValue}`
);
}
}

if (setting.type == "array") {
Expand Down
4 changes: 4 additions & 0 deletions server/src/Helpers/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ export function isKickChannel(
export function isError(compareData: unknown): compareData is Error {
return compareData instanceof Error;
}

export function isNumber(compareData: string): boolean {
return !isNaN(Number(compareData));
}
56 changes: 55 additions & 1 deletion server/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,65 @@ describe("Config", () => {
it("choice values", () => {
const config = Config.getCleanInstance();
config.config = {};

// object
config.setConfig("date_format", "dd-MM-yyyy");
expect(config.cfg("date_format")).toBe("dd-MM-yyyy");
expect(() =>
config.setConfig("date_format", "dd-mm-yyyy")
).toThrowError();
config.setConfig("date_format", "dd-MM-yyyy");
expect(config.cfg("date_format")).toBe("dd-MM-yyyy");

// array
config.setConfig("vod_container", "mkv");
expect(config.cfg("vod_container")).toBe("mkv");
expect(() => config.setConfig("vod_container", "asdf")).toThrowError();
expect(config.cfg("vod_container")).toBe("mkv");
});

it("number values", () => {
const config = Config.getCleanInstance();
config.config = {};

// number
config.setConfig("server_port", 1234);
expect(config.cfg("server_port")).toBe(1234);
expect(() => config.setConfig("server_port", "asdf")).toThrowError();
expect(config.cfg("server_port")).toBe(1234);

// cast to number
config.setConfig("server_port", "1234");
expect(config.cfg("server_port")).toBe(1234);
});

it("boolean values", () => {
const config = Config.getCleanInstance();
config.config = {};

// boolean
config.setConfig("trust_proxy", true);
expect(config.cfg("trust_proxy")).toBe(true);
expect(() => config.setConfig("trust_proxy", "asdf")).toThrowError();
expect(config.cfg("trust_proxy")).toBe(true);

// cast to boolean
config.setConfig("trust_proxy", "1");
expect(config.cfg("trust_proxy")).toBe(true);
config.setConfig("trust_proxy", "0");
expect(config.cfg("trust_proxy")).toBe(false);
});

it("stripslashes", () => {
const config = Config.getCleanInstance();
config.config = {};

// windows
config.setConfig("bin_dir", "C:\\Program Files\\ffmpeg\\bin\\");
expect(config.cfg("bin_dir")).toBe("C:\\Program Files\\ffmpeg\\bin");

// linux
config.setConfig("bin_dir", "/usr/bin/");
expect(config.cfg("bin_dir")).toBe("/usr/bin");
});

it("setting exists", () => {
Expand Down

0 comments on commit b045400

Please sign in to comment.