- Introduced billing capabilities, allowing users to request payments for subscription periods (3/6/12 months) with admin approval via Telegram. - Updated role management to include a `BillingEnabled` property, preventing billing for admin roles. - Enhanced the `CreateRoleCommand` and `UpdateRoleCommand` to accept billing parameters, ensuring proper handling during role creation and updates. - Added new endpoints for billing management and integrated billing checks into VPN config creation to enforce payment requirements. - Updated related services, models, and tests to support the new billing features, ensuring comprehensive coverage and functionality. - Enhanced documentation to reflect the new billing processes and role management changes.
119 lines
3.7 KiB
C#
119 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,
|
|
MaxConfigs: 3,
|
|
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);
|
|
}
|
|
}
|