Esempio n. 1
0
        public async Task User_Cannot_Edit_Post_Published_By_Another_User()
        {
            Result editResult;
            var    picture = (Picture)Convert.FromBase64String(DatabaseFixture.GetTestPictureBase64());
            var    author  = new User((Nickname)"authoruser1", (FullName)"author1 user1", Password.Create("password").Value, (Email)"*****@*****.**", "my bio");
            var    reader  = new User((Nickname)"readeruser", (FullName)"reader user", Password.Create("password").Value, (Email)"*****@*****.**", "my bio");
            var    post    = new Post(author, picture, (PostText)"test post 1");

            using (var session = _testFixture.OpenSession(_output))
            {
                await session.SaveAsync(author);

                await session.SaveAsync(reader);

                await session.SaveAsync(post);
            }

            var command = new EditPostCommand(reader.ID, post.ID, "edited text on post 1", picture);

            using (var session = _testFixture.OpenSession(_output))
            {
                var sut = new EditPostCommandHandler(session, Log.Logger);
                editResult = await sut.Handle(command, default);
            }

            editResult.IsSuccess.Should().BeFalse($"You're not allowed to edit post {post.ID}.");
        }
Esempio n. 2
0
        public async Task User_Can_Edit_His_Own_Posts()
        {
            Result editResult;
            var    picture = (Picture)Convert.FromBase64String(DatabaseFixture.GetTestPictureBase64());
            var    author  = new User((Nickname)"authoruser1", (FullName)"author1 user1", Password.Create("password").Value, (Email)"*****@*****.**", "my bio");
            var    post    = new Post(author, picture, (PostText)"test post 1");

            using (var session = _testFixture.OpenSession(_output))
            {
                await session.SaveAsync(author);

                await session.SaveAsync(post);
            }

            var command = new EditPostCommand(author.ID, post.ID, "edited text on post 1", picture);

            using (var session = _testFixture.OpenSession(_output))
            {
                var sut = new EditPostCommandHandler(session, Log.Logger);
                editResult = await sut.Handle(command, default);
            }

            using (var session = _testFixture.OpenSession(_output))
            {
                using (new AssertionScope())
                {
                    session.Load <Post>(post.ID).Text.Value.Should().Be("edited text on post 1");
                    editResult.IsSuccess.Should().BeTrue();
                }
            }
        }
Esempio n. 3
0
        public void ShouldNotCallHandleIfPostNotExist()
        {
            dbSetPost.Setup(x => x.FindAsync(id)).Returns(null);
            context.Setup(x => x.Posts).Returns(dbSetPost.Object);

            EditPostCommandHandler editPostCommandHandler = new EditPostCommandHandler(context.Object, stringLocalizer.Object);
            EditPostCommand        editPostCommand        = new EditPostCommand(postDto);

            Func <Task> act = async() => await editPostCommandHandler.Handle(editPostCommand, new CancellationToken());

            act.Should().ThrowAsync <NotFoundException>();
        }
Esempio n. 4
0
        public void ShouldNotCallHandleIfNotSavedChanges()
        {
            dbSetPost.Setup(x => x.FindAsync(id)).Returns(new ValueTask <Post>(Task.FromResult(post)));
            context.Setup(x => x.Posts).Returns(dbSetPost.Object);
            context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(0));

            EditPostCommandHandler editPostCommandHandler = new EditPostCommandHandler(context.Object, stringLocalizer.Object);
            EditPostCommand        editPostCommand        = new EditPostCommand(postDto);

            Func <Task> act = async() => await editPostCommandHandler.Handle(editPostCommand, new CancellationToken());

            act.Should().ThrowAsync <RestException>();
        }
Esempio n. 5
0
        public async Task ShouldCallHandle()
        {
            dbSetPost.Setup(x => x.FindAsync(id)).Returns(new ValueTask <Post>(Task.FromResult(post)));
            context.Setup(x => x.Posts).Returns(dbSetPost.Object);
            context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(1));

            EditPostCommandHandler editPostCommandHandler = new EditPostCommandHandler(context.Object, stringLocalizer.Object);
            EditPostCommand        editPostCommand        = new EditPostCommand(postDto);

            var result = await editPostCommandHandler.Handle(editPostCommand, new CancellationToken());

            result.Should().Be(Unit.Value);
        }