Files
PnvPanel/backend/tests/PnvPanel.Application.Tests/Configs/Create/CreateVpnConfigCommandHandlerTests.cs
T
Leonid Pershin fad03c2834
CI / Backend (build + test) (push) Failing after 1m23s
CI / Frontend (lint + typecheck + build) (push) Successful in 34s
Enhance user plan management and update related endpoints
- Added new configuration options for user plans in `.env.example`, including `Plans__MaxCustomConfigCount` and `Plans__MinCustomConfigCount`.
- Introduced `MapPlanEndpoints` in `Program.cs` to handle plan-related API routes.
- Implemented `SetUserPlan` endpoint in `RoleEndpoints` to allow admins to assign plans to users.
- Removed deprecated role request approval endpoints from `AdminSupportEndpoints`.
- Updated `ITelegramNotifier` and related classes to reflect changes in role request handling and payment notifications.
- Refactored role management commands to remove `MaxConfigs` and focus on `MaxIpLimit` and billing settings.
- Enhanced billing request handling to accommodate plan changes instead of role changes.
- Updated various interfaces and command handlers to support new plan management features.
2026-07-23 22:52:20 +03:00

130 lines
4.4 KiB
C#

using NSubstitute;
using PnvPanel.Application.Common.Interfaces;
using PnvPanel.Application.Common.Models;
using PnvPanel.Application.Configs;
using PnvPanel.Application.Configs.Create;
using PnvPanel.Application.Tests.TestSupport;
using PnvPanel.Domain.Inbounds;
using PnvPanel.Infrastructure.Persistence;
using Xunit;
namespace PnvPanel.Application.Tests.Configs.Create;
/// <summary>
/// Только ветки ДО ReserveQuotaSlotAsync (billing-guard) — дальше хендлер уходит в
/// pg_advisory_xact_lock, который InMemory-провайдер не поддерживает (см. CLAUDE.md/
/// ConfigQuotaTests в IntegrationTests для позитивного пути и гонок).
/// </summary>
public class CreateVpnConfigCommandHandlerTests
{
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
private readonly IXuiPanelGateway _gateway = Substitute.For<IXuiPanelGateway>();
private static CurrentUserProfile Profile(
Guid userId,
Guid roleId,
bool billingEnabled,
DateTimeOffset? billingPaidUntil
) =>
new(
userId,
"alice",
roleId,
"premium",
IsActivated: true,
IsBlocked: false,
ConfigQuota: 5,
PlanId: null,
MaxIpLimit: 3,
SubscriptionToken: "sub-token",
BillingEnabled: billingEnabled,
BillingPaidUntil: billingPaidUntil,
BillingSuspended: false
);
private async Task<(Inbound inbound, Guid roleId)> SeedAllowedInboundAsync(AppDbContext dbContext)
{
var roleId = Guid.NewGuid();
var node = Domain.Nodes.Node.Register(
"node-1",
new Uri("https://node1.example.com"),
new Domain.Nodes.NodeCredentials("admin", "protected"),
null
);
var inbound = Inbound.FromRemote(node.Id, "1", VpnProtocol.Vless, "remark", 443);
inbound.Publish(null, [roleId]);
dbContext.Nodes.Add(node);
dbContext.Inbounds.Add(inbound);
await dbContext.SaveChangesAsync(CancellationToken.None);
return (inbound, roleId);
}
[Fact]
public async Task Handle_WhenBillingEnabledAndPaidUntilExpired_ReturnsBillingRequired()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var (inbound, roleId) = await SeedAllowedInboundAsync(dbContext);
_identityService
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
.Returns(Profile(userId, roleId, billingEnabled: true, billingPaidUntil: DateTimeOffset.UtcNow.AddDays(-1)));
var handler = new CreateVpnConfigCommandHandler(
dbContext,
_identityService,
_gateway,
FakeCurrentUser.Authenticated(userId)
);
var result = await handler.Handle(
new CreateVpnConfigCommand(inbound.Id, null),
CancellationToken.None
);
Assert.False(result.IsSuccess);
Assert.Equal(ConfigErrors.BillingRequired, result.Error);
await _gateway
.DidNotReceive()
.AddClientAsync(
Arg.Any<Domain.Nodes.Node>(),
Arg.Any<string>(),
Arg.Any<VpnProtocol>(),
Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<int>(),
Arg.Any<DateTimeOffset?>(),
Arg.Any<CancellationToken>()
);
}
[Fact]
public async Task Handle_WhenBillingEnabledAndPaidUntilNull_ReturnsBillingRequired()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var (inbound, roleId) = await SeedAllowedInboundAsync(dbContext);
_identityService
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
.Returns(Profile(userId, roleId, billingEnabled: true, billingPaidUntil: null));
var handler = new CreateVpnConfigCommandHandler(
dbContext,
_identityService,
_gateway,
FakeCurrentUser.Authenticated(userId)
);
var result = await handler.Handle(
new CreateVpnConfigCommand(inbound.Id, null),
CancellationToken.None
);
Assert.False(result.IsSuccess);
Assert.Equal(ConfigErrors.BillingRequired, result.Error);
}
}