public async Task Post_Should_Throw_Exception_If_The_PaymentRequest_Is_Null() { // Arrange // Act async Task HandleRequest() => await _paymentsController.Post(null, default(CancellationToken)); // Assert await Assert.ThrowsAsync <ArgumentNullException>(HandleRequest); }
public void Post_NoValue() { // Act var actionsResult = _paymentsController.Post(null).GetAwaiter().GetResult(); var result = actionsResult as StatusCodeResult; // Assert Assert.AreEqual(400, result.StatusCode, "The result should be as expected"); }
public async Task PaymentWithDifferingTicketIdReturnsBadRequest() { // arrange var paymentRequest = new PaymentRequest { TicketId = 33, CreditCard = "1111 1111 1111 1111" }; // act var result = await _controller.Post(12, paymentRequest); // assert Assert.IsType <BadRequestObjectResult>(result); }
public void TestPaymentDetailsSaved() { // We do not need to do this step if the payment was saved in database with a identity or primary key. if (File.Exists(filePath)) { File.Delete(filePath); } PaymentModel model = new PaymentModel { AccountName = "Test", AccountNumber = 12345678, Amount = 200.00M, BSB = 123456, Currency = "AUD", Reference = "None" }; PaymentsController pc = new PaymentsController(); pc.Request = new HttpRequestMessage(); pc.Configuration = new System.Web.Http.HttpConfiguration(); HttpResponseMessage response = pc.Post(model); Assert.IsTrue(response.IsSuccessStatusCode); var savedPayment = ReadContentFromFile(); Assert.AreEqual(model.AccountNumber, savedPayment.AccountNumber); }
public void PostPayment_ShouldAccept() { CardDto card = GenerateRandomCard(); PaymentRequestDto paymentRequest = new PaymentRequestDto(); paymentRequest.MerchantId = 1; paymentRequest.Amount = 25.99; paymentRequest.Currency = "CAD"; paymentRequest.Card = card; var okResult = _paymentsController.Post(paymentRequest); Assert.IsType <OkObjectResult>(okResult.Result); OkObjectResult objectResult = okResult.Result as OkObjectResult; Assert.IsType <PaymentResponseDto>(objectResult.Value); PaymentResponseDto paymentResponse = objectResult.Value as PaymentResponseDto; Assert.True(paymentResponse.Id > 0); Assert.NotNull(paymentResponse.Status); }
public void ReturnStringWhenPaymentMethodIsBoleto() { //Arrange const int billLength = 47; var parm = new PaymentRequest { Amount = 100, Type = "BOLETO", Client = new ClientRequest { Id = 10 }, Buyer = new BuyerRequest { Cpf = "1111111", Email = "*****@*****.**", Name = "Hari Seldon" } }; //Act var postResult = _controller.Post(parm) as CreatedAtRouteResult; //Assert Assert.True(postResult.Value.ToString().Length == billLength); }
public async Task Should_Return_Bad_request_When_Merchant__Not_Identified() { // Arrange var paymentServiceMock = new Mock <IPaymentService>(); paymentServiceMock.Setup(x => x.CreatePayment(It.IsAny <CreatePaymentRequest>(), It.IsAny <Merchant>())) .Returns((CreatePaymentRequest x, Merchant y) => Task.FromResult((CreatePaymentResponse)null)); var controller = new PaymentsController(paymentServiceMock.Object); //Act var result = (await controller.Post(Mock.Of <CreatePaymentRequest>())) as BadRequestObjectResult; // Assert Assert.Equal(result.StatusCode, BadRequestResultCode); }
public async Task Post_InvalidModel_Returns400() { var mockPaymentRepo = new Mock <IPaymentRepository>(); var mockLogger = new Mock <ILogger <PaymentsController> >(); var mockMapper = new Mock <IMapper>(); var mockBankRequestService = new Mock <IBankRequestService>(); var mockEncryptionService = new Mock <IEncryptionService>(); var paymentsController = new PaymentsController(mockPaymentRepo.Object, mockBankRequestService.Object, mockEncryptionService.Object, mockMapper.Object, mockLogger.Object); paymentsController.ModelState.AddModelError("Name", "Required"); var response = await paymentsController.Post(new PostPaymentRequest { CardNumber = "4111" }); Assert.IsType <BadRequestResult>(response); }
public async Task GivenModelInvalidRenderIndexViewWithModelErrors() { _controller.ModelState.AddModelError("", "test"); var model = new PaymentModel(); var result = await _controller.Post(model); var viewResult = result.ShouldBeAssignableTo <ViewResult>(); viewResult.ViewData.ModelState.IsValid.ShouldBeFalse(); viewResult.ViewName.ShouldBe(nameof(PaymentsController.Index)); }
public async Task Should_Return_OK_When_Merchant_Identified() { // Arrange var paymentServiceMock = new Mock <IPaymentService>(); paymentServiceMock.Setup(x => x.CreatePayment(It.IsAny <CreatePaymentRequest>(), It.IsAny <Merchant>())) .Returns((CreatePaymentRequest x, Merchant y) => Task.FromResult(new CreatePaymentResponse())); var controller = new PaymentsController(paymentServiceMock.Object); //Act var result = (await controller.Post(Mock.Of <CreatePaymentRequest>())) as OkObjectResult; // Assert paymentServiceMock.Verify(x => x.CreatePayment(It.IsAny <CreatePaymentRequest>(), It.IsAny <Merchant>()), Times.Once); Assert.Equal(result.StatusCode, OkResultCode); Assert.IsType <CreatePaymentResponse>(result.Value); }
public async Task Post_DatabaseFails_Returns500() { var mockPaymentRepo = new Mock <IPaymentRepository>(); var mockLogger = new Mock <ILogger <PaymentsController> >(); var mockMapper = new Mock <IMapper>(); var mockBankRequestService = new Mock <IBankRequestService>(); var mockEncryptionService = new Mock <IEncryptionService>(); var payment = new Payment { Id = Guid.NewGuid() }; mockPaymentRepo.Setup(x => x.AddPaymentAsync(It.IsAny <Payment>())).Throws(new DbUpdateException()); mockMapper.Setup(x => x.Map <Payment>(It.IsAny <PostPaymentRequest>())).Returns(payment); var paymentsController = new PaymentsController(mockPaymentRepo.Object, mockBankRequestService.Object, mockEncryptionService.Object, mockMapper.Object, mockLogger.Object); var response = await paymentsController.Post(new PostPaymentRequest { CardNumber = "4111111111111111" }); var statusCodeResult = Assert.IsType <StatusCodeResult>(response); Assert.Equal(500, statusCodeResult.StatusCode); }
public async Task Post_Success_Returns200() { var mockPaymentRepo = new Mock <IPaymentRepository>(); var mockLogger = new Mock <ILogger <PaymentsController> >(); var mockMapper = new Mock <IMapper>(); var mockBankRequestService = new Mock <IBankRequestService>(); var mockEncryptionService = new Mock <IEncryptionService>(); var payment = new Payment { Id = Guid.NewGuid() }; mockMapper.Setup(x => x.Map <Payment>(It.IsAny <PostPaymentRequest>())).Returns(payment); mockBankRequestService.Setup(x => x.PostBankRequestAsync(It.IsAny <BankRequest>())).ReturnsAsync(() => { return(new BankResponse()); }); var paymentsController = new PaymentsController(mockPaymentRepo.Object, mockBankRequestService.Object, mockEncryptionService.Object, mockMapper.Object, mockLogger.Object); var response = await paymentsController.Post(new PostPaymentRequest { CardNumber = "4111111111111111" }); var okResult = Assert.IsType <OkObjectResult>(response); var paymentResponse = Assert.IsType <PostPaymentResponse>(okResult.Value); Assert.Equal(payment.Id, paymentResponse.PaymentId); }
public async void PostAsync_True() { //Arrange string connectionString = @"Data Source=VLAD191100\VLAD191100;Database=HyperWalletTest;User ID=vlad191100;Password=Vlad18201111;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; DbContextOptionsBuilder <Context> options = new DbContextOptionsBuilder <Context>(); options.UseSqlServer(connectionString); Context context = new Context(options.Options); PaymentsController controller = new PaymentsController(context); string id = new Random().Next().ToString(); Payment payment = new Payment() { Amount = 20, Id = id.ToString(), Currency = "USD", DestinationToken = "usr-4beda015-edb5-4dd1-a881-60ae59c8db50" }; //Act bool result = await controller.Post(payment); //Assert Assert.True(result); }
public void Post_Payment_Returns_Created_Result() { var result = _paymentsController.Post(_newPayment); Assert.IsType <CreatedAtRouteNegotiatedContentResult <Payment> >(result); }