Files
PnvPanel/backend/tests/PnvPanel.Application.Tests/Support/ListSelectableRoles/ListSelectableRolesQueryHandlerTests.cs
T
Leonid Pershin b6bffcc302
CI / Backend (build + test) (push) Successful in 1m22s
CI / Frontend (lint + typecheck + build) (push) Successful in 44s
Enhance role selection functionality in ListSelectableRolesQueryHandler and update frontend role display
- Updated ListSelectableRolesQueryHandler to include IIdentityService and ICurrentUser for user authorization and profile retrieval.
- Added logic to filter out the current user's role and admin roles from the selectable roles list.
- Enhanced CreateRoleRequestDialog to display role options with additional information, including max configs and IP limits, using localization support.
- Updated i18n resources to include new role option formatting for both Russian and English.
2026-07-14 23:31:56 +03:00

95 lines
3.1 KiB
C#

using NSubstitute;
using PnvPanel.Application.Common.Interfaces;
using PnvPanel.Application.Support.ListSelectableRoles;
using PnvPanel.Application.Tests.TestSupport;
using Xunit;
namespace PnvPanel.Application.Tests.Support.ListSelectableRoles;
public class ListSelectableRolesQueryHandlerTests
{
private readonly IRoleService _roleService = Substitute.For<IRoleService>();
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
private static CurrentUserProfile Profile(Guid userId, Guid roleId, string role) =>
new(
userId,
"user",
roleId,
role,
IsActivated: true,
IsBlocked: false,
MaxConfigs: 3,
MaxIpLimit: 1,
SubscriptionToken: "token"
);
[Fact]
public async Task Handle_ExcludesAdminAndCurrentUserRole()
{
var userId = Guid.NewGuid();
var currentRoleId = Guid.NewGuid();
var extendedRoleId = Guid.NewGuid();
var adminRoleId = Guid.NewGuid();
_roleService
.ListRolesAsync(Arg.Any<CancellationToken>())
.Returns(
new List<RoleDto>
{
new(adminRoleId, "admin", -1, -1, IsSystem: true),
new(currentRoleId, "user", 3, 1, IsSystem: true),
new(extendedRoleId, "extended", 10, 5, IsSystem: false),
}
);
_identityService
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
.Returns(Profile(userId, currentRoleId, "user"));
var currentUser = FakeCurrentUser.Authenticated(userId, "alice");
var handler = new ListSelectableRolesQueryHandler(
_roleService,
_identityService,
currentUser
);
var result = await handler.Handle(new ListSelectableRolesQuery(), CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.Equal(["extended"], result.Value.Select(r => r.Name));
}
[Fact]
public async Task Handle_WhenProfileMissing_StillExcludesAdmin()
{
var userId = Guid.NewGuid();
var userRoleId = Guid.NewGuid();
var adminRoleId = Guid.NewGuid();
_roleService
.ListRolesAsync(Arg.Any<CancellationToken>())
.Returns(
new List<RoleDto>
{
new(adminRoleId, "admin", -1, -1, IsSystem: true),
new(userRoleId, "user", 3, 1, IsSystem: true),
}
);
_identityService
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
.Returns((CurrentUserProfile?)null);
var currentUser = FakeCurrentUser.Authenticated(userId, "alice");
var handler = new ListSelectableRolesQueryHandler(
_roleService,
_identityService,
currentUser
);
var result = await handler.Handle(new ListSelectableRolesQuery(), CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.Equal(["user"], result.Value.Select(r => r.Name));
}
}