diff --git a/sugoi-api-ldap-store-provider/src/main/java/fr/insee/sugoi/store/ldap/LdapReaderStore.java b/sugoi-api-ldap-store-provider/src/main/java/fr/insee/sugoi/store/ldap/LdapReaderStore.java index 09520620..3aadc017 100644 --- a/sugoi-api-ldap-store-provider/src/main/java/fr/insee/sugoi/store/ldap/LdapReaderStore.java +++ b/sugoi-api-ldap-store-provider/src/main/java/fr/insee/sugoi/store/ldap/LdapReaderStore.java @@ -369,11 +369,15 @@ public Optional getUserByMail(String mail) { logger.debug("Searching user with mail {}", mail); User searchedUser = new User(); searchedUser.setMail(mail); - PageResult users = - searchUsers(searchedUser, new PageableResult(2, 0, null), SearchType.OR.name()); + List users = + searchUsers(searchedUser, new PageableResult(2, 0, null), SearchType.OR.name()) + .getResults() + .stream() + .filter(u -> u.getMail().equalsIgnoreCase(mail)) + .collect(Collectors.toList()); User user = null; - if (users.getResults().size() == 1) { - user = users.getResults().get(0); + if (users.size() == 1) { + user = users.get(0); if (user.getAddress() != null && user.getAddress().getId() != null) { PostalAddress address = getAddress(user.getAddress().getId()); if (address != null) { @@ -384,7 +388,7 @@ public Optional getUserByMail(String mail) { if (user.getOrganization() != null) { user.setOrganization(getOrganization(user.getOrganization().getIdentifiant()).orElse(null)); } - } else if (users.getResults().size() > 1) { + } else if (users.size() > 1) { throw new MultipleUserWithSameMailException(mail); } return Optional.ofNullable(user); diff --git a/sugoi-api-ldap-store-provider/src/test/java/fr/insee/sugoi/ldap/LdapReaderStoreTest.java b/sugoi-api-ldap-store-provider/src/test/java/fr/insee/sugoi/ldap/LdapReaderStoreTest.java index 2fa8150f..601e34ea 100644 --- a/sugoi-api-ldap-store-provider/src/test/java/fr/insee/sugoi/ldap/LdapReaderStoreTest.java +++ b/sugoi-api-ldap-store-provider/src/test/java/fr/insee/sugoi/ldap/LdapReaderStoreTest.java @@ -15,9 +15,11 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertThrows; import fr.insee.sugoi.ldap.utils.config.LdapConfigKeys; import fr.insee.sugoi.model.*; +import fr.insee.sugoi.model.exceptions.MultipleUserWithSameMailException; import fr.insee.sugoi.model.fixtures.StoreMappingFixture; import fr.insee.sugoi.model.paging.PageResult; import fr.insee.sugoi.model.paging.PageableResult; @@ -371,4 +373,44 @@ public void userWithoutPasswordShouldHaveValueHasPassword() { ldapReaderStore.getUser("agarder").get().getAttributes().get("hasPassword"), is(false)); } + + @Test + @DisplayName( + "Given a user which mail is unique in the realm" + + "the user should be retrievable via its mail") + public void getUserByMailTest() { + assertThat( + "Should be the user mail1", + ldapReaderStore.getUserByMail("userwithuniqueemail@insee.fr").get().getUsername(), + is("mail1")); + } + + @Test + @DisplayName( + "Given a user which mail is unique in the realm " + + "even though his mail is a sub of another user mail " + + "the user should be retrievable via its mail") + public void getUserByMailWithSubMailTest() { + assertThat( + "Should be the user mailsub", + ldapReaderStore.getUserByMail("userwithsubemail@insee.fr").get().getUsername(), + is("mailsub")); + } + + @Test + @DisplayName("Given we search a user by a mail that does not exist, " + "no user should be given") + public void getNoneExistingUserByMailTest() { + assertThat( + "Should not get a user", + ldapReaderStore.getUserByMail("notexistingmail@insee.fr").isEmpty(), + is(true)); + } + + @Test + @DisplayName("Given several users have the same mail, " + "an exception should be raised") + public void getConflictingMailUserTest() { + assertThrows( + MultipleUserWithSameMailException.class, + () -> ldapReaderStore.getUserByMail("conflictingmail@insee.fr")); + } } diff --git a/sugoi-api-ldap-store-provider/src/test/resources/ldap.ldif b/sugoi-api-ldap-store-provider/src/test/resources/ldap.ldif index 11d46ee0..f3b6b2ae 100644 --- a/sugoi-api-ldap-store-provider/src/test/resources/ldap.ldif +++ b/sugoi-api-ldap-store-provider/src/test/resources/ldap.ldif @@ -397,6 +397,61 @@ pwdReset: false uid: havepwdreset cn: havepwdreset +dn: uid=mail1,ou=contacts,ou=clients_domaine1,o=insee,c=fr +objectClass: top +objectClass: inseeCompte +objectClass: inseeContact +objectClass: inseeAttributsAuthentification +objectClass: inseeAttributsHabilitation +objectClass: inseeAttributsCommunication +uid: mail1 +cn: mail1 +mail: userwithuniqueemail@insee.fr + +dn: uid=mailsub,ou=contacts,ou=clients_domaine1,o=insee,c=fr +objectClass: top +objectClass: inseeCompte +objectClass: inseeContact +objectClass: inseeAttributsAuthentification +objectClass: inseeAttributsHabilitation +objectClass: inseeAttributsCommunication +uid: mailsub +cn: mailsub +mail: userwithsubemail@insee.fr + +dn: uid=mailext,ou=contacts,ou=clients_domaine1,o=insee,c=fr +objectClass: top +objectClass: inseeCompte +objectClass: inseeContact +objectClass: inseeAttributsAuthentification +objectClass: inseeAttributsHabilitation +objectClass: inseeAttributsCommunication +uid: mailext +cn: mailext +mail: extuserwithsubemail@insee.fr + +dn: uid=mailconflict1,ou=contacts,ou=clients_domaine1,o=insee,c=fr +objectClass: top +objectClass: inseeCompte +objectClass: inseeContact +objectClass: inseeAttributsAuthentification +objectClass: inseeAttributsHabilitation +objectClass: inseeAttributsCommunication +uid: mailconflict1 +cn: mailconflict1 +mail: conflictingmail@insee.fr + +dn: uid=mailconflict2,ou=contacts,ou=clients_domaine1,o=insee,c=fr +objectClass: top +objectClass: inseeCompte +objectClass: inseeContact +objectClass: inseeAttributsAuthentification +objectClass: inseeAttributsHabilitation +objectClass: inseeAttributsCommunication +uid: mailconflict2 +cn: mailconflict2 +mail: conflictingmail@insee.fr + dn: uid=testo,ou=organisations,ou=clients_domaine1,o=insee,c=fr objectClass: top objectClass: inseeOrganisation