public async Task TestHandle_UserIsAlreadyFollower_ShouldThrowBadRequestException(string username, string currentUserEmail)
        {
            // Arrange
            var otherUserProfile   = new UserProfile(Guid.NewGuid().ToString(), "email", username);
            var currentUserProfile = new UserProfile(Guid.NewGuid().ToString(), currentUserEmail, "currentUsername");

            Context.UserProfiles.Add(otherUserProfile);
            Context.UserProfiles.Add(currentUserProfile);
            await Context.SaveChangesAsync();

            Context.UserFollowers.Add(new UserFollower(otherUserProfile.Id, currentUserProfile.Id));
            await Context.SaveChangesAsync();

            var command = new FollowUserCommand {
                Username = username
            };

            var currentUser = Mock.Of <ICurrentUserService>(s => s.UserId == currentUserProfile.Id);

            var sut = new FollowUserCommand.Handler(currentUser, Context);

            // Act
            var act = new Func <Task <Unit> >(async() => await sut.Handle(command, CancellationToken.None));

            // Assert
            act.Should().Throw <BadRequestException>();
        }
        public async Task TestHandle_ShoudCreateNewUserFollower(string username, string currentUserEmail)
        {
            // Arrange
            var otherUserProfile   = new UserProfile(Guid.NewGuid().ToString(), "email", username);
            var currentUserProfile = new UserProfile(Guid.NewGuid().ToString(), currentUserEmail, "currentUsername");

            Context.UserProfiles.Add(otherUserProfile);
            Context.UserProfiles.Add(currentUserProfile);
            await Context.SaveChangesAsync();

            var command = new FollowUserCommand {
                Username = username
            };

            var currentUser = Mock.Of <ICurrentUserService>(s => s.UserId == currentUserProfile.Id);

            var sut = new FollowUserCommand.Handler(currentUser, Context);

            // Act
            await sut.Handle(command, CancellationToken.None);

            var following = await Context.UserFollowers
                            .SingleOrDefaultAsync();

            // Assert
            following.Should().NotBeNull();
            following.FollowerId.Should().Be(currentUserProfile.Id);
            following.UserId.Should().Be(otherUserProfile.Id);
        }
        public async Task <IActionResult> Follow(Guid userId, [FromBody] FollowUserInputModel followUserInputModel)
        {
            var followUserCommand = new FollowUserCommand(userId, followUserInputModel.FolloweeId);

            await _mediator.Send(followUserCommand);

            return(NoContent());
        }
        public async Task <IActionResult> FollowUser(FollowUserCommand command)
        {
            if (!ModelState.IsValid)
            {
                return(NoContent());
            }

            await Mediator.Send(command);

            return(NoContent());
        }
        public async Task GivenValidRequest_WhenTheFollowerTriesToUnfollowThemselves_ThrowsApiException()
        {
            // Arrange, verify the user is not currently being followed by the requester
            var followUserCommand = new FollowUserCommand(TestConstants.TestUserName);

            // Act
            var request = new FollowUserCommandHandler(CurrentUserContext, Context, Mapper, UserManager, new DateTimeTest());

            // Assert
            await Should.ThrowAsync <ConduitApiException>(async() =>
            {
                await request.Handle(followUserCommand, CancellationToken.None);
            });
        }
        public async Task GivenValidRequest_WhenTheFollowerDoesNotExist_ThrowsApiException()
        {
            // Arrange, verify the user is not currently being followed by the requester
            var followUserCommand = new FollowUserCommand("this.user.does.not.exist");

            // Act
            var request = new FollowUserCommandHandler(CurrentUserContext, Context, Mapper, UserManager, new DateTimeTest());

            // Assert
            await Should.ThrowAsync <ConduitApiException>(async() =>
            {
                await request.Handle(followUserCommand, CancellationToken.None);
            });
        }
        public void TestHandle_OtherUserDoesntExist_ShouldThrowEntityNotFoundException(string username, string currentUserEmail)
        {
            // Arrange
            var command = new FollowUserCommand {
                Username = username
            };

            var currentUser = Mock.Of <ICurrentUserService>();

            var sut = new FollowUserCommand.Handler(currentUser, Context);

            // Act
            var act = new Func <Task <Unit> >(async() => await sut.Handle(command, CancellationToken.None));

            // Assert
            act.Should().Throw <EntityNotFoundException <UserProfile> >().And.Message.Should().Contain(username);
        }
        public async Task GivenValidRequest_WhenTheFollowerExists_ReturnsProfileViewModelAndAddsFollowee()
        {
            // Arrange, verify the user is not currently being followed by the requester
            var followUserCommand = new FollowUserCommand("test.user2");
            var user2             = Context.Users.FirstOrDefault(u => u.UserName == "test.user2");

            user2.ShouldNotBeNull();
            user2.Followers.ShouldBeEmpty();

            // Act
            var request  = new FollowUserCommandHandler(CurrentUserContext, Context, Mapper, UserManager, new DateTimeTest());
            var response = await request.Handle(followUserCommand, CancellationToken.None);

            // Assert
            response.ShouldNotBeNull();
            response.ShouldBeOfType <ProfileViewModel>();
            response.Profile.ShouldNotBeNull();
            response.Profile.ShouldBeOfType <ProfileDto>();
            user2.Followers.ShouldNotBeEmpty();
        }
Exemple #9
0
        public async Task InputDataOk_Called_ReturnUnit()
        {
            // Arrange
            var userId  = Guid.NewGuid();
            var fixture = new Fixture();

            var followUserInputModel = fixture.Create <FollowUserInputModel>();

            var userRepositoryMock = new Mock <IUserRepository>();

            var followUserCommand = new FollowUserCommand(userId, followUserInputModel.FolloweeId);

            var followUserCommandHandler = new FollowUserCommandHandler(userRepositoryMock.Object);

            // Act
            var unitValue = await followUserCommandHandler.Handle(followUserCommand, new CancellationToken());

            // Assert
            var userFollower = new UserFollower(userId, followUserInputModel.FolloweeId);

            userRepositoryMock.Verify(ur => ur.AddFollowee(userId, followUserInputModel.FolloweeId), Times.Once);
        }