Пример #1
0
        public async Task GetRequestWithCountdownIdAndAuthenticatedUser_ReturnsCommentListViewModelWithCorrectResults() {
            const int countdownId = 123;

            var currentAccount = new Account {Id = 1337};

            var mockCommentRepository = new Mock<ICommentRepository>();
            mockCommentRepository
                .Setup(x => x.GetCountdownCommentsAsync(countdownId, currentAccount.Id))
                .Returns(
                    Task.FromResult<IEnumerable<CommentAggregate>>(new List<CommentAggregate> {
                        new CommentAggregate(),
                        new CommentAggregate(),
                        new CommentAggregate()
                    }));

            var mockContextService = new Mock<IContextService>();
            mockContextService
                .Setup(x => x.GetCurrentAccountAsync())
                .Returns(Task.FromResult(currentAccount));

            var commentController = new CommentController(mockCommentRepository.Object, new Mock<IVoteService>().Object, mockContextService.Object);

            dynamic result = (await commentController.GetAll(countdownId)).Data;
            var model = result as CommentListViewModel;

            Assert.IsNotNull(model);
            Assert.AreEqual(3, model.Comments.Count());
        }
Пример #2
0
        public async Task PostCommentViewModelCurrentUserIsAuthenticated_ReturnsJsonResulACommentViewModel() {
            var repository = new FakeCommentRepository();

            var contextService = new Mock<IContextService>();
            var account = new Account {Id = 12345, DisplayName = "TestDisplayName", Email = "*****@*****.**"};
            contextService
                .Setup(x => x.GetCurrentAccountAsync())
                .Returns(Task.FromResult(account));

            var commentController = new CommentController(repository, new Mock<IVoteService>().Object, contextService.Object);
            var commentViewModel = new CommentViewModel {
                CountdownId = 123,
                Text = "Comment content..."
            };

            dynamic result = (await commentController.Create(commentViewModel)).Data;

            var model = result as CommentViewModel;
            Assert.IsNotNull(model);
            Assert.AreEqual(account.DisplayName, model.CreatedByName);
            Assert.AreEqual(account.Email, model.CreatedByEmail);

            DateTime localTime = DateTime.UtcNow.ToLocalTime();
            Assert.AreEqual(localTime.Date, model.CreatedOn.Date);
            Assert.AreEqual(localTime.Hour, model.CreatedOn.Hour);
            Assert.AreEqual(localTime.Minute, model.CreatedOn.Minute);
            Assert.AreEqual(localTime.Second, model.CreatedOn.Second);

            Assert.AreEqual(commentViewModel.Text, model.Text);
            Assert.AreEqual(commentViewModel.CountdownId, model.CountdownId);
        }
Пример #3
0
        public async Task DeleteRequestWithCommentId_DeletesTheComment() {
            const long id = 123;
            const int currentAccountId = 1337;

            var commentRepository = new FakeCommentRepository();
            await commentRepository.CreateAsync(new Comment {Id = id, CreatedById = currentAccountId});

            var mockContextService = new Mock<IContextService>();
            mockContextService
                .Setup(x => x.GetCurrentAccountAsync())
                .Returns(Task.FromResult(new Account {Id = currentAccountId}));

            var commentController = new CommentController(commentRepository, new Mock<IVoteService>().Object, mockContextService.Object);

            await commentController.Delete(id);

            Assert.AreEqual(0, commentRepository.InMemoryComments.Count);
        }
Пример #4
0
        public async Task DeleteRequestWithCommentIdThatDoesntBelongToTheCurrentUser_ThrowsAnHttpExceptionForbidden() {
            const long id = 123;

            var mockContextService = new Mock<IContextService>();
            mockContextService
                .Setup(x => x.GetCurrentAccountAsync())
                .Returns(Task.FromResult(new Account {Id = 1337}));

            var commentRepository = new FakeCommentRepository();
            await commentRepository.CreateAsync(new Comment {Id = id, CreatedById = 1234});

            var commentController = new CommentController(commentRepository, new Mock<IVoteService>().Object, mockContextService.Object);

            var ex = Assert.Throws<HttpException>(async () => await commentController.Delete(id));

            Assert.AreEqual(403, ex.GetHttpCode());
            Assert.AreEqual("Forbidden", ex.Message);
            Assert.AreEqual(1, commentRepository.InMemoryComments.Count);
        }
Пример #5
0
        public async Task PostCommentViewModelCurrentUserIsAuthenticated_CreatesTheCommentWithTheCorrectValues() {
            var repository = new FakeCommentRepository();

            var contextService = new Mock<IContextService>();
            var account = new Account {Id = 123};
            contextService
                .Setup(x => x.GetCurrentAccountAsync())
                .Returns(Task.FromResult(account));

            var commentController = new CommentController(repository, new Mock<IVoteService>().Object, contextService.Object);
            var commentViewModel = new CommentViewModel {
                CountdownId = 123,
                Text = "Comment content..."
            };

            await commentController.Create(commentViewModel);

            Assert.AreEqual(1, repository.InMemoryComments.Count);
            Comment comment = repository.InMemoryComments[0];
            Assert.AreEqual(commentViewModel.Text, comment.Text);
            Assert.AreEqual(commentViewModel.CountdownId, comment.CountdownId);
            Assert.AreEqual(account, comment.Account);
            Assert.AreEqual(DateTime.UtcNow.Date, comment.CreatedOn.Date);
        }