Beispiel #1
0
        public async Task TestHandle_UserIsNotFollower_ShouldThrowEntityNotFoundException(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 UnfollowUserCommand {
                Username = username
            };

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

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

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

            // Assert
            act.Should().Throw <EntityNotFoundException <UserFollower> >()
            .And.Message.Should().Contain(otherUserProfile.Id.ToString())
            .And.Contain(currentUserProfile.Id.ToString());
        }
Beispiel #2
0
        public async Task TestHandle_ShoudDeleteUserFollower(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 UnfollowUserCommand {
                Username = username
            };

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

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

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

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

            // Assert
            following.Should().BeNull();
        }
        public async Task <IActionResult> UnfollowUser(UnfollowUserCommand command)
        {
            if (!ModelState.IsValid)
            {
                return(NoContent());
            }

            await Mediator.Send(command);

            return(NoContent());
        }
        public async Task GivenValidRequest_WhenTheFollowerExists_ReturnsProfileViewModelAndRemovesFollowee()
        {
            // Arrange, verify the user is currently being followed by the requester
            var unfollowUserCommand = new UnfollowUserCommand("joey.mckenzie");
            var userFollowee        = Context.Users.FirstOrDefault(u => u.UserName == "joey.mckenzie");
            var userFollower        = Context.Users.FirstOrDefault(u => u.UserName == "test.user");

            userFollowee.ShouldNotBeNull();
            userFollowee.Followers.ShouldContain(u => u.UserFollower == userFollower);
            userFollower.ShouldNotBeNull();
            userFollower.Following.ShouldContain(u => u.UserFollowing == userFollowee);

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

            // Assert
            response.ShouldNotBeNull();
            response.ShouldBeOfType <ProfileViewModel>();
            response.Profile.ShouldNotBeNull();
            response.Profile.ShouldBeOfType <ProfileDto>();
            userFollowee.Followers.ShouldNotContain(u => u.UserFollower == userFollowee);
        }