コード例 #1
0
        public async Task CreatePaymentAsync_WhenReturnsError_ExpectedBadRequestAsync()
        {
            var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
            {
                new Claim("sub", Guid.NewGuid().ToString()),
            }, "mock"));

            _classUnderTest.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = user
                }
            };

            CreatePayment actualCommand = null;

            var paymentFailure = GetMockedPayment();

            var createPayment = new Seq <Failure>
            {
                Failure.Of(paymentFailure, "Value -1 has to be non-negative")
            };

            _mocker.GetMock <IMediator>()
            .Setup(x => x.Send(It.IsAny <CreatePayment>(), It.IsAny <CancellationToken>()))
            .Callback <IRequest <Either <Seq <Failure>, int> >, CancellationToken>((command, ct) => actualCommand = (CreatePayment)command)
            .ReturnsAsync(Left <Seq <Failure>, int>(createPayment));

            var request = new CreatePaymentRequest
            {
                CardDetails = new CreatePaymentRequest.CardDto
                {
                    CVV        = 123,
                    Expiration = new CreatePaymentRequest.ExpirationDateDto
                    {
                        Month = 3,
                        Year  = 2033
                    },
                    Number = "1234-5678-9101-1121"
                },
                Value = new CreatePaymentRequest.MoneyDto
                {
                    Amount          = 12345,
                    ISOCurrencyCode = "EUR"
                }
            };

            var response = await _classUnderTest.CreatePaymentAsync(request);

            response.Should().BeOfType <BadRequestObjectResult>();
            _mocker.GetMock <IMediator>().Verify(x => x.Send(It.IsAny <CreatePayment>(), It.IsAny <CancellationToken>()), Times.Once);
        }
コード例 #2
0
        public async void CreatePaymentAsync_ShouldHaveIdInCreatedAt()
        {
            var accountId = Guid.NewGuid();
            var paymentId = Guid.NewGuid();

            var mediator = new Mock <IMediator>();

            mediator.Setup(m => m.Send(It.IsAny <CreateAccountPaymentCommand>(), It.IsAny <CancellationToken>())).ReturnsAsync(paymentId);

            var controller = new PaymentController(mediator.Object);

            var result = (CreatedAtActionResult)await controller.CreatePaymentAsync(accountId, new CreatePaymentCommand { Amount = 10, Date = new DateTime(2020, 3, 1) });

            var id = Guid.Parse(result.RouteValues["id"].ToString());

            Assert.Equal(paymentId, id);
        }
コード例 #3
0
        public void CreatePaymentAsync_Validation_Error_Returns_418()
        {
            // Arrange
            var validationProviderMock = _mocks.Create <IValidate <PaymentModel> >();

            validationProviderMock.Setup(m => m.Validate(It.IsAny <PaymentModel>()))
            .Returns(ValidationResult.ToTeapotResult(ApiOffences.Missing, "Test"));

            var controller = new PaymentController(null, validationProviderMock.Object);

            // Act
            var res = controller.CreatePaymentAsync(null).Result;

            // Assert
            _mocks.Verify();
            Assert.AreEqual(((ValidationResult)res.Result).StatusCode, 418);
        }
コード例 #4
0
        public void CreatePaymentAsync_Success_Returns_PaymentModel()
        {
            // Arrange
            var model = new PaymentModel()
            {
                CardHolderName = "Mr Tinkle",
                CardNumber     = "1234323545353",
                ExpiryDate     = "2020-12-08",
                Amount         = 100.2m,
                Currency       = "AAA",
                Cvv            = "123",
                State          = PaymentState.New
            };

            var validationProviderMock = _mocks.Create <IValidate <PaymentModel> >();

            validationProviderMock.Setup(m => m.Validate(It.IsAny <PaymentModel>()))
            .Returns <ValidationResult>(null);

            var paymentBLLMock = _mocks.Create <IPaymentBLL>();

            paymentBLLMock.Setup(m => m.CreatePaymentAsync(It.Is <PaymentDto>(p =>
                                                                              p.CardHolderName == model.CardHolderName &&
                                                                              p.CardNumber == model.CardNumber &&
                                                                              p.ExpiryDate == model.ExpiryDate &&
                                                                              p.Amount == model.Amount &&
                                                                              p.Currency == model.Currency &&
                                                                              p.Cvv == model.Cvv &&
                                                                              p.PaymentDate != null &&
                                                                              p.State == model.State)))
            .Returns(Task.FromResult(true));

            var controller = new PaymentController(paymentBLLMock.Object, validationProviderMock.Object);

            // Act
            var res = controller.CreatePaymentAsync(model).Result;

            // Assert
            _mocks.Verify();
            Assert.IsInstanceOfType(res.Value, typeof(PaymentModel));
        }
コード例 #5
0
        public void CreatePaymentAsync_Create_Error_Returns_500()
        {
            // Arrange
            var validationProviderMock = _mocks.Create <IValidate <PaymentModel> >();

            validationProviderMock.Setup(m => m.Validate(It.IsAny <PaymentModel>()))
            .Returns <ValidationResult>(null);

            var paymentBLLMock = _mocks.Create <IPaymentBLL>();

            paymentBLLMock.Setup(m => m.CreatePaymentAsync(It.IsAny <PaymentDto>()))
            .Returns(Task.FromResult(false));

            var controller = new PaymentController(paymentBLLMock.Object, validationProviderMock.Object);

            // Act
            var res = controller.CreatePaymentAsync(new PaymentModel()).Result;

            // Assert
            _mocks.Verify();
            Assert.AreEqual(((StatusCodeResult)res.Result).StatusCode, 500);
        }