Feature: let the web console move a host to another group
The Go server now keeps a per-device group override (device_groups.json) edited from the card's "..." menu below Edit remark. The override wins over the group the host reported (or the offline history record) in the device list, the group filter, get_groups and the MCP host tools, so offline records can be regrouped and the change survives restarts. An empty value clears the override; names are capped at 23 characters, matching the Win32 regroup dialog. The command is admin-only and honours the device ACL; it refuses IDs the server has never seen (online or in history) so a stale card cannot create an orphan entry, and the remark handler gets the same guard. Each change is audited as admin.device.group, deleting a device drops its override, and values loaded from disk are trimmed and capped like remarks. The menu entry is gated on the groupEdit capability, so the shared page hides it on the MFC server. Like remarks, the override is a Go-side label: no CMD_SET_GROUP is sent, so the host keeps reporting its own group to other masters. Verified with go test ./..., go vet, gofmt and a node --check pass on the page. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
5f764b2425
commit
034b11fa6a
16 files changed
+895
-54
No files matched your search
@@ -0,0 +1,230 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// TestDeviceGroupsSetPersistClear covers the store: sanitize, save, reload,
|
||||
// clear, the rune cap and the corrupt-file write protection.
|
||||
func TestDeviceGroupsSetPersistClear(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "device_groups.json")
|
||||
groups := NewDeviceGroups(path)
|
||||
|
||||
if err := groups.Set("dev-1", " Office\nWest\t", "admin"); err != nil {
|
||||
t.Fatalf("Set: %v", err)
|
||||
}
|
||||
if got := groups.Group("dev-1"); got != "Office West" {
|
||||
t.Fatalf("Group = %q, want %q", got, "Office West")
|
||||
}
|
||||
|
||||
reloaded := NewDeviceGroups(path)
|
||||
if err := reloaded.Load(); err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if got := reloaded.Group("dev-1"); got != "Office West" {
|
||||
t.Fatalf("reloaded Group = %q, want %q", got, "Office West")
|
||||
}
|
||||
|
||||
// The cap matches the Win32 regroup dialog (< 24 characters).
|
||||
if err := groups.Set("dev-1", strings.Repeat("x", MaxGroupRunes+1), "admin"); err == nil {
|
||||
t.Fatal("Set accepted an over-long group")
|
||||
}
|
||||
if err := groups.Set("dev-1", strings.Repeat("y", MaxGroupRunes), "admin"); err != nil {
|
||||
t.Fatalf("Set at the limit: %v", err)
|
||||
}
|
||||
|
||||
// An empty group clears the override.
|
||||
if err := groups.Set("dev-1", " ", "admin"); err != nil {
|
||||
t.Fatalf("clear: %v", err)
|
||||
}
|
||||
if got := groups.Group("dev-1"); got != "" {
|
||||
t.Fatalf("Group after clear = %q, want empty", got)
|
||||
}
|
||||
|
||||
// Groups() is the bulk snapshot callers take per listing; it must be a copy.
|
||||
if err := groups.Set("dev-2", "Bulk", "admin"); err != nil {
|
||||
t.Fatalf("Set dev-2: %v", err)
|
||||
}
|
||||
snap := groups.Groups()
|
||||
if snap["dev-2"] != "Bulk" {
|
||||
t.Fatalf("Groups()[dev-2] = %q, want %q", snap["dev-2"], "Bulk")
|
||||
}
|
||||
snap["dev-2"] = "mutated"
|
||||
if got := groups.Group("dev-2"); got != "Bulk" {
|
||||
t.Fatalf("mutating the snapshot changed the store: %q", got)
|
||||
}
|
||||
if err := groups.Remove("dev-2"); err != nil {
|
||||
t.Fatalf("Remove: %v", err)
|
||||
}
|
||||
if err := groups.Remove("dev-2"); err != nil {
|
||||
t.Fatalf("Remove twice: %v", err)
|
||||
}
|
||||
if got := groups.Group("dev-2"); got != "" {
|
||||
t.Fatalf("Group after Remove = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeviceGroupsBadFileRefusesWrites: an unparsable file must never be
|
||||
// replaced by the first save.
|
||||
func TestDeviceGroupsBadFileRefusesWrites(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "device_groups.json")
|
||||
bad := []byte("{not json")
|
||||
if err := os.WriteFile(path, bad, 0644); err != nil {
|
||||
t.Fatalf("seed: %v", err)
|
||||
}
|
||||
groups := NewDeviceGroups(path)
|
||||
if err := groups.Load(); err == nil {
|
||||
t.Fatal("Load accepted a malformed file")
|
||||
}
|
||||
if err := groups.Set("dev-1", "x", "admin"); err == nil {
|
||||
t.Fatal("Set wrote over a file that failed to load")
|
||||
}
|
||||
if got, _ := os.ReadFile(path); string(got) != string(bad) {
|
||||
t.Fatalf("original file was modified: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeviceGroupsLoadAppliesInvariants: values read back from disk are
|
||||
// sanitized and capped, so a restored file cannot break the card layout.
|
||||
func TestDeviceGroupsLoadAppliesInvariants(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "device_groups.json")
|
||||
long := "Team\nA " + strings.Repeat("x", MaxGroupRunes)
|
||||
data, err := json.Marshal(deviceGroupsFile{Version: deviceGroupsFileVersion,
|
||||
Devices: map[string]DeviceGroupEntry{"dev-1": {Group: long}}})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(path, data, 0644); err != nil {
|
||||
t.Fatalf("seed: %v", err)
|
||||
}
|
||||
groups := NewDeviceGroups(path)
|
||||
if err := groups.Load(); err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
got := groups.Group("dev-1")
|
||||
if strings.ContainsAny(got, "\r\n\t") {
|
||||
t.Fatalf("loaded group kept control characters: %q", got)
|
||||
}
|
||||
if n := utf8.RuneCountInString(got); n != MaxGroupRunes {
|
||||
t.Fatalf("loaded group has %d runes, want %d", n, MaxGroupRunes)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetDeviceGroupHandler exercises the WebSocket path: the superadmin moves
|
||||
// a host, device_list reports the override plus groupEdit, get_groups offers
|
||||
// the new name, and clearing restores the reported group.
|
||||
func TestSetDeviceGroupHandler(t *testing.T) {
|
||||
wh, _, _ := aclTestHub(t)
|
||||
groups := NewDeviceGroups(filepath.Join(t.TempDir(), "device_groups.json"))
|
||||
wh.withDeviceGroups(groups)
|
||||
|
||||
c, tok := aclTestClient(t, wh, "admin", "admin")
|
||||
|
||||
send := func(id, group string) map[string]any {
|
||||
raw, err := json.Marshal(map[string]any{"token": tok, "id": id, "group": group})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
wh.dispatch(c, "set_device_group", raw)
|
||||
return readUntilCmd(t, c, "set_device_group_result")
|
||||
}
|
||||
|
||||
res := send("pub", " Office\nWest ")
|
||||
if res["ok"] != true {
|
||||
t.Fatalf("set group failed: %v", res)
|
||||
}
|
||||
if res["group"] != "Office West" {
|
||||
t.Fatalf("reply group = %v, want %q", res["group"], "Office West")
|
||||
}
|
||||
|
||||
// device_list reports the override and the capability the menu keys off.
|
||||
rawList, _ := json.Marshal(map[string]any{"token": tok})
|
||||
wh.dispatch(c, "get_devices", rawList)
|
||||
list := readUntilCmd(t, c, "device_list")
|
||||
if list["groupEdit"] != true {
|
||||
t.Fatalf("device_list groupEdit = %v, want true", list["groupEdit"])
|
||||
}
|
||||
found := false
|
||||
for _, it := range list["devices"].([]any) {
|
||||
m := it.(map[string]any)
|
||||
if m["id"] == "pub" {
|
||||
found = true
|
||||
if m["group"] != "Office West" {
|
||||
t.Fatalf("device_list group = %v, want override", m["group"])
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("device_list did not include the public device")
|
||||
}
|
||||
|
||||
// get_groups offers the override name.
|
||||
rawGroups, _ := json.Marshal(map[string]any{"token": tok})
|
||||
wh.dispatch(c, "get_groups", rawGroups)
|
||||
gr := readUntilCmd(t, c, "groups")
|
||||
var names []string
|
||||
for _, g := range gr["groups"].([]any) {
|
||||
names = append(names, g.(string))
|
||||
}
|
||||
if !slices.Contains(names, "Office West") {
|
||||
t.Fatalf("groups = %v, want the override included", names)
|
||||
}
|
||||
// The overridden device's stale reported group must not linger in the list.
|
||||
if slices.Contains(names, "shared") {
|
||||
t.Fatalf("groups = %v, want the overridden reported group omitted", names)
|
||||
}
|
||||
|
||||
// A stale card (device deleted between render and save) must not create an
|
||||
// orphan override.
|
||||
if res := send("ghost-id", "Nowhere"); res["ok"] == true {
|
||||
t.Fatalf("group override accepted for an unknown device: %v", res)
|
||||
}
|
||||
|
||||
// Clearing restores the group the host reported.
|
||||
res = send("pub", "")
|
||||
if res["ok"] != true {
|
||||
t.Fatalf("clear group failed: %v", res)
|
||||
}
|
||||
wh.dispatch(c, "get_devices", rawList)
|
||||
list = readUntilCmd(t, c, "device_list")
|
||||
for _, it := range list["devices"].([]any) {
|
||||
m := it.(map[string]any)
|
||||
if m["id"] == "pub" && m["group"] != "shared" {
|
||||
t.Fatalf("group after clear = %v, want reported %q", m["group"], "shared")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetDeviceGroupPermissions: viewers cannot edit, and an admin that cannot
|
||||
// see a private device cannot move it.
|
||||
func TestSetDeviceGroupPermissions(t *testing.T) {
|
||||
wh, _, _ := aclTestHub(t)
|
||||
wh.withDeviceGroups(NewDeviceGroups(""))
|
||||
|
||||
attempt := func(c *wsClient, tok, id string) map[string]any {
|
||||
raw, _ := json.Marshal(map[string]any{"token": tok, "id": id, "group": "x"})
|
||||
wh.dispatch(c, "set_device_group", raw)
|
||||
return readUntilCmd(t, c, "set_device_group_result")
|
||||
}
|
||||
|
||||
cv, tokv := aclTestClient(t, wh, "viewer1", "viewer")
|
||||
if res := attempt(cv, tokv, "pub"); res["ok"] == true {
|
||||
t.Fatal("viewer was allowed to move a host")
|
||||
}
|
||||
|
||||
ca, toka := aclTestClient(t, wh, "other-admin", "admin")
|
||||
if res := attempt(ca, toka, "a-dev"); res["ok"] == true {
|
||||
t.Fatal("admin without device access was allowed to move a host")
|
||||
}
|
||||
|
||||
cs, toks := aclTestClient(t, wh, "admin", "admin")
|
||||
if res := attempt(cs, toks, "a-dev"); res["ok"] != true {
|
||||
t.Fatalf("superadmin was refused: %v", res)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user