Exemplo n.º 1
0
        public async Task PostFromAnAuthenticatedUserWithValidViewModel_CreatesACommentWithCorrectlyData()
        {
            TestableCommentController controller = TestableCommentController.Create();
            const int currentUserAccountId       = 345;
            DateTime  systemTime = new DateTime(2017, 9, 22, 16, 43, 7);

            controller.MockContextService.Setup(x => x.CurrentUserAccountId).Returns(currentUserAccountId);
            controller.MockSystemClock.Setup(x => x.UtcNow).Returns(systemTime);

            var model = new CommentCreateViewModel {
                CountdownId = 123,
                Text        = "Testing"
            };

            EmptyResult result = await controller.Create(model) as EmptyResult;

            Comment comment = controller.CommentRepository.Comments.FirstOrDefault();

            Assert.IsNotNull(result);
            Assert.IsNotNull(comment);
            Assert.AreEqual(model.Text, comment.Text);
            Assert.AreEqual(model.CountdownId, comment.CountdownId);
            Assert.AreEqual(systemTime, comment.CreatedOn);
            Assert.AreEqual(currentUserAccountId, comment.CreatedByAccountId);
        }
Exemplo n.º 2
0
        public async Task GetRequest_ReturnsAModelWithCorrectNumberOfComments()
        {
            TestableCommentController controller = TestableCommentController.Create();

            controller.CommentRepository.CommentAggregates.Add(new CommentAggregate());
            controller.CommentRepository.CommentAggregates.Add(new CommentAggregate());
            controller.CommentRepository.CommentAggregates.Add(new CommentAggregate());

            var model = new CommentListViewModel {
                CountdownId = 123,
                Token       = 123,
                Page        = 5
            };

            JsonResult result = await controller.Index(model) as JsonResult;

            Assert.IsNotNull(result);

            CommentListViewModel resultModel = result.Data as CommentListViewModel;

            Assert.IsNotNull(resultModel);
            Assert.AreEqual(3, resultModel.Comments.Count());
            Assert.AreEqual(3, resultModel.Total);
            Assert.AreEqual(model.Page, resultModel.Page);
            Assert.AreEqual(model.Token, resultModel.Token);
            Assert.AreEqual(model.DisplayOrderType, resultModel.DisplayOrderType);
        }
Exemplo n.º 3
0
        public async Task PostFromAnAuthenticatedUser_NotifiesConnectedHubClients()
        {
            const int  voteValue  = 1;
            const int  accountId  = 7;
            const long commentId  = 3;
            var        systemTime = new DateTime(2017, 10, 22, 7, 31, 53);

            TestableCommentController controller = TestableCommentController.Create();

            controller.MockContextService.Setup(x => x.CurrentUserAccountId).Returns(accountId);
            controller.MockSystemClock.Setup(x => x.UtcNow).Returns(systemTime);

            controller.CommentRepository.Comments = new List <Comment> {
                new Comment {
                    Id = commentId, CreatedByAccountId = 9
                }
            };

            controller.CommentRepository.CommentAggregates = new List <CommentAggregate> {
                new CommentAggregate {
                    Id = commentId, CreatedByAccountId = 9, VoteScore = 3, CurrentUserVote = voteValue
                }
            };

            await controller.Vote(new CommentVoteViewModel { CommentId = commentId, Value = voteValue });

            controller.MockNotificationService
            .Verify(x => x.UpdateClientsAfterVote(It.Is <CommentAggregate>(c => c.Id == commentId)),
                    Times.Once());
        }
Exemplo n.º 4
0
        public async Task PostFromAnAuthenticatedUserWithAnInvalidViewModel_ReturnsHttpStatusCode400BadRequest()
        {
            TestableCommentController controller = TestableCommentController.Create();

            controller.ModelState.AddModelError("Nope", "Error Message");

            HttpStatusCodeResult result = await controller.Create(new CommentCreateViewModel()) as HttpStatusCodeResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(400, result.StatusCode);
            Assert.AreEqual("Bad Request", result.StatusDescription);
        }
Exemplo n.º 5
0
        public async Task PostFromAnAuthenticatedUserWithExistingVoteWithDifferentValue_DeletesVoteAndReturnsResultWithNewValue()
        {
            const int  voteValue  = 1;
            const int  accountId  = 7;
            const long commentId  = 3;
            var        systemTime = new DateTime(2017, 10, 22, 7, 31, 53);

            TestableCommentController controller = TestableCommentController.Create();

            controller.MockContextService.Setup(x => x.CurrentUserAccountId).Returns(accountId);
            controller.MockSystemClock.Setup(x => x.UtcNow).Returns(systemTime);

            controller.VoteRepository.Votes.Add(new Core.Entities.Vote {
                CommentId         = commentId,
                CastedByAccountId = accountId,
                Value             = -1,
                CastedOn          = new DateTime(2017, 10, 11, 7, 31, 53)
            });

            controller.CommentRepository.Comments = new List <Comment> {
                new Comment {
                    Id = commentId, CreatedByAccountId = 9
                }
            };

            controller.CommentRepository.CommentAggregates = new List <CommentAggregate> {
                new CommentAggregate {
                    Id = commentId, CreatedByAccountId = 9, VoteScore = 3, CurrentUserVote = voteValue
                }
            };

            JsonResult result = await controller.Vote(new CommentVoteViewModel { CommentId = commentId, Value = voteValue }) as JsonResult;

            Assert.IsNotNull(result);

            CommentVoteViewModel model = result.Data as CommentVoteViewModel;

            Assert.IsNotNull(model);
            Assert.AreEqual(commentId, model.CommentId);
            Assert.AreEqual(3, model.VoteScore);
            Assert.AreEqual(voteValue, model.CurrentUserVote);

            Core.Entities.Vote vote = controller.VoteRepository.Votes.FirstOrDefault();
            Assert.IsNotNull(vote);
            Assert.AreEqual(commentId, vote.CommentId);
            Assert.AreEqual(voteValue, vote.Value);
            Assert.AreEqual(accountId, vote.CastedByAccountId);
            Assert.AreEqual(systemTime, vote.CastedOn);
        }
Exemplo n.º 6
0
        public async Task PostFromAnAuthenticatedUserWithValidViewModel_ReturnsEmptyResult()
        {
            TestableCommentController controller = TestableCommentController.Create();

            controller.MockContextService.Setup(x => x.CurrentUserAccountId).Returns(123);

            var model = new CommentCreateViewModel {
                CountdownId = 123,
                Text        = "Testing"
            };

            EmptyResult result = await controller.Create(model) as EmptyResult;

            Assert.IsNotNull(result);
        }
Exemplo n.º 7
0
        public async Task GetRequest_ReturnsCommentListViewModel()
        {
            TestableCommentController controller = TestableCommentController.Create();
            var model = new CommentListViewModel {
                CountdownId = 123,
                Token       = 123,
                Page        = 1
            };

            JsonResult result = await controller.Index(model) as JsonResult;

            Assert.IsNotNull(result);

            CommentListViewModel resultModel = result.Data as CommentListViewModel;

            Assert.IsNotNull(resultModel);
            Assert.AreEqual(model.CountdownId, resultModel.CountdownId);
            Assert.AreEqual(model.Token, resultModel.Token);
            Assert.AreEqual(model.Page, resultModel.Page);
        }
Exemplo n.º 8
0
        public async Task PostFromAnAuthenticatedUserForACommentCreatedByCurrentUserId_ReturnsHttpStatusCode400BadRequest()
        {
            const int  accountId = 7;
            const long commentId = 3;

            TestableCommentController controller = TestableCommentController.Create();

            controller.MockContextService.Setup(x => x.CurrentUserAccountId).Returns(accountId);
            controller.CommentRepository.Comments = new List <Comment> {
                new Comment {
                    Id = commentId, CreatedByAccountId = accountId
                }
            };

            HttpStatusCodeResult result = await controller.Vote(new CommentVoteViewModel { CommentId = commentId, Value = 1 }) as HttpStatusCodeResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(400, result.StatusCode);
            Assert.AreEqual("Bad Request", result.StatusDescription);
        }
Exemplo n.º 9
0
        public async Task PostFromAnAuthenticatedUserWithAnExistingVoteWithSameValue_DeletesVoteAndReturnsResultWithANeutralValue()
        {
            const int  voteValue = 1;
            const int  accountId = 7;
            const long commentId = 3;

            TestableCommentController controller = TestableCommentController.Create();

            controller.MockContextService.Setup(x => x.CurrentUserAccountId).Returns(accountId);

            controller.VoteRepository.Votes.Add(new Core.Entities.Vote {
                CommentId         = commentId,
                CastedByAccountId = accountId,
                Value             = voteValue
            });

            controller.CommentRepository.Comments = new List <Comment> {
                new Comment {
                    Id = commentId, CreatedByAccountId = 9
                }
            };

            controller.CommentRepository.CommentAggregates = new List <CommentAggregate> {
                new CommentAggregate {
                    Id = commentId, CreatedByAccountId = 9, VoteScore = 3, CurrentUserVote = null
                }
            };

            JsonResult result = await controller.Vote(new CommentVoteViewModel { CommentId = commentId, Value = voteValue }) as JsonResult;

            Assert.IsNotNull(result);

            CommentVoteViewModel model = result.Data as CommentVoteViewModel;

            Assert.IsNotNull(model);
            Assert.AreEqual(commentId, model.CommentId);
            Assert.AreEqual(0, model.Value);
            Assert.AreEqual(3, model.VoteScore);
            Assert.IsNull(model.CurrentUserVote);
            Assert.IsEmpty(controller.VoteRepository.Votes);
        }