Merge branch 'master' into feature/2.0-scenarios

This commit is contained in:
Cooldude2606 authored and GitHub committed 2024-11-22 13:36:33 +00:00
commit e4f0ab1e8f
27 files changed
+285 -59

No files matched your search

+65 -3
View File
@@ -1,4 +1,5 @@
"use strict";
const fs = require("fs-extra");
const assert = require("assert").strict;
const { PlayerStats } = require("@clusterio/lib");
const { ControllerUser, UserManager } = require("@clusterio/controller");
@@ -8,7 +9,7 @@ describe("controller/src/UserManager", function() {
const userManager = new UserManager({});
it("should track online users", function() {
let user = ControllerUser.fromJSON({ name: "admin", roles: [1] });
userManager.users.set(user.name, user);
userManager.users.set(user.id, user);
assert(!userManager.onlineUsers.has(user));
assert.deepEqual(user.instances, new Set());
@@ -36,8 +37,8 @@ describe("controller/src/UserManager", function() {
it("should remove the instance stats from existing users", function() {
let user1 = ControllerUser.fromJSON({ name: "admin", roles: [1] });
let user2 = ControllerUser.fromJSON({ name: "player", roles: [1] });
userManager.users.set(user1.name, user1);
userManager.users.set(user2.name, user2);
userManager.users.set(user1.id, user1);
userManager.users.set(user2.id, user2);
user1.instanceStats.set(10, new PlayerStats({ join_count: 1 }));
user1.instanceStats.set(11, new PlayerStats({ join_count: 6 }));
user1.recalculatePlayerStats();
@@ -55,5 +56,66 @@ describe("controller/src/UserManager", function() {
assert.equal(user2.playerStats.joinCount, 3);
});
});
describe(".load()", function() {
it("should merge users on load", async function() {
// Create a user db file containing duplicate user ids
const names = ["user1", "User1", "user3"];
names.forEach(name => {
const user = ControllerUser.fromJSON({ name: name, roles: [1] });
userManager.users.set(user.name, user);
});
// Setup the player stats
const user1Before = userManager.users.get("user1");
user1Before.instanceStats.set(9, new PlayerStats({ join_count: 3 }));
user1Before.instanceStats.set(10, new PlayerStats({ join_count: 5 }));
user1Before.banReason = "ban reason here";
user1Before.isWhitelisted = true;
user1Before.isBanned = true;
user1Before.isAdmin = true;
user1Before.updatedAtMs = 100;
const user2Before = userManager.users.get("User1");
user2Before.instanceStats.set(10, new PlayerStats({ join_count: 7 }));
user2Before.instanceStats.set(11, new PlayerStats({ join_count: 9 }));
user2Before.isWhitelisted = false;
user2Before.isBanned = false;
user2Before.isAdmin = false;
user1Before.updatedAtMs = 200;
const user3Before = userManager.users.get("user3");
user3Before.instanceStats.set(10, new PlayerStats({ join_count: 11 }));
user3Before.instanceStats.set(11, new PlayerStats({ join_count: 13 }));
user3Before.isWhitelisted = false;
user3Before.isAdmin = true;
// Save and reload the user data
await fs.emptyDir("./temp/test/user_manager");
await userManager.save("./temp/test/user_manager/load.json");
names.map(name => userManager.users.delete(name));
await userManager.load("./temp/test/user_manager/load.json");
// Check it was loaded correctly
const user1After = userManager.users.get("user1");
const user2After = userManager.users.get("User1");
const user3After = userManager.users.get("user3");
assert.notEqual(user1After, undefined, "'user1' was not loaded");
assert.equal(user2After, undefined, "'User1' was loaded");
assert.notEqual(user3After, undefined, "'user3' was not loaded");
assert.equal(user1After.isAdmin, false, "User 1 is admin");
assert.equal(user1After.isBanned, true, "User 1 is not banned");
assert.equal(user1After.isWhitelisted, true, "User 1 is not whitelisted");
assert.equal(user1After.banReason, "ban reason here", "User 1 ban reason is wrong");
assert.equal(user1After.instanceStats.get(9)?.joinCount, 3, "User 1 instance 9 stats are wrong");
assert.equal(user1After.instanceStats.get(10)?.joinCount, 12, "User 1 instance 10 stats are wrong");
assert.equal(user1After.instanceStats.get(11)?.joinCount, 9, "User 1 instance 11 stats are wrong");
assert.equal(user3After.isAdmin, true, "User 3 is not admin");
assert.equal(user3After.isWhitelisted, false, "User 3 is whitelisted");
assert.equal(user3After.instanceStats.get(10)?.joinCount, 11, "User 3 instance 10 stats are wrong");
assert.equal(user3After.instanceStats.get(11)?.joinCount, 13, "User 3 instance 11 stats are wrong");
});
});
});
});
+1 -1
View File
@@ -104,7 +104,7 @@ describe("controller/src/routes", function() {
data: "totally a zip file",
});
assert.equal(response.statusCode, 401);
controller.userManager.users.get("test").tokenValidAfter = Math.floor((Date.now() + 60e3) / 1000);
controller.userManager.getByName("test").tokenValidAfter = Math.floor((Date.now() + 60e3) / 1000);
response = await phin({
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=file.zip`, method: "POST",
headers: {
+44 -7
View File
@@ -13,7 +13,7 @@ const { wait } = lib;
const testStrings = require("../lib/factorio/test_strings");
const {
TestControl, TestControlConnector, url, controlToken, slowTest,
exec, execCtl, sendRcon, getControl, spawn, instancesDir, factorioDir,
exec, execCtl, sendRcon, getControl, spawnNode, instancesDir, factorioDir,
} = require("./index");
@@ -29,7 +29,7 @@ async function checkInstanceStatus(id, status) {
}
async function spawnAltHost(config = "alt-host-config.json") {
return await spawn("alt-host:", `node ../../packages/host run --config ${config}`, /Started host/);
return await spawnNode("alt-host:", `../../packages/host run --config ${config}`, /Started host/);
}
async function startAltHost() {
@@ -97,7 +97,7 @@ async function deleteSave(instanceId, save) {
async function getUsers() {
let users = await getControl().send(new lib.UserListRequest());
return new Map(users.map(user => [user.name, user]));
return new Map(users.map(user => [user.id, user]));
}
function jsonArg(value) {
@@ -442,6 +442,10 @@ describe("Integration of Clusterio", function() {
});
describe("instance send-rcon", function() {
this.afterAll(async function() {
// Prevents cascading failure where enable_script_commands is expected to be true
await execCtl("instance config set test factorio.enable_script_commands true");
});
it("sends the command", async function() {
slowTest(this);
await execCtl("instance send-rcon test technobabble");
@@ -465,6 +469,24 @@ describe("Integration of Clusterio", function() {
}
assert(received, "InstanceSaveDetailsUpdatesEvent not sent");
});
it("should prevent execution of script commands when disabled", async function() {
slowTest(this);
await execCtl("instance send-rcon test /c");
const r1 = await getControl().send(
new lib.LogQueryRequest(false, false, [], [44], undefined, 10, "desc")
);
assert(r1.log.some(info => /\[COMMAND\]/.test(info.message)), "Command was not sent");
await execCtl("instance config set test factorio.enable_script_commands false");
assert.rejects(
execCtl("instance send-rcon test /c"),
new Error(
"Attempted to use script command while disabled. " +
"See config factorio.enable_script_commands.\nCommand: /c"
)
);
await execCtl("instance config set test factorio.enable_script_commands true");
});
});
describe("instance config set-prop", function() {
@@ -641,6 +663,11 @@ describe("Integration of Clusterio", function() {
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");
// 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"), "Username was not converted to User ID");
});
it("should send whitelist commands to running instances", async function() {
slowTest(this);
@@ -669,6 +696,11 @@ describe("Integration of Clusterio", function() {
"test_rcon_whitelist is not whitelisted.\n", "User is whitelisted");
assert.equal(removeCommandStateUpper,
"test_RCON_whitelist is not whitelisted.\n", "User is whitelisted");
// 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"), "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
@@ -692,6 +724,11 @@ describe("Integration of Clusterio", function() {
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");
// 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"), "Username was not converted to User ID");
});
});
@@ -1256,10 +1293,10 @@ describe("Integration of Clusterio", function() {
getControl().userUpdates = [];
await execCtl("user create temp");
let users = await getControl().send(new lib.UserListRequest());
let tempUser = users.find(user => user.name === "temp");
let tempUser = users.find(user => user.id === "temp");
assert(tempUser, "user was not created");
assert.equal(getControl().userUpdates.length, 1);
assert.equal(getControl().userUpdates[0].name, "temp");
assert.equal(getControl().userUpdates[0].id, "temp");
});
});
@@ -1288,7 +1325,7 @@ describe("Integration of Clusterio", function() {
getControl().userUpdates = [];
await execCtl('user set-roles temp "Cluster Admin"');
let users = await getControl().send(new lib.UserListRequest());
let tempUser = users.find(user => user.name === "temp");
let tempUser = users.find(user => user.id === "temp");
assert.deepEqual(tempUser.roleIds, new Set([0]));
assert.equal(getControl().userUpdates.length, 1);
});
@@ -1324,7 +1361,7 @@ describe("Integration of Clusterio", function() {
getControl().userUpdates = [];
await execCtl("user delete temp");
let users = await getControl().send(new lib.UserListRequest());
let tempUser = users.find(user => user.name === "temp");
let tempUser = users.find(user => user.id === "temp");
assert(!tempUser, "user was note deleted");
assert.equal(getControl().userUpdates.length, 1);
assert.equal(getControl().userUpdates[0].isDeleted, true);
+8 -3
View File
@@ -179,7 +179,7 @@ async function exec(command, options = {}) {
}
async function execCtl(...args) {
args[0] = `node ../../packages/ctl ${args[0]}`;
args[0] = `node --enable-source-maps ../../packages/ctl ${args[0]}`;
return await exec(...args);
}
@@ -221,6 +221,10 @@ function spawn(name, cmd, waitFor) {
});
}
async function spawnNode(name, cmd, waitFor) {
return await spawn(name, `node --enable-source-maps ${cmd}`, waitFor);
}
before(async function() {
this.timeout(40000);
@@ -302,7 +306,7 @@ before(async function() {
await exec("node ../../packages/controller bootstrap create-ctl-config test");
await exec("node ../../packages/ctl control-config set control.tls_ca ../../test/file/tls/cert.pem");
controllerProcess = await spawn("controller:", "node ../../packages/controller run", /Started controller/);
controllerProcess = await spawnNode("controller:", "../../packages/controller run", /Started controller/);
await execCtl("host create-config --id 4 --name host --generate-token");
@@ -310,7 +314,7 @@ before(async function() {
await exec(`node ../../packages/host config set host.factorio_directory ${relativeFactorioDir}`);
await exec("node ../../packages/host config set host.tls_ca ../../test/file/tls/cert.pem");
hostProcess = await spawn("host:", "node ../../packages/host run", /Started host/);
hostProcess = await spawnNode("host:", "../../packages/host run", /Started host/);
let tlsCa = await fs.readFile("test/file/tls/cert.pem");
let controlConnector = new TestControlConnector(url, 2, tlsCa);
@@ -366,6 +370,7 @@ module.exports = {
sendRcon,
getControl,
spawn,
spawnNode,
url,
controlToken,
+1 -1
View File
@@ -53,7 +53,7 @@ function connectInstance(controller, host, instanceId) {
function connectControl(controller, controlId) {
const [controllerSide, controlSide] = lib.VirtualConnector.makePair(addr("controller"), addr({ controlId }));
const registerData = new lib.RegisterControlData("", "0.0.0");
let user = controller.userManager.users.get("test");
let user = controller.userManager.getByName("test");
if (!user) {
user = controller.userManager.createUser("test");
}
+1
View File
@@ -129,6 +129,7 @@ class MockInstance extends lib.Link {
this.mockConfigEntries = new Map([
["instance.id", 7357],
["factorio.enable_save_patching", true],
["factorio.enable_script_commands", true],
]);
this.config = {
get: (name) => {