using PnvPanel.Application.Billing; using PnvPanel.Application.Billing.CancelPaymentRequest; using PnvPanel.Application.Tests.TestSupport; using PnvPanel.Domain.Billing; using Xunit; namespace PnvPanel.Application.Tests.Billing.CancelPaymentRequest; public class CancelPaymentRequestCommandHandlerTests { [Fact] public async Task Handle_WhenAwaitingPayment_Cancels() { using var dbContext = InMemoryDbContextFactory.Create(); var userId = Guid.NewGuid(); var request = PaymentRequest.Create(userId, PaymentPeriod.Quarter, 1000); dbContext.PaymentRequests.Add(request); await dbContext.SaveChangesAsync(CancellationToken.None); var handler = new CancelPaymentRequestCommandHandler( dbContext, FakeCurrentUser.Authenticated(userId) ); var result = await handler.Handle( new CancelPaymentRequestCommand(request.Id), CancellationToken.None ); Assert.True(result.IsSuccess); Assert.Equal(PaymentRequestStatus.Cancelled, request.Status); } [Fact] public async Task Handle_WhenAwaitingConfirmation_ReturnsNotCancellable() { using var dbContext = InMemoryDbContextFactory.Create(); var userId = Guid.NewGuid(); var request = PaymentRequest.Create(userId, PaymentPeriod.Quarter, 1000); request.MarkPaymentSent(); dbContext.PaymentRequests.Add(request); await dbContext.SaveChangesAsync(CancellationToken.None); var handler = new CancelPaymentRequestCommandHandler( dbContext, FakeCurrentUser.Authenticated(userId) ); var result = await handler.Handle( new CancelPaymentRequestCommand(request.Id), CancellationToken.None ); Assert.False(result.IsSuccess); Assert.Equal(BillingErrors.RequestNotCancellable, result.Error); } [Fact] public async Task Handle_WhenOwnedByAnotherUser_ReturnsNotFound() { using var dbContext = InMemoryDbContextFactory.Create(); var request = PaymentRequest.Create(Guid.NewGuid(), PaymentPeriod.Quarter, 1000); dbContext.PaymentRequests.Add(request); await dbContext.SaveChangesAsync(CancellationToken.None); var handler = new CancelPaymentRequestCommandHandler( dbContext, FakeCurrentUser.Authenticated(Guid.NewGuid()) ); var result = await handler.Handle( new CancelPaymentRequestCommand(request.Id), CancellationToken.None ); Assert.False(result.IsSuccess); Assert.Equal(BillingErrors.RequestNotFound, result.Error); } }