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);
        }