public async Task RequestFriendship_ShouldReturnForbiddenResult_WhenWhenRequesterAndAddresseeIdAreTheSame() { // Arrange RequestFriendshipBody body = new RequestFriendshipBody { AddresseeId = 1 }; Claim expectedNameIdentifierClaim = new Claim(ClaimTypes.NameIdentifier, body.AddresseeId.ToString()); Mock <ClaimsPrincipal> userMock = new Mock <ClaimsPrincipal>(); userMock .Setup(m => m.FindFirst(ClaimTypes.NameIdentifier)) .Returns(expectedNameIdentifierClaim); FriendshipController controller = new FriendshipController(null, null) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = userMock.Object } } }; // Act ActionResult <FriendshipResource> response = await controller.RequestFriendship(body); // Assert ObjectResult result = Assert.IsType <ObjectResult>(response.Result); ErrorResource error = Assert.IsType <ErrorResource>(result.Value); Assert.Equal(StatusCodes.Status403Forbidden, error.StatusCode); }
public async Task RequestFriendship_ShouldReturnBadRequestResult_WhenModelValidationFails() { // Arrange RequestFriendshipBody model = new RequestFriendshipBody { AddresseeId = -123 }; FriendshipController controller = new FriendshipController(null, null); controller.ModelState.AddModelError("", ""); // Act ActionResult <FriendshipResource> response = await controller.RequestFriendship(model); // Assert Assert.IsType <BadRequestObjectResult>(response.Result); }
public async Task RequestFriendship_ShouldReturnForbiddenResult_WhenUserCombinationAlreadyExists() { // Arrange RequestFriendshipBody body = new RequestFriendshipBody { AddresseeId = 1 }; Claim expectedNameIdentifierClaim = new Claim(ClaimTypes.NameIdentifier, "2"); Mock <ClaimsPrincipal> userMock = new Mock <ClaimsPrincipal>(); userMock .Setup(m => m.FindFirst(ClaimTypes.NameIdentifier)) .Returns(expectedNameIdentifierClaim); Mock <IMediator> mediatorMock = new Mock <IMediator>(); mediatorMock .Setup(m => m.Send(It.IsAny <UserExistsQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(true); mediatorMock .Setup(m => m.Send(It.IsAny <FriendshipCombinationExistsQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(true); FriendshipController controller = new FriendshipController(mediatorMock.Object, null) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = userMock.Object } } }; // Act ActionResult <FriendshipResource> response = await controller.RequestFriendship(body); // Assert ObjectResult result = Assert.IsType <ObjectResult>(response.Result); ErrorResource error = Assert.IsType <ErrorResource>(result.Value); Assert.Equal(StatusCodes.Status403Forbidden, error.StatusCode); }
public async Task RequestFriendship_ShouldReturnCreatedResult_WithCreatedResource() { // Arrange RequestFriendshipBody body = new RequestFriendshipBody { AddresseeId = 2 }; FriendshipResource expectedFriendship = new FriendshipResource { AddresseeId = 2, RequesterId = 1, FriendshipId = 1 }; Claim expectedNameIdentifierClaim = new Claim(ClaimTypes.NameIdentifier, "1"); Mock <ClaimsPrincipal> userMock = new Mock <ClaimsPrincipal>(); userMock .Setup(m => m.FindFirst(ClaimTypes.NameIdentifier)) .Returns(expectedNameIdentifierClaim); Mock <IMediator> mediatorMock = new Mock <IMediator>(); mediatorMock .Setup(m => m.Send(It.IsAny <RequestFriendshipCommand>(), It.IsAny <CancellationToken>())) .ReturnsAsync(expectedFriendship); mediatorMock .Setup(m => m.Send(It.IsAny <UserExistsQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(true); mediatorMock .Setup(m => m.Send(It.IsAny <FriendshipCombinationExistsQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(false); MapperConfiguration mapperConfiguration = new MapperConfiguration(config => { config.CreateMap <RequestFriendshipBody, RequestFriendshipCommand>(); }); IMapper mapperMock = mapperConfiguration.CreateMapper(); FriendshipController controller = new FriendshipController(mediatorMock.Object, mapperMock) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = userMock.Object } } }; // Act ActionResult <FriendshipResource> response = await controller.RequestFriendship(body); // Assert CreatedAtActionResult result = Assert.IsType <CreatedAtActionResult>(response.Result); Assert.Equal(expectedFriendship, result.Value); }
public async Task <ActionResult <FriendshipResource> > RequestFriendship([FromBody] RequestFriendshipBody body, CancellationToken cancellationToken = default) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Get the current user id int requesterId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); // Check if requester + addressee id are the same if (requesterId == body.AddresseeId) { return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResource { StatusCode = StatusCodes.Status403Forbidden, Message = "You cannot create a friendship with yourself" })); } // Check if the addressed user exists UserExistsQuery existsQuery = new UserExistsQuery { UserId = body.AddresseeId }; bool userExists = await _mediator.Send(existsQuery, cancellationToken); if (!userExists) { return(NotFound(new ErrorResource { StatusCode = StatusCodes.Status404NotFound, Message = $"User with ID '{body.AddresseeId}' does not exist" })); } // Check if there is already a friendship with the given user combination FriendshipCombinationExistsQuery combinationExistsQuery = new FriendshipCombinationExistsQuery { RequesterId = requesterId, AddresseeId = body.AddresseeId }; bool combinationExists = await _mediator.Send(combinationExistsQuery, cancellationToken); if (combinationExists) { return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResource { StatusCode = StatusCodes.Status403Forbidden, Message = $"There is already a friendship with user {body.AddresseeId}" })); } // Create friendship RequestFriendshipCommand command = _mapper.Map <RequestFriendshipBody, RequestFriendshipCommand>(body); FriendshipResource friendship = await _mediator.Send(command, cancellationToken); return(CreatedAtAction(nameof(GetFriendshipById), new { friendshipId = friendship.FriendshipId }, friendship)); }