public async Task <ActionResult> Post([FromBody] PagamentoDtoCreate pagamento) { if (!ModelState.IsValid) { return(StatusCode((int)HttpStatusCode.BadRequest, "Ocorreu um Erro Desconhecido")); } if (pagamento.Valor < 0) { return(StatusCode((int)HttpStatusCode.PreconditionFailed, "Os valores informados não são válidos")); } try { var result = _service.ProcessarPagamento(pagamento); if (result != null) { return(Ok(result)); } else { return(BadRequest()); } } catch (ArgumentException e) { return(StatusCode((int)HttpStatusCode.InternalServerError, e.Message)); } }
public async Task E_Possivel_Realizar_Cotroller_BadRequest() { var serviceMock = new Mock <IPagamentoService>(); var Valor = Faker.RandomNumber.Next(0, 10000); var cartao = new cartao { titular = Faker.Name.FullName(), numero = Faker.Name.FullName(), cvv = Faker.Name.FullName(), bandeira = Faker.Name.FullName(), data_expiracao = Faker.Name.FullName() }; _controller = new PagamentoController(serviceMock.Object); _controller.ModelState.AddModelError("Name", "É um campo obrigatório"); Mock <IUrlHelper> url = new Mock <IUrlHelper>(); url.Setup(x => x.Link(It.IsAny <string>(), It.IsAny <object>())).Returns("http://localhost:5000"); _controller.Url = url.Object; var PagamentoDtoCreate = new PagamentoDtoCreate { Valor = Valor, Cartao = cartao, }; var result = await _controller.Post(PagamentoDtoCreate); ObjectResult resultValue = Assert.IsType <ObjectResult>(result); Assert.Equal(400, resultValue.StatusCode); Assert.Equal(resultValue.Value, "Ocorreu um Erro Desconhecido"); }
public async Task E_Possivel_Realizar_Cotroller_OutRange() { var serviceMock = new Mock <IPagamentoService>(); var Valor = Faker.RandomNumber.Next(-10, 0); var cartao = new cartao { titular = Faker.Name.FullName(), numero = Faker.Name.FullName(), cvv = Faker.Name.FullName(), bandeira = Faker.Name.FullName(), data_expiracao = Faker.Name.FullName(), }; _controller = new PagamentoController(serviceMock.Object); Mock <IUrlHelper> url = new Mock <IUrlHelper>(); url.Setup(x => x.Link(It.IsAny <string>(), It.IsAny <object>())).Returns("http://localhost:5000"); _controller.Url = url.Object; var PagamentoDtoCreate = new PagamentoDtoCreate { Valor = Valor, Cartao = cartao, }; var result = await _controller.Post(PagamentoDtoCreate); ObjectResult resultValue = Assert.IsType <ObjectResult>(result); Assert.Equal(412, resultValue.StatusCode); Assert.Equal(resultValue.Value, "Os valores informados não são válidos"); }
public PagamentoDtoCreateResult ProcessarPagamento(PagamentoDtoCreate Pagamento) { var result = new PagamentoDtoCreateResult { Valor = Pagamento.Valor, Estado = null, }; if (Pagamento.Valor > 100) { result.Estado = "Aprovado"; } else { result.Estado = "Reprovado"; } return(result); }
public async Task E_Possivel_Realizar_Cotroller_Created() { var serviceMock = new Mock <IPagamentoService>(); var Valor = Faker.RandomNumber.Next(0, 10000); var cartao = new cartao { titular = Faker.Name.FullName(), numero = Faker.Name.FullName(), cvv = Faker.Name.FullName(), bandeira = Faker.Name.FullName(), data_expiracao = Faker.Name.FullName(), }; _controller = new PagamentoController(serviceMock.Object); Mock <IUrlHelper> url = new Mock <IUrlHelper>(); url.Setup(x => x.Link(It.IsAny <string>(), It.IsAny <object>())).Returns("http://localhost:5000"); _controller.Url = url.Object; var PagamentoDtoCreate = new PagamentoDtoCreate { Valor = Valor, Cartao = cartao, }; var result = await _controller.Post(PagamentoDtoCreate); Assert.True(result is OkObjectResult); var resultValue = ((OkObjectResult)result).Value as PagamentoDtoCreateResult; Assert.NotNull(resultValue); Assert.Equal(PagamentoDtoCreate.Valor, resultValue.Valor); Assert.Equal("Aprovado", resultValue.Estado); }