- Added new endpoints for creating and managing extension requests, allowing users to request billing period extensions. - Implemented admin approval processes for extension requests via Telegram, including inline buttons for approval and rejection. - Introduced a gifting feature for admins to grant additional billing days directly to users without a request. - Updated the support ticket model to accommodate extension requests and their associated properties. - Enhanced the Telegram notifier to inform admins of new extension requests and notify users of approval or rejection. - Updated frontend components to support the new extension request and gifting functionalities, including user interfaces for managing these features. - Revised API documentation to reflect the new endpoints and their usage in the billing context.
133 lines
4.6 KiB
C#
133 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,
|
|
MaxConfigs: 5,
|
|
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);
|
|
}
|
|
}
|