public async Task AddUserToTeam_WhenUserNotFound_ReturnTeamNotFound() { var claims = new Claim("roles", "TeamLead"); var token = GetJwtToken(new[] { claims }); AddAuth(token); var request = new AssignUserToTeamRequest() { TeamId = TeamsDbSet.Get().First().Id, UserId = Guid.NewGuid() }; var httpResponse = await PostAsync(UserControllerRoutes.AddUserToTeam, request); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.BodyAs <ApiResponse <TimeTrackingUserDetailsDto> >(); response.VerifyNotSuccessResponseWithErrorCodeAndMessage(ErrorCode.UserNotFound); }
public async Task AddUserToTeam_WhenRequestNotValid_ReturnValidationErrors() { var claims = new Claim("roles", "TeamLead"); var token = GetJwtToken(new[] { claims }); AddAuth(token); var request = new AssignUserToTeamRequest() { TeamId = Guid.Empty, UserId = Guid.Empty }; var httpResponse = await PostAsync(UserControllerRoutes.AddUserToTeam, request); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.BodyAs <ApiResponse <TimeTrackingUserDetailsDto> >(); response.CheckValidationException(2); //team id is empty request = new AssignUserToTeamRequest() { UserId = Guid.NewGuid() }; httpResponse = await PostAsync(UserControllerRoutes.AddUserToTeam, request); httpResponse.EnsureSuccessStatusCode(); response = await httpResponse.BodyAs <ApiResponse <TimeTrackingUserDetailsDto> >(); response.CheckValidationException(1); //user id is empty request = new AssignUserToTeamRequest() { TeamId = Guid.NewGuid() }; httpResponse = await PostAsync(UserControllerRoutes.AddUserToTeam, request); httpResponse.EnsureSuccessStatusCode(); response = await httpResponse.BodyAs <ApiResponse <TimeTrackingUserDetailsDto> >(); response.CheckValidationException(1); }
public async Task <ApiResponse <TimeTrackingUserDto> > AddUserToTeam(AssignUserToTeamRequest request) { try { var teamFounded = await _teamRepository.GetByIdAsync(request.TeamId); if (teamFounded == null) { _logger.LogWarning("Failed to found team by id {0}", request.TeamId); return(new ApiResponse <TimeTrackingUserDto>() { StatusCode = 400, IsSuccess = false, ResponseException = new ApiError(ErrorCode.TeamNotFound, ErrorCode.TeamNotFound.GetDescription()) }); } var userFound = await _userRepository.GetByIdAsync(request.UserId); if (userFound == null) { _logger.LogWarning("Failed to find user by id {0}", request.UserId); return(new ApiResponse <TimeTrackingUserDto>() { StatusCode = 400, IsSuccess = false, ResponseException = new ApiError(ErrorCode.UserNotFound, ErrorCode.UserNotFound.GetDescription()) }); } userFound.TeamId = request.TeamId; userFound = await _userRepository.UpdateAsync(userFound); return(new ApiResponse <TimeTrackingUserDto>(_userMapper.MapToModel(userFound))); } catch (Exception ex) { _logger.LogWarning(ex, "An error occured while assigning user {0] to team team {1} ", request.UserId, request.TeamId); return(ApiResponse <TimeTrackingUserDto> .InternalError()); } }
public async Task AddUserToTeam_WhenRequestValid_ReturnValidationTimeTrackingUser() { var claims = new Claim("roles", "TeamLead"); var token = GetJwtToken(new[] { claims }); AddAuth(token); var request = new AssignUserToTeamRequest() { TeamId = TeamsDbSet.Get().First().Id, UserId = UsersDbSet.Get().First().Id }; var httpResponse = await PostAsync(UserControllerRoutes.AddUserToTeam, request); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.BodyAs <ApiResponse <TimeTrackingUserDetailsDto> >(); response.VerifySuccessResponse(); var user = await GetUserFromDatabase(request.UserId); user.TeamId.Should().Be(request.TeamId); await ReSeedDatabase(); }
//[Authorize(Roles = "TeamLead")] public async Task <ApiResponse <TimeTrackingUserDto> > AddUserToTeam([FromBody] AssignUserToTeamRequest request) { return(await _userService.AddUserToTeam(request)); }