96d042b0c6
A trigger now carries lists of devices and groups (JSON columns, like a role's device scope) or All Devices, instead of one device or one group. A firing reaches the de-duplicated union; event triggers fire for a device in any target. Saving needs at least one target, every target must exist, All Devices stands alone, and a device-scoped role must have every target in scope (as single targets did). At fire time a group or device that no longer exists is skipped and noted in the last result; the rest still run. Deleting a group or removing a device drops it from schedules' targets, disabling only those left with none. A schedule saved without a time zone stays UTC. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
9.1 KiB
C#
174 lines
9.1 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using Backend.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Shared.Contracts.Api;
|
|
using Shared.Contracts.Models;
|
|
using Shared.Contracts.Serialization;
|
|
using Xunit;
|
|
|
|
namespace Backend.Tests;
|
|
|
|
public class GroupNestingTests(BackendTestFixture fixture) : IClassFixture<BackendTestFixture>
|
|
{
|
|
[Fact]
|
|
public async Task AFolderIncludesTheDevicesOfItsNestedGroups()
|
|
{
|
|
using var client = await fixture.SignedInAsync("group-admin");
|
|
var a = await fixture.SeedDeviceAsync("NEST-A");
|
|
var b = await fixture.SeedDeviceAsync("NEST-B", tags: ["nest-web"]);
|
|
|
|
var folder = await CreateAsync(client, new SaveDeviceGroupRequest("Sites", null, GroupKind.Folder, null));
|
|
var office = await CreateAsync(client, new SaveDeviceGroupRequest("Office", null, GroupKind.Static, null, folder.Id));
|
|
var web = await CreateAsync(client, new SaveDeviceGroupRequest("Web", null, GroupKind.Dynamic,
|
|
Rules("tags", RuleOperator.Equals, "nest-web"), office.Id));
|
|
await AddMembersAsync(client, office.Id, a);
|
|
|
|
var groups = await ListAsync(client);
|
|
Assert.Equal(2, groups.Single(g => g.Id == folder.Id).MemberCount);
|
|
Assert.Equal(0, groups.Single(g => g.Id == folder.Id).DirectMemberCount);
|
|
Assert.Equal(2, groups.Single(g => g.Id == office.Id).MemberCount);
|
|
Assert.Equal(1, groups.Single(g => g.Id == office.Id).DirectMemberCount);
|
|
Assert.Equal(office.Id, groups.Single(g => g.Id == web.Id).ParentId);
|
|
|
|
var members = await client.GetFromJsonAsync<List<DeviceDto>>(
|
|
ApiRoutes.For.GroupMembers(folder.Id), JsonDefaults.Options);
|
|
Assert.Equal(["NEST-A", "NEST-B"], members!.Select(d => d.Hostname).Order());
|
|
|
|
var devices = await client.GetFromJsonAsync<List<DeviceDto>>(ApiRoutes.Devices, JsonDefaults.Options);
|
|
Assert.Equal([web.Id], devices!.Single(d => d.ClientId == b).GroupIds);
|
|
|
|
var detail = await client.GetFromJsonAsync<DeviceDetailDto>(ApiRoutes.For.Device(b), JsonDefaults.Options);
|
|
Assert.Contains(detail!.Groups!, g => g.Id == folder.Id && g.Inherited);
|
|
Assert.Contains(detail.Groups!, g => g.Id == web.Id && !g.Inherited);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AGroupCannotMoveIntoItsOwnSubtree()
|
|
{
|
|
using var client = await fixture.SignedInAsync("group-admin");
|
|
var parent = await CreateAsync(client, new SaveDeviceGroupRequest("Cycle-P", null, GroupKind.Folder, null));
|
|
var child = await CreateAsync(client, new SaveDeviceGroupRequest("Cycle-C", null, GroupKind.Folder, null, parent.Id));
|
|
|
|
var intoChild = await client.PostAsJsonAsync(
|
|
ApiRoutes.For.GroupMove(parent.Id), new MoveGroupRequest(child.Id), JsonDefaults.Options);
|
|
var intoSelf = await client.PostAsJsonAsync(
|
|
ApiRoutes.For.GroupMove(parent.Id), new MoveGroupRequest(parent.Id), JsonDefaults.Options);
|
|
var toTop = await client.PostAsJsonAsync(
|
|
ApiRoutes.For.GroupMove(child.Id), new MoveGroupRequest(null), JsonDefaults.Options);
|
|
|
|
Assert.Equal(HttpStatusCode.BadRequest, intoChild.StatusCode);
|
|
Assert.Equal(HttpStatusCode.BadRequest, intoSelf.StatusCode);
|
|
Assert.Equal(HttpStatusCode.OK, toTop.StatusCode);
|
|
Assert.Null((await ListAsync(client)).Single(g => g.Id == child.Id).ParentId);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeletingAGroupLiftsItsChildrenAndDisablesItsTriggers()
|
|
{
|
|
using var client = await fixture.SignedInAsync("group-admin");
|
|
var top = await CreateAsync(client, new SaveDeviceGroupRequest("Del-Top", null, GroupKind.Folder, null));
|
|
var doomed = await CreateAsync(client, new SaveDeviceGroupRequest("Del-Mid", null, GroupKind.Folder, null, top.Id));
|
|
var child = await CreateAsync(client, new SaveDeviceGroupRequest("Del-Leaf", null, GroupKind.Static, null, doomed.Id));
|
|
var scriptId = await fixture.SeedScriptAsync("del-script");
|
|
var trigger = await client.PostAsJsonAsync(ApiRoutes.Triggers, new SaveScriptTriggerRequest(
|
|
scriptId, ScriptTriggerType.Scheduled, GroupIds: [doomed.Id], CronExpression: "0 1 * * *"), JsonDefaults.Options);
|
|
trigger.EnsureSuccessStatusCode();
|
|
var triggerId = (await trigger.Content.ReadFromJsonAsync<Dictionary<string, Guid>>(JsonDefaults.Options))!["id"];
|
|
|
|
// A second trigger also aims at another group: it loses only the deleted one.
|
|
var survivor = await CreateAsync(client, new SaveDeviceGroupRequest("Del-Other", null, GroupKind.Static, null));
|
|
var mixed = await client.PostAsJsonAsync(ApiRoutes.Triggers, new SaveScriptTriggerRequest(
|
|
scriptId, ScriptTriggerType.Scheduled, GroupIds: [doomed.Id, survivor.Id], CronExpression: "0 1 * * *"),
|
|
JsonDefaults.Options);
|
|
mixed.EnsureSuccessStatusCode();
|
|
var mixedId = (await mixed.Content.ReadFromJsonAsync<Dictionary<string, Guid>>(JsonDefaults.Options))!["id"];
|
|
|
|
var response = await client.DeleteAsync(ApiRoutes.For.Group(doomed.Id));
|
|
|
|
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
|
|
Assert.Equal(top.Id, (await ListAsync(client)).Single(g => g.Id == child.Id).ParentId);
|
|
var triggers = await client.GetFromJsonAsync<List<ScriptTriggerDto>>(ApiRoutes.Triggers, JsonDefaults.Options);
|
|
var disabled = triggers!.Single(t => t.Id == triggerId);
|
|
Assert.False(disabled.Enabled);
|
|
Assert.Empty(disabled.Targets);
|
|
Assert.Contains("was deleted", disabled.LastResult);
|
|
|
|
var kept = triggers!.Single(t => t.Id == mixedId);
|
|
Assert.True(kept.Enabled);
|
|
Assert.Equal(survivor.Id.ToString("D"), Assert.Single(kept.Targets).Id);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AGroupThatScopesARoleCannotBeDeleted()
|
|
{
|
|
using var client = await fixture.SignedInAsync("group-admin");
|
|
var group = await CreateAsync(client, new SaveDeviceGroupRequest("Scoping", null, GroupKind.Static, null));
|
|
await fixture.WithDbAsync(async db =>
|
|
{
|
|
db.Roles.Add(new RoleEntity
|
|
{
|
|
Id = Guid.NewGuid(), Name = $"scoped-by-{group.Id:N}",
|
|
PermissionsJson = JsonDefaults.Serialize(new[] { "devices.view" }),
|
|
DeviceScopeGroupIdsJson = JsonDefaults.Serialize(new[] { group.Id }),
|
|
CreatedUtc = DateTimeOffset.UtcNow, UpdatedUtc = DateTimeOffset.UtcNow,
|
|
});
|
|
await db.SaveChangesAsync();
|
|
});
|
|
fixture.Services.GetRequiredService<Backend.Auth.RoleCache>().Invalidate();
|
|
|
|
var response = await client.DeleteAsync(ApiRoutes.For.Group(group.Id));
|
|
|
|
Assert.Equal(HttpStatusCode.Conflict, response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SwitchingAStaticGroupToDynamicDropsItsList()
|
|
{
|
|
using var client = await fixture.SignedInAsync("group-admin");
|
|
var device = await fixture.SeedDeviceAsync("SWITCH-HOST");
|
|
var group = await CreateAsync(client, new SaveDeviceGroupRequest("Switch", null, GroupKind.Static, null));
|
|
await AddMembersAsync(client, group.Id, device);
|
|
|
|
var update = await client.PutAsJsonAsync(ApiRoutes.For.Group(group.Id), new SaveDeviceGroupRequest(
|
|
"Switch", null, GroupKind.Dynamic, Rules("hostname", RuleOperator.Equals, "nobody")), JsonDefaults.Options);
|
|
update.EnsureSuccessStatusCode();
|
|
|
|
Assert.Equal(0, await fixture.WithDbAsync(db => db.DeviceGroupMembers.CountAsync(m => m.GroupId == group.Id)));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AFolderRefusesDevices()
|
|
{
|
|
using var client = await fixture.SignedInAsync("group-admin");
|
|
var device = await fixture.SeedDeviceAsync("FOLDER-HOST");
|
|
var folder = await CreateAsync(client, new SaveDeviceGroupRequest("Folder-only", null, GroupKind.Folder, null));
|
|
|
|
var response = await client.PostAsJsonAsync(ApiRoutes.For.GroupMembers(folder.Id),
|
|
new UpdateGroupMembersRequest([device], []), JsonDefaults.Options);
|
|
|
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
}
|
|
|
|
private static GroupRuleSet Rules(string field, RuleOperator op, string value)
|
|
=> new(RuleCombine.And, [new RuleGroup(RuleCombine.And, [new RuleCondition(field, op, value)])]);
|
|
|
|
private static async Task<DeviceGroupDto> CreateAsync(HttpClient client, SaveDeviceGroupRequest request)
|
|
{
|
|
var response = await client.PostAsJsonAsync(ApiRoutes.Groups, request, JsonDefaults.Options);
|
|
response.EnsureSuccessStatusCode();
|
|
return (await response.Content.ReadFromJsonAsync<DeviceGroupDto>(JsonDefaults.Options))!;
|
|
}
|
|
|
|
private static async Task AddMembersAsync(HttpClient client, Guid groupId, params string[] clientIds)
|
|
{
|
|
var response = await client.PostAsJsonAsync(ApiRoutes.For.GroupMembers(groupId),
|
|
new UpdateGroupMembersRequest(clientIds, []), JsonDefaults.Options);
|
|
response.EnsureSuccessStatusCode();
|
|
}
|
|
|
|
private static async Task<List<DeviceGroupDto>> ListAsync(HttpClient client)
|
|
=> (await client.GetFromJsonAsync<List<DeviceGroupDto>>(ApiRoutes.Groups, JsonDefaults.Options))!;
|
|
}
|