public async Task <IActionResult> PostRideAsync(RideViewModel rideViewModel) { var ride = mapper.Map <Ride>(rideViewModel); var result = await rideService.AddRideAsync(ride); if (result.Success) { return(Ok(result.Message)); } return(BadRequest(result.Message)); }
public async Task Create_Success_ReturnsOkResponse() { _rideService.AddRideAsync(null, null).ReturnsForAnyArgs(new CreateRideResponse()); //Setup the user so we have access to the claim stored in 'Constants.UserIdClaim' //Source: https://hudosvibe.net/post/mock-user-identity-in-asp.net-mvc-core-controller-tests _ridesController.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(Constants.UserIdClaim, "SomeCustomerId") //This would be the id of the customer })) } }; //Authorization would normally come from the header, but we have set the 'User' property above, so the value //of the authorization param is not needed in this test.(the authorization param is purely added to be able //to make the swagger documentation... var response = await _ridesController.Create(null, null) as ObjectResult; Assert.That(response.StatusCode, Is.EqualTo(StatusCodes.Status200OK)); }
public async Task <IActionResult> Create([FromHeader] string authorization, [FromBody] CreateRideRequest request) { //Get the customerId that is stored as a claim in the token var customerId = User.Claims.FirstOrDefault(x => x.Type == Constants.UserIdClaim)?.Value; //This should never happen, but better safe than sorry if (string.IsNullOrEmpty(customerId)) { throw new UserIdInvalidException( $"The supplied JSON Web Token does not contain a valid value in the '{ Constants.UserIdClaim }' claim."); } var response = await _rideService.AddRideAsync(request, customerId); return(Ok(response)); }