public async Task ShouldReturnErrorWhenAdministratorIdDoesNotMatch()
        {
            Guid   channelId              = Guid.NewGuid();
            Guid   adminstratorId         = Guid.NewGuid();
            Guid   commandAdministratorId = Guid.NewGuid();
            string channelDescription     = "The first channel";
            var    fakeUserRepository     = new Mock <IUserRepository>();
            var    fakeChannelRepository  = new Mock <IChannelRepository>();

            fakeChannelRepository
            .Setup(repository => repository.GetById(It.IsAny <Guid>()))
            .ReturnsAsync((Guid id) => new GetChannelByIdQueryResult
            {
                Id              = channelId,
                Description     = channelDescription,
                AdministratorId = adminstratorId
            });
            var command = new UpdateChannelDescriptionCommand
            {
                Id              = channelId,
                Description     = channelDescription,
                AdministratorId = commandAdministratorId
            };
            var handler = new ChannelHandler(fakeChannelRepository.Object, fakeUserRepository.Object);

            ICommandResult result = await handler.HandleAsync(command);

            result.Success.Should().BeFalse();
            result.Errors.Should().HaveCountGreaterThan(0);
            handler.Invalid.Should().BeTrue();
        }
Ejemplo n.º 2
0
        public async Task <ICommandResult> HandleAsync(UpdateChannelDescriptionCommand command)
        {
            command.Validate();
            if (command.Invalid)
            {
                AddNotifications(command);
                return(new CommandResult(false, "Could not update channel description", Errors));
            }
            GetChannelByIdQueryResult channel = await _channelRepository.GetById(command.Id);

            if (channel is null)
            {
                AddNotification(nameof(command.Id), "Channel not found");
                return(new CommandResult(false, "Could not update channel description", Errors));
            }
            if (command.AdministratorId != channel.AdministratorId)
            {
                AddNotification(nameof(command.AdministratorId), "AdministratorId does't match channel administrator id");
                return(new CommandResult(false, "User does't have permission to update channel description", Errors));
            }
            if (channel.Description != command.Description)
            {
                await _channelRepository.UpdateChannelDescription(command.Id, command.Description);
            }
            return(new CommandResult(true, "Description successfully updated", new ChannelOutput
            {
                Id = command.Id,
                Description = command.Description
            }));
        }
        public void ShouldReturnErrorWhenDescriptionLenghtIsGreaterThan100()
        {
            var command = new UpdateChannelDescriptionCommand
            {
                Id              = Guid.NewGuid(),
                Description     = "PS15TihJoUQEydtvAZFa5SeaHcDNdosgagsPHrLIPS15TihJoUQEydtvAZFa5SeaHcDNdosgagsPHrLIPS15TihJoUQEydtvAZFa5SeaHcDNdosgagsPHrLI",
                AdministratorId = Guid.NewGuid()
            };

            command.Validate();

            command.Invalid.Should().BeTrue();
        }
        public void ShouldReturnSuccessWhenDescriptionIsEmpty()
        {
            var command = new UpdateChannelDescriptionCommand
            {
                Id              = Guid.NewGuid(),
                Description     = "",
                AdministratorId = Guid.NewGuid()
            };

            command.Validate();

            command.Valid.Should().BeTrue();
        }
        public async Task ShouldReturnErrorWhenDescriptionLenghtIsGreaterThan100()
        {
            var fakeUserRepository    = new Mock <IUserRepository>();
            var fakeChannelRepository = new Mock <IChannelRepository>();
            var command = new UpdateChannelDescriptionCommand
            {
                Id              = Guid.NewGuid(),
                Description     = "PS15TihJoUQEydtvAZFa5SeaHcDNdosgagsPHrLIPS15TihJoUQEydtvAZFa5SeaHcDNdosgagsPHrLIPS15TihJoUQEydtvAZFa5SeaHcDNdosgagsPHrLI",
                AdministratorId = Guid.NewGuid()
            };
            var handler = new ChannelHandler(fakeChannelRepository.Object, fakeUserRepository.Object);

            ICommandResult result = await handler.HandleAsync(command);

            result.Success.Should().BeFalse();
            result.Errors.Should().HaveCountGreaterThan(0);
            handler.Invalid.Should().BeTrue();
        }
Ejemplo n.º 6
0
        public async Task <ActionResult> UpdateChannelDescription(UpdateChannelDescriptionViewModel data,
                                                                  [FromRoute] Guid id,
                                                                  [FromHeader] Guid authorization,
                                                                  [FromServices] IHandler <UpdateChannelDescriptionCommand> handler)
        {
            var command = new UpdateChannelDescriptionCommand
            {
                Id              = id,
                Description     = data.Description,
                AdministratorId = authorization
            };
            ICommandResult result = await handler.HandleAsync(command);

            if (!result.Success)
            {
                return(BadRequest(new ErrorViewModel(result)));
            }
            return(NoContent());
        }
        public async Task ShouldReturnErrorWhenChannelNotFound()
        {
            var fakeUserRepository    = new Mock <IUserRepository>();
            var fakeChannelRepository = new Mock <IChannelRepository>();

            fakeChannelRepository
            .Setup(repository => repository.GetById(It.IsAny <Guid>()))
            .Returns(Task.FromResult <GetChannelByIdQueryResult>(null));
            var command = new UpdateChannelDescriptionCommand
            {
                Id              = Guid.NewGuid(),
                Description     = "The first channel",
                AdministratorId = Guid.NewGuid()
            };
            var handler = new ChannelHandler(fakeChannelRepository.Object, fakeUserRepository.Object);

            ICommandResult result = await handler.HandleAsync(command);

            result.Success.Should().BeFalse();
            result.Errors.Should().HaveCountGreaterThan(0);
            handler.Invalid.Should().BeTrue();
        }
        public async Task ShouldReturnSuccessWhenDescriptionsAreNotEqual()
        {
            Guid   channelId             = Guid.NewGuid();
            Guid   adminstratorId        = Guid.NewGuid();
            string channelDescription    = "newer description";
            var    fakeUserRepository    = new Mock <IUserRepository>();
            var    fakeChannelRepository = new Mock <IChannelRepository>();

            fakeChannelRepository
            .Setup(repository => repository.GetById(It.IsAny <Guid>()))
            .ReturnsAsync((Guid id) => new GetChannelByIdQueryResult
            {
                Id              = channelId,
                Description     = "older description",
                AdministratorId = adminstratorId
            });
            var command = new UpdateChannelDescriptionCommand
            {
                Id              = channelId,
                Description     = channelDescription,
                AdministratorId = adminstratorId
            };
            var handler = new ChannelHandler(fakeChannelRepository.Object, fakeUserRepository.Object);

            ICommandResult result = await handler.HandleAsync(command);

            var resultOutput = result.Data as ChannelOutput;

            result.Success.Should().BeTrue();
            result.Data.Should().NotBeNull();
            result.Errors.Should().BeNullOrEmpty();
            resultOutput.Should().NotBeNull();
            resultOutput?.Description.Should().Be(command.Description);
            handler.Valid.Should().BeTrue();
            fakeChannelRepository.Verify(
                repository => repository.UpdateChannelDescription(It.Is <Guid>(id => id == command.Id),
                                                                  It.Is <string>(description => description == command.Description)),
                Times.Once());
        }