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.
This commit is contained in:
+139
@@ -0,0 +1,139 @@
|
||||
using NSubstitute;
|
||||
using PnvPanel.Application.Admin.Users;
|
||||
using PnvPanel.Application.Common.Interfaces;
|
||||
using PnvPanel.Application.Common.Models;
|
||||
using PnvPanel.Application.Plans;
|
||||
using PnvPanel.Application.Tests.TestSupport;
|
||||
using PnvPanel.Domain.Plans;
|
||||
using Xunit;
|
||||
|
||||
namespace PnvPanel.Application.Tests.Admin.Users;
|
||||
|
||||
public class AdminSetUserPlanCommandHandlerTests
|
||||
{
|
||||
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
|
||||
|
||||
private static CurrentUserProfile Profile(Guid userId, string role, int configQuota) =>
|
||||
new(
|
||||
userId,
|
||||
"alice",
|
||||
Guid.NewGuid(),
|
||||
role,
|
||||
IsActivated: true,
|
||||
IsBlocked: false,
|
||||
ConfigQuota: configQuota,
|
||||
PlanId: null,
|
||||
MaxIpLimit: 2,
|
||||
SubscriptionToken: "sub-token",
|
||||
BillingEnabled: false,
|
||||
BillingPaidUntil: null,
|
||||
BillingSuspended: false
|
||||
);
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenUnlimitedForNonAdmin_ReturnsUnlimitedOnlyForAdmin()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
_identityService
|
||||
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
||||
.Returns(Profile(userId, "user", 3));
|
||||
|
||||
var handler = new AdminSetUserPlanCommandHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
FakeCurrentUser.Authenticated(Guid.NewGuid())
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new AdminSetUserPlanCommand(userId, null, RoleQuota.Unlimited),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.False(result.IsSuccess);
|
||||
Assert.Equal(PlanErrors.UnlimitedOnlyForAdmin, result.Error);
|
||||
await _identityService
|
||||
.DidNotReceive()
|
||||
.SetConfigQuotaAsync(Arg.Any<Guid>(), Arg.Any<int>(), Arg.Any<Guid?>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenUnlimitedForAdmin_Succeeds()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
_identityService
|
||||
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
||||
.Returns(Profile(userId, "admin", -1));
|
||||
_identityService
|
||||
.SetConfigQuotaAsync(userId, RoleQuota.Unlimited, null, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Success());
|
||||
|
||||
var handler = new AdminSetUserPlanCommandHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
FakeCurrentUser.Authenticated(Guid.NewGuid())
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new AdminSetUserPlanCommand(userId, null, RoleQuota.Unlimited),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
await _identityService
|
||||
.Received(1)
|
||||
.SetConfigQuotaAsync(userId, RoleQuota.Unlimited, null, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WithPlanId_UsesPlanConfigCount()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
var plan = Plan.Create("Плюс", 6, 0);
|
||||
dbContext.Plans.Add(plan);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
_identityService
|
||||
.SetConfigQuotaAsync(userId, 6, plan.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Success());
|
||||
|
||||
var handler = new AdminSetUserPlanCommandHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
FakeCurrentUser.Authenticated(Guid.NewGuid())
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new AdminSetUserPlanCommand(userId, plan.Id, null),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
await _identityService
|
||||
.Received(1)
|
||||
.SetConfigQuotaAsync(userId, 6, plan.Id, Arg.Any<CancellationToken>());
|
||||
Assert.Single(dbContext.AuditLogs.Local);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenPlanNotFound_ReturnsNotFound()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
|
||||
var handler = new AdminSetUserPlanCommandHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
FakeCurrentUser.Authenticated(Guid.NewGuid())
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new AdminSetUserPlanCommand(Guid.NewGuid(), Guid.NewGuid(), null),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.False(result.IsSuccess);
|
||||
Assert.Equal(PlanErrors.NotFound, result.Error);
|
||||
}
|
||||
}
|
||||
@@ -56,13 +56,13 @@ public class ListUsersQueryHandlerTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenPendingRequestIsRoleChangeTopUp_LeavesBillingPendingReviewFalse()
|
||||
public async Task Handle_WhenPendingRequestIsPlanChangeTopUp_LeavesBillingPendingReviewFalse()
|
||||
{
|
||||
// RoleChangeTopUp — доплата за смену роли, не подписка; не должна показывать пользователя
|
||||
// PlanChangeTopUp — доплата за смену тарифа, не подписка; не должна показывать пользователя
|
||||
// как "оплата на проверке" в админке (см. CLAUDE.md про Kind).
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
var request = PaymentRequest.CreateRoleChangeTopUp(userId, 500);
|
||||
var request = PaymentRequest.CreatePlanChangeTopUp(userId, 500);
|
||||
request.MarkPaymentSent();
|
||||
dbContext.PaymentRequests.Add(request);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
Reference in New Issue
Block a user