- Introduced billing capabilities, allowing users to request payments for subscription periods (3/6/12 months) with admin approval via Telegram. - Updated role management to include a `BillingEnabled` property, preventing billing for admin roles. - Enhanced the `CreateRoleCommand` and `UpdateRoleCommand` to accept billing parameters, ensuring proper handling during role creation and updates. - Added new endpoints for billing management and integrated billing checks into VPN config creation to enforce payment requirements. - Updated related services, models, and tests to support the new billing features, ensuring comprehensive coverage and functionality. - Enhanced documentation to reflect the new billing processes and role management changes.
98 lines
3.3 KiB
C#
98 lines
3.3 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",
|
|
BillingEnabled: false,
|
|
BillingPaidUntil: null,
|
|
BillingSuspended: false
|
|
);
|
|
|
|
[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, BillingEnabled: false),
|
|
new(currentRoleId, "user", 3, 1, IsSystem: true, BillingEnabled: false),
|
|
new(extendedRoleId, "extended", 10, 5, IsSystem: false, BillingEnabled: 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, BillingEnabled: false),
|
|
new(userRoleId, "user", 3, 1, IsSystem: true, BillingEnabled: false),
|
|
}
|
|
);
|
|
_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));
|
|
}
|
|
}
|