Skip to content

Commit

Permalink
#931: new Rest EndPoint isUserMemberOfTeams()
Browse files Browse the repository at this point in the history
  • Loading branch information
clean-coder committed Aug 6, 2024
1 parent 882b132 commit 43c86bc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import ch.puzzle.okr.dto.NewUserDto;
import ch.puzzle.okr.dto.UserDto;
import ch.puzzle.okr.dto.userOkrData.UserOkrDataDto;
import ch.puzzle.okr.dto.userOkrData.UserKeyResultDataDto;
import ch.puzzle.okr.mapper.UserMapper;
import ch.puzzle.okr.service.authorization.AuthorizationService;
import ch.puzzle.okr.service.authorization.UserAuthorizationService;
Expand Down Expand Up @@ -94,6 +93,18 @@ public Boolean isUserOwnerOfKeyResults(
return this.userAuthorizationService.isUserOwnerOfKeyResults(id);
}

@Operation(summary = "Check if User is member of Teams", description = "Check if User is member of any Team.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "true if user is member of a Team", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = Boolean.class)) }), })

@GetMapping(path = "/{id}/ismemberofteams")
public Boolean isUserMemberOfTeams(
@Parameter(description = "The ID of the user.", required = true) @PathVariable long id) {

return this.userAuthorizationService.isUserMemberOfTeams(id);
}

@Operation(summary = "Get User OKR Data", description = "Get User OKR Data")
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Returned User OKR Data.", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = UserOkrDataDto.class)) }), })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ public List<User> createUsers(List<User> userList) {
return userBusinessService.createUsers(userList);
}

public boolean isUserMemberOfTeams(long id) {
// TODO check Role
List<UserTeam> userTeamList = userBusinessService.getUserById(id).getUserTeamList();
return userTeamList != null && !userTeamList.isEmpty();
}

public boolean isUserOwnerOfKeyResults(long id) {
// TODO check Role
return keyResultBusinessService.isUserOwnerOfKeyResults(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ch.puzzle.okr.models.User;
import ch.puzzle.okr.models.authorization.AuthorizationUser;
import ch.puzzle.okr.service.business.UserBusinessService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand Down Expand Up @@ -106,4 +107,35 @@ void createUsers_shouldThrowErrorIfLoggedInUserIsNotOkrChampion() {
assertThrows(OkrResponseStatusException.class,
() -> userAuthorizationService.createUsers(List.of(user, user2)));
}

@DisplayName("isUserMemberOfTeams() should return false if user is not member of teams")
@Test
void isUserMemberOfTeamsShouldReturnFalseIfUserIsNotMemberOfTeams() {
// arrange
Long userId = 1L;
User userWithoutTeams = defaultUser(userId);
when(userBusinessService.getUserById(userId)).thenReturn(userWithoutTeams);

// act
boolean isUserMemberOfTeams = userAuthorizationService.isUserMemberOfTeams(1L);

// assert
assertFalse(isUserMemberOfTeams);
}

@DisplayName("isUserMemberOfTeams() should return true if user is member of teams")
@Test
void isUserMemberOfTeamsShouldReturnTrueIfUserIsMemberOfTeams() {
// arrange
User userWithTeams = user2;
Long userId = user2.getId();
when(userBusinessService.getUserById(userId)).thenReturn(userWithTeams);

// act
boolean isUserMemberOfTeams = userAuthorizationService.isUserMemberOfTeams(userId);

// assert
assertTrue(isUserMemberOfTeams);
}

}

0 comments on commit 43c86bc

Please sign in to comment.