public void Get_GetAllTest_InvalidSessionKey_NoUserWithSuchSessionKey()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            userFakeRepository.entities.Add(user);

            PostsController threadsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(threadsController);

            threadsController.GetAll("1InvalidSessionKeyvHlVfHGotklitbwHdYFkgwIRcIQjRAPQ");
        }
        public object GetService(Type serviceType)
        {
            var dbContext = new ForumDbContext();

            if (serviceType == typeof(UsersController))
            {
                var repository = new EfRepository<User>(dbContext);
                var userController = new UsersController(repository);

                return userController;
            }
            else if (serviceType == typeof(ThreadsController))
            {
                var categoryRepository = new EfRepository<Category>(dbContext);
                var threadRepository = new EfRepository<Thread>(dbContext);
                var userRepository = new EfRepository<User>(dbContext);

                var threadController = new ThreadsController(
                    categoryRepository, threadRepository, userRepository);

                return threadController;
            }
            else if (serviceType == typeof(PostsController))
            {
                var postRepository = new EfRepository<Post>(dbContext);
                var userRepository = new EfRepository<User>(dbContext);
                var threadRepository = new EfRepository<Thread>(dbContext);
                var voteRepository = new EfRepository<Vote>(dbContext);
                var commentRepository = new EfRepository<Comment>(dbContext);

                var postsController = new PostsController(
                    postRepository, userRepository, threadRepository, voteRepository, commentRepository);

                return postsController;
            }
            else if (serviceType == typeof(CategoriesController))
            {
                var categoryRepository = new EfRepository<Category>(dbContext);
                var userRepository = new EfRepository<User>(dbContext);
                var threadRepository = new EfRepository<Thread>(dbContext);

                var categoriesController = new CategoriesController(categoryRepository, userRepository, threadRepository);

                return categoriesController;
            }
            else
            {
                return null;
            }
        }
        public void Get_GetAllTest_ValidExecution_CheckingForCorrectOrder()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            for (int i = 1; i <= 20; i++)
            {
                Post post = new Post()
                {
                    Content = string.Format("Test post content {0}", i),
                    DateCreated = DateTime.Now.AddDays(i),
                    User = user
                };

                user.Posts.Add(post);
                postFakeRepository.entities.Add(post);
            }

            userFakeRepository.entities.Add(user);

            PostsController postsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(postsController);

            var posts = postsController.GetAll(user.SessionKey).ToArray<PostModel>();

            for (int i = 0; i < posts.Length - 1; i++)
            {
                int result = posts[i].PostDate.CompareTo(posts[i + 1].PostDate);
                Assert.IsTrue(result > 0);
            }
        }
        public void Get_GetAllTest_ValidExecution_ReturningSomePosts()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            for (int i = 1; i <= 20; i++)
            {
                Post post = new Post()
                {
                    Content = string.Format("Test post content {0}", i),
                    DateCreated = DateTime.Now.AddDays(i),
                    User = user
                };

                user.Posts.Add(post);
                postFakeRepository.entities.Add(post);
            }

            userFakeRepository.entities.Add(user);

            PostsController postsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(postsController);

            var posts = postsController.GetAll(user.SessionKey).ToList<PostModel>();
            int expectedPostCount = 20;

            Assert.AreEqual(expectedPostCount, posts.Count);
        }
        public void Post_VoteForPostTest_ValidExecutionOfTheMethod()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            Thread thread = new Thread()
            {
                Title = "Test title",
                Creator = user,
                Content = "Test Content",
                DateCreated = DateTime.Now
            };

            Post post = new Post()
            {
                Content = "Test post content",
                DateCreated = DateTime.Now,
                User = user,
                Thread = thread
            };

            VoteModel voteModel = new VoteModel()
            {
                Value = 5
            };

            userFakeRepository.entities.Add(user);
            threadFakeRepository.entities.Add(thread);
            postFakeRepository.entities.Add(post);

            PostsController postsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(postsController);

            var response = postsController.VoteForPost(voteModel, post.Id, user.SessionKey);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
        public void Post_VoteForPostTest_InvalidVoteValue_ValueTooLarge()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            Thread thread = new Thread()
            {
                Title = "Test title",
                Creator = user,
                Content = "Test Content",
                DateCreated = DateTime.Now
            };

            Post post = new Post()
            {
                Content = "Test post content",
                DateCreated = DateTime.Now,
                User = user,
                Thread = thread
            };

            VoteModel voteModel = new VoteModel()
            {
                Value = 20 // Invalid vote value, too large.
            };

            userFakeRepository.entities.Add(user);
            threadFakeRepository.entities.Add(thread);
            postFakeRepository.entities.Add(post);

            PostsController postsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(postsController);

            int invalidPostId = 100;
            var response = postsController.VoteForPost(voteModel, invalidPostId, user.SessionKey);
        }
        public void Post_CreatePostTest_InvalidThreadId()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            Thread thread = new Thread()
            {
                Title = "Test title",
                Creator = user,
                Content = "Test Content",
                DateCreated = DateTime.Now
            };

            PostCreateModel model = new PostCreateModel()
            {
                Content = "Test post content",
                PostDate = DateTime.Now,
                ThreadId = 100 // Invalid Id.
            };

            userFakeRepository.entities.Add(user);
            threadFakeRepository.entities.Add(thread);

            PostsController postsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(postsController);

            var response = postsController.CreatePost(model, user.SessionKey);
        }
        public void Post_CommentForPostTest_InvalidSessionKey_NoUserWithSuchSessionKey()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            Thread thread = new Thread()
            {
                Title = "Test title",
                Creator = user,
                Content = "Test Content",
                DateCreated = DateTime.Now
            };

            Post post = new Post()
            {
                Content = "Test post content",
                DateCreated = DateTime.Now,
                User = user,
                Thread = thread
            };

            CommentModel commentModel = new CommentModel()
            {
                Content = "Just a test comment.",
                CommentDate = DateTime.Now
            };

            userFakeRepository.entities.Add(user);
            threadFakeRepository.entities.Add(thread);
            postFakeRepository.entities.Add(post);

            PostsController postsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(postsController);

            var response = postsController.CommentForPost(commentModel, post.Id, "1InvalidSessionKeyvHlVfHGotklitbwHdYFkgwIRcIQjRAPQ");
        }
        public void Get_GetPageTest_GettingLast5PostsFromTotalOf30()
        {
            FakeRepository<Post> postFakeRepository = new FakeRepository<Post>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Vote> voteFakeRepository = new FakeRepository<Vote>();
            FakeRepository<Comment> commentFakeRepository = new FakeRepository<Comment>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            for (int i = 1; i <= 30; i++)
            {
                Post post = new Post()
                {
                    Content = string.Format("Test post content {0}", i),
                    DateCreated = DateTime.Now.AddDays(i),
                    User = user
                };

                user.Posts.Add(post);
                postFakeRepository.entities.Add(post);
            }

            userFakeRepository.entities.Add(user);

            PostsController postsController = new PostsController(
                postFakeRepository, userFakeRepository, threadFakeRepository, voteFakeRepository, commentFakeRepository);

            SetupController(postsController);

            var lastFivePosts = postsController.GetPage(5, 5, user.SessionKey).ToArray<PostModel>();
            int expectedPostCount = 5;

            Assert.AreEqual(expectedPostCount, lastFivePosts.Length);

            // Checking if the last 5 posts are the ones they should be.
            for (int i = 0; i < lastFivePosts.Length; i++)
            {
                string expectedPostContent = string.Format("Test post content {0}", lastFivePosts.Length - i);
                Assert.AreEqual(expectedPostContent, lastFivePosts[i].Content);
            }
        }