2022-06-19 00:25:21 +00:00
|
|
|
import {expect} from "chai";
|
|
|
|
import sinon from "ts-sinon";
|
2018-03-10 11:54:51 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
import log from "../../server/log";
|
|
|
|
import Config from "../../server/config";
|
|
|
|
import TestUtil from "../util";
|
2018-03-10 11:54:51 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("mergeConfig", function () {
|
|
|
|
it("should mutate object", function () {
|
2018-03-10 11:54:51 +00:00
|
|
|
const config = {
|
|
|
|
ip: "default",
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any;
|
2018-03-10 11:54:51 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(config, {
|
2019-07-17 09:33:59 +00:00
|
|
|
ip: "overridden",
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any)
|
2019-07-17 09:33:59 +00:00
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
ip: "overridden",
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(config).to.deep.equal({
|
|
|
|
ip: "overridden",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should merge new properties", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
ip: "default",
|
|
|
|
newProp: "this should appear too",
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
ip: "overridden",
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
ip: "overridden",
|
|
|
|
newProp: "this should appear too",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should extend objects", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
tlsOptions: {},
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
tlsOptions: {
|
|
|
|
user: "test",
|
|
|
|
thing: 123,
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
tlsOptions: {
|
|
|
|
user: "test",
|
|
|
|
thing: 123,
|
|
|
|
},
|
|
|
|
});
|
2019-07-08 11:12:31 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should warn for unknown top level keys", function () {
|
2019-07-08 11:12:31 +00:00
|
|
|
let warning = "";
|
2022-06-19 00:25:21 +00:00
|
|
|
const warnStub = sinon
|
|
|
|
.stub(log, "warn")
|
|
|
|
.callsFake(TestUtil.sanitizeLog((str) => (warning += str)));
|
2019-07-08 11:12:31 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
optionOne: 123,
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
optionOne: 456,
|
|
|
|
optionTwo: 789,
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2019-07-08 11:12:31 +00:00
|
|
|
optionOne: 456,
|
|
|
|
optionTwo: 789,
|
|
|
|
});
|
2019-03-20 10:58:30 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
warnStub.restore();
|
2019-07-08 11:12:31 +00:00
|
|
|
expect(warning).to.equal('Unknown key "optionTwo", please verify your config.\n');
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should not warn for unknown second level keys", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
optionOne: {
|
|
|
|
subOne: 123,
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
optionOne: {
|
|
|
|
subOne: 123,
|
|
|
|
subTwo: 123,
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2019-07-08 11:12:31 +00:00
|
|
|
optionOne: {
|
|
|
|
subOne: 123,
|
|
|
|
subTwo: 123,
|
|
|
|
},
|
|
|
|
});
|
2018-03-10 11:54:51 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should allow changing nulls", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
oidentd: null,
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
oidentd: "some path",
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
oidentd: "some path",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should allow changing nulls with objects", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
webirc: null,
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
webirc: {
|
|
|
|
serverone: "password",
|
|
|
|
servertwo: "password2",
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2019-07-08 11:12:31 +00:00
|
|
|
webirc: {
|
|
|
|
serverone: "password",
|
|
|
|
servertwo: "password2",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should allow changing nulls with objects that has function", function () {
|
2019-07-08 11:12:31 +00:00
|
|
|
const callbackFunction = () => ({});
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
webirc: null,
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
webirc: {
|
|
|
|
servercb: callbackFunction,
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2019-07-08 11:12:31 +00:00
|
|
|
webirc: {
|
|
|
|
servercb: callbackFunction,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should keep new properties inside of objects", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
nestedOnce: {
|
|
|
|
ip: "default",
|
|
|
|
},
|
|
|
|
nestedTwice: {
|
|
|
|
thing: "default",
|
|
|
|
nested: {
|
|
|
|
otherThing: "also default",
|
|
|
|
newThing: "but also this",
|
|
|
|
},
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
nestedOnce: {},
|
|
|
|
nestedTwice: {
|
|
|
|
nested: {
|
|
|
|
otherThing: "overridden",
|
|
|
|
},
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
nestedOnce: {
|
|
|
|
ip: "default",
|
|
|
|
},
|
|
|
|
nestedTwice: {
|
|
|
|
thing: "default",
|
|
|
|
nested: {
|
|
|
|
otherThing: "overridden",
|
|
|
|
newThing: "but also this",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should not merge arrays", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
test: ["sqlite", "text"],
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
test: ["sqlite"],
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
test: ["sqlite"],
|
|
|
|
});
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
test: ["sqlite", "text"],
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
test: [],
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
test: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should change order in arrays", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
test: ["sqlite", "text"],
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
test: ["text", "sqlite"],
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
test: ["text", "sqlite"],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should only merge same type", function () {
|
2022-06-19 00:25:21 +00:00
|
|
|
const logWarnStub = sinon.stub(log, "warn");
|
2018-03-10 11:54:51 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
shouldBeObject: {
|
|
|
|
thing: "yes",
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
shouldBeObject: "bad type",
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
shouldBeObject: {
|
|
|
|
thing: "yes",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config._merge_config_objects(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
shouldBeString: "string",
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
shouldBeString: 1234567,
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
shouldBeString: "string",
|
|
|
|
});
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
logWarnStub.restore();
|
2018-03-10 11:54:51 +00:00
|
|
|
});
|
|
|
|
});
|