Files
PnvPanel/backend/tests/PnvPanel.Application.Tests/Common/Behaviors/RequireActivationBehaviorTests.cs
T
Leonid Pershin 14b64a3140
CI / Backend (build + test) (push) Successful in 1m26s
CI / Frontend (lint + typecheck + build) (push) Successful in 30s
Implement activation checks across various commands and queries
- Introduced `IRequiresActivation` interface to enforce activation requirements for multiple commands and queries, ensuring that only activated users can create, edit, or access configurations, news, and applications.
- Updated the `RequireActivationBehavior` to handle activation checks uniformly, returning appropriate errors for unauthenticated or inactive users.
- Enhanced error handling by adding `NotActivated` error to provide clear feedback for users attempting to access restricted features.
- Updated documentation to reflect the new activation requirements and their implications on user access and functionality.
2026-07-13 18:51:03 +03:00

86 lines
3.1 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");
[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);
}
}