Пример #1
0
        public async Task GivenSlugOfExistingArticle_DeletesArticleWithTags()
        {
            // Arrange
            var user = new UserProfile(Guid.NewGuid().ToString(), "*****@*****.**", "username");

            var article = new Article("test title", "test", "test", new DateTime(1111, 1, 1), user,
                                      new List <Tag> {
                new Tag("test tag")
            });

            Context.Articles.Add(article);
            await Context.SaveChangesAsync();

            var currentUser = Mock.Of <ICurrentUserService>(s => s.UserId == user.Id);

            var command = new DeleteArticleCommand {
                Slug = article.Slug
            };

            var sut = new DeleteArticleCommand.Handler(Context, currentUser);

            // Act
            await sut.Handle(command, CancellationToken.None);

            var existingArticle = await Context.Articles
                                  .SingleOrDefaultAsync(a => a.Id == article.Id);

            var tags = await Context.ArticleTags.Where(a => a.ArticleId == article.Id).ToListAsync();

            // Assert
            existingArticle.Should().BeNull();
            tags.Should().BeEmpty();
        }
Пример #2
0
        public void TestDeleteArticleCommand_NotFound()
        {
            var mediator    = new Mock <IMediator>();
            var articleRepo = new Mock <IArticleRepo>();

            articleRepo.Setup(x => x.GetById(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult((Article)null));
            var deleteCommand = new DeleteArticleCommand()
            {
                Id = Guid.NewGuid()
            };
            var handler = new DeleteArticleCommand.Handler(articleRepo.Object, mediator.Object);

            Assert.ThrowsAsync <NotFoundException>(() => handler.Handle(deleteCommand, new CancellationToken()));
        }
Пример #3
0
        public async Task GivenSlugOfNonExistingArticle_ThrowsEntityNotFoundException()
        {
            // Arrange
            var currentUser = Mock.Of <ICurrentUserService>();

            var command = new DeleteArticleCommand {
                Slug = "some slug"
            };

            var sut = new DeleteArticleCommand.Handler(Context, currentUser);

            // Act
            var action = new Func <Task <Unit> >(() => sut.Handle(command, CancellationToken.None));

            // Assert
            await action.Should().ThrowAsync <EntityNotFoundException <Article> >();
        }
Пример #4
0
        public async Task GivenUserThatIsNotAuthor_ThrowsBadRequestException()
        {
            // Arrange
            var article = new Article("test title", "test", "test", new DateTime(1111, 1, 1),
                                      new UserProfile(Guid.NewGuid().ToString(), "*****@*****.**", "username"));

            Context.Articles.Add(article);
            await Context.SaveChangesAsync();

            var currentUser = Mock.Of <ICurrentUserService>();

            var command = new DeleteArticleCommand {
                Slug = article.Slug
            };

            var sut = new DeleteArticleCommand.Handler(Context, currentUser);

            // Act
            var action = new Func <Task <Unit> >(() => sut.Handle(command, CancellationToken.None));

            // Assert
            await action.Should().ThrowAsync <BadRequestException>();
        }