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

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

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

            //act

            //assert
            FluentActions.Invoking(() =>
                                   SendAsync(addRoleCommand)).Should().Throw <NotFoundException>();
        }
        public async Task ShouldAddRoleToUser()
        {
            //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
            };

            //act
            await SendAsync(addRoleCommand);

            var result = await GetRolesForUserAsync(user.Username);

            //assert
            result.Should().HaveCount(2);
            result.Should().Contain("Premium");
        }
        public void ShouldNotCallHandleIfUserNotExist()
        {
            identityService.Setup(x => x.GetUserByUsernameAsync(user.UserName)).Returns(Task.FromResult((AppUser)null));

            AddRoleToUserCommandHandler addRoleToUserCommandHandler = new AddRoleToUserCommandHandler(identityService.Object, roleManager.Object, stringLocalizer.Object);
            AddRoleToUserCommand        addRoleToUserCommand        = new AddRoleToUserCommand(userDto);

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

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

            AddRoleToUserCommandHandler addRoleToUserCommandHandler = new AddRoleToUserCommandHandler(identityService.Object, roleManager.Object, stringLocalizer.Object);
            AddRoleToUserCommand        addRoleToUserCommand        = new AddRoleToUserCommand(userDto);

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

            var result = await addRoleToUserCommandHandler.Handle(addRoleToUserCommand, new CancellationToken());

            result.Succeeded.Should().BeTrue();
        }
Beispiel #5
0
        public async Task <IActionResult> CreateUserRole(int id,
                                                         [FromBody] UserRolesForUpdateDto userRolesForUpdate)
        {
            if (userRolesForUpdate == null)
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new request");
                return(BadRequest(ModelState));
            }

            var command = new AddRoleToUserCommand(id, userRolesForUpdate.Role);

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

            var commandResult = await _mediator.Send(command);

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

            return(Ok());
        }
Beispiel #6
0
 public async Task <ActionResult <bool> > AddRole([FromBody] AddRoleToUserCommand request)
 {
     return(Ok(await Mediator.Send(request)));
 }