elsa-core/test/unit/Elsa.Identity.UnitTests/Services/RoleManagerTests.cs
Sipke Schoorstra c32a5a192c
[codex] Enforce role assignment authorization (#7501)
* Enforce role assignment authorization

* Address role authorization review feedback

* Address role authorization review follow-up
2026-05-20 14:04:20 +02:00

45 lines
1.3 KiB
C#

using Elsa.Common.Services;
using Elsa.Identity.Entities;
using Elsa.Identity.Providers;
using Elsa.Identity.Services;
namespace Elsa.Identity.UnitTests.Services;
public class RoleManagerTests
{
private readonly MemoryRoleStore _roleStore;
private readonly RoleManager _manager;
public RoleManagerTests()
{
_roleStore = new MemoryRoleStore(new MemoryStore<Role>());
_manager = new RoleManager(_roleStore, new StoreBasedRoleProvider(_roleStore));
}
[Fact]
public async Task CreateRoleRejectsExistingRoleId()
{
await _roleStore.SaveAsync(new Role
{
Id = "admin",
Name = "Admin",
Permissions = [PermissionNames.All]
});
await Assert.ThrowsAsync<InvalidOperationException>(() => _manager.CreateRoleAsync("Replacement", [], "admin"));
var role = await _roleStore.FindAsync(new() { Id = "admin" });
Assert.NotNull(role);
Assert.Equal("Admin", role.Name);
Assert.Equal([PermissionNames.All], role.Permissions);
}
[Fact]
public async Task CreateRoleRejectsProvidedAdminRoleIdCollision()
{
var manager = new RoleManager(_roleStore, new AdminRoleProvider());
await Assert.ThrowsAsync<InvalidOperationException>(() => manager.CreateRoleAsync("Replacement", [], "admin"));
}
}