public async Task ShouldThrowExceptionWhenWrongUser()
        {
            //arrange
            await SeedRoles();

            var registerUserCommand = new RegisterUserCommand()
            {
                Username = "******",
                Email    = "scott@localhost",
                Password = "******"
            };
            var user = await SendAsync(registerUserCommand);

            var removeRoleCommand = new RemoveRoleFromUserCommand()
            {
                Role     = "Standard",
                Username = "******"
            };

            //act

            //assert
            FluentActions.Invoking(() =>
                                   SendAsync(removeRoleCommand)).Should().Throw <NotFoundException>();
        }
        public void ShouldNotCallHandleIfUserNotExist()
        {
            identityService.Setup(x => x.GetUserByUsernameAsync(user.UserName)).Returns(Task.FromResult((AppUser)null));

            RemoveRoleFromUserCommandHandler removeRoleFromUserCommandHandler = new RemoveRoleFromUserCommandHandler(identityService.Object, roleManager.Object, stringLocalizer.Object);
            RemoveRoleFromUserCommand        removeRoleFromUserCommand        = new RemoveRoleFromUserCommand(userDto);

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

            act.Should().Throw <NotFoundException>();
        }
        public async Task ShouldCallHandle()
        {
            identityService.Setup(x => x.GetUserByUsernameAsync(user.UserName)).Returns(Task.FromResult(user));

            RemoveRoleFromUserCommandHandler removeRoleFromUserCommandHandler = new RemoveRoleFromUserCommandHandler(identityService.Object, roleManager.Object, stringLocalizer.Object);
            RemoveRoleFromUserCommand        removeRoleFromUserCommand        = new RemoveRoleFromUserCommand(userDto);

            roleManager.Setup(x => x.RoleExistsAsync(removeRoleFromUserCommand.Role)).Returns(Task.FromResult(true));
            identityService.Setup(x => x.RemoveRoleFromUserAsync(user, removeRoleFromUserCommand.Role)).Returns(Task.FromResult(Result.Success()));

            var result = await removeRoleFromUserCommandHandler.Handle(removeRoleFromUserCommand, new CancellationToken());

            result.Succeeded.Should().BeTrue();
        }
Beispiel #4
0
        public async Task <IActionResult> RemoveUserRole(int id, string role)
        {
            if (String.IsNullOrWhiteSpace(role))
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new request");
                return(BadRequest(ModelState));
            }

            var command = new RemoveRoleFromUserCommand(id, role);

            _logger.LogInformation(
                $"----- Sending command: RemoveRoleFromUserCommand - {command.UserId}");

            var commandResult = await _mediator.Send(command);

            if (!commandResult)
            {
                return(BadRequest("Command not created"));
            }

            return(Ok());
        }
        public async Task ShouldRemoveRoleFromUser()
        {
            //arrange
            await SeedRoles();

            var registerUserCommand = new RegisterUserCommand()
            {
                Username = "******",
                Email    = "scott@localhost",
                Password = "******"
            };
            var user = await SendAsync(registerUserCommand);

            var addRoleCommand = new AddRoleToUserCommand()
            {
                Role     = "Premium",
                Username = user.Username
            };

            await SendAsync(addRoleCommand);

            var removeRoleCommand = new RemoveRoleFromUserCommand()
            {
                Role     = "Premium",
                Username = user.Username
            };


            //act
            await SendAsync(removeRoleCommand);

            var result = await GetRolesForUserAsync(user.Username);

            //assert
            result.Should().HaveCount(1);
            result.Should().NotContain("Premium");
        }