From ef192a3e3762485d4eb6a6c585d093916632515d Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Wed, 3 Apr 2024 13:40:56 +0200 Subject: [PATCH 01/10] fix: * Rename getSessionsByConsultantAndGroupOrFeedbackGroupIds to getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds * Adapt the getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds method * Adapt the tests --- .../api/service/session/SessionService.java | 17 ++++++++-- .../ConsultantSessionListService.java | 2 +- .../service/session/SessionServiceTest.java | 31 +++++++++++++++++-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java b/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java index 48c8058bf..ff8177cb8 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java @@ -38,6 +38,7 @@ import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -49,6 +50,7 @@ import javax.ws.rs.BadRequestException; import lombok.NonNull; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.lang.Nullable; import org.springframework.stereotype.Service; @@ -56,6 +58,7 @@ /** Service for sessions */ @Service @RequiredArgsConstructor +@Slf4j public class SessionService { private final @NonNull SessionRepository sessionRepository; @@ -437,11 +440,21 @@ private List fetchAgencies(List sessions) { * @param roles the roles of the given consultant * @return {@link ConsultantSessionResponseDTO} */ - public List getSessionsByConsultantAndGroupOrFeedbackGroupIds( + public List getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds( Consultant consultant, Set rcGroupIds, Set roles) { checkForUserOrConsultantRole(roles); var sessions = sessionRepository.findByGroupOrFeedbackGroupIds(rcGroupIds); - sessions.forEach(session -> checkConsultantAssignment(consultant, session)); + + Iterator iterator = sessions.iterator(); + while (iterator.hasNext()) { + Session session = iterator.next(); + try { + checkConsultantAssignment(consultant, session); + } catch (ForbiddenException e) { + log.info(e.getMessage()); + iterator.remove(); + } + } return mapSessionsToConsultantSessionDto(sessions); } diff --git a/src/main/java/de/caritas/cob/userservice/api/service/sessionlist/ConsultantSessionListService.java b/src/main/java/de/caritas/cob/userservice/api/service/sessionlist/ConsultantSessionListService.java index b333615e1..b5fabef37 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/sessionlist/ConsultantSessionListService.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/sessionlist/ConsultantSessionListService.java @@ -45,7 +45,7 @@ public List retrieveSessionsForConsultantAndGroupI Consultant consultant, List rcGroupIds, Set roles) { var groupIds = new HashSet<>(rcGroupIds); var sessions = - sessionService.getSessionsByConsultantAndGroupOrFeedbackGroupIds( + sessionService.getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds( consultant, groupIds, roles); var chats = chatService.getChatSessionsForConsultantByGroupIds(groupIds); diff --git a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java index f6cf4d17d..c527cd953 100644 --- a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java @@ -783,7 +783,7 @@ void initializeSession_Should_initializePeerChat_When_consultingTypeSettingsHasP @Test void - getSessionsByConsultantAndGroupOrFeedbackGroupIds_should_find_new_anonymous_enquiry_if_consultant_may_advise_consulting_type() { + getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds_should_find_new_anonymous_enquiry_if_consultant_may_advise_consulting_type() { Session anonymousEnquiry = createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); when(sessionRepository.findByGroupOrFeedbackGroupIds(singleton("rcGroupId"))) @@ -794,12 +794,39 @@ void initializeSession_Should_initializePeerChat_When_consultingTypeSettingsHasP var consultant = createConsultantWithAgencies(agency); var sessionResponse = - sessionService.getSessionsByConsultantAndGroupOrFeedbackGroupIds( + sessionService.getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds( consultant, singleton("rcGroupId"), singleton(UserRole.CONSULTANT.getValue())); assertEquals(1, sessionResponse.size()); } + @Test + void + getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds_should_only_return_the_sessions_the_consultant_can_see() { + Session allowedSession = + createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); + Session notAllowedSession = + createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); + List sessions = new ArrayList<>(); + sessions.add(notAllowedSession); + sessions.add(allowedSession); + ConsultantAgency agency = new ConsultantAgency(); + agency.setAgencyId(4711L); + var consultant = createConsultantWithAgencies(agency); + allowedSession.setConsultant(consultant); + allowedSession.setId(1L); + notAllowedSession.setId(2L); + when(sessionRepository.findByGroupOrFeedbackGroupIds(singleton("rcGroupId"))) + .thenReturn(sessions); + + var sessionResponse = + sessionService.getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds( + consultant, singleton("rcGroupId"), singleton(UserRole.CONSULTANT.getValue())); + + assertEquals(1, sessionResponse.size()); + assertEquals(sessionResponse.get(0).getSession().getId(),allowedSession.getId()); + } + @Test void getSessionsByIds_should_find_new_anonymous_enquiry_if_consultant_may_advise_consulting_type() { From f3ca251b234bbfaefbe2b67f8576010da1fb5faa Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Wed, 3 Apr 2024 14:25:04 +0200 Subject: [PATCH 02/10] fix: * Rename getSessionsByConsultantAndGroupOrFeedbackGroupIds to getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds * Adapt the getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds method * Adapt the tests --- .../cob/userservice/api/service/session/SessionService.java | 5 +++-- .../userservice/api/service/session/SessionServiceTest.java | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java b/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java index ff8177cb8..c72df5177 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java @@ -440,8 +440,9 @@ private List fetchAgencies(List sessions) { * @param roles the roles of the given consultant * @return {@link ConsultantSessionResponseDTO} */ - public List getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds( - Consultant consultant, Set rcGroupIds, Set roles) { + public List + getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds( + Consultant consultant, Set rcGroupIds, Set roles) { checkForUserOrConsultantRole(roles); var sessions = sessionRepository.findByGroupOrFeedbackGroupIds(rcGroupIds); diff --git a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java index c527cd953..8bca0daa8 100644 --- a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java @@ -783,7 +783,7 @@ void initializeSession_Should_initializePeerChat_When_consultingTypeSettingsHasP @Test void - getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds_should_find_new_anonymous_enquiry_if_consultant_may_advise_consulting_type() { + getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds_should_find_new_anonymous_enquiry_if_consultant_may_advise_consulting_type() { Session anonymousEnquiry = createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); when(sessionRepository.findByGroupOrFeedbackGroupIds(singleton("rcGroupId"))) @@ -802,7 +802,7 @@ void initializeSession_Should_initializePeerChat_When_consultingTypeSettingsHasP @Test void - getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds_should_only_return_the_sessions_the_consultant_can_see() { + getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds_should_only_return_the_sessions_the_consultant_can_see() { Session allowedSession = createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); Session notAllowedSession = @@ -824,7 +824,7 @@ void initializeSession_Should_initializePeerChat_When_consultingTypeSettingsHasP consultant, singleton("rcGroupId"), singleton(UserRole.CONSULTANT.getValue())); assertEquals(1, sessionResponse.size()); - assertEquals(sessionResponse.get(0).getSession().getId(),allowedSession.getId()); + assertEquals(sessionResponse.get(0).getSession().getId(), allowedSession.getId()); } @Test From 48206df823912978b1ca8dfdd9c9fe0762b73d2f Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Thu, 4 Apr 2024 10:08:28 +0200 Subject: [PATCH 03/10] fix: * Added a new boolean method and used java stream logic to filter the list as commented on the PR --- .../api/service/session/SessionService.java | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java b/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java index c72df5177..539ee0c1a 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java @@ -446,17 +446,11 @@ private List fetchAgencies(List sessions) { checkForUserOrConsultantRole(roles); var sessions = sessionRepository.findByGroupOrFeedbackGroupIds(rcGroupIds); - Iterator iterator = sessions.iterator(); - while (iterator.hasNext()) { - Session session = iterator.next(); - try { - checkConsultantAssignment(consultant, session); - } catch (ForbiddenException e) { - log.info(e.getMessage()); - iterator.remove(); - } - } - return mapSessionsToConsultantSessionDto(sessions); + List allowedSessions = sessions.stream() + .filter(session -> isConsultantPermittedToSession(consultant, session)) + .collect(Collectors.toList()); + + return mapSessionsToConsultantSessionDto(allowedSessions); } /** @@ -550,6 +544,16 @@ private void checkIfConsultantAndNotAssignedToSessionOrAgency( } } + private boolean isConsultantPermittedToSession(Consultant consultant, Session session) { + try { + checkConsultantAssignment(consultant, session); + } catch (ForbiddenException e) { + log.info(e.getMessage()); + return false; + } + return true; + } + private void checkConsultantAssignment(Consultant consultant, Session session) { if (session.isAdvisedBy(consultant) || isAllowedToAdvise(consultant, session) From cf861b3999f213db39335ca444a4d91d212529ef Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Thu, 4 Apr 2024 10:20:07 +0200 Subject: [PATCH 04/10] fix: * change the asserts to use fluent assertj assertions instead as commented on PR --- .../api/service/session/SessionServiceTest.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java index 8bca0daa8..66b03656a 100644 --- a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java @@ -26,6 +26,7 @@ import static java.util.Collections.singleton; import static java.util.Collections.singletonList; import static liquibase.util.BooleanUtils.isTrue; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.CoreMatchers.everyItem; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.MatcherAssert.assertThat; @@ -803,6 +804,7 @@ void initializeSession_Should_initializePeerChat_When_consultingTypeSettingsHasP @Test void getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds_should_only_return_the_sessions_the_consultant_can_see() { + //given Session allowedSession = createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); Session notAllowedSession = @@ -818,13 +820,13 @@ void initializeSession_Should_initializePeerChat_When_consultingTypeSettingsHasP notAllowedSession.setId(2L); when(sessionRepository.findByGroupOrFeedbackGroupIds(singleton("rcGroupId"))) .thenReturn(sessions); - + //when var sessionResponse = sessionService.getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds( consultant, singleton("rcGroupId"), singleton(UserRole.CONSULTANT.getValue())); - - assertEquals(1, sessionResponse.size()); - assertEquals(sessionResponse.get(0).getSession().getId(), allowedSession.getId()); + //then + assertThat(sessionResponse).hasSize(1); + assertThat(sessionResponse.get(0).getSession().getId()).isEqualTo(allowedSession.getId()); } @Test From 98e9f58d74aa725e9d568ccd1b75939d054e7198 Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Thu, 4 Apr 2024 10:41:16 +0200 Subject: [PATCH 05/10] fix: * extract the given object creation to smaller methods as commented on the PR. --- .../service/session/SessionServiceTest.java | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java index 66b03656a..085ac374f 100644 --- a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java @@ -805,19 +805,14 @@ void initializeSession_Should_initializePeerChat_When_consultingTypeSettingsHasP void getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds_should_only_return_the_sessions_the_consultant_can_see() { //given - Session allowedSession = - createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); - Session notAllowedSession = - createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); List sessions = new ArrayList<>(); - sessions.add(notAllowedSession); - sessions.add(allowedSession); ConsultantAgency agency = new ConsultantAgency(); agency.setAgencyId(4711L); var consultant = createConsultantWithAgencies(agency); - allowedSession.setConsultant(consultant); - allowedSession.setId(1L); - notAllowedSession.setId(2L); + var allowedSession = giveAllowedSessionWithID(1L, consultant); + var unhallowedSession = giveUnhallowedSessionWithID(2L); + sessions.add(unhallowedSession); + sessions.add(allowedSession); when(sessionRepository.findByGroupOrFeedbackGroupIds(singleton("rcGroupId"))) .thenReturn(sessions); //when @@ -911,6 +906,21 @@ private List getSomeUserId(String someUserId, Session an someUserId, singleton(anonymousEnquiry.getId()), singleton(UserRole.ANONYMOUS.getValue())); } + private Session giveAllowedSessionWithID(Long id, Consultant consultant){ + Session allowedSession = + createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); + allowedSession.setId(id); + allowedSession.setConsultant(consultant); + return allowedSession; + } + + private Session giveUnhallowedSessionWithID(Long id){ + Session unhallowedSession = + createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); + unhallowedSession.setId(id); + return unhallowedSession; + } + private Session createAnonymousNewEnquiryWithConsultingType(int consultingTypeId) { var session = easyRandom.nextObject(Session.class); session.setAgencyId(null); From 37ca1738132148db0f83dec8922128e5d64d08a8 Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Thu, 4 Apr 2024 10:44:53 +0200 Subject: [PATCH 06/10] fix: * run mvn spotless:apply --- .../api/service/session/SessionService.java | 8 ++++---- .../api/service/session/SessionServiceTest.java | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java b/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java index 539ee0c1a..177233fe9 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java @@ -38,7 +38,6 @@ import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collection; -import java.util.Iterator; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -446,9 +445,10 @@ private List fetchAgencies(List sessions) { checkForUserOrConsultantRole(roles); var sessions = sessionRepository.findByGroupOrFeedbackGroupIds(rcGroupIds); - List allowedSessions = sessions.stream() - .filter(session -> isConsultantPermittedToSession(consultant, session)) - .collect(Collectors.toList()); + List allowedSessions = + sessions.stream() + .filter(session -> isConsultantPermittedToSession(consultant, session)) + .collect(Collectors.toList()); return mapSessionsToConsultantSessionDto(allowedSessions); } diff --git a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java index 085ac374f..aa637a8ea 100644 --- a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java @@ -804,7 +804,7 @@ void initializeSession_Should_initializePeerChat_When_consultingTypeSettingsHasP @Test void getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds_should_only_return_the_sessions_the_consultant_can_see() { - //given + // given List sessions = new ArrayList<>(); ConsultantAgency agency = new ConsultantAgency(); agency.setAgencyId(4711L); @@ -815,11 +815,11 @@ void initializeSession_Should_initializePeerChat_When_consultingTypeSettingsHasP sessions.add(allowedSession); when(sessionRepository.findByGroupOrFeedbackGroupIds(singleton("rcGroupId"))) .thenReturn(sessions); - //when + // when var sessionResponse = sessionService.getAllowedSessionsByConsultantAndGroupOrFeedbackGroupIds( consultant, singleton("rcGroupId"), singleton(UserRole.CONSULTANT.getValue())); - //then + // then assertThat(sessionResponse).hasSize(1); assertThat(sessionResponse.get(0).getSession().getId()).isEqualTo(allowedSession.getId()); } @@ -906,7 +906,7 @@ private List getSomeUserId(String someUserId, Session an someUserId, singleton(anonymousEnquiry.getId()), singleton(UserRole.ANONYMOUS.getValue())); } - private Session giveAllowedSessionWithID(Long id, Consultant consultant){ + private Session giveAllowedSessionWithID(Long id, Consultant consultant) { Session allowedSession = createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); allowedSession.setId(id); @@ -914,7 +914,7 @@ private Session giveAllowedSessionWithID(Long id, Consultant consultant){ return allowedSession; } - private Session giveUnhallowedSessionWithID(Long id){ + private Session giveUnhallowedSessionWithID(Long id) { Session unhallowedSession = createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); unhallowedSession.setId(id); From 861249c91707b04aba74ba273f656e39f0df3fbc Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Thu, 4 Apr 2024 11:52:17 +0200 Subject: [PATCH 07/10] fix: * fixed typo --- .../userservice/api/service/session/SessionServiceTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java index aa637a8ea..8005e4019 100644 --- a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java @@ -810,8 +810,8 @@ void initializeSession_Should_initializePeerChat_When_consultingTypeSettingsHasP agency.setAgencyId(4711L); var consultant = createConsultantWithAgencies(agency); var allowedSession = giveAllowedSessionWithID(1L, consultant); - var unhallowedSession = giveUnhallowedSessionWithID(2L); - sessions.add(unhallowedSession); + var notAllowed = giveNotallowedSessionWithID(2L); + sessions.add(notAllowed); sessions.add(allowedSession); when(sessionRepository.findByGroupOrFeedbackGroupIds(singleton("rcGroupId"))) .thenReturn(sessions); @@ -914,7 +914,7 @@ private Session giveAllowedSessionWithID(Long id, Consultant consultant) { return allowedSession; } - private Session giveUnhallowedSessionWithID(Long id) { + private Session giveNotallowedSessionWithID(Long id) { Session unhallowedSession = createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); unhallowedSession.setId(id); From d88e1ce06df8dde4b12cac3e6e5842d608d90279 Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Thu, 4 Apr 2024 11:54:30 +0200 Subject: [PATCH 08/10] fix: * fixed typo --- .../userservice/api/service/session/SessionServiceTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java index 8005e4019..bf14e1ee5 100644 --- a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java @@ -810,8 +810,8 @@ void initializeSession_Should_initializePeerChat_When_consultingTypeSettingsHasP agency.setAgencyId(4711L); var consultant = createConsultantWithAgencies(agency); var allowedSession = giveAllowedSessionWithID(1L, consultant); - var notAllowed = giveNotallowedSessionWithID(2L); - sessions.add(notAllowed); + var notAllowedSession = giveNotallowedSessionWithID(2L); + sessions.add(notAllowedSession); sessions.add(allowedSession); when(sessionRepository.findByGroupOrFeedbackGroupIds(singleton("rcGroupId"))) .thenReturn(sessions); From 34077ebd96d790070dcd68411b5632d68f7cf77a Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Thu, 4 Apr 2024 14:23:46 +0200 Subject: [PATCH 09/10] fix: * improve the test code --- .../api/service/session/SessionServiceTest.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java index bf14e1ee5..1c69b67ae 100644 --- a/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/service/session/SessionServiceTest.java @@ -810,8 +810,7 @@ void initializeSession_Should_initializePeerChat_When_consultingTypeSettingsHasP agency.setAgencyId(4711L); var consultant = createConsultantWithAgencies(agency); var allowedSession = giveAllowedSessionWithID(1L, consultant); - var notAllowedSession = giveNotallowedSessionWithID(2L); - sessions.add(notAllowedSession); + sessions.add(giveAllowedSessionWithID(2L, null)); sessions.add(allowedSession); when(sessionRepository.findByGroupOrFeedbackGroupIds(singleton("rcGroupId"))) .thenReturn(sessions); @@ -914,13 +913,6 @@ private Session giveAllowedSessionWithID(Long id, Consultant consultant) { return allowedSession; } - private Session giveNotallowedSessionWithID(Long id) { - Session unhallowedSession = - createAnonymousNewEnquiryWithConsultingType(AGENCY_DTO_SUCHT.getConsultingType()); - unhallowedSession.setId(id); - return unhallowedSession; - } - private Session createAnonymousNewEnquiryWithConsultingType(int consultingTypeId) { var session = easyRandom.nextObject(Session.class); session.setAgencyId(null); From c8d42c0b223355bcaa277ca3ad34eb7f42fe0e61 Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Thu, 4 Apr 2024 16:20:25 +0200 Subject: [PATCH 10/10] fix: * fix failing tests --- .../UserControllerSessionE2EIT.java | 48 +++---------------- 1 file changed, 7 insertions(+), 41 deletions(-) diff --git a/src/test/java/de/caritas/cob/userservice/api/adapters/web/controller/UserControllerSessionE2EIT.java b/src/test/java/de/caritas/cob/userservice/api/adapters/web/controller/UserControllerSessionE2EIT.java index 570aef110..16272c7ac 100644 --- a/src/test/java/de/caritas/cob/userservice/api/adapters/web/controller/UserControllerSessionE2EIT.java +++ b/src/test/java/de/caritas/cob/userservice/api/adapters/web/controller/UserControllerSessionE2EIT.java @@ -99,6 +99,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.keycloak.admin.client.Keycloak; import org.keycloak.admin.client.resource.RealmResource; import org.keycloak.admin.client.resource.RoleMappingResource; @@ -710,36 +712,19 @@ void getSessionsForGroupOrFeedbackGroupIdsShouldBeForbiddenIfUserDoesNotParticip .andExpect(status().isForbidden()); } - @Test + @ParameterizedTest @WithMockUser(authorities = {AuthorityValue.CONSULTANT_DEFAULT}) + @ValueSource(strings = {"QBv2xym9qQ2DoAxkR", "doesNotExist", "mzAdWzQEobJ2PkoxP"}) void - getSessionsForGroupOrFeedbackGroupIdsShouldBeForbiddenIfConsultantDoesNotParticipateInSession() - throws Exception { - givenAConsultantWithSessions(); - givenNoRocketChatSubscriptionUpdates(); - givenNoRocketChatRoomUpdates(); - - mockMvc - .perform( - get("/users/sessions/room?rcGroupIds=QBv2xym9qQ2DoAxkR") - .cookie(CSRF_COOKIE) - .header(CSRF_HEADER, CSRF_VALUE) - .header(RC_TOKEN_HEADER_PARAMETER_NAME, RC_TOKEN) - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isForbidden()); - } - - @Test - @WithMockUser(authorities = {AuthorityValue.CONSULTANT_DEFAULT}) - void getSessionsForGroupOrFeedbackGroupIdsShouldBeNoContentIfNoSessionsFoundForIds() - throws Exception { + getSessionsForGroupOrFeedbackGroupIdsShouldBeNoContentIfConsultantDoesNotParticipateInSessionOrNoSessionsFoundForIdsOrNewEnquiriesForConsultantsNotInAgency( + String rcGroupId) throws Exception { givenAConsultantWithSessions(); givenNoRocketChatSubscriptionUpdates(); givenNoRocketChatRoomUpdates(); mockMvc .perform( - get("/users/sessions/room?rcGroupIds=doesNotExist") + get("/users/sessions/room?rcGroupIds=" + rcGroupId) .cookie(CSRF_COOKIE) .header(CSRF_HEADER, CSRF_VALUE) .header(RC_TOKEN_HEADER_PARAMETER_NAME, RC_TOKEN) @@ -768,25 +753,6 @@ void getSessionsForGroupOrFeedbackGroupIdsShouldBeNoContentIfNoSessionsFoundForI .andExpect(jsonPath("sessions", hasSize(1))); } - @Test - @WithMockUser(authorities = {AuthorityValue.CONSULTANT_DEFAULT}) - void - getSessionsForGroupOrFeedbackGroupIdsShouldReturnForbiddenForNewEnquiriesForConsultantsNotInAgency() - throws Exception { - givenAConsultantWithSessions(); - givenNoRocketChatSubscriptionUpdates(); - givenNoRocketChatRoomUpdates(); - - mockMvc - .perform( - get("/users/sessions/room?rcGroupIds=mzAdWzQEobJ2PkoxP") - .cookie(CSRF_COOKIE) - .header(CSRF_HEADER, CSRF_VALUE) - .header(RC_TOKEN_HEADER_PARAMETER_NAME, RC_TOKEN) - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isForbidden()); - } - @Test @WithMockUser(authorities = {AuthorityValue.USER_DEFAULT}) void getSessionForIdShouldFindSessionsBySessionId() throws Exception {