- 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.
233 lines
7.0 KiB
C#
233 lines
7.0 KiB
C#
using PnvPanel.Domain.Configs;
|
|
using PnvPanel.Domain.Exceptions;
|
|
using PnvPanel.Domain.Inbounds;
|
|
using Xunit;
|
|
|
|
namespace PnvPanel.Domain.Tests.Configs;
|
|
|
|
public class VpnConfigTests
|
|
{
|
|
[Fact]
|
|
public void Create_SetsActiveStatusAndGeneratesEmailAndToken()
|
|
{
|
|
var userId = Guid.NewGuid();
|
|
var inboundId = Guid.NewGuid();
|
|
|
|
var config = VpnConfig.Create(userId, inboundId, VpnProtocol.Vless, "My device");
|
|
|
|
Assert.Equal(userId, config.UserId);
|
|
Assert.Equal(inboundId, config.InboundId);
|
|
Assert.Equal(VpnProtocol.Vless, config.Protocol);
|
|
Assert.Equal("My device", config.Label);
|
|
Assert.Equal(ConfigStatus.Active, config.Status);
|
|
Assert.Equal(string.Empty, config.ClientExternalId);
|
|
Assert.False(string.IsNullOrWhiteSpace(config.ClientEmail));
|
|
Assert.StartsWith("pnv_", config.ClientEmail);
|
|
Assert.False(string.IsNullOrWhiteSpace(config.SubscriptionToken));
|
|
Assert.NotEqual(Guid.Empty, config.Id);
|
|
}
|
|
|
|
[Fact]
|
|
public void Create_GeneratesUniqueSubscriptionTokensAndClientEmails()
|
|
{
|
|
var userId = Guid.NewGuid();
|
|
var a = VpnConfig.Create(userId, Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
var b = VpnConfig.Create(userId, Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
|
|
Assert.NotEqual(a.SubscriptionToken, b.SubscriptionToken);
|
|
Assert.NotEqual(a.ClientEmail, b.ClientEmail);
|
|
}
|
|
|
|
[Fact]
|
|
public void AssignRemoteClient_SetsClientExternalId()
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Trojan, null);
|
|
|
|
config.AssignRemoteClient("some-remote-password");
|
|
|
|
Assert.Equal("some-remote-password", config.ClientExternalId);
|
|
}
|
|
|
|
[Fact]
|
|
public void Rotate_WhenActive_ChangesEmailExternalIdAndToken()
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
config.AssignRemoteClient("old-id");
|
|
var oldToken = config.SubscriptionToken;
|
|
var oldEmail = config.ClientEmail;
|
|
|
|
config.Rotate("new-email", "new-id");
|
|
|
|
Assert.Equal("new-email", config.ClientEmail);
|
|
Assert.Equal("new-id", config.ClientExternalId);
|
|
Assert.NotEqual(oldToken, config.SubscriptionToken);
|
|
Assert.NotEqual(oldEmail, config.ClientEmail);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(ConfigStatus.Revoked)]
|
|
[InlineData(ConfigStatus.Disabled)]
|
|
public void Rotate_WhenNotActive_Throws(ConfigStatus status)
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
MoveToStatus(config, status);
|
|
|
|
Assert.Throws<DomainException>(() => config.Rotate("e", "i"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Revoke_WhenActive_SetsRevokedStatus()
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
|
|
config.Revoke();
|
|
|
|
Assert.Equal(ConfigStatus.Revoked, config.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void Revoke_WhenAlreadyRevoked_Throws()
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
config.Revoke();
|
|
|
|
Assert.Throws<DomainException>(() => config.Revoke());
|
|
}
|
|
|
|
[Fact]
|
|
public void Disable_WhenActive_SetsDisabled()
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
|
|
config.Disable();
|
|
|
|
Assert.Equal(ConfigStatus.Disabled, config.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void Disable_WhenRevoked_DoesNotChangeStatus()
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
config.Revoke();
|
|
|
|
config.Disable();
|
|
|
|
Assert.Equal(ConfigStatus.Revoked, config.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void Enable_WhenDisabled_ReturnsToActive()
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
config.Disable();
|
|
|
|
config.Enable();
|
|
|
|
Assert.Equal(ConfigStatus.Active, config.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void Enable_WhenRevoked_DoesNotResurrect()
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
config.Revoke();
|
|
|
|
config.Enable();
|
|
|
|
Assert.Equal(ConfigStatus.Revoked, config.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void Suspend_WhenActive_SetsExpired()
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
|
|
config.Suspend();
|
|
|
|
Assert.Equal(ConfigStatus.Expired, config.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void Suspend_WhenDisabledByAdmin_DoesNotOverrideBlock()
|
|
{
|
|
// Suspend (биллинг) не должен путать своё состояние с Disable (блокировка админом) — иначе
|
|
// Resume() ошибочно вернёт в Active конфиг, погашенный не за неуплату.
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
config.Disable();
|
|
|
|
config.Suspend();
|
|
|
|
Assert.Equal(ConfigStatus.Disabled, config.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void Resume_WhenExpired_ReturnsToActive()
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
config.Suspend();
|
|
|
|
config.Resume();
|
|
|
|
Assert.Equal(ConfigStatus.Active, config.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void Resume_WhenDisabledByAdmin_DoesNotResurrect()
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
config.Disable();
|
|
|
|
config.Resume();
|
|
|
|
Assert.Equal(ConfigStatus.Disabled, config.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void SetBillingExpiry_SetsExpiresAt()
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
var expiresAt = DateTimeOffset.UtcNow.AddMonths(3);
|
|
|
|
config.SetBillingExpiry(expiresAt);
|
|
|
|
Assert.Equal(expiresAt, config.ExpiresAt);
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateTraffic_SetsBytesAndLastSyncAt()
|
|
{
|
|
var config = VpnConfig.Create(Guid.NewGuid(), Guid.NewGuid(), VpnProtocol.Vless, null);
|
|
|
|
config.UpdateTraffic(100, 200);
|
|
|
|
Assert.Equal(100, config.UsedUpBytes);
|
|
Assert.Equal(200, config.UsedDownBytes);
|
|
Assert.NotNull(config.LastSyncAt);
|
|
}
|
|
|
|
[Fact]
|
|
public void GenerateClientEmail_IsDeterministicPrefixWithRandomSuffix()
|
|
{
|
|
var userId = Guid.NewGuid();
|
|
var email1 = VpnConfig.GenerateClientEmail(userId);
|
|
var email2 = VpnConfig.GenerateClientEmail(userId);
|
|
|
|
var expectedPrefix = $"pnv_{userId:N}"[..12];
|
|
Assert.StartsWith(expectedPrefix, email1);
|
|
Assert.NotEqual(email1, email2);
|
|
}
|
|
|
|
private static void MoveToStatus(VpnConfig config, ConfigStatus status)
|
|
{
|
|
switch (status)
|
|
{
|
|
case ConfigStatus.Revoked:
|
|
config.Revoke();
|
|
break;
|
|
case ConfigStatus.Disabled:
|
|
config.Disable();
|
|
break;
|
|
}
|
|
}
|
|
}
|