Пример #1
0
        public async Task DeleteCommentTestWithMoreThenOne()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            dbContext.Comments.Add(new Comment
            {
                Id = 1,
            });
            dbContext.Comments.Add(new Comment
            {
                Id = 3,
            });
            dbContext.Comments.Add(new Comment
            {
                Id = 2,
            });
            await dbContext.SaveChangesAsync();

            var repository = new EfDeletableEntityRepository <Comment>(dbContext);
            var service    = new CommentsService(repository);
            await service.DeleteCommentAsync(1);

            Assert.Equal(2, service.GetCommentsCount());
        }
Пример #2
0
        public async Task DeleteCommentsWithOtherUserIdMustReturnFalse()
        {
            var list     = new List <Comment>();
            var mockRepo = new Mock <IDeletableEntityRepository <Comment> >();

            mockRepo.Setup(x => x.All()).Returns(list.AsQueryable());
            mockRepo.Setup(x => x.AddAsync(It.IsAny <Comment>()))
            .Callback(
                (Comment comment) => list.Add(comment));
            mockRepo.Setup(x => x.Delete(It.IsAny <Comment>()))
            .Callback((Comment comment) => list.Remove(comment));
            var service = new CommentsService(mockRepo.Object);

            var comment = service.AddCommentAsync("test", 1, "PeshoId");

            Assert.False(await service.DeleteCommentAsync(0, "GoshoId"));
        }
Пример #3
0
        public async Task TestDeleteCommentAsync_WithInvalidData_ShouldThrowError()
        {
            var context    = ApplicationDbContextInMemoryFactory.InitializeContext();
            var repository = new EfDeletableEntityRepository <Comment>(context);

            var commentId = "asdf";
            await repository.AddAsync(new Comment { Id = commentId });

            await repository.SaveChangesAsync();

            var service = new CommentsService(repository);

            var invalidId = "aaaa";
            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await service.DeleteCommentAsync(invalidId);
            });
        }
Пример #4
0
        public async Task TestDeleteCommentAsync_WithValidData_ShouldCreateExerciseCorrectly()
        {
            var context    = ApplicationDbContextInMemoryFactory.InitializeContext();
            var repository = new EfDeletableEntityRepository <Comment>(context);

            var commentId = "asdf";
            await repository.AddAsync(new Comment { Id = commentId });

            await repository.SaveChangesAsync();

            var service = new CommentsService(repository);

            await service.DeleteCommentAsync(commentId);

            var deletedComment = repository.AllWithDeleted().Where(c => c.Id == commentId).FirstOrDefault();

            Assert.True(deletedComment.IsDeleted);
        }
        public async Task DeleteCommentAsyncThrowsArgumentErrorWhenGivenInvalidAuthorId()
        {
            var commentsList  = new List <Comment>();
            var commentNumber = "test123";
            var comment       = new Comment()
            {
                CommentNumber   = "test123",
                CommentAuthorId = "pesho",
            };

            commentsList.Add(comment);

            var mockCommentsRepo = new Mock <ICommentsRepository>();

            mockCommentsRepo.Setup(x => x.GetCommentByCommentNumber(commentNumber)).Returns(commentsList.FirstOrDefault(x => x.CommentNumber == commentNumber));
            mockCommentsRepo.Setup(x => x.Delete(It.IsAny <Comment>())).Callback((Comment comment) => comment.IsDeleted = true);

            var service = new CommentsService(mockCommentsRepo.Object, null);

            await Assert.ThrowsAsync <ArgumentException>(() => service.DeleteCommentAsync("test123", "gosho"));
        }
Пример #6
0
        public async Task <IActionResult> Delete([FromRoute] string id, [FromBody] string articleSlug)
        {
            Comment comment = await _commentService.GetCommentByIdAsync(id);

            if (comment == null)
            {
                return(StatusCodeAndDtoWrapper.BuildGenericNotFound());
            }

            var result = await _authorizationService.AuthorizeAsync(User, comment,
                                                                    _configService.GetDeleteCommentPolicyName());

            if (result.Succeeded)
            {
                int result2 = await _commentService.DeleteCommentAsync(id);

                return(RedirectToAction("GetArticleBySlug", "Articles", new { slug = articleSlug }));
            }
            else
            {
                throw new PermissionDeniedException();
            }
        }
        public async Task DeleteCommentAsyncWorksWithCorrectInfo()
        {
            var commentsList  = new List <Comment>();
            var commentNumber = "test123";
            var comment       = new Comment()
            {
                CommentNumber   = "test123",
                CommentAuthorId = "pesho",
            };

            commentsList.Add(comment);

            var mockCommentsRepo = new Mock <ICommentsRepository>();

            mockCommentsRepo.Setup(x => x.GetCommentByCommentNumber(commentNumber)).Returns(commentsList.FirstOrDefault(x => x.CommentNumber == commentNumber));
            mockCommentsRepo.Setup(x => x.Delete(It.IsAny <Comment>())).Callback((Comment comment) => comment.IsDeleted = true);

            var service = new CommentsService(mockCommentsRepo.Object, null);

            await service.DeleteCommentAsync("test123", "pesho");

            Assert.Single(commentsList);
            Assert.True(commentsList.First().IsDeleted);
        }