public void TestPromoteUserWithRoleUserFails() { var service = new UserService(_users, _roles, _mapper); var promotionRequest = new DemoteUserCommand { Id = 1 }; Assert.ThrowsAsync <EntityNotFoundException>(async() => await service.DemoteUser(promotionRequest)); }
public async void TestDemoteUserWithRoleModerator() { var service = new UserService(_users, _roles, _mapper); var promotionRequest = new DemoteUserCommand { Id = 2 }; var r = await service.DemoteUser(promotionRequest); Assert.Equal("user", r.Role); }
public async Task <IActionResult> DemoteUser(string userId) { var command = new DemoteUserCommand() { UserId = userId }; _ = await Mediator.Send(command); return(RedirectToAction("EditUser", new { id = userId })); }
public async Task <DomainUser> DemoteUser(DemoteUserCommand cmd) { var user = await _users.FindById(cmd.Id); if (user?.Role?.RoleName != Roles.Moderator) { throw new EntityNotFoundException(nameof(user), $"(id == {cmd.Id} && role == moderator)"); } var modRole = await _roles.FindByRole(Roles.User); user.Role = modRole; var demoted = await _users.UpdateUser(user); return(_mapper.Map <DomainUser>(demoted)); }