Implement billing functionality and enhance role management
- 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.
This commit is contained in:
+85
@@ -0,0 +1,85 @@
|
||||
using NSubstitute;
|
||||
using PnvPanel.Application.Billing.GetMyBillingStatus;
|
||||
using PnvPanel.Application.Common.Interfaces;
|
||||
using PnvPanel.Application.Tests.TestSupport;
|
||||
using PnvPanel.Domain.Billing;
|
||||
using Xunit;
|
||||
|
||||
namespace PnvPanel.Application.Tests.Billing.GetMyBillingStatus;
|
||||
|
||||
public class GetMyBillingStatusQueryHandlerTests
|
||||
{
|
||||
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
|
||||
|
||||
private static CurrentUserProfile Profile(
|
||||
Guid userId,
|
||||
bool billingEnabled,
|
||||
DateTimeOffset? paidUntil = null,
|
||||
bool suspended = false
|
||||
) =>
|
||||
new(
|
||||
userId,
|
||||
"alice",
|
||||
Guid.NewGuid(),
|
||||
"premium",
|
||||
IsActivated: true,
|
||||
IsBlocked: false,
|
||||
MaxConfigs: 5,
|
||||
MaxIpLimit: 3,
|
||||
SubscriptionToken: "sub-token",
|
||||
BillingEnabled: billingEnabled,
|
||||
BillingPaidUntil: paidUntil,
|
||||
BillingSuspended: suspended
|
||||
);
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenBillingNotEnabled_ReturnsDisabledStatusWithoutQueryingRequests()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
_identityService
|
||||
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
||||
.Returns(Profile(userId, billingEnabled: false));
|
||||
|
||||
var handler = new GetMyBillingStatusQueryHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
FakeCurrentUser.Authenticated(userId)
|
||||
);
|
||||
|
||||
var result = await handler.Handle(new GetMyBillingStatusQuery(), CancellationToken.None);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.False(result.Value.BillingEnabled);
|
||||
Assert.Null(result.Value.ActiveRequest);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenActiveRequestExists_IncludesItInStatus()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
var paidUntil = DateTimeOffset.UtcNow.AddDays(10);
|
||||
var request = PaymentRequest.Create(userId, PaymentPeriod.Quarter, 1500);
|
||||
dbContext.PaymentRequests.Add(request);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
_identityService
|
||||
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
||||
.Returns(Profile(userId, billingEnabled: true, paidUntil: paidUntil));
|
||||
|
||||
var handler = new GetMyBillingStatusQueryHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
FakeCurrentUser.Authenticated(userId)
|
||||
);
|
||||
|
||||
var result = await handler.Handle(new GetMyBillingStatusQuery(), CancellationToken.None);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.True(result.Value.BillingEnabled);
|
||||
Assert.Equal(paidUntil, result.Value.PaidUntil);
|
||||
Assert.NotNull(result.Value.ActiveRequest);
|
||||
Assert.Equal(request.Id, result.Value.ActiveRequest!.Id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user