From 43c86bc46ed364ec307b40640386792b5be5bd5f Mon Sep 17 00:00:00 2001 From: clean-coder Date: Tue, 6 Aug 2024 06:47:20 +0200 Subject: [PATCH] #931: new Rest EndPoint isUserMemberOfTeams() --- .../puzzle/okr/controller/UserController.java | 13 +++++++- .../UserAuthorizationService.java | 7 ++++ .../UserAuthorizationServiceTest.java | 32 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/ch/puzzle/okr/controller/UserController.java b/backend/src/main/java/ch/puzzle/okr/controller/UserController.java index 55beab0473..c8a2d61db8 100644 --- a/backend/src/main/java/ch/puzzle/okr/controller/UserController.java +++ b/backend/src/main/java/ch/puzzle/okr/controller/UserController.java @@ -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; @@ -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)) }), }) diff --git a/backend/src/main/java/ch/puzzle/okr/service/authorization/UserAuthorizationService.java b/backend/src/main/java/ch/puzzle/okr/service/authorization/UserAuthorizationService.java index 7828b3b4de..c95ab16253 100644 --- a/backend/src/main/java/ch/puzzle/okr/service/authorization/UserAuthorizationService.java +++ b/backend/src/main/java/ch/puzzle/okr/service/authorization/UserAuthorizationService.java @@ -65,7 +65,14 @@ public List createUsers(List userList) { return userBusinessService.createUsers(userList); } + public boolean isUserMemberOfTeams(long id) { + // TODO check Role + List userTeamList = userBusinessService.getUserById(id).getUserTeamList(); + return userTeamList != null && !userTeamList.isEmpty(); + } + public boolean isUserOwnerOfKeyResults(long id) { + // TODO check Role return keyResultBusinessService.isUserOwnerOfKeyResults(id); } diff --git a/backend/src/test/java/ch/puzzle/okr/service/authorization/UserAuthorizationServiceTest.java b/backend/src/test/java/ch/puzzle/okr/service/authorization/UserAuthorizationServiceTest.java index 8575cdd561..73af1ff069 100644 --- a/backend/src/test/java/ch/puzzle/okr/service/authorization/UserAuthorizationServiceTest.java +++ b/backend/src/test/java/ch/puzzle/okr/service/authorization/UserAuthorizationServiceTest.java @@ -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; @@ -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); + } + }