- 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.
101 lines
3.9 KiB
C#
101 lines
3.9 KiB
C#
using NSubstitute;
|
|
using PnvPanel.Application.Auth;
|
|
using PnvPanel.Application.Auth.Login;
|
|
using PnvPanel.Application.Common.Interfaces;
|
|
using PnvPanel.Application.Common.Models;
|
|
using Xunit;
|
|
|
|
namespace PnvPanel.Application.Tests.Auth;
|
|
|
|
public class LoginCommandHandlerTests
|
|
{
|
|
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
|
|
private readonly IJwtTokenService _jwtTokenService = Substitute.For<IJwtTokenService>();
|
|
private readonly IRefreshTokenService _refreshTokenService =
|
|
Substitute.For<IRefreshTokenService>();
|
|
|
|
private LoginCommandHandler CreateHandler() =>
|
|
new(_identityService, _jwtTokenService, _refreshTokenService);
|
|
|
|
[Fact]
|
|
public async Task Handle_WithValidCredentials_ReturnsAuthResult()
|
|
{
|
|
var userId = Guid.NewGuid();
|
|
var authUser = new AuthenticatedUser(userId, "alice", "user");
|
|
var profile = new CurrentUserProfile(
|
|
userId,
|
|
"alice",
|
|
Guid.NewGuid(),
|
|
"user",
|
|
IsActivated: true,
|
|
IsBlocked: false,
|
|
MaxConfigs: 3,
|
|
MaxIpLimit: RoleQuota.Unlimited,
|
|
SubscriptionToken: "sub-token",
|
|
BillingEnabled: false,
|
|
BillingPaidUntil: null,
|
|
BillingSuspended: false
|
|
);
|
|
|
|
_identityService
|
|
.ValidateCredentialsAsync("alice", "P@ssw0rd", Arg.Any<CancellationToken>())
|
|
.Returns(Result.Success(authUser));
|
|
_identityService.GetProfileAsync(userId, Arg.Any<CancellationToken>()).Returns(profile);
|
|
_identityService
|
|
.GetTelegramLinkInfoAsync(userId, Arg.Any<CancellationToken>())
|
|
.Returns(new TelegramLinkInfo(true, 123456, "alice_tg"));
|
|
_jwtTokenService
|
|
.GenerateAccessToken(Arg.Any<AuthenticatedUser>())
|
|
.Returns(("access-token", DateTimeOffset.UtcNow.AddMinutes(15)));
|
|
_refreshTokenService
|
|
.IssueAsync(userId, Arg.Any<CancellationToken>())
|
|
.Returns(new IssuedRefreshToken("refresh-token", DateTimeOffset.UtcNow.AddDays(30)));
|
|
|
|
var result = await CreateHandler()
|
|
.Handle(new LoginCommand("alice", "P@ssw0rd"), CancellationToken.None);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal("access-token", result.Value.AccessToken);
|
|
Assert.Equal("refresh-token", result.Value.RefreshToken);
|
|
Assert.Equal("alice", result.Value.User.UserName);
|
|
Assert.True(result.Value.User.TelegramLinked);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WithInvalidCredentials_ReturnsFailureWithoutIssuingTokens()
|
|
{
|
|
_identityService
|
|
.ValidateCredentialsAsync("alice", "wrong", Arg.Any<CancellationToken>())
|
|
.Returns(Result.Failure<AuthenticatedUser>(AuthErrors.InvalidCredentials));
|
|
|
|
var result = await CreateHandler()
|
|
.Handle(new LoginCommand("alice", "wrong"), CancellationToken.None);
|
|
|
|
Assert.False(result.IsSuccess);
|
|
Assert.Equal(AuthErrors.InvalidCredentials, result.Error);
|
|
await _refreshTokenService
|
|
.DidNotReceive()
|
|
.IssueAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenProfileMissingAfterValidCredentials_ReturnsInvalidCredentials()
|
|
{
|
|
var userId = Guid.NewGuid();
|
|
var authUser = new AuthenticatedUser(userId, "alice", "user");
|
|
|
|
_identityService
|
|
.ValidateCredentialsAsync("alice", "P@ssw0rd", Arg.Any<CancellationToken>())
|
|
.Returns(Result.Success(authUser));
|
|
_identityService
|
|
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
|
.Returns((CurrentUserProfile?)null);
|
|
|
|
var result = await CreateHandler()
|
|
.Handle(new LoginCommand("alice", "P@ssw0rd"), CancellationToken.None);
|
|
|
|
Assert.False(result.IsSuccess);
|
|
Assert.Equal(AuthErrors.InvalidCredentials, result.Error);
|
|
}
|
|
}
|