using NSubstitute; using PnvPanel.Application.Auth; using PnvPanel.Application.Auth.ChangePassword; using PnvPanel.Application.Common.Interfaces; using PnvPanel.Application.Common.Models; using PnvPanel.Application.Tests.TestSupport; using Xunit; namespace PnvPanel.Application.Tests.Auth; public class ChangePasswordCommandHandlerTests { private readonly IIdentityService _identityService = Substitute.For(); [Fact] public async Task Handle_Unauthenticated_ReturnsUnauthorized() { var handler = new ChangePasswordCommandHandler( _identityService, FakeCurrentUser.Anonymous() ); var result = await handler.Handle( new ChangePasswordCommand("old", "new"), CancellationToken.None ); Assert.False(result.IsSuccess); Assert.Equal(AuthErrors.Unauthorized, result.Error); await _identityService .DidNotReceive() .ChangePasswordAsync( Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any() ); } [Fact] public async Task Handle_Authenticated_DelegatesToIdentityService() { var userId = Guid.NewGuid(); _identityService .ChangePasswordAsync(userId, "old-pass", "new-pass", Arg.Any()) .Returns(Result.Success()); var handler = new ChangePasswordCommandHandler( _identityService, FakeCurrentUser.Authenticated(userId) ); var result = await handler.Handle( new ChangePasswordCommand("old-pass", "new-pass"), CancellationToken.None ); Assert.True(result.IsSuccess); await _identityService .Received(1) .ChangePasswordAsync(userId, "old-pass", "new-pass", Arg.Any()); } [Fact] public async Task Handle_IdentityServiceFails_PropagatesFailure() { var userId = Guid.NewGuid(); var error = Error.Validation("Auth.WrongCurrentPassword", "Текущий пароль неверен."); _identityService .ChangePasswordAsync(userId, "wrong", "new-pass", Arg.Any()) .Returns(Result.Failure(error)); var handler = new ChangePasswordCommandHandler( _identityService, FakeCurrentUser.Authenticated(userId) ); var result = await handler.Handle( new ChangePasswordCommand("wrong", "new-pass"), CancellationToken.None ); Assert.False(result.IsSuccess); Assert.Equal(error, result.Error); } }