public void GetFollowersNotBeingFollowedUserNames_ReturnsNullReferenceException_WhenUserIsNull()
        {
            // Act & Assert
            var ex = Assert.Throws <NullReferenceException>(() => UserNetworkHelpers.GetFollowersNotBeingFollowedUserNames(null));

            Assert.Equal("The user is null", ex.Message);
        }
        public void GetFollowersNotBeingFollowedUserNames_ReturnsNullReferenceException_WhenFollowingCollectionIsNull()
        {
            // Arrange
            var user = new ApplicationUser()
            {
                UserName = "******"
            };

            user.Following = null;
            user.Followers = new List <Network>();

            // Act & Assert
            var ex = Assert.Throws <NullReferenceException>(() => UserNetworkHelpers.GetFollowersNotBeingFollowedUserNames(user));

            Assert.Equal("The following collection is null", ex.Message);
        }
예제 #3
0
        public async Task <IActionResult> GetNetworkSummaryAsync()
        {
            try
            {
                var requestingUser = await _userManager.GetUserWithNetworkAsync(User.Identity.Name);

                if (requestingUser is null)
                {
                    _logger.LogError(LoggingEvents.GetItemNotFound, "requesting user not found");
                    return(StatusCode(500, "requesting user not found"));
                }

                var model = new NetworkSummaryDto
                {
                    FollowersCount = requestingUser.Followers.Count,

                    FollowingCount = requestingUser.Following.Count
                };

                var followersNotBeingFollowed = UserNetworkHelpers.GetFollowersNotBeingFollowedUserNames(requestingUser);

                if (followersNotBeingFollowed.Any())
                {
                    // ToDo: needs to be paged or Take(x)
                    var users = await _userManager.GetUsersAsync(user => followersNotBeingFollowed.Contains(user.UserName));

                    model.SuggestedUsersToFollow = _mapper.Map <IEnumerable <ApplicationUser>, IEnumerable <NetworkUserViewModel> >(users);
                    return(Ok(model));
                }
                else
                {
                    // ToDo: needs to be paged or Take(x)
                    var followingUsernamesList = UserNetworkHelpers.GetFollowingUserNames(requestingUser.Following);
                    var users = await _userManager.GetUsersAsync(user => !followingUsernamesList.Contains(user.UserName) && user.UserName != requestingUser.UserName);

                    model.SuggestedUsersToFollow = _mapper.Map <IEnumerable <ApplicationUser>, IEnumerable <NetworkUserViewModel> >(users);
                    return(Ok(model));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.Exception, ex, $"an unexpected error occurred");
                return(StatusCode(500, "an unexpected error occurred"));
            }
        }
        public void GetFollowersNotBeingFollowedUserNames_ReturnsEmptyCollection_WhenInputCollectionIsEmpty(int length)
        {
            // Arrange
            var user = new ApplicationUser()
            {
                UserName = "******"
            };

            user.Following = GetDynamicNetworkCollection(length);
            user.Followers = new List <Network>();

            // Act
            var result = UserNetworkHelpers.GetFollowersNotBeingFollowedUserNames(user);

            // Assert
            Assert.IsAssignableFrom <IEnumerable <String> >(result);
            Assert.Empty(result);
        }
        public void GetFollowersNotBeingFollowedUserNames_ReturnsCollection_WhenCollectionsAreGreaterThanOne()
        {
            // Arrange
            var user = new ApplicationUser()
            {
                UserName = "******"
            };

            user.Following = GetDynamicNetworkCollection(3);
            user.Followers = GetDynamicNetworkCollection(6);

            // Act
            var result = UserNetworkHelpers.GetFollowersNotBeingFollowedUserNames(user);

            // Assert
            var t = Assert.IsAssignableFrom <IEnumerable <String> >(result);

            Assert.Equal(3, t.Count());
        }