using Microsoft.Extensions.Logging; using NSubstitute; using PnvPanel.Application.Admin.Support; using PnvPanel.Application.Common.Interfaces; using PnvPanel.Application.Common.Models; using PnvPanel.Application.Support; using PnvPanel.Application.Tests.TestSupport; using PnvPanel.Domain.Configs; using PnvPanel.Domain.Inbounds; using PnvPanel.Domain.Nodes; using PnvPanel.Domain.Support; using Xunit; namespace PnvPanel.Application.Tests.Admin.Support; public class ApproveExtensionRequestCommandHandlerTests { private readonly IIdentityService _identityService = Substitute.For(); private readonly IXuiPanelGateway _gateway = Substitute.For(); private readonly IRealtimeNotifier _notifier = Substitute.For(); private readonly ITelegramNotifier _telegramNotifier = Substitute.For(); private readonly ILogger _logger = Substitute.For< ILogger >(); private static CurrentUserProfile Profile(Guid userId, DateTimeOffset? paidUntil) => new( userId, "alice", Guid.NewGuid(), "premium", IsActivated: true, IsBlocked: false, MaxConfigs: 5, MaxIpLimit: 3, SubscriptionToken: "sub-token", BillingEnabled: true, BillingPaidUntil: paidUntil, BillingSuspended: paidUntil is null ); private ApproveExtensionRequestCommandHandler CreateHandler( PnvPanel.Infrastructure.Persistence.AppDbContext dbContext, Guid adminId ) => new( dbContext, _identityService, _gateway, _notifier, _telegramNotifier, FakeCurrentUser.Authenticated(adminId, "admin"), _logger ); [Fact] public async Task Handle_WhenNoPriorPayment_ExtendsFromNowAndResolves() { using var dbContext = InMemoryDbContextFactory.Create(); var adminId = Guid.NewGuid(); var userId = Guid.NewGuid(); var ticket = SupportTicket.CreateExtensionRequest(userId, 10); dbContext.SupportTickets.Add(ticket); await dbContext.SaveChangesAsync(CancellationToken.None); _identityService .GetProfileAsync(userId, Arg.Any()) .Returns(Profile(userId, paidUntil: null)); _identityService .ExtendBillingPaidUntilAsync(userId, Arg.Any(), Arg.Any()) .Returns(Result.Success()); var handler = CreateHandler(dbContext, adminId); var before = DateTimeOffset.UtcNow; var result = await handler.Handle( new ApproveExtensionRequestCommand(ticket.Id), CancellationToken.None ); Assert.True(result.IsSuccess); Assert.Equal(TicketStatus.Resolved, ticket.Status); await _identityService .Received(1) .ExtendBillingPaidUntilAsync( userId, Arg.Is(d => d >= before.AddDays(10).AddMinutes(-1)), Arg.Any() ); } [Fact] public async Task Handle_ResumesExpiredConfigs() { using var dbContext = InMemoryDbContextFactory.Create(); var adminId = Guid.NewGuid(); var userId = Guid.NewGuid(); var node = Node.Register( "node-1", new Uri("https://node1.example.com"), new NodeCredentials("admin", "protected"), null ); var inbound = Inbound.FromRemote(node.Id, "1", VpnProtocol.Vless, "remark", 443); var config = VpnConfig.Create(userId, inbound.Id, VpnProtocol.Vless, null); config.AssignRemoteClient("ext-1"); config.Suspend(); dbContext.Nodes.Add(node); dbContext.Inbounds.Add(inbound); dbContext.VpnConfigs.Add(config); var ticket = SupportTicket.CreateExtensionRequest(userId, 5); dbContext.SupportTickets.Add(ticket); await dbContext.SaveChangesAsync(CancellationToken.None); _identityService .GetProfileAsync(userId, Arg.Any()) .Returns(Profile(userId, paidUntil: null)); _identityService .ExtendBillingPaidUntilAsync(userId, Arg.Any(), Arg.Any()) .Returns(Result.Success()); _gateway .UpdateClientAsync( Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), enable: true, Arg.Any() ) .Returns(Result.Success()); var handler = CreateHandler(dbContext, adminId); var result = await handler.Handle( new ApproveExtensionRequestCommand(ticket.Id), CancellationToken.None ); Assert.True(result.IsSuccess); Assert.Equal(ConfigStatus.Active, config.Status); Assert.NotNull(config.ExpiresAt); } [Fact] public async Task Handle_WhenTicketNotOpen_ReturnsNotOpen() { using var dbContext = InMemoryDbContextFactory.Create(); var adminId = Guid.NewGuid(); var userId = Guid.NewGuid(); var ticket = SupportTicket.CreateExtensionRequest(userId, 5); ticket.Close(); dbContext.SupportTickets.Add(ticket); await dbContext.SaveChangesAsync(CancellationToken.None); var handler = CreateHandler(dbContext, adminId); var result = await handler.Handle( new ApproveExtensionRequestCommand(ticket.Id), CancellationToken.None ); Assert.False(result.IsSuccess); Assert.Equal(SupportErrors.NotOpen, result.Error); } [Fact] public async Task Handle_WhenNotExtensionRequest_ReturnsNotExtensionRequest() { using var dbContext = InMemoryDbContextFactory.Create(); var adminId = Guid.NewGuid(); var ticket = SupportTicket.CreateBugReport(Guid.NewGuid()); dbContext.SupportTickets.Add(ticket); await dbContext.SaveChangesAsync(CancellationToken.None); var handler = CreateHandler(dbContext, adminId); var result = await handler.Handle( new ApproveExtensionRequestCommand(ticket.Id), CancellationToken.None ); Assert.False(result.IsSuccess); Assert.Equal(SupportErrors.NotExtensionRequest, result.Error); } }