public void UpdateIsFollowing_ReturnsNullReferenceException_WhenUsernameIsNull()
        {
            // Arrange
            var viewModel = new NetworkUserViewModel()
            {
                UserName = "******"
            };

            // Act & Assert
            var ex = Assert.Throws <ArgumentNullException>(() => UserNetworkHelpers.UpdateIsFollowing(null, new List <Network>()));

            Assert.Equal("The requestingUsername is null (Parameter 'requestingUsername')", ex.Message);
        }
        public void UpdateIsFollowing_ReturnsNullReferenceException_WhenFollowingCollectionIsNull()
        {
            // Arrange
            var viewModel = new NetworkUserViewModel()
            {
                UserName = "******"
            };

            // Act & Assert
            var ex = Assert.Throws <ArgumentNullException>(() => UserNetworkHelpers.UpdateIsFollowing(viewModel.UserName, null));

            Assert.Equal("The following collection is null (Parameter 'requestedUsersFollowing')", ex.Message);
        }
        public void UpdateIsFollowing_ReturnsIsFollowEqualsTrue_WhenViewModelUserIsInFollowingCollection()
        {
            // Arrange
            var viewModel = new NetworkUserViewModel()
            {
                UserName = "******"
            };
            var following = GetDynamicNetworkCollection(8);

            // Act
            var result = UserNetworkHelpers.UpdateIsFollowing(viewModel.UserName, following);

            // Assert
            var returnModel = Assert.IsType <bool>(result);

            //Assert.Equal(viewModel.UserName, returnModel.UserName);
            Assert.True(result);
        }
        public void UpdateIsFollowing_ReturnsIsFollowEqualsFalse_WhenViewModelUserIsNotInFollowingCollection(int length)
        {
            // Arrange
            var viewModel = new NetworkUserViewModel()
            {
                UserName = "******"
            };
            var following = GetDynamicNetworkCollection(length);

            // Act
            var result = UserNetworkHelpers.UpdateIsFollowing(viewModel.UserName, following);

            // Assert
            var returnModel = Assert.IsType <bool>(result);

            //Assert.Equal(viewModel.UserName, returnModel.UserName);
            Assert.False(result);
        }
Example #5
0
        public async Task <IActionResult> PostUnfollowUserAsync(NetworkUserViewModel userToUnfollowDetails) //, int currentPage)
        {
            try
            {
                var requestingUser = await _userManager.GetUserWithNetworkAsync(User.Identity.Name);

                if (requestingUser is null)
                {
                    _logger.LogError(LoggingEvents.UpdateItem, "Requesting user not found");
                    return(NotFound("Requesting user not found"));
                }

                var userToUnfollow = await _userManager.GetUserWithNetworkAsync(userToUnfollowDetails.UserName);

                if (userToUnfollow is null)
                {
                    _logger.LogError(LoggingEvents.UpdateItem, "User to Unfollow not found");
                    return(NotFound("User to Unfollow not found"));
                }

                if (requestingUser == userToUnfollow)
                {
                    return(BadRequest("Trying to unfollow yourself"));
                }

                _networkRepository.UnFollow(requestingUser, userToUnfollow);

                await _unitOfWork.CompleteAsync();

                var viewModel = _mapper.Map <ApplicationUser, NetworkUserViewModel>(userToUnfollow);

                viewModel.IsFollowing = UserNetworkHelpers.UpdateIsFollowing(viewModel.UserName, requestingUser.Following);

                return(Ok(viewModel));
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.Exception, ex, $"an unexpected error occurred");
                return(StatusCode(500, "an unexpected error occurred"));
            }
        }