Ejemplo n.º 1
0
        async public void EmptySearchPhrasesReturnNothing()
        {
            var context = DbContextCreator.CreateTestContext("SearchTestDBEmptySearchPhrasesAreHandled");

            var user = new ApplicationUser
            {
                Email       = "*****@*****.**",
                UserName    = "******",
                Description = "I love fitness",
                Id          = "jimmy-id"
            };

            context.Add(user);
            await context.SaveChangesAsync();

            SearchController searchController = new SearchController(context);

            var foundUsers = await searchController.GetSearchedUsers(null);

            var hasNoResults = true;

            foreach (var foundUser in foundUsers.Value)
            {
                hasNoResults = false;
            }

            Assert.True(hasNoResults);
        }
Ejemplo n.º 2
0
        public async void UserIsUpdated()
        {
            // Arrange
            #region context preperation
            var context = DbContextCreator.CreateTestContext("UsersTestDbUserIsUpdated");
            #endregion

            #region data preperation
            ApplicationUser user        = PutOneUserInDb(context);
            var             updatedUser = new ApplicationUser
            {
                Id          = user.Id,
                UserName    = "******",
                Description = "Need a will? Call McGill.",
            };

            #endregion

            // Act
            UsersController usersController = CreateUsersController(context, updatedUser.Id);
            var             result          = await usersController.PutApplicationUser(updatedUser);

            // Assert
            var storedData = await context.Users.SingleAsync(u => u.Id == user.Id);

            Assert.Equal(updatedUser.UserName, storedData.UserName);
            Assert.Equal(updatedUser.Description, storedData.Description);

            var actualUser = await usersController.GetUser(user.Id);

            Assert.NotNull(actualUser);
        }
Ejemplo n.º 3
0
        public async void PostsInCorrectOrder()
        {
            ApplicationDbContext context = DbContextCreator.CreateTestContext("PostsTestDbPostsInCorrectOrder");
            string authenticatedUserId   = "current-user-id";
            var    user     = CreateDummyUser(authenticatedUserId);
            var    category = CreateDummyCategory(1);

            context.Users.Add(user);
            context.Categories.Add(category);
            await context.SaveChangesAsync();

            var postsController = CreatePostsController(context, authenticatedUserId);

            var postOld = CreateDummyPost(user.Id, category.Id);
            var postNew = CreateDummyPost(user.Id, category.Id);
            await postsController.PostPost(postOld);

            await postsController.PostPost(postNew);

            var response = await postsController.GetPosts(null, null, null);

            var listIds = new List <Nullable <int> >();

            foreach (var post in response.Value)
            {
                var dictionary = new RouteValueDictionary(post);
                listIds.Add(dictionary["Id"] as Nullable <int>);
            }

            Assert.Equal(postNew.Id, listIds.First());
        }
Ejemplo n.º 4
0
        public async void GetAllFollowers()
        {
            // Arrange
            #region context preperation
            var context = DbContextCreator.CreateTestContext("UsersTestDbGetAllFollowers");
            #endregion

            #region data preperation

            ApplicationUser follower = PutOneUserInDb(context, "first", "john");
            ApplicationUser followee = PutOneUserInDb(context, "second", "oh Hi mark");
            context.Followers.Add(new FollowerFollowee {
                FolloweeId = followee.Id, FollowerId = follower.Id
            });
            context.SaveChanges();
            #endregion

            //Act
            UsersController usersController = CreateUsersController(context, follower.Id);
            var             users           = await usersController.GetAllFollowers(followee.Id);

            //Assert
            List <string> listIds = new List <string>();
            foreach (var user in users.Value)
            {
                var dictionary = new RouteValueDictionary(user);
                listIds.Add(dictionary["Id"] as string);
            }

            Assert.Equal(follower.Id, listIds.First());
            Assert.Single(listIds);
        }
Ejemplo n.º 5
0
        public async void CanLikeAndUnlikeAPost()
        {
            ApplicationDbContext context = DbContextCreator.CreateTestContext("CanLikeAndUnlikeAPost");
            string authenticatedUserId   = "current-user-id";
            var    category = CreateDummyCategory(1);
            var    post     = CreateDummyPost(authenticatedUserId, category.Id);

            context.Categories.Add(category);
            context.Posts.Add(post);
            await context.SaveChangesAsync();

            var postsController = CreatePostsController(context, authenticatedUserId);

            var initialLikeCount = (from like in context.Likes where like.PostId == post.Id select like.Id).Count();

            Assert.Equal(0, initialLikeCount);

            await postsController.PutPostLike(post.Id, post.ApplicationUserId);

            var likeCountAfterLike = (from like in context.Likes where like.PostId == post.Id select like.Id).Count();

            Assert.Equal(1, likeCountAfterLike);

            await postsController.PutPostUnlike(post.Id, post.ApplicationUserId);

            var likeCountAfterUnlike = (from like in context.Likes where like.PostId == post.Id select like.Id).Count();

            Assert.Equal(0, initialLikeCount);
        }
Ejemplo n.º 6
0
        async public void UsersCanBeSearched()
        {
            //Arrange
            #region context preperation

            var context = DbContextCreator.CreateTestContext("SearchTestDB");

            #endregion

            #region data preparation

            var userOne = new ApplicationUser
            {
                Email       = "*****@*****.**",
                UserName    = "******",
                Description = "Bodybuilding guy",
                Id          = "specificusername-id"
            };
            context.Users.Add(userOne);

            var userTwo = new ApplicationUser
            {
                Email       = "*****@*****.**",
                UserName    = "******",
                Description = "The best fitness account",
                Id          = "veryspecificusername-id"
            };
            context.Users.Add(userTwo);

            await context.SaveChangesAsync();

            #endregion

            //Act
            SearchController searchController = new SearchController(context);

            string searchQueryOne = "VerySpecificUsername999";
            string searchQueryTwo = "specificusername0 ";

            var resultOne = searchController.GetSearchedUsers(searchQueryOne);
            var resultTwo = searchController.GetSearchedUsers(searchQueryTwo);

            bool[] arrayOne = SearchUserLoop(searchQueryOne, resultOne);

            bool onlyOneUserInFirstResult  = arrayOne[0];
            bool specificUserInFirstResult = arrayOne[1];

            bool[] arrayTwo = SearchUserLoop(searchQueryTwo, resultTwo);

            bool onlyOneUserInSecondResult  = arrayTwo[0];
            bool specificUserInSecondResult = arrayTwo[1];

            //Assert
            Assert.True(onlyOneUserInFirstResult);
            Assert.True(specificUserInFirstResult);

            Assert.True(onlyOneUserInSecondResult);
            Assert.True(specificUserInSecondResult);
        }
Ejemplo n.º 7
0
        public async void CantGetNonExistingPostById()
        {
            ApplicationDbContext context = DbContextCreator.CreateTestContext("PostsTestDbGetNonExistingPostById");
            string authenticatedUserId   = "current-user-id";
            var    postsController       = CreatePostsController(context, authenticatedUserId);

            var response = await postsController.GetPost(2);

            Assert.IsAssignableFrom <NotFoundResult>(response.Result);
        }
Ejemplo n.º 8
0
        public async void DeletingNonExistingPostsIsHandled()
        {
            ApplicationDbContext context = DbContextCreator.CreateTestContext("DeletingNonExistingPostsIsHandled");
            string authenticatedUserId   = "current-user-id";
            var    postsController       = CreatePostsController(context, authenticatedUserId);

            int idToDelete = 4;

            Assert.False(context.Posts.Any(e => e.Id == idToDelete));
            var response = await postsController.DeletePost(idToDelete);

            Assert.IsAssignableFrom <NotFoundResult>(response.Result);
        }
Ejemplo n.º 9
0
        public async void CanNotCreatePostAsOtherUser()
        {
            ApplicationDbContext context = DbContextCreator.CreateTestContext("PostControllerTestCanNotCreatePostAsOtherUser");
            string authenticatedUserId   = "hackerman";
            var    category = CreateDummyCategory(1);

            context.Categories.Add(category);
            await context.SaveChangesAsync();

            var postsController = CreatePostsController(context, authenticatedUserId);

            var response = await postsController.PostPost(CreateDummyPost("another-user", 1));

            Assert.True(context.Posts.Count() == 0);
            Assert.IsAssignableFrom <ForbidResult>(response.Result);
        }
Ejemplo n.º 10
0
        public async void CanGetPostById()
        {
            ApplicationDbContext context = DbContextCreator.CreateTestContext("PostsTestDbGetPostById");
            string authenticatedUserId   = "current-user-id";
            var    category = CreateDummyCategory(1);
            var    post     = CreateDummyPost(authenticatedUserId, category.Id);

            context.Categories.Add(category);
            context.Posts.Add(post);
            await context.SaveChangesAsync();

            var postsController = CreatePostsController(context, authenticatedUserId);

            var response = await postsController.GetPost(post.Id);

            Assert.Equal(post, response.Value);
        }
Ejemplo n.º 11
0
        public async void CantUnLikeANonExistingPost()
        {
            ApplicationDbContext context = DbContextCreator.CreateTestContext("PostsTestDbUnLikeANonExistingPost");
            string authenticatedUserId   = "current-user-id";
            var    category = CreateDummyCategory(1);
            var    post     = CreateDummyPost(authenticatedUserId, category.Id);

            context.Categories.Add(category);
            context.Posts.Add(post);
            await context.SaveChangesAsync();

            var postsController = CreatePostsController(context, authenticatedUserId);

            int nonExistingPostId = 111;
            var response          = await postsController.PutPostUnlike(nonExistingPostId, post.ApplicationUserId);

            Assert.IsAssignableFrom <BadRequestResult>(response.Result);
        }
Ejemplo n.º 12
0
        public async void CanDeletePostOwnedByUser()
        {
            ApplicationDbContext context = DbContextCreator.CreateTestContext("postControllerTestCanDeletePost");
            string authenticatedUserId   = "current-user-id";
            var    category = CreateDummyCategory(1);
            var    post     = CreateDummyPost(authenticatedUserId, category.Id);

            context.Categories.Add(category);
            context.Posts.Add(post);
            await context.SaveChangesAsync();

            var postsController = CreatePostsController(context, authenticatedUserId);

            Assert.True(context.Posts.Any(e => e.Id == post.Id));

            await postsController.DeletePost(post.Id);

            Assert.False(context.Posts.Any(e => e.Id == post.Id));
        }
Ejemplo n.º 13
0
        public async void PutFollow()
        {
            // Arrange
            #region context preperation
            var context = DbContextCreator.CreateTestContext("UsersTestDbPutFollow");
            #endregion

            #region data preperation

            ApplicationUser follower = PutOneUserInDb(context, "first", "john");
            ApplicationUser followee = PutOneUserInDb(context, "second", "oh Hi mark");
            #endregion

            //Act
            UsersController usersController = CreateUsersController(context, follower.Id);
            await usersController.PutFollow(follower.Id, followee.Id);

            //Assert
            Assert.True(context.Followers.Any(f => f.FollowerId == follower.Id && f.FolloweeId == followee.Id));
        }
        public async void GetCategories()
        {
            ApplicationDbContext context = DbContextCreator.CreateTestContext("CategoriesTestDb");
            CategoriesController c       = new CategoriesController(context);

            context.Categories.Add(new Category {
                Name = "food"
            });
            context.SaveChanges();

            var result = await c.GetCategories();

            List <string> listIds = new List <string>();

            foreach (var category in result.Value)
            {
                var dictionary = new RouteValueDictionary(category);
                listIds.Add(dictionary["Name"] as string);
            }

            Assert.Equal("food", listIds.First());
        }