Exemplo n.º 1
0
    public async Task RequestFriendshipCommandHandler_ShouldReturnCreatedFriendship()
    {
        // Arrange
        RequestFriendshipCommand request = new RequestFriendshipCommand {
            AddresseeId = 2
        };

        _unitOfWorkMock
        .Setup(m => m.CommitAsync(It.IsAny <CancellationToken>()))
        .ReturnsAsync(1);

        _unitOfWorkMock
        .Setup(m => m.Friendships.Add(It.IsAny <Friendship>(), It.IsAny <CancellationToken>()))
        .Returns(Task.CompletedTask)
        .Callback <Friendship, CancellationToken>((f, _) => f.FriendshipId = 1);

        RequestFriendshipCommand.Handler handler = new RequestFriendshipCommand.Handler(_userProviderMock.Object, _unitOfWorkMock.Object, _dateProviderMock.Object, _mapperMock);

        // Act
        FriendshipResource friendship = await handler.Handle(request);

        // Assert
        Assert.NotNull(friendship);
        Assert.NotEqual(0, friendship.FriendshipId);
        Assert.Equal(1, friendship.RequesterId);
        Assert.Equal(2, friendship.AddresseeId);
    }
Exemplo n.º 2
0
        public async Task GetFriendshipById_ShouldReturnOkResult_WhenFriendshipExists()
        {
            // Arrange
            const int friendshipId = 1;

            FriendshipResource expectedFriendship = new FriendshipResource {
                FriendshipId = friendshipId
            };

            Mock <IMediator> mediatorMock = new Mock <IMediator>();

            mediatorMock
            .Setup(m => m.Send(It.IsAny <GetFriendshipByIdQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(expectedFriendship);

            FriendshipController controller = new FriendshipController(mediatorMock.Object, null);

            // Act
            ActionResult <FriendshipResource> response = await controller.GetFriendshipById(friendshipId);

            // Assert
            OkObjectResult result = Assert.IsType <OkObjectResult>(response.Result);

            FriendshipResource friendship = Assert.IsType <FriendshipResource>(result.Value);

            Assert.Equal(friendshipId, friendship.FriendshipId);
        }
Exemplo n.º 3
0
        public async Task GetFriendshipByIdQueryHandler_ShouldReturnFriendship_WhenFriendshipExists()
        {
            // Arrange
            GetFriendshipByIdQuery request = new GetFriendshipByIdQuery {
                FriendshipId = 1
            };

            IEnumerable <Friendship> expectedFriendships = new[]
            {
                new Friendship {
                    FriendshipId = 1
                }
            };

            Mock <IQueryable <Friendship> > friendshipQueryableMock = expectedFriendships
                                                                      .AsQueryable()
                                                                      .BuildMock();

            _unitOfWorkMock
            .Setup(m => m.Friendships.GetById(request.FriendshipId))
            .Returns(friendshipQueryableMock.Object);

            GetFriendshipByIdQuery.Handler handler = new GetFriendshipByIdQuery.Handler(_unitOfWorkMock.Object, _mapperMock);

            // Act
            FriendshipResource friendship = await handler.Handle(request);

            // Assert
            Assert.NotNull(friendship);
            Assert.Equal(1, friendship.FriendshipId);
        }
Exemplo n.º 4
0
    public async Task GetFriendshipByIdQueryHandler_ShouldReturnNull_WhenFriendshipIsNotFound()
    {
        // Arrange
        GetFriendshipByIdQuery request = new() { FriendshipId = 2151 };

        _unitOfWorkMock
        .Setup(m => m.Friendships.GetByIdAsync(request.FriendshipId))
        .ReturnsAsync(null as Friendship);

        GetFriendshipByIdQuery.Handler handler = new(_unitOfWorkMock.Object, _mapperMock);

        // Act
        FriendshipResource friendship = await handler.Handle(request);

        // Assert
        Assert.Null(friendship);
    }
Exemplo n.º 5
0
        public async Task <ActionResult <FriendshipResource> > GetFriendshipById([FromRoute] int friendshipId, CancellationToken cancellationToken = default)
        {
            GetFriendshipByIdQuery query = new GetFriendshipByIdQuery
            {
                FriendshipId = friendshipId
            };

            FriendshipResource friendship = await _mediator.Send(query, cancellationToken);

            if (friendship == null)
            {
                return(NotFound(new ErrorResource
                {
                    StatusCode = StatusCodes.Status404NotFound,
                    Message = $"Friendship with ID '{friendshipId}' does not exist"
                }));
            }

            return(Ok(friendship));
        }
Exemplo n.º 6
0
    public async Task GetFriendshipByIdQueryHandler_ShouldReturnFriendship_WhenFriendshipExists()
    {
        // Arrange
        GetFriendshipByIdQuery request = new() { FriendshipId = 1 };

        Friendship expectedFriendship = new() { FriendshipId = 1 };

        _unitOfWorkMock
        .Setup(m => m.Friendships.GetByIdAsync(request.FriendshipId))
        .ReturnsAsync(expectedFriendship);

        GetFriendshipByIdQuery.Handler handler = new(_unitOfWorkMock.Object, _mapperMock);

        // Act
        FriendshipResource friendship = await handler.Handle(request);

        // Assert
        Assert.NotNull(friendship);
        Assert.Equal(1, friendship.FriendshipId);
    }
}
Exemplo n.º 7
0
        public async Task GetFriendshipByIdQueryHandler_ShouldReturnNull_WhenFriendshipIsNotFound()
        {
            // Arrange
            GetFriendshipByIdQuery request = new GetFriendshipByIdQuery {
                FriendshipId = 2151
            };

            Mock <IQueryable <Friendship> > expectedFriendships = Enumerable
                                                                  .Empty <Friendship>()
                                                                  .AsQueryable()
                                                                  .BuildMock();

            _unitOfWorkMock
            .Setup(m => m.Friendships.GetById(request.FriendshipId))
            .Returns(expectedFriendships.Object);

            GetFriendshipByIdQuery.Handler handler = new GetFriendshipByIdQuery.Handler(_unitOfWorkMock.Object, _mapperMock);

            // Act
            FriendshipResource friendship = await handler.Handle(request);

            // Assert
            Assert.Null(friendship);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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));
        }