Files
PnvPanel/backend/tests/PnvPanel.Application.Tests/Common/Behaviors/RequireActivationBehaviorTests.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

120 lines
3.7 KiB
C#

using NSubstitute;
using PnvPanel.Application.Auth;
using PnvPanel.Application.Common.Behaviors;
using PnvPanel.Application.Common.Interfaces;
using PnvPanel.Application.Common.Messaging;
using PnvPanel.Application.Common.Models;
using Xunit;
namespace PnvPanel.Application.Tests.Common.Behaviors;
public class RequireActivationBehaviorTests
{
private sealed record DummyRequest : IRequiresActivation;
private readonly ICurrentUser _currentUser = Substitute.For<ICurrentUser>();
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
private RequireActivationBehavior<DummyRequest, Result<string>> CreateBehavior() =>
new(_currentUser, _identityService);
private static CurrentUserProfile CreateProfile(Guid userId, bool isActivated) =>
new(
userId,
"alice",
Guid.NewGuid(),
"user",
IsActivated: isActivated,
IsBlocked: false,
ConfigQuota: 3,
PlanId: null,
MaxIpLimit: RoleQuota.Unlimited,
SubscriptionToken: "sub-token",
BillingEnabled: false,
BillingPaidUntil: null,
BillingSuspended: false
);
[Fact]
public async Task Handle_WhenActivated_CallsNext()
{
var userId = Guid.NewGuid();
_currentUser.UserId.Returns(userId);
_identityService
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
.Returns(CreateProfile(userId, isActivated: true));
var result = await CreateBehavior()
.Handle(
new DummyRequest(),
() => Task.FromResult(Result.Success("ok")),
CancellationToken.None
);
Assert.True(result.IsSuccess);
Assert.Equal("ok", result.Value);
}
[Fact]
public async Task Handle_WhenNotActivated_ReturnsNotActivatedWithoutCallingNext()
{
var userId = Guid.NewGuid();
_currentUser.UserId.Returns(userId);
_identityService
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
.Returns(CreateProfile(userId, isActivated: false));
var nextCalled = false;
var result = await CreateBehavior()
.Handle(
new DummyRequest(),
() =>
{
nextCalled = true;
return Task.FromResult(Result.Success("ok"));
},
CancellationToken.None
);
Assert.False(result.IsSuccess);
Assert.Equal(AuthErrors.NotActivated, result.Error);
Assert.False(nextCalled);
}
[Fact]
public async Task Handle_WhenNoCurrentUser_ReturnsUnauthorized()
{
_currentUser.UserId.Returns((Guid?)null);
var result = await CreateBehavior()
.Handle(
new DummyRequest(),
() => Task.FromResult(Result.Success("ok")),
CancellationToken.None
);
Assert.False(result.IsSuccess);
Assert.Equal(AuthErrors.Unauthorized, result.Error);
}
[Fact]
public async Task Handle_WhenProfileMissing_ReturnsUnauthorized()
{
var userId = Guid.NewGuid();
_currentUser.UserId.Returns(userId);
_identityService
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
.Returns((CurrentUserProfile?)null);
var result = await CreateBehavior()
.Handle(
new DummyRequest(),
() => Task.FromResult(Result.Success("ok")),
CancellationToken.None
);
Assert.False(result.IsSuccess);
Assert.Equal(AuthErrors.Unauthorized, result.Error);
}
}