diff --git a/src/main/java/de/caritas/cob/videoservice/api/service/RejectVideoCallService.java b/src/main/java/de/caritas/cob/videoservice/api/service/RejectVideoCallService.java index c2004e1..b225737 100644 --- a/src/main/java/de/caritas/cob/videoservice/api/service/RejectVideoCallService.java +++ b/src/main/java/de/caritas/cob/videoservice/api/service/RejectVideoCallService.java @@ -19,6 +19,7 @@ public class RejectVideoCallService { private final @NonNull MessageControllerApi messageControllerApi; private final @NonNull SecurityHeaderSupplier securityHeaderSupplier; + private final @NonNull TenantHeaderSupplier tenantHeaderSupplier; /** * Sends a system message with rejection type to the message service. @@ -34,6 +35,7 @@ public void rejectVideoCall(RejectVideoCallDTO rejectVideoCallDto) { private void addDefaultHeaders(ApiClient apiClient) { var headers = this.securityHeaderSupplier.getKeycloakAndCsrfHttpHeaders(); + tenantHeaderSupplier.addTenantHeader(headers); headers.forEach((key, value) -> apiClient.addDefaultHeader(key, value.iterator().next())); } diff --git a/src/main/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplier.java b/src/main/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplier.java index 39f4ad8..7989075 100644 --- a/src/main/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplier.java +++ b/src/main/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplier.java @@ -2,7 +2,6 @@ import de.caritas.cob.videoservice.api.tenant.TenantContext; import java.util.Optional; -import javax.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -33,12 +32,11 @@ public void addTenantHeader(HttpHeaders headers) { /** * Resolve tenantID from current request. * - * @return + * @return the id of the tenant */ public Optional getTenantFromHeader() { - HttpServletRequest request = - ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()) - .getRequest(); + var request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()) + .getRequest(); try { return Optional.of(Long.parseLong(request.getHeader("tenantId"))); } catch (NumberFormatException exception) { @@ -46,4 +44,5 @@ public Optional getTenantFromHeader() { return Optional.empty(); } } + } diff --git a/src/test/java/de/caritas/cob/videoservice/api/service/RejectVideoCallServiceTest.java b/src/test/java/de/caritas/cob/videoservice/api/service/RejectVideoCallServiceTest.java index 96891c0..fd79d9f 100644 --- a/src/test/java/de/caritas/cob/videoservice/api/service/RejectVideoCallServiceTest.java +++ b/src/test/java/de/caritas/cob/videoservice/api/service/RejectVideoCallServiceTest.java @@ -1,7 +1,6 @@ package de.caritas.cob.videoservice.api.service; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.times; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -31,14 +30,18 @@ public class RejectVideoCallServiceTest { @Mock private SecurityHeaderSupplier securityHeaderSupplier; + @Mock + private TenantHeaderSupplier tenantHeaderSupplier; + @Mock private ApiClient apiClient; @Test public void rejectVideoCall_Should_useServicesCorrectly() { + var securityHeaders = new HttpHeaders(); when(this.messageControllerApi.getApiClient()).thenReturn(this.apiClient); - when(this.securityHeaderSupplier.getKeycloakAndCsrfHttpHeaders()).thenReturn(new HttpHeaders()); - RejectVideoCallDTO rejectVideoCallDto = new EasyRandom().nextObject(RejectVideoCallDTO.class); + when(this.securityHeaderSupplier.getKeycloakAndCsrfHttpHeaders()).thenReturn(securityHeaders); + var rejectVideoCallDto = new EasyRandom().nextObject(RejectVideoCallDTO.class); this.rejectVideoCallService.rejectVideoCall(rejectVideoCallDto); @@ -46,9 +49,10 @@ public void rejectVideoCall_Should_useServicesCorrectly() { .eventType(EventTypeEnum.IGNORED_CALL) .initiatorUserName(rejectVideoCallDto.getInitiatorUsername()) .initiatorRcUserId(rejectVideoCallDto.getInitiatorRcUserId()); - verify(this.securityHeaderSupplier, times(1)).getKeycloakAndCsrfHttpHeaders(); - verify(this.messageControllerApi, times(1)) - .createVideoHintMessage(eq(rejectVideoCallDto.getRcGroupId()), eq(expectedMessage)); + verify(this.securityHeaderSupplier).getKeycloakAndCsrfHttpHeaders(); + verify(this.tenantHeaderSupplier).addTenantHeader(securityHeaders); + verify(this.messageControllerApi) + .createVideoHintMessage(rejectVideoCallDto.getRcGroupId(), expectedMessage); } } diff --git a/src/test/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplierTest.java b/src/test/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplierTest.java new file mode 100644 index 0000000..bb85658 --- /dev/null +++ b/src/test/java/de/caritas/cob/videoservice/api/service/TenantHeaderSupplierTest.java @@ -0,0 +1,69 @@ +package de.caritas.cob.videoservice.api.service; + + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.core.Is.is; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +import de.caritas.cob.videoservice.api.tenant.TenantContext; +import javax.servlet.http.HttpServletRequest; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpHeaders; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +class TenantHeaderSupplierTest { + + private final TenantHeaderSupplier tenantHeaderSupplier = new TenantHeaderSupplier(); + + @Test + void addTenantHeader_Should_addTenantIdToHeaders_When_multiTenancyIsEnabled() { + setField(tenantHeaderSupplier, "multitenancy", true); + var httpHeaders = new HttpHeaders(); + TenantContext.setCurrentTenant(1L); + + tenantHeaderSupplier.addTenantHeader(httpHeaders); + + assertThat(httpHeaders.get("tenantId").get(0), is("1")); + } + + @Test + void addTenantHeader_Should_notAddTenantIdToHeaders_When_multiTenancyIsDisabled() { + setField(tenantHeaderSupplier, "multitenancy", false); + var httpHeaders = new HttpHeaders(); + + tenantHeaderSupplier.addTenantHeader(httpHeaders); + + assertThat(httpHeaders.get("tenantId"), nullValue()); + } + + @Test + void getTenantFromHeader_Should_returnTenantId_When_tenantIdExistsInHeader() { + givenMockedServletRequestWithTenandIdHeaderValue("1"); + + var result = tenantHeaderSupplier.getTenantFromHeader(); + + assertThat(result.isPresent(), is(true)); + assertThat(result.get(), is(1L)); + } + + @Test + void getTenantFromHeader_Should_returnOptionalEmpty_When_tenantIdIsNoNumber() { + givenMockedServletRequestWithTenandIdHeaderValue("no number"); + + var result = tenantHeaderSupplier.getTenantFromHeader(); + + assertThat(result.isPresent(), is(false)); + } + + private void givenMockedServletRequestWithTenandIdHeaderValue(String tenantIdValue) { + var mockedServletRequest = mock(HttpServletRequest.class); + when(mockedServletRequest.getHeader("tenantId")).thenReturn(tenantIdValue); + var requestAttributes = new ServletRequestAttributes(mockedServletRequest); + RequestContextHolder.setRequestAttributes(requestAttributes); + } + +}