Refactor user and instance manager (#837)

* Refactor UserManager to use Datastore

* Rename lib.User to UserDetails also adds to/fromUserDetails

* Fix existing tests

* Add user class tests

* Add tests for finaliseUsers

* Move HostDetails to own file with tests

* Move InstanceDetails to its own file with tests

* Rename HostInfo and add tests

* Rename InstanceInfo and add tests

- Also changes the names of the host properties related to instances
- Also removes the need to specify the location of InstanceInfo

* Make json round trip test validate schema

* Add InstanceManager

* Fix lint errors

* Extra tests

* Add final test coverage

* Add one last test to InstanceManager

* Response to comments

* Use generator rather than return anonumous generator

Co-authored-by: Hornwitser <github@hornwitser.no>

* Fix lint issue with generator

---------

Co-authored-by: Hornwitser <github@hornwitser.no>
This commit is contained in:
Cooldude2606andHornwitser authored and GitHub committed 2026-03-01 23:04:37 +00:00
1 parent 5bea930d05
commit 2235294c59
60 files changed
+3371 -1227

No files matched your search

+15 -15
View File
@@ -1,36 +1,36 @@
"use strict";
const assert = require("assert").strict;
const { ControllerUser } = require("@clusterio/controller");
const lib = require("@clusterio/lib");
const { User } = require("@clusterio/controller");
describe("controller/src/ControllerUser", function() {
describe("class ControllerUser", function() {
describe("controller/src/User", function() {
describe("class User", function() {
const roles = new Map([
[1, new lib.Role(1, "a", "a role", new Set(["core.admin"]))],
[2, new lib.Role(2, "b", "b role", new Set(["user-test"]))],
]);
it("should round trip serialize", function() {
function test_roundtrip(serialized) {
let user = ControllerUser.fromJSON(serialized, roles);
let user_serialized = user.toJSON(true);
function testRoundTrip(serialized) {
let user = User.fromJSON(serialized, {}, roles);
let user_serialized = user.toJSON();
assert.deepEqual(user_serialized, serialized);
let user_deserialized = ControllerUser.fromJSON(user_serialized, roles);
let user_deserialized = User.fromJSON(user_serialized, {}, roles);
assert.deepEqual(user_deserialized, user);
}
test_roundtrip({ name: "admin", roles: [1] });
test_roundtrip({ name: "user", roles: [2], token_valid_after: 12345 });
test_roundtrip({ name: "user", is_admin: true, is_whitelisted: true });
test_roundtrip({ name: "user", is_banned: true, ban_reason: "Bad user" });
test_roundtrip({ name: "user", instance_stats: [[1, { join_count: 1 }]]});
testRoundTrip({ name: "admin", roles: [1] });
testRoundTrip({ name: "user", roles: [2], token_valid_after: 12345 });
testRoundTrip({ name: "user", is_admin: true, is_whitelisted: true });
testRoundTrip({ name: "user", is_banned: true, ban_reason: "Bad user" });
testRoundTrip({ name: "user", instance_stats: [[1, { join_count: 1 }]]});
});
describe(".checkPermission()", function() {
it("should correctly resolve permissions", function() {
lib.definePermission({ name: "user-test", title: "Test", description: "User Test" });
let a = ControllerUser.fromJSON({ name: "admin", roles: [1] }, roles);
let b = ControllerUser.fromJSON({ name: "user", roles: [2] }, roles);
let c = ControllerUser.fromJSON({ name: "null", roles: [] }, roles);
let a = User.fromJSON({ name: "admin", roles: [1] }, {}, roles);
let b = User.fromJSON({ name: "user", roles: [2] }, {}, roles);
let c = User.fromJSON({ name: "null", roles: [] }, {}, roles);
a.checkPermission("core.control.connect");
assert.throws(() => b.checkPermission("core.control.connect"), new Error("Permission denied"));