public async Task Proccess_And_Persist_Payment_When_Acquiring_Bank_Fails_To_Process_Payment()
        {
            var someId = Guid.NewGuid().ToString();
            var mockPaymentRepository = new Mock <IAggregateStore <Payment> >();
            var mockCyptoService      = new Mock <ICryptoService>();
            var mockBankClient        = new Mock <IAquiringBankClient>();
            var mockLogger            = new Mock <ILogger>();

            mockBankClient.Setup(x => x.ProcessPayment(It.IsAny <BankPaymentRequest>()))
            .ReturnsAsync(new BankPaymentResponse {
                PaymentIdentifier = someId, PaymentStatus = PaymentStatus.Failed
            });
            mockCyptoService.Setup(x => x.Encrypt(It.IsAny <string>(), It.IsAny <string>()))
            .Returns("__encripted__");
            var sut = new ProcessPaymentCommandHandler(mockPaymentRepository.Object, mockCyptoService.Object, mockBankClient.Object, mockLogger.Object);
            ProcessPaymentCommand command = new ProcessPaymentCommand
            {
                Amount          = 100,
                Currency        = "EUR",
                CardExpiryYear  = "24",
                CardExpiryMonth = "4",
                CardNumber      = "5564876598743467",
                CVV             = "782",
            };

            await sut.Handle(command, new System.Threading.CancellationToken());

            mockCyptoService.Verify(x => x.Encrypt(command.CardNumber, It.IsAny <string>()), Times.Once);
            mockPaymentRepository.Verify(x => x.AppendChanges(It.Is <Payment>(y => y.CardNumber == "__encripted__")), Times.Once);
            mockPaymentRepository.Verify(x => x.AppendChanges(It.Is <Payment>(y => y.BankPaymentIdentifier == someId)), Times.Once);
            mockPaymentRepository.Verify(x => x.AppendChanges(It.Is <Payment>(y => y.PaymentStatus == PaymentStatus.Failed)), Times.Once);
        }
Пример #2
0
        public void ProcessPayment_Should_ThrowException_When_NoInvoiceFoundForInvoiceId()
        {
            var command = new ProcessPaymentCommand
            {
                InvoiceId = Guid.NewGuid(),
                Amount    = 10,
            };
            var handler = new ProcessPaymentCommandHandler(InvoiceRepository);

            Assert.ThrowsAsync <NotFoundException>(async() => await handler.Handle(command, CancellationToken.None));
        }
Пример #3
0
        public void Handle_WhenClosed_ShouldThrowException()
        {
            _mediator.Setup(m => m.Send(It.IsAny <ReduceBalanceCommand>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(true));
            var sut     = new ProcessPaymentCommandHandler(_context, _mediator.Object, CurrentUser);
            var request = new ProcessPaymentCommand
            {
                Id = Guid.Parse("debf1d88-47ac-4fe4-a0b0-ce42f72ea66e")
            };

            Assert.ThrowsAsync <ValidationException>(() => sut.Handle(request, CancellationToken.None));
        }
Пример #4
0
        public async Task ProcessPayment_PayCorrectAmount_InvoicePaidOff()
        {
            var command = new ProcessPaymentCommand
            {
                InvoiceId = TestData.Invoice1Id,
                Amount    = 10,
            };
            var handler = new ProcessPaymentCommandHandler(InvoiceRepository);
            var result  = await handler.Handle(command, CancellationToken.None);

            var invoice = DbContext.Invoices.First(x => x.Id == command.InvoiceId);

            Assert.AreEqual(10, invoice.AmountPaid);
            Assert.AreEqual(10, invoice.Payments.Sum(x => x.Amount));
        }
Пример #5
0
        public void Handle_WhenPendingWithLowBalance_ShouldSetToClosed()
        {
            _mediator.Setup(m => m.Send(It.IsAny <ReduceBalanceCommand>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(false));
            var sut     = new ProcessPaymentCommandHandler(_context, _mediator.Object, CurrentUser);
            var request = new ProcessPaymentCommand
            {
                Id = Guid.Parse("b162e88d-a3a6-4341-87da-725658d743f3")
            };

            var result = sut.Handle(request, CancellationToken.None).Result;

            Assert.AreEqual(Unit.Value, result);
            var record = _context.Payments.Single(i => i.Id == request.Id);

            Assert.AreEqual(PaymentStatus.Closed, record.Status);
        }
Пример #6
0
        public void ProcessPayment_Should_ThrowException_When_PartialAmountPaid_And_NewAmountTooMuch()
        {
            var command = new ProcessPaymentCommand
            {
                InvoiceId = TestData.Invoice3Id,
                Amount    = 100,
            };
            var handler = new ProcessPaymentCommandHandler(InvoiceRepository);

            Assert.ThrowsAsync <Exceptions.InvalidOperationException>(async() => await handler.Handle(command, CancellationToken.None));
        }
Пример #7
0
        public void ProcessPayment_Should_ThrowException_When_NoPaymentNeeded()
        {
            var command = new ProcessPaymentCommand
            {
                InvoiceId = TestData.Invoice4Id,
                Amount    = 10,
            };
            var handler = new ProcessPaymentCommandHandler(InvoiceRepository);

            Assert.ThrowsAsync <Exceptions.InvalidOperationException>(async() => await handler.Handle(command, CancellationToken.None));
        }
Пример #8
0
        public void ProcessPayment_Should_ThrowException_When_InvoiceInvalidState()
        {
            var guid = Guid.NewGuid();

            DbContext.Invoices.Add(new Invoice()
            {
                Id       = guid,
                Amount   = Decimal.Zero,
                Payments = new List <Payment>()
                {
                    new Payment()
                    {
                        Amount = 100
                    }
                }
            });

            var command = new ProcessPaymentCommand
            {
                InvoiceId = guid,
                Amount    = 10,
            };
            var handler = new ProcessPaymentCommandHandler(InvoiceRepository);

            Assert.ThrowsAsync <Exceptions.InvalidOperationException>(async() => await handler.Handle(command, CancellationToken.None));
        }