diff --git a/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/UserController.java b/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/UserController.java index 947fe7b5b..70d7547eb 100644 --- a/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/UserController.java +++ b/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/UserController.java @@ -94,6 +94,7 @@ import de.caritas.cob.userservice.api.service.SessionDataService; import de.caritas.cob.userservice.api.service.archive.SessionArchiveService; import de.caritas.cob.userservice.api.service.archive.SessionDeleteService; +import de.caritas.cob.userservice.api.service.helper.EmailUrlDecoder; import de.caritas.cob.userservice.api.service.session.SessionFilter; import de.caritas.cob.userservice.api.service.session.SessionService; import de.caritas.cob.userservice.api.service.user.UserAccountService; @@ -115,6 +116,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import lombok.val; +import org.apache.commons.validator.routines.EmailValidator; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; @@ -766,7 +768,7 @@ public ResponseEntity> getConsultants(@RequestParam @Override public ResponseEntity searchConsultants( String query, Integer page, Integer perPage, String field, String order) { - var decodedInfix = URLDecoder.decode(query, StandardCharsets.UTF_8).trim(); + var decodedInfix = determineDecodedInfix(query).trim(); var isAscending = order.equalsIgnoreCase("asc"); var mappedField = consultantDtoMapper.mappedFieldOf(field); var resultMap = @@ -793,6 +795,14 @@ public ResponseEntity searchConsultants( return ResponseEntity.ok(result); } + private String determineDecodedInfix(String query) { + if (EmailValidator.getInstance().isValid(query)) { + return EmailUrlDecoder.decodeEmailQuery(query); + } else { + return URLDecoder.decode(query, StandardCharsets.UTF_8).trim(); + } + } + private void removeAgenciesWithoutAccessRight( ConsultantAdminResponseDTO response, Collection agenciesToFilterConsultants) { List agencies = response.getEmbedded().getAgencies(); diff --git a/src/main/java/de/caritas/cob/userservice/api/service/helper/EmailUrlDecoder.java b/src/main/java/de/caritas/cob/userservice/api/service/helper/EmailUrlDecoder.java index a92be6ebc..043595b20 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/helper/EmailUrlDecoder.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/helper/EmailUrlDecoder.java @@ -17,6 +17,6 @@ public static String decodeEmailQuery(String query) { strings.stream() .map(part -> URLDecoder.decode(part, StandardCharsets.UTF_8).trim()) .collect(Collectors.toList()); - return Joiner.on(PLUS).join(decodedList); + return Joiner.on(PLUS).join(decodedList).trim(); } } diff --git a/src/test/java/de/caritas/cob/userservice/api/service/helper/EmailUrlDecoderTest.java b/src/test/java/de/caritas/cob/userservice/api/service/helper/EmailUrlDecoderTest.java index 6381f0446..7f98356eb 100644 --- a/src/test/java/de/caritas/cob/userservice/api/service/helper/EmailUrlDecoderTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/service/helper/EmailUrlDecoderTest.java @@ -2,29 +2,21 @@ import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; class EmailUrlDecoderTest { - @Test - public void decodeEmailQuery_Should_notChangeProperEmail() { - assertThat(EmailUrlDecoder.decodeEmailQuery("testmail@gmail.com")) - .isEqualTo("testmail@gmail.com"); - assertThat(EmailUrlDecoder.decodeEmailQuery("testmail%40virtual-identity.com")) - .isEqualTo("testmail@virtual-identity.com"); - assertThat(EmailUrlDecoder.decodeEmailQuery("test.mail%40virtual-identity.com")) - .isEqualTo("test.mail@virtual-identity.com"); - } - - @Test - public void decodeEmailQuery_Should_decodeProperlyMailsWithPlus() { - assertThat(EmailUrlDecoder.decodeEmailQuery("testmail%2Bext%40gmail.com")) - .isEqualTo("testmail+ext@gmail.com"); - assertThat(EmailUrlDecoder.decodeEmailQuery("testmail%2Bext%40virtual-identity.com")) - .isEqualTo("testmail+ext@virtual-identity.com"); - assertThat( - EmailUrlDecoder.decodeEmailQuery( - "test.user.ext%2Btenant14.02.v2%40virtual-identity.com")) - .isEqualTo("test.user.ext+tenant14.02.v2@virtual-identity.com"); + @ParameterizedTest + @CsvSource({ + "testmail%2Bext%40gmail.com,testmail+ext@gmail.com", + "testmail%2Bext%40virtual-identity.com,testmail+ext@virtual-identity.com", + "test.user.ext%2Btenant14.02.v2%40virtual-identity.com,test.user.ext+tenant14.02.v2@virtual-identity.com", + "testmail@gmail.com,testmail@gmail.com", + "testmail%40virtual-identity.com,testmail@virtual-identity.com", + "test.mail%40virtual-identity.com,test.mail@virtual-identity.com", + }) + void decodeEmailQuery_Should_decodeProperlyMailsWithPlus(String input, String expectedOutput) { + assertThat(EmailUrlDecoder.decodeEmailQuery(input)).isEqualTo(expectedOutput); } }