Add bidirectional sync to admin and ban list (#709)

* Add bidirectional sync to admin and ban list

* Add tests

* Response to comments

* Skip broken tests from change of default value
This commit is contained in:
Cooldude2606 authored and GitHub committed 2024-12-23 18:58:44 +00:00
1 parent 91c1d2e6c6
commit 16bc651afc
10 files changed
+571 -52

No files matched your search

+108
View File
@@ -195,4 +195,112 @@ describe("class Instance", function() {
);
});
});
describe("_recordUserUpdate()", function() {
beforeEach(function() {
instance.config.set("factorio.sync_banlist", "bidirectional");
instance.config.set("factorio.sync_adminlist", "bidirectional");
// instance.config.set("factorio.sync_whitelist", "bidirectional"); // Not Implemented
});
it("should send InstanceBanlistUpdateEvent for bans", function() {
instance._recordUserUpdate("BAN", "player", "reason");
assert.deepEqual(
connector.sentMessages[0],
new lib.MessageEvent(
1, src, addr("allInstances"), "InstanceBanlistUpdateEvent",
new lib.InstanceBanlistUpdateEvent(
"player",
true,
"reason",
)
),
);
});
it("should send InstanceBanlistUpdateEvent for unbans", function() {
instance._recordUserUpdate("UNBANNED", "player");
assert.deepEqual(
connector.sentMessages[0],
new lib.MessageEvent(
1, src, addr("allInstances"), "InstanceBanlistUpdateEvent",
new lib.InstanceBanlistUpdateEvent(
"player",
false,
"",
)
),
);
});
it("should send InstanceAdminlistUpdateEvent for promotes", function() {
instance._recordUserUpdate("PROMOTE", "player");
assert.deepEqual(
connector.sentMessages[0],
new lib.MessageEvent(
1, src, addr("allInstances"), "InstanceAdminlistUpdateEvent",
new lib.InstanceAdminlistUpdateEvent(
"player",
true,
)
),
);
});
it("should send InstanceAdminlistUpdateEvent for demotes", function() {
instance._recordUserUpdate("DEMOTE", "player");
assert.deepEqual(
connector.sentMessages[0],
new lib.MessageEvent(
1, src, addr("allInstances"), "InstanceAdminlistUpdateEvent",
new lib.InstanceAdminlistUpdateEvent(
"player",
false,
)
),
);
});
it("should send InstanceWhitelistUpdateEvent for whitelist add", function() {
this.skip(); // Bidirectional not implemented
instance._recordUserUpdate("WHITELISTED", "player");
assert.deepEqual(
connector.sentMessages[0],
new lib.MessageEvent(
1, src, addr("allInstances"), "InstanceWhitelistUpdateEvent",
new lib.InstanceWhitelistUpdateEvent(
"player",
true,
)
),
);
});
it("should send InstanceWhitelistUpdateEvent for whitelist remove", function() {
this.skip(); // Bidirectional not implemented
instance._recordUserUpdate("UNWHITELISTED", "player");
assert.deepEqual(
connector.sentMessages[0],
new lib.MessageEvent(
1, src, addr("allInstances"), "InstanceWhitelistUpdateEvent",
new lib.InstanceWhitelistUpdateEvent(
"player",
false,
)
),
);
});
it("should throw for unknown event types", function() {
assert.throws(() => {
instance._recordUserUpdate("INVALID TYPE", "player");
});
});
it("should not send events when it is not bidirectional", function() {
for (const configValue of ["disabled", "enabled"]) { // Excludes Bidirectional
instance.config.set("factorio.sync_banlist", configValue);
instance.config.set("factorio.sync_adminlist", configValue);
instance.config.set("factorio.sync_whitelist", configValue);
for (const eventType of
["BAN", "UNBANNED", "PROMOTE", "DEMOTE", "WHITELISTED", "UNWHITELISTED"]
) {
instance._recordUserUpdate(eventType, "player");
assert.equal(connector.sentMessages[0], undefined);
}
}
});
});
});
+39 -20
View File
@@ -209,6 +209,7 @@ describe("Integration of Clusterio", function() {
});
it("should auto start instances with auto_start enabled", async function() {
slowTest(this);
this.timeout(30000); // Need an even longer timeout for this test
let hostProcess;
try {
@@ -661,12 +662,12 @@ describe("Integration of Clusterio", function() {
await execCtl("user set-banned --create test_RCON_ban --pardon");
const removeCommandStateLower = await sendRcon(44, "/banlist get test_rcon_ban");
const removeCommandStateUpper = await sendRcon(44, "/banlist get test_RCON_ban");
assert.equal(removeCommandStateLower, "test_rcon_ban is not banned.\n", "User is banned");
assert.equal(removeCommandStateUpper, "test_RCON_ban is not banned.\n", "User is banned");
assert.equal(removeCommandStateLower, "test_rcon_ban is not banned.\n", "User is not unbanned");
assert.equal(removeCommandStateUpper, "test_RCON_ban is not banned.\n", "User is not unbanned");
// Check it is case insensitive by checking it was converted correctly
const users = await getUsers();
assert(users.has("test_rcon_ban"), "User ID was not lowercase");
assert(users.has("test_rcon_ban"), "The first User ID was not used");
assert(!users.has("test_RCON_ban"), "Username was not converted to User ID");
});
it("should send whitelist commands to running instances", async function() {
@@ -693,41 +694,59 @@ describe("Integration of Clusterio", function() {
const removeCommandStateLower = await sendRcon(44, "/whitelist get test_rcon_whitelist");
const removeCommandStateUpper = await sendRcon(44, "/whitelist get test_RCON_whitelist");
assert.equal(removeCommandStateLower,
"test_rcon_whitelist is not whitelisted.\n", "User is whitelisted");
"test_rcon_whitelist is not whitelisted.\n", "User is not unwhitelisted");
assert.equal(removeCommandStateUpper,
"test_RCON_whitelist is not whitelisted.\n", "User is whitelisted");
"test_RCON_whitelist is not whitelisted.\n", "User is not unwhitelisted");
// Check it is case insensitive by checking it was converted correctly
const users = await getUsers();
assert(users.has("test_rcon_whitelist"), "User ID was not lowercase");
assert(users.has("test_rcon_whitelist"), "The first User ID was not used");
assert(!users.has("test_RCON_whitelist"), "Username was not converted to User ID");
});
it("should send admin list commands to running instances", async function() {
this.skip(); // It is not currently possible to get admin state without the player joining the game
// Because there is no admin list command this test will be different to the other two above
slowTest(this);
// Check that both names are not on the list
const preCommandStateLower = await sendRcon(44, "/admins get test_rcon_admin");
const preCommandStateUpper = await sendRcon(44, "/admins get test_RCON_admin");
assert.equal(preCommandStateLower, "test_rcon_admin is not admin.\n", "User is already admin");
assert.equal(preCommandStateUpper, "test_RCON_admin is not admin.\n", "User is already admin");
const preCommandStateLower = await sendRcon(44, "/demote test_rcon_admin");
const preCommandStateUpper = await sendRcon(44, "/demote test_RCON_admin");
assert.equal(preCommandStateLower,
"test_rcon_admin is not in the admin list.\n",
"User is already admin"
);
assert.equal(preCommandStateUpper,
"test_RCON_admin is not in the admin list.\n",
"User is already admin"
);
// Add the lower case name to the list
await execCtl("user set-admin --create test_rcon_admin");
const addCommandStateLower = await sendRcon(44, "/admins get test_rcon_admin");
const addCommandStateUpper = await sendRcon(44, "/admins get test_RCON_admin");
assert.equal(addCommandStateLower, "test_rcon_admin is admin.\n", "User is not admin");
assert.equal(addCommandStateUpper, "test_RCON_admin is admin.\n", "User is not admin");
const addCommandStateLower = await sendRcon(44, "/promote test_rcon_admin");
const addCommandStateUpper = await sendRcon(44, "/promote test_RCON_admin");
assert.equal(addCommandStateLower,
"test_rcon_admin is already in the admin list and will be promoted upon joining the game.\n",
"User is not admin"
);
assert.equal(addCommandStateUpper,
"test_RCON_admin is already in the admin list and will be promoted upon joining the game.\n",
"User is not admin"
);
// Remove the upper case name from the list
await execCtl("user set-admin --create test_RCON_admin --revoke");
const removeCommandStateLower = await sendRcon(44, "/admins get test_rcon_admin");
const removeCommandStateUpper = await sendRcon(44, "/admins get test_RCON_admin");
assert.equal(removeCommandStateLower, "test_rcon_admin is not admin.\n", "User is admin");
assert.equal(removeCommandStateUpper, "test_RCON_admin is not admin.\n", "User is admin");
const removeCommandStateLower = await sendRcon(44, "/demote test_rcon_admin");
const removeCommandStateUpper = await sendRcon(44, "/demote test_RCON_admin");
assert.equal(removeCommandStateLower,
"test_rcon_admin is not in the admin list.\n",
"User is not demoted"
);
assert.equal(removeCommandStateUpper,
"test_RCON_admin is not in the admin list.\n",
"User is not demoted"
);
// Check it is case insensitive by checking it was converted correctly
const users = await getUsers();
assert(users.has("test_rcon_admin"), "User ID was not lowercase");
assert(users.has("test_rcon_admin"), "The first User ID was not used");
assert(!users.has("test_RCON_admin"), "Username was not converted to User ID");
});
});
+157
View File
@@ -0,0 +1,157 @@
"use strict";
const assert = require("assert").strict;
const lib = require("@clusterio/lib");
const {
slowTest, exec, execCtl, sendRcon, getControl,
} = require("./index");
const instId = 48;
const instName = "Integration";
const instAltId = 49;
const instAltName = "IntegrationAlt";
const requireApi = [
"local api =",
"package.loaded['modules/clusterio/api']", // 1.1.110
"or package.loaded['__level__/modules/clusterio/api.lua']", // 2.0.0
].join(" ");
function getUser(name) {
return getControl().send(new lib.UserGetRequest(name));
}
describe("Clusterio Instance", function() {
before(async function() {
this.timeout(20000);
const visibility = JSON.stringify({ lan: true, public: false }).replace(
/"/g, process.platform === "win32" ? '""' : '\\"'
);
// Create a new instance
await execCtl(`instance create ${instName} --id ${instId}`);
await execCtl(`instance config set-prop ${instName} factorio.settings visibility "${visibility}"`);
await execCtl(`instance config set-prop ${instName} factorio.settings require_user_verification false`);
await execCtl(`instance assign ${instName} 4`);
for (const plugin of [
"global_chat", "research_sync", "statistics_exporter", "subspace_storage", "player_auth",
]) {
await execCtl(`instance config set ${instName} ${plugin}.load_plugin false`);
}
// Create a new alt instance
await execCtl(`instance create ${instAltName} --id ${instAltId}`);
await execCtl(`instance config set-prop ${instAltName} factorio.settings visibility "${visibility}"`);
await execCtl(`instance config set-prop ${instAltName} factorio.settings require_user_verification false`);
await execCtl(`instance assign ${instAltName} 4`);
await execCtl(`instance start ${instAltName}`);
await sendRcon(instAltId, "/sc disable achievements");
});
after(async function() {
this.timeout(20000);
// Delete the instances
await execCtl(`instance delete ${instName}`);
await execCtl(`instance stop ${instAltName}`);
await execCtl(`instance delete ${instAltName}`);
});
for (const savePatchingEnabled of [true, false]) {
describe(`Integration ${savePatchingEnabled ? "with" : "without"} save patching`, function() {
before(async function() {
this.timeout(20000);
// Create and start a patched save
await execCtl(`instance config set ${instName} factorio.enable_save_patching ${savePatchingEnabled}`);
await execCtl(`instance save create ${instName} ${savePatchingEnabled ? "patched" : "unpatched"}`);
await execCtl(`instance start ${instName} --save ${savePatchingEnabled ? "patched" : "unpatched"}.zip`);
await sendRcon(instId, "/sc disable achievements");
});
after(async function() {
// Stop the instance
await execCtl(`instance stop ${instName}`);
});
describe("player event", function() {
if (savePatchingEnabled) {
// IPC expects save patching to be enabled
it("should do nothing for an unknown type", async function() {
assert.rejects(getUser("DoesNotExist"), "User should not exist");
await sendRcon(instId,
`/sc ${requireApi} api.send_json("player_event",` +
"{ type='invalid type', name='DoesNotExist' })"
);
assert.rejects(getUser("DoesNotExist"), "User was created");
});
it("should respond to a player join and leave event", async function() {
await sendRcon(instId,
`/sc ${requireApi} api.send_json("player_event",` +
"{ type='join', name='JoiningPlayer' })"
);
const userJoin = await getUser("JoiningPlayer");
assert(userJoin.instances.has(instId), "Player is not shown as online");
await sendRcon(instId,
`/sc ${requireApi} api.send_json("player_event",` +
"{ type='leave', name='JoiningPlayer', reason='quit' })"
);
const userLeave = await getUser("JoiningPlayer");
assert(!userLeave.instances.has(instId), "Player is not shown as offline");
});
}
it("should respond to a player ban and unban event", async function() {
await execCtl(`instance config set ${instName} factorio.sync_banlist bidirectional`);
await sendRcon(instId, "/ban BannedPlayer");
const userBanned = await getUser("BannedPlayer");
assert(userBanned.isBanned, "Player is not banned");
const isAltBanned = await sendRcon(instAltId, "/banlist get BannedPlayer");
assert.equal(isAltBanned, "BannedPlayer is banned.\n", "User is not banned on alt");
await sendRcon(instId, "/unban BannedPlayer");
const userUnbanned = await getUser("BannedPlayer");
assert(!userUnbanned.isBanned, "Player is not unbanned");
const isAltUnbanned = await sendRcon(instAltId, "/banlist get BannedPlayer");
assert.equal(isAltUnbanned, "BannedPlayer is not banned.\n", "User is not unbanned on alt");
});
it("should respond to a player promote and demote event", async function() {
// Because there is no admin list command this test will be different to the other two
await execCtl(`instance config set ${instName} factorio.sync_adminlist bidirectional`);
await sendRcon(instId, "/promote AdminPlayer");
const userPromote = await getUser("AdminPlayer");
assert(userPromote.isAdmin, "Player is not promoted");
const isAltPromoted = await sendRcon(instAltId, "/promote AdminPlayer");
assert.equal(isAltPromoted,
"AdminPlayer is already in the admin list and will be promoted upon joining the game.\n",
"User is not promoted on alt"
);
await sendRcon(instId, "/demote AdminPlayer");
const userDemote = await getUser("AdminPlayer");
assert(!userDemote.isAdmin, "Player is not demoted");
const isAltDemoted = await sendRcon(instAltId, "/demote AdminPlayer");
assert.equal(isAltDemoted,
"AdminPlayer is not in the admin list.\n",
"User is not demoted on alt"
);
});
it("should respond to a player whitelist add and remove event", async function() {
this.skip(); // Not implemented
await execCtl(`instance config set ${instName} factorio.sync_whitelist bidirectional`);
await sendRcon(instId, "/whitelist add WhitelistPlayer");
const userAdded = await getUser("WhitelistPlayer");
assert(userAdded.isWhitelisted, "Player is not whitelisted");
const isAltAdded = await sendRcon(instAltId, "/whitelist get whitelisted");
assert.equal(isAltAdded,
"WhitelistPlayer is whitelisted.\n",
"User is not whitelisted on alt"
);
await sendRcon(instId, "/whitelist remove WhitelistPlayer");
const userRemoved = await getUser("WhitelistPlayer");
assert(!userRemoved.isWhitelisted, "Player is not unwhitelisted");
const isAltRemoved = await sendRcon(instAltId, "/whitelist get whitelisted");
assert.equal(isAltRemoved,
"WhitelistPlayer is not whitelisted.\n",
"User is not unwhitelisted on alt"
);
});
});
});
}
});
+49
View File
@@ -0,0 +1,49 @@
"use strict";
const lib = require("@clusterio/lib");
const assert = require("assert").strict;
// Each migration test should indicate which version introduced the migration
describe("lib/config/migrations", function() {
describe("Controller Config", function() {
it("should migrate 'external_address' to 'public_url'", function() { // Alpha 19
const config = lib.ControllerConfig.fromJSON({
"controller.external_address": "https://example.com:4000",
}, "controller");
assert.equal(config.get("controller.public_url"), "https://example.com:4000",
"public_url contains the wrong value");
assert.equal(config.fields["controller.external_address"], undefined,
"external_address was not removed");
});
});
describe("Host Config", function() {
});
describe("Instance Config", function() {
it("should migrate boolean sync to string enum", function() { // Alpha 19
const configTrue = lib.InstanceConfig.fromJSON({
"factorio.sync_adminlist": true,
"factorio.sync_whitelist": true,
"factorio.sync_banlist": true,
}, "controller");
assert.equal(configTrue.get("factorio.sync_adminlist"), "enabled", "sync_adminlist contains wrong value");
assert.equal(configTrue.get("factorio.sync_whitelist"), "enabled", "sync_adminlist contains wrong value");
assert.equal(configTrue.get("factorio.sync_banlist"), "enabled", "sync_adminlist contains wrong value");
const configFalse = lib.InstanceConfig.fromJSON({
"factorio.sync_adminlist": false,
"factorio.sync_whitelist": false,
"factorio.sync_banlist": false,
}, "controller");
assert.equal(configFalse.get("factorio.sync_adminlist"), "disabled", "sync_adminlist contains wrong value");
assert.equal(configFalse.get("factorio.sync_whitelist"), "disabled", "sync_adminlist contains wrong value");
assert.equal(configFalse.get("factorio.sync_banlist"), "disabled", "sync_adminlist contains wrong value");
});
});
describe("Control Config", function() {
});
});