Files
PnvPanel/backend/tests/PnvPanel.Application.Tests/Admin/Users/ListUsersQueryHandlerTests.cs
T
Leonid Pershin 4b34c37ce3
CI / Backend (build + test) (push) Failing after 2m14s
CI / Frontend (lint + typecheck + build) (push) Successful in 51s
Enhance user management and node health check features
- Updated `ListUsersQueryHandler` to include plan names and config quotas in `UserSummaryDto`, enriching user data retrieval.
- Implemented `WithPlanNamesAsync` method to fetch plan names based on user plan IDs, improving user experience in the admin interface.
- Enhanced `Node` class with a `ConsecutiveProbeFailures` property for better status management during health checks.
- Modified `NodeHealthCheckService` to utilize the new `RecordProbe` method, implementing a hysteresis mechanism for node status changes.
- Updated frontend components to display user config quotas and plan names, improving clarity in user management.
- Enhanced tests for user listing and node status handling to ensure robust functionality and coverage.
- Updated documentation to reflect changes in user and node management features.
2026-08-05 08:34:17 +03:00

144 lines
5.5 KiB
C#

using NSubstitute;
using PnvPanel.Application.Admin.Users;
using PnvPanel.Application.Common.Interfaces;
using PnvPanel.Application.Common.Models;
using PnvPanel.Application.Tests.TestSupport;
using PnvPanel.Domain.Billing;
using PnvPanel.Domain.Plans;
using Xunit;
namespace PnvPanel.Application.Tests.Admin.Users;
public class ListUsersQueryHandlerTests
{
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
private static UserSummaryDto Summary(Guid id, int configQuota = 3, Guid? planId = null) =>
new(
id,
"alice",
"premium",
true,
false,
DateTimeOffset.UtcNow,
true,
DateTimeOffset.UtcNow.AddDays(-1),
configQuota,
planId
);
[Fact]
public async Task Handle_WhenUserHasAwaitingConfirmationSubscriptionRequest_SetsBillingPendingReview()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var request = PaymentRequest.Create(userId, PaymentPeriod.Quarter, 1000);
request.MarkPaymentSent();
dbContext.PaymentRequests.Add(request);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.ListUsersAsync(1, 20, null, null, null, null, null, Arg.Any<CancellationToken>())
.Returns(new PagedList<UserSummaryDto>([Summary(userId)], 1, 1, 20));
var handler = new ListUsersQueryHandler(_identityService, dbContext);
var result = await handler.Handle(new ListUsersQuery(1, 20, null, null, null, null, null), CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.True(result.Value.Items.Single().BillingPendingReview);
}
[Fact]
public async Task Handle_WhenNoPendingRequest_LeavesBillingPendingReviewFalse()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
_identityService
.ListUsersAsync(1, 20, null, null, null, null, null, Arg.Any<CancellationToken>())
.Returns(new PagedList<UserSummaryDto>([Summary(userId)], 1, 1, 20));
var handler = new ListUsersQueryHandler(_identityService, dbContext);
var result = await handler.Handle(new ListUsersQuery(1, 20, null, null, null, null, null), CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.False(result.Value.Items.Single().BillingPendingReview);
}
[Fact]
public async Task Handle_WhenPendingRequestIsPlanChangeTopUp_LeavesBillingPendingReviewFalse()
{
// PlanChangeTopUp — доплата за смену тарифа, не подписка; не должна показывать пользователя
// как "оплата на проверке" в админке (см. CLAUDE.md про Kind).
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var request = PaymentRequest.CreatePlanChangeTopUp(userId, 500);
request.MarkPaymentSent();
dbContext.PaymentRequests.Add(request);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.ListUsersAsync(1, 20, null, null, null, null, null, Arg.Any<CancellationToken>())
.Returns(new PagedList<UserSummaryDto>([Summary(userId)], 1, 1, 20));
var handler = new ListUsersQueryHandler(_identityService, dbContext);
var result = await handler.Handle(new ListUsersQuery(1, 20, null, null, null, null, null), CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.False(result.Value.Items.Single().BillingPendingReview);
}
[Fact]
public async Task Handle_WhenUserHasCatalogPlan_FillsPlanName()
{
using var dbContext = InMemoryDbContextFactory.Create();
var plan = Plan.Create("Плюс", 6, 1);
dbContext.Plans.Add(plan);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.ListUsersAsync(1, 20, null, null, null, null, null, Arg.Any<CancellationToken>())
.Returns(
new PagedList<UserSummaryDto>(
[Summary(Guid.NewGuid(), configQuota: 6, planId: plan.Id)],
1,
1,
20
)
);
var handler = new ListUsersQueryHandler(_identityService, dbContext);
var result = await handler.Handle(new ListUsersQuery(1, 20, null, null, null, null, null), CancellationToken.None);
Assert.True(result.IsSuccess);
var item = result.Value.Items.Single();
Assert.Equal("Плюс", item.PlanName);
Assert.Equal(6, item.ConfigQuota);
}
[Fact]
public async Task Handle_WhenQuotaIsCustom_LeavesPlanNameNull()
{
using var dbContext = InMemoryDbContextFactory.Create();
_identityService
.ListUsersAsync(1, 20, null, null, null, null, null, Arg.Any<CancellationToken>())
.Returns(
new PagedList<UserSummaryDto>([Summary(Guid.NewGuid(), configQuota: 12)], 1, 1, 20)
);
var handler = new ListUsersQueryHandler(_identityService, dbContext);
var result = await handler.Handle(new ListUsersQuery(1, 20, null, null, null, null, null), CancellationToken.None);
Assert.True(result.IsSuccess);
var item = result.Value.Items.Single();
Assert.Null(item.PlanName);
Assert.Equal(12, item.ConfigQuota);
}
}