- 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.
134 lines
4.6 KiB
C#
134 lines
4.6 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
using PnvPanel.Application.Admin.Billing;
|
|
using PnvPanel.Application.Admin.Users;
|
|
using PnvPanel.Application.Billing;
|
|
using PnvPanel.Application.Common.Interfaces;
|
|
using PnvPanel.Application.Common.Models;
|
|
using PnvPanel.Application.Tests.TestSupport;
|
|
using Xunit;
|
|
|
|
namespace PnvPanel.Application.Tests.Admin.Billing;
|
|
|
|
public class GrantBillingGiftCommandHandlerTests
|
|
{
|
|
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
|
|
private readonly IXuiPanelGateway _gateway = Substitute.For<IXuiPanelGateway>();
|
|
private readonly IRealtimeNotifier _notifier = Substitute.For<IRealtimeNotifier>();
|
|
private readonly ITelegramNotifier _telegramNotifier = Substitute.For<ITelegramNotifier>();
|
|
private readonly ILogger<GrantBillingGiftCommandHandler> _logger = Substitute.For<
|
|
ILogger<GrantBillingGiftCommandHandler>
|
|
>();
|
|
|
|
private static CurrentUserProfile Profile(Guid userId, bool billingEnabled, DateTimeOffset? paidUntil) =>
|
|
new(
|
|
userId,
|
|
"alice",
|
|
Guid.NewGuid(),
|
|
"premium",
|
|
IsActivated: true,
|
|
IsBlocked: false,
|
|
ConfigQuota: 5,
|
|
PlanId: null,
|
|
MaxIpLimit: 3,
|
|
SubscriptionToken: "sub-token",
|
|
BillingEnabled: billingEnabled,
|
|
BillingPaidUntil: paidUntil,
|
|
BillingSuspended: false
|
|
);
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenBillingEnabled_ExtendsPaidUntilAndNotifiesUser()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var adminId = Guid.NewGuid();
|
|
var userId = Guid.NewGuid();
|
|
_identityService
|
|
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
|
.Returns(Profile(userId, billingEnabled: true, paidUntil: null));
|
|
_identityService
|
|
.ExtendBillingPaidUntilAsync(userId, Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>())
|
|
.Returns(Result.Success());
|
|
|
|
var handler = new GrantBillingGiftCommandHandler(
|
|
dbContext,
|
|
_identityService,
|
|
_gateway,
|
|
_notifier,
|
|
_telegramNotifier,
|
|
FakeCurrentUser.Authenticated(adminId, "admin"),
|
|
_logger
|
|
);
|
|
|
|
var before = DateTimeOffset.UtcNow;
|
|
var result = await handler.Handle(new GrantBillingGiftCommand(userId, 30), CancellationToken.None);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
await _identityService
|
|
.Received(1)
|
|
.ExtendBillingPaidUntilAsync(
|
|
userId,
|
|
Arg.Is<DateTimeOffset>(d => d >= before.AddDays(30).AddMinutes(-1)),
|
|
Arg.Any<CancellationToken>()
|
|
);
|
|
await _telegramNotifier
|
|
.Received(1)
|
|
.NotifyUserAsync(
|
|
userId,
|
|
Arg.Is<string>(m => m!.Contains("30")),
|
|
Arg.Any<string?>(),
|
|
Arg.Any<CancellationToken>()
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenBillingNotEnabled_ReturnsNotEnabled()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
_identityService
|
|
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
|
.Returns(Profile(userId, billingEnabled: false, paidUntil: null));
|
|
|
|
var handler = new GrantBillingGiftCommandHandler(
|
|
dbContext,
|
|
_identityService,
|
|
_gateway,
|
|
_notifier,
|
|
_telegramNotifier,
|
|
FakeCurrentUser.Authenticated(Guid.NewGuid(), "admin"),
|
|
_logger
|
|
);
|
|
|
|
var result = await handler.Handle(new GrantBillingGiftCommand(userId, 30), CancellationToken.None);
|
|
|
|
Assert.False(result.IsSuccess);
|
|
Assert.Equal(BillingErrors.NotEnabled, result.Error);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenUserNotFound_ReturnsUserNotFound()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
_identityService
|
|
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
|
.Returns((CurrentUserProfile?)null);
|
|
|
|
var handler = new GrantBillingGiftCommandHandler(
|
|
dbContext,
|
|
_identityService,
|
|
_gateway,
|
|
_notifier,
|
|
_telegramNotifier,
|
|
FakeCurrentUser.Authenticated(Guid.NewGuid(), "admin"),
|
|
_logger
|
|
);
|
|
|
|
var result = await handler.Handle(new GrantBillingGiftCommand(userId, 30), CancellationToken.None);
|
|
|
|
Assert.False(result.IsSuccess);
|
|
Assert.Equal(UserErrors.NotFound, result.Error);
|
|
}
|
|
}
|