public async Task TestHandle_ShouldUpdateArticleCorrectly(string slug, string newTitle, string newDescription,
                                                                  string newBody)
        {
            // Arrange
            var user = new UserProfile(Guid.NewGuid().ToString(), "email", "username");

            var article = new Article(slug, null, null, new DateTime(), user);

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

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

            var command = new UpdateArticleCommand
            {
                Slug        = slug,
                Body        = newBody,
                Title       = newTitle,
                Description = newDescription
            };

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

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

            var updatedArticle = await Context.Articles.SingleAsync();

            // Assert
            updatedArticle.Title.Should().Be(newTitle);
            updatedArticle.Description.Should().Be(newDescription);
            updatedArticle.Body.Should().Be(newBody);
            updatedArticle.Slug.Should().Be(newTitle);
        }
        public void TestUpdateArticleCommand_AuthorNotFound()
        {
            var mediator = new Mock <IMediator>();

            var articleRepo = new Mock <IArticleRepo>();

            articleRepo.Setup(x => x.GetById(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(new Article(
                                                                                                                             new Author()
            {
                Id        = Guid.NewGuid(),
                FirstName = "Firstname",
                LastName  = "Lastname"
            })
            {
                Id      = Guid.NewGuid(),
                Heading = "Heading",
                Text    = "Text",
                Year    = 2020
            }));
            var authorRepo = new Mock <IAuthorRepo>();

            authorRepo.Setup(x => x.GetById(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult((Author)null));

            var updateCommand = new UpdateArticleCommand()
            {
                Id = Guid.NewGuid(), AuthorId = Guid.NewGuid(), Heading = "heading", Text = "Text", Year = 2020
            };
            var handler = new UpdateArticleCommand.Handler(articleRepo.Object, authorRepo.Object, mediator.Object);

            Assert.ThrowsAsync <NotFoundException>(() => handler.Handle(updateCommand, new CancellationToken()));
        }
        public void TestHandle_ArticleNotFound_ShouldThrowEntityNotFoundException(string slug)
        {
            // Arrange
            var currentUserMock = new Mock <ICurrentUserService>();

            var command = new UpdateArticleCommand
            {
                Slug = slug
            };

            var sut = new UpdateArticleCommand.Handler(Context, currentUserMock.Object);

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

            // Assert
            act.Should().Throw <EntityNotFoundException <Article> >().And.Message.Should().Contain(slug);
        }
        public async Task TestHandle_UserIsNotAuthor_ShouldThrowBadRequestException(string slug)
        {
            // Arrange
            var article = new Article(slug, null, null, new DateTime(),
                                      new UserProfile(string.Empty, string.Empty, string.Empty));

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

            var currentUserMock = new Mock <ICurrentUserService>();

            var command = new UpdateArticleCommand
            {
                Slug = slug
            };

            var sut = new UpdateArticleCommand.Handler(Context, currentUserMock.Object);

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

            // Assert
            act.Should().Throw <BadRequestException>();
        }