Skip to content

Commit

Permalink
Merge pull request #3 from virtualidentityag/bugfix-lost-domain-to-cts
Browse files Browse the repository at this point in the history
Fix: provide origin header read and use host as fallback
  • Loading branch information
tkuzynow authored Feb 18, 2022
2 parents 58f73f3 + 027a2f0 commit 819e677
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 158 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ VOLUME ["/tmp","/log"]
EXPOSE 8080
ARG JAR_FILE
COPY ./AgencyService.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ENTRYPOINT ["java","-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.caritas.cob.agencyservice.api.service;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

import de.caritas.cob.agencyservice.api.service.securityheader.SecurityHeaderSupplier;
import de.caritas.cob.agencyservice.config.ConsultingTypeCachingConfig;
import de.caritas.cob.agencyservice.consultingtypeservice.generated.ApiClient;
Expand All @@ -23,6 +25,9 @@
@RequiredArgsConstructor
public class ConsultingTypeService {

private static final String ORIGIN_HEADER_NAME = "origin";
private static final String HOST_HEADER_NAME = "host";

private final @NonNull ConsultingTypeControllerApi consultingTypeControllerApi;
private final @NonNull SecurityHeaderSupplier securityHeaderSupplier;

Expand All @@ -49,7 +54,7 @@ private void addDefaultHeaders(ApiClient apiClient) {
private void addOriginHeader(HttpHeaders headers) {
String originHeaderValue = getOriginHeaderValue();
if (originHeaderValue != null) {
headers.add("origin", originHeaderValue);
headers.add(ORIGIN_HEADER_NAME, originHeaderValue);
}
}

Expand All @@ -58,9 +63,11 @@ private String getOriginHeaderValue() {
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest();

return Collections.list(request.getHeaderNames())
var requestHeaders = Collections.list(request.getHeaderNames())
.stream()
.collect(Collectors.toMap(h -> h, request::getHeader)).get("host");
.collect(Collectors.toMap(headerName -> headerName, request::getHeader));
return isNotBlank(requestHeaders.get(ORIGIN_HEADER_NAME)) ? requestHeaders
.get(ORIGIN_HEADER_NAME) : requestHeaders.get(HOST_HEADER_NAME);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.Assertions.assertThrows;

import de.caritas.cob.agencyservice.api.model.AgencyLinks;
import de.caritas.cob.agencyservice.api.model.HalLink.MethodEnum;
import de.caritas.cob.agencyservice.api.repository.agency.Agency;
import org.jeasy.random.EasyRandom;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

public class AgencyLinksBuilderTest {
@ExtendWith(MockitoExtension.class)
class AgencyLinksBuilderTest {

@Test
public void buildAgencyLinks_Should_returnAgencyLinks_When_AgencyIsSet() {
void buildAgencyLinks_Should_returnAgencyLinks_When_AgencyIsSet() {

EasyRandom easyRandom = new EasyRandom();
Agency agency = easyRandom.nextObject(Agency.class);
var easyRandom = new EasyRandom();
var agency = easyRandom.nextObject(Agency.class);

AgencyLinks agencyLinks = AgencyLinksBuilder.getInstance(agency)
var agencyLinks = AgencyLinksBuilder.getInstance(agency)
.buildAgencyLinks();

assertThat(agencyLinks, notNullValue());
Expand All @@ -38,14 +41,11 @@ public void buildAgencyLinks_Should_returnAgencyLinks_When_AgencyIsSet() {
assertThat(agencyLinks.getPostcodeRanges().getMethod(), is(MethodEnum.GET));
assertThat(agencyLinks.getPostcodeRanges().getHref(),
is(String.format("/agencyadmin/postcoderanges/%s", agency.getId())));

}

@Test(expected = NullPointerException.class)
public void buildAgencyLinks_Should_ThrowNullPointerException_WhenAgencyIsNotSet() {

AgencyLinksBuilder.getInstance(null).buildAgencyLinks();

@Test
void buildAgencyLinks_Should_ThrowNullPointerException_WhenAgencyIsNotSet() {
assertThrows(NullPointerException.class, () -> AgencyLinksBuilder.getInstance(null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
import static org.hamcrest.Matchers.nullValue;

import de.caritas.cob.agencyservice.api.model.HalLink.MethodEnum;
import de.caritas.cob.agencyservice.api.model.PaginationLinks;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

public class DiocesePaginationLinkBuilderTest {
@ExtendWith(MockitoExtension.class)
class DiocesePaginationLinkBuilderTest {

@Test
public void buildPaginationLinks_Should_returnPaginationLinks_When_allParametersAreSet() {
PaginationLinks paginationLinks = DiocesePaginationLinkBuilder.getInstance()
void buildPaginationLinks_Should_returnPaginationLinks_When_allParametersAreSet() {
var paginationLinks = DiocesePaginationLinkBuilder.getInstance()
.withPage(2)
.withPerPage(20)
.withTotalPages(5)
Expand All @@ -36,8 +38,8 @@ public void buildPaginationLinks_Should_returnPaginationLinks_When_allParameters
}

@Test
public void buildPaginationLinks_Should_havePreviousLink_When_currentPageIsNotTheFirst() {
PaginationLinks paginationLinks = DiocesePaginationLinkBuilder.getInstance()
void buildPaginationLinks_Should_havePreviousLink_When_currentPageIsNotTheFirst() {
var paginationLinks = DiocesePaginationLinkBuilder.getInstance()
.withPage(2)
.withPerPage(20)
.withTotalPages(5)
Expand All @@ -49,8 +51,8 @@ public void buildPaginationLinks_Should_havePreviousLink_When_currentPageIsNotTh
}

@Test
public void buildPaginationLinks_ShouldNot_havePreviousLink_When_currentPageIsTheFirst() {
PaginationLinks paginationLinks = DiocesePaginationLinkBuilder.getInstance()
void buildPaginationLinks_ShouldNot_havePreviousLink_When_currentPageIsTheFirst() {
var paginationLinks = DiocesePaginationLinkBuilder.getInstance()
.withPage(1)
.withPerPage(20)
.withTotalPages(5)
Expand All @@ -60,8 +62,8 @@ public void buildPaginationLinks_ShouldNot_havePreviousLink_When_currentPageIsTh
}

@Test
public void buildPaginationLinks_Should_haveNextLink_When_currentPageIsNotTheLast() {
PaginationLinks paginationLinks = DiocesePaginationLinkBuilder.getInstance()
void buildPaginationLinks_Should_haveNextLink_When_currentPageIsNotTheLast() {
var paginationLinks = DiocesePaginationLinkBuilder.getInstance()
.withPage(2)
.withPerPage(20)
.withTotalPages(5)
Expand All @@ -73,8 +75,8 @@ public void buildPaginationLinks_Should_haveNextLink_When_currentPageIsNotTheLas
}

@Test
public void buildPaginationLinks_ShouldNot_haveNextLink_When_currentPageIsTheLast() {
PaginationLinks paginationLinks = DiocesePaginationLinkBuilder.getInstance()
void buildPaginationLinks_ShouldNot_haveNextLink_When_currentPageIsTheLast() {
var paginationLinks = DiocesePaginationLinkBuilder.getInstance()
.withPage(5)
.withPerPage(20)
.withTotalPages(5)
Expand All @@ -84,8 +86,8 @@ public void buildPaginationLinks_ShouldNot_haveNextLink_When_currentPageIsTheLas
}

@Test
public void buildPaginationLinks_Should_returnSelfLink() {
PaginationLinks paginationLinks = DiocesePaginationLinkBuilder.getInstance()
void buildPaginationLinks_Should_returnSelfLink() {
var paginationLinks = DiocesePaginationLinkBuilder.getInstance()
.withPage(1)
.withPerPage(20)
.buildPaginationLinks();
Expand All @@ -96,8 +98,8 @@ public void buildPaginationLinks_Should_returnSelfLink() {
}

@Test
public void buildPaginationLinks_Should_returnDefaultPaginationValues_When_noParametersAreSet() {
PaginationLinks paginationLinks = DiocesePaginationLinkBuilder.getInstance()
void buildPaginationLinks_Should_returnDefaultPaginationValues_When_noParametersAreSet() {
var paginationLinks = DiocesePaginationLinkBuilder.getInstance()
.buildPaginationLinks();

assertThat(paginationLinks, notNullValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
import static org.hamcrest.Matchers.notNullValue;

import de.caritas.cob.agencyservice.api.model.HalLink.MethodEnum;
import de.caritas.cob.agencyservice.api.model.RootDTO;
import de.caritas.cob.agencyservice.api.model.RootLinks;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

public class RootDTOBuilderTest {
@ExtendWith(MockitoExtension.class)
class RootDTOBuilderTest {

@Test
public void buildRootDTO_Should_returnRootDTOWithHalLinks() {
RootDTO rootDTO = new RootDTOBuilder().buildRootDTO();
void buildRootDTO_Should_returnRootDTOWithHalLinks() {
var rootDTO = new RootDTOBuilder().buildRootDTO();

assertThat(rootDTO, notNullValue());
RootLinks rootLinks = rootDTO.getLinks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
import static org.hamcrest.Matchers.nullValue;

import de.caritas.cob.agencyservice.api.model.HalLink.MethodEnum;
import de.caritas.cob.agencyservice.api.model.SearchResultLinks;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

public class SearchResultLinkBuilderTest {
@ExtendWith(MockitoExtension.class)
class SearchResultLinkBuilderTest {

@Test
public void buildSearchResultLinks_Should_returnSearchResultLinks_When_allParametersAreSet() {
SearchResultLinks searchResultLinks = SearchResultLinkBuilder.getInstance()
void buildSearchResultLinks_Should_returnSearchResultLinks_When_allParametersAreSet() {
var searchResultLinks = SearchResultLinkBuilder.getInstance()
.withKeyword("keyword")
.withPage(2)
.withPerPage(20)
Expand All @@ -41,8 +43,8 @@ public void buildSearchResultLinks_Should_returnSearchResultLinks_When_allParame
}

@Test
public void buildAgencyAdminSearchResult_Should_havePreviousLink_When_currentPageIsNotTheFirst() {
SearchResultLinks searchResultLinks = SearchResultLinkBuilder.getInstance()
void buildAgencyAdminSearchResult_Should_havePreviousLink_When_currentPageIsNotTheFirst() {
var searchResultLinks = SearchResultLinkBuilder.getInstance()
.withKeyword("keyword")
.withPage(2)
.withPerPage(20)
Expand All @@ -55,8 +57,8 @@ public void buildAgencyAdminSearchResult_Should_havePreviousLink_When_currentPag
}

@Test
public void buildAgencyAdminSearchResult_ShouldNot_havePreviousLink_When_currentPageIsTheFirst() {
SearchResultLinks searchResultLinks = SearchResultLinkBuilder.getInstance()
void buildAgencyAdminSearchResult_ShouldNot_havePreviousLink_When_currentPageIsTheFirst() {
var searchResultLinks = SearchResultLinkBuilder.getInstance()
.withKeyword("keyword")
.withPage(1)
.withPerPage(20)
Expand All @@ -67,8 +69,8 @@ public void buildAgencyAdminSearchResult_ShouldNot_havePreviousLink_When_current
}

@Test
public void buildAgencyAdminSearchResult_Should_haveNextLink_When_currentPageIsNotTheLast() {
SearchResultLinks searchResultLinks = SearchResultLinkBuilder.getInstance()
void buildAgencyAdminSearchResult_Should_haveNextLink_When_currentPageIsNotTheLast() {
var searchResultLinks = SearchResultLinkBuilder.getInstance()
.withKeyword("keyword")
.withPage(2)
.withPerPage(20)
Expand All @@ -81,8 +83,8 @@ public void buildAgencyAdminSearchResult_Should_haveNextLink_When_currentPageIsN
}

@Test
public void buildAgencyAdminSearchResult_ShouldNot_haveNextLink_When_currentPageIsTheLast() {
SearchResultLinks searchResultLinks = SearchResultLinkBuilder.getInstance()
void buildAgencyAdminSearchResult_ShouldNot_haveNextLink_When_currentPageIsTheLast() {
var searchResultLinks = SearchResultLinkBuilder.getInstance()
.withKeyword("keyword")
.withPage(3)
.withPerPage(20)
Expand All @@ -93,8 +95,8 @@ public void buildAgencyAdminSearchResult_ShouldNot_haveNextLink_When_currentPage
}

@Test
public void buildSearchResultLinks_Should_returnPlaceHolderSelfLink_When_keywordIsNotSet() {
SearchResultLinks searchResultLinks = SearchResultLinkBuilder.getInstance()
void buildSearchResultLinks_Should_returnPlaceHolderSelfLink_When_keywordIsNotSet() {
var searchResultLinks = SearchResultLinkBuilder.getInstance()
.withPage(1)
.withPerPage(20)
.buildSearchResultLinks();
Expand All @@ -105,8 +107,8 @@ public void buildSearchResultLinks_Should_returnPlaceHolderSelfLink_When_keyword
}

@Test
public void buildSearchResultLinks_Should_returnDefaultPaginationValues_When_noParametersAreSet() {
SearchResultLinks searchResultLinks = SearchResultLinkBuilder.getInstance()
void buildSearchResultLinks_Should_returnDefaultPaginationValues_When_noParametersAreSet() {
var searchResultLinks = SearchResultLinkBuilder.getInstance()
.buildSearchResultLinks();

assertThat(searchResultLinks, notNullValue());
Expand Down
Loading

0 comments on commit 819e677

Please sign in to comment.