Skip to content

Commit

Permalink
[BUG] 🐛 Fix conflict when getting by mail a user with a similar mail
Browse files Browse the repository at this point in the history
Signed-off-by: Cécile Chemin <[email protected]>
  • Loading branch information
CChemin committed Nov 21, 2023
1 parent 99ef8d4 commit dbe91e6
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,15 @@ public Optional<User> getUserByMail(String mail) {
logger.debug("Searching user with mail {}", mail);
User searchedUser = new User();
searchedUser.setMail(mail);
PageResult<User> users =
searchUsers(searchedUser, new PageableResult(2, 0, null), SearchType.OR.name());
List<User> 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) {
Expand All @@ -384,7 +388,7 @@ public Optional<User> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("[email protected]").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("[email protected]").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("[email protected]").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("[email protected]"));
}
}
55 changes: 55 additions & 0 deletions sugoi-api-ldap-store-provider/src/test/resources/ldap.ldif
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]

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: [email protected]

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: [email protected]

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: [email protected]

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: [email protected]

dn: uid=testo,ou=organisations,ou=clients_domaine1,o=insee,c=fr
objectClass: top
objectClass: inseeOrganisation
Expand Down

0 comments on commit dbe91e6

Please sign in to comment.