Files
PnvPanel/backend/tests/PnvPanel.Application.Tests/Support/CreateExtensionRequestTicketCommandHandlerTests.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

126 lines
4.2 KiB
C#

using NSubstitute;
using PnvPanel.Application.Billing;
using PnvPanel.Application.Common.Interfaces;
using PnvPanel.Application.Support;
using PnvPanel.Application.Support.CreateExtensionRequest;
using PnvPanel.Application.Tests.TestSupport;
using PnvPanel.Domain.Support;
using Xunit;
namespace PnvPanel.Application.Tests.Support;
public class CreateExtensionRequestTicketCommandHandlerTests
{
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
private readonly IRealtimeNotifier _notifier = Substitute.For<IRealtimeNotifier>();
private readonly ITelegramNotifier _telegramNotifier = Substitute.For<ITelegramNotifier>();
private static CurrentUserProfile Profile(Guid userId, bool billingEnabled) =>
new(
userId,
"alice",
Guid.NewGuid(),
"premium",
IsActivated: true,
IsBlocked: false,
ConfigQuota: 5,
PlanId: null,
MaxIpLimit: 3,
SubscriptionToken: "sub-token",
BillingEnabled: billingEnabled,
BillingPaidUntil: null,
BillingSuspended: false
);
[Fact]
public async Task Handle_WhenBillingEnabled_CreatesTicketAndNotifiesAdmins()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
_identityService
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
.Returns(Profile(userId, billingEnabled: true));
var handler = new CreateExtensionRequestTicketCommandHandler(
dbContext,
_identityService,
_notifier,
_telegramNotifier,
FakeCurrentUser.Authenticated(userId, "alice")
);
var result = await handler.Handle(
new CreateExtensionRequestTicketCommand(14, "нужно продлить"),
CancellationToken.None
);
Assert.True(result.IsSuccess);
Assert.Equal(TicketType.ExtensionRequest, result.Value.Type);
Assert.Equal(14, result.Value.RequestedDays);
await _telegramNotifier
.Received(1)
.NotifyAdminsExtensionRequestCreatedAsync(
Arg.Any<Guid>(),
"alice",
14,
"нужно продлить",
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));
var handler = new CreateExtensionRequestTicketCommandHandler(
dbContext,
_identityService,
_notifier,
_telegramNotifier,
FakeCurrentUser.Authenticated(userId)
);
var result = await handler.Handle(
new CreateExtensionRequestTicketCommand(14, "нужно продлить"),
CancellationToken.None
);
Assert.False(result.IsSuccess);
Assert.Equal(BillingErrors.NotEnabled, result.Error);
}
[Fact]
public async Task Handle_WhenPendingExtensionRequestExists_ReturnsConflict()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
dbContext.SupportTickets.Add(SupportTicket.CreateExtensionRequest(userId, 7));
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
.Returns(Profile(userId, billingEnabled: true));
var handler = new CreateExtensionRequestTicketCommandHandler(
dbContext,
_identityService,
_notifier,
_telegramNotifier,
FakeCurrentUser.Authenticated(userId)
);
var result = await handler.Handle(
new CreateExtensionRequestTicketCommand(3, "ещё заявка"),
CancellationToken.None
);
Assert.False(result.IsSuccess);
Assert.Equal(SupportErrors.ExtensionRequestAlreadyPending, result.Error);
}
}