You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2023/07/28 03:26:11 UTC

[james-project] 04/07: JAMES-3930 Test suite

This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit ed1da2eb719fd524d551166a9669cef409850196
Author: Quan Tran <hq...@linagora.com>
AuthorDate: Fri Jul 21 17:55:51 2023 +0700

    JAMES-3930 Test suite
---
 server/apps/memory-app/pom.xml                     |  12 +
 .../MemoryLdapLocalPartLoginIntegrationTest.java   | 170 ++++++++++++
 .../java/org/apache/james/data/DockerLdapRule.java |  60 ++++-
 .../james/user/ldap/DockerLdapSingleton.java       |   5 +
 ...LDAPRepositoryWithLocalPartAsLoginNameTest.java | 288 +++++++++++++++++++++
 .../localpartLogin/ldif-files/populate.ldif        |  65 +++++
 6 files changed, 586 insertions(+), 14 deletions(-)

diff --git a/server/apps/memory-app/pom.xml b/server/apps/memory-app/pom.xml
index 1a0e896325..7d112b7977 100644
--- a/server/apps/memory-app/pom.xml
+++ b/server/apps/memory-app/pom.xml
@@ -70,6 +70,12 @@
             <artifactId>james-server-cli</artifactId>
             <scope>runtime</scope>
         </dependency>
+        <dependency>
+            <groupId>${james.groupId}</groupId>
+            <artifactId>james-server-data-ldap</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>${james.groupId}</groupId>
             <artifactId>james-server-guice-common</artifactId>
@@ -80,6 +86,12 @@
             <groupId>${james.groupId}</groupId>
             <artifactId>james-server-guice-data-ldap</artifactId>
         </dependency>
+        <dependency>
+            <groupId>${james.groupId}</groupId>
+            <artifactId>james-server-guice-data-ldap</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>${james.groupId}</groupId>
             <artifactId>james-server-guice-imap</artifactId>
diff --git a/server/apps/memory-app/src/test/java/org/apache/james/MemoryLdapLocalPartLoginIntegrationTest.java b/server/apps/memory-app/src/test/java/org/apache/james/MemoryLdapLocalPartLoginIntegrationTest.java
new file mode 100644
index 0000000000..d5388018db
--- /dev/null
+++ b/server/apps/memory-app/src/test/java/org/apache/james/MemoryLdapLocalPartLoginIntegrationTest.java
@@ -0,0 +1,170 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.apache.james.MailsShouldBeWellReceived.JAMES_SERVER_HOST;
+import static org.apache.james.data.UsersRepositoryModuleChooser.Implementation.LDAP;
+import static org.apache.james.jmap.JMAPTestingConstants.LOCALHOST_IP;
+import static org.apache.james.user.ldap.DockerLdapSingleton.JAMES_USER;
+import static org.apache.james.user.ldap.DockerLdapSingleton.PASSWORD;
+import static org.apache.james.user.ldap.ReadOnlyUsersLDAPRepositoryWithLocalPartAsLoginNameTest.JAMES_USER_APP1;
+import static org.apache.james.user.ldap.ReadOnlyUsersLDAPRepositoryWithLocalPartAsLoginNameTest.JAMES_USER_WITH_DOMAIN_PART;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.util.Base64;
+
+import org.apache.commons.net.imap.IMAPClient;
+import org.apache.commons.net.smtp.SMTPClient;
+import org.apache.james.data.DockerLdapRule;
+import org.apache.james.data.LdapTestExtension;
+import org.apache.james.modules.TestJMAPServerModule;
+import org.apache.james.modules.protocols.ImapGuiceProbe;
+import org.apache.james.modules.protocols.SmtpGuiceProbe;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+class MemoryLdapLocalPartLoginIntegrationTest {
+    @RegisterExtension
+    static JamesServerExtension jamesServerExtension = new JamesServerBuilder<MemoryJamesConfiguration>(tmpDir ->
+        MemoryJamesConfiguration.builder()
+            .workingDirectory(tmpDir)
+            .configurationFromClasspath()
+            .usersRepository(LDAP)
+            .build())
+        .server(configuration -> MemoryJamesServerMain.createServer(configuration)
+            .overrideWith(new TestJMAPServerModule()))
+        .extension(new LdapTestExtension(new DockerLdapRule(true)))
+        .build();
+
+    @Nested
+    class IMAP {
+        @Test
+        void imapFQDNLoginShouldSucceed(GuiceJamesServer server) throws IOException {
+            IMAPClient imapClient = new IMAPClient();
+            imapClient.connect(JAMES_SERVER_HOST, server.getProbe(ImapGuiceProbe.class).getImapPort());
+
+            assertThat(imapClient.login(JAMES_USER_WITH_DOMAIN_PART.asString(), PASSWORD)).isTrue();
+        }
+
+        @Test
+        void imapLoginUsingValidLocalPartCredentialShouldSucceed(GuiceJamesServer server) throws IOException {
+            IMAPClient imapClient = new IMAPClient();
+            imapClient.connect(JAMES_SERVER_HOST, server.getProbe(ImapGuiceProbe.class).getImapPort());
+
+            assertThat(imapClient.login(JAMES_USER.getLocalPart(), PASSWORD)).isTrue();
+        }
+
+        @Test
+        void imapLoginUsingInvalidLocalPartCredentialShouldFail(GuiceJamesServer server) throws IOException {
+            IMAPClient imapClient = new IMAPClient();
+            imapClient.connect(JAMES_SERVER_HOST, server.getProbe(ImapGuiceProbe.class).getImapPort());
+
+            assertThat(imapClient.login(JAMES_USER.getLocalPart(), "wrongPassword")).isFalse();
+        }
+
+        @Test
+        void imapLoginUsingValidAppCredentialShouldSucceed(GuiceJamesServer server) throws IOException {
+            IMAPClient imapClient = new IMAPClient();
+            imapClient.connect(JAMES_SERVER_HOST, server.getProbe(ImapGuiceProbe.class).getImapPort());
+
+            assertThat(imapClient.login(JAMES_USER_APP1.asString(), PASSWORD)).isTrue();
+        }
+
+        @Test
+        void imapLoginUsingInvalidAppCredentialShouldSucceed(GuiceJamesServer server) throws IOException {
+            IMAPClient imapClient = new IMAPClient();
+            imapClient.connect(JAMES_SERVER_HOST, server.getProbe(ImapGuiceProbe.class).getImapPort());
+
+            assertThat(imapClient.login(JAMES_USER_APP1.asString(), "wrongPassword")).isFalse();
+        }
+    }
+
+    @Nested
+    class SMTP {
+        @Test
+        void smtpFQDNAuthPlainShouldSucceed(GuiceJamesServer server) throws Exception {
+            SMTPClient smtpClient = new SMTPClient();
+            smtpClient.connect(LOCALHOST_IP, server.getProbe(SmtpGuiceProbe.class).getSmtpAuthRequiredPort().getValue());
+
+            authenticate(smtpClient, JAMES_USER_WITH_DOMAIN_PART.asString(), PASSWORD);
+
+            assertThat(smtpClient.getReplyCode())
+                .as("authenticated")
+                .isEqualTo(235);
+        }
+
+        @Test
+        void smtpAuthPlainUsingValidLocalPartCredentialShouldSucceed(GuiceJamesServer server) throws Exception {
+            SMTPClient smtpClient = new SMTPClient();
+            smtpClient.connect(LOCALHOST_IP, server.getProbe(SmtpGuiceProbe.class).getSmtpAuthRequiredPort().getValue());
+
+            authenticate(smtpClient, JAMES_USER.getLocalPart(), PASSWORD);
+
+            assertThat(smtpClient.getReplyCode())
+                .as("authenticated")
+                .isEqualTo(235);
+        }
+
+        @Test
+        void smtpAuthPlainUsingInvalidLocalPartCredentialShouldFail(GuiceJamesServer server) throws Exception {
+            SMTPClient smtpClient = new SMTPClient();
+            smtpClient.connect(LOCALHOST_IP, server.getProbe(SmtpGuiceProbe.class).getSmtpAuthRequiredPort().getValue());
+
+            authenticate(smtpClient, JAMES_USER.getLocalPart(), "wrongPassword");
+
+            assertThat(smtpClient.getReplyCode())
+                .as("authentication rejected")
+                .isEqualTo(535);
+        }
+
+        @Test
+        void smtpAuthPlainUsingValidAppCredentialShouldSucceed(GuiceJamesServer server) throws Exception {
+            SMTPClient smtpClient = new SMTPClient();
+            smtpClient.connect(LOCALHOST_IP, server.getProbe(SmtpGuiceProbe.class).getSmtpAuthRequiredPort().getValue());
+
+            authenticate(smtpClient, JAMES_USER_APP1.asString(), PASSWORD);
+
+            assertThat(smtpClient.getReplyCode())
+                .as("authenticated")
+                .isEqualTo(235);
+        }
+
+        @Test
+        void smtpAuthPlainUsingInvalidAppCredentialShouldFail(GuiceJamesServer server) throws Exception {
+            SMTPClient smtpClient = new SMTPClient();
+            smtpClient.connect(LOCALHOST_IP, server.getProbe(SmtpGuiceProbe.class).getSmtpAuthRequiredPort().getValue());
+
+            authenticate(smtpClient, JAMES_USER_APP1.asString(), "wrongPassword");
+
+            assertThat(smtpClient.getReplyCode())
+                .as("authentication rejected")
+                .isEqualTo(535);
+        }
+
+        private void authenticate(SMTPClient smtpClient, String loginName, String password) throws IOException {
+            smtpClient.sendCommand("AUTH PLAIN");
+            smtpClient.sendCommand(Base64.getEncoder().encodeToString(("\0" + loginName + "\0" + password + "\0").getBytes(UTF_8)));
+        }
+    }
+
+}
diff --git a/server/container/guice/data-ldap/src/test/java/org/apache/james/data/DockerLdapRule.java b/server/container/guice/data-ldap/src/test/java/org/apache/james/data/DockerLdapRule.java
index d45f2437e5..c6dc2d68dc 100644
--- a/server/container/guice/data-ldap/src/test/java/org/apache/james/data/DockerLdapRule.java
+++ b/server/container/guice/data-ldap/src/test/java/org/apache/james/data/DockerLdapRule.java
@@ -26,6 +26,7 @@ import java.util.stream.Collectors;
 import org.apache.commons.configuration2.ex.ConfigurationException;
 import org.apache.james.GuiceModuleTestRule;
 import org.apache.james.user.ldap.DockerLdapSingleton;
+import org.apache.james.user.ldap.LdapGenericContainer;
 import org.apache.james.user.ldap.LdapRepositoryConfiguration;
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
@@ -34,11 +35,28 @@ import com.github.fge.lambdas.Throwing;
 import com.google.inject.Module;
 
 public class DockerLdapRule implements GuiceModuleTestRule {
+    private final boolean localPartLoginSupported;
+    private final LdapGenericContainer ldapGenericContainer;
+
+    public DockerLdapRule() {
+        this.localPartLoginSupported = false;
+        this.ldapGenericContainer = DockerLdapSingleton.ldapContainer;
+    }
+
+    public DockerLdapRule(boolean localPartLoginSupported) {
+        this.localPartLoginSupported = localPartLoginSupported;
+
+        if (localPartLoginSupported) {
+            this.ldapGenericContainer = DockerLdapSingleton.ldapContainerWithLocalPartLogin;
+        } else {
+            this.ldapGenericContainer = DockerLdapSingleton.ldapContainer;
+        }
+    }
 
     @Override
     public Module getModule() {
         return binder -> binder.bind(LdapRepositoryConfiguration.class)
-            .toInstance(computeConfiguration(List.of(DockerLdapSingleton.ldapContainer.getLdapHost())));
+            .toInstance(computeConfiguration(List.of(ldapGenericContainer.getLdapHost())));
     }
 
     @Override
@@ -50,25 +68,39 @@ public class DockerLdapRule implements GuiceModuleTestRule {
         List<URI> uris = ldapIps.stream()
             .map(Throwing.function(URI::new))
             .collect(Collectors.toUnmodifiableList());
-        try {
-            return LdapRepositoryConfiguration.builder()
-                .ldapHosts(uris)
-                .principal("cn=admin,dc=james,dc=org")
-                .credentials("mysecretpassword")
-                .userBase("ou=People,dc=james,dc=org")
-                .userIdAttribute("uid")
-                .userObjectClass("inetOrgPerson")
-                .build();
-        } catch (ConfigurationException e) {
-            throw new RuntimeException(e);
+
+        LdapRepositoryConfiguration.Builder ldapConfigurationBuilder = LdapRepositoryConfiguration.builder()
+            .ldapHosts(uris)
+            .principal("cn=admin,dc=james,dc=org")
+            .credentials("mysecretpassword")
+            .userBase("ou=People,dc=james,dc=org")
+            .userObjectClass("inetOrgPerson");
+
+        if (localPartLoginSupported) {
+            try {
+                return ldapConfigurationBuilder
+                    .resolveLocalPartAttribute("uid")
+                    .userIdAttribute("mail")
+                    .build();
+            } catch (ConfigurationException e) {
+                throw new RuntimeException(e);
+            }
+        } else {
+            try {
+                return ldapConfigurationBuilder
+                    .userIdAttribute("uid")
+                    .build();
+            } catch (ConfigurationException e) {
+                throw new RuntimeException(e);
+            }
         }
     }
 
     public void start() {
-        DockerLdapSingleton.ldapContainer.start();
+        ldapGenericContainer.start();
     }
 
     public void stop() {
-        DockerLdapSingleton.ldapContainer.stop();
+        ldapGenericContainer.stop();
     }
 }
diff --git a/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/DockerLdapSingleton.java b/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/DockerLdapSingleton.java
index 262101484d..d43459a693 100644
--- a/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/DockerLdapSingleton.java
+++ b/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/DockerLdapSingleton.java
@@ -34,4 +34,9 @@ public class DockerLdapSingleton {
         .password(ADMIN_PASSWORD)
         .build();
 
+    public static final LdapGenericContainer ldapContainerWithLocalPartLogin = LdapGenericContainer.builder()
+        .domain(DOMAIN)
+        .password(ADMIN_PASSWORD)
+        .dockerFilePrefix("localpartLogin/")
+        .build();
 }
diff --git a/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryWithLocalPartAsLoginNameTest.java b/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryWithLocalPartAsLoginNameTest.java
new file mode 100644
index 0000000000..5c54751da2
--- /dev/null
+++ b/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryWithLocalPartAsLoginNameTest.java
@@ -0,0 +1,288 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.user.ldap;
+
+import static org.apache.james.user.ldap.DockerLdapSingleton.ADMIN;
+import static org.apache.james.user.ldap.DockerLdapSingleton.ADMIN_PASSWORD;
+import static org.apache.james.user.ldap.DockerLdapSingleton.DOMAIN;
+import static org.apache.james.user.ldap.DockerLdapSingleton.JAMES_USER;
+import static org.apache.james.user.ldap.DockerLdapSingleton.PASSWORD;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Optional;
+
+import org.apache.commons.configuration2.HierarchicalConfiguration;
+import org.apache.commons.configuration2.plist.PropertyListConfiguration;
+import org.apache.commons.configuration2.tree.ImmutableNode;
+import org.apache.james.core.Username;
+import org.apache.james.domainlist.api.DomainList;
+import org.apache.james.user.api.InvalidUsernameException;
+import org.apache.james.user.lib.UsersRepositoryContract;
+import org.assertj.core.api.SoftAssertions;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import com.github.fge.lambdas.Throwing;
+
+public class ReadOnlyUsersLDAPRepositoryWithLocalPartAsLoginNameTest {
+    public static final Username JAMES_USER_WITH_DOMAIN_PART = Username.of(JAMES_USER.getLocalPart() + "@" + DOMAIN);
+    public static final Username ALICE_WITH_DOMAIN_PART = Username.of("alice" + "@" + DOMAIN);
+    public static final Username JAMES_USER_APP1 = Username.of("james-user-app1");
+    public static final Username JAMES_USER_APP2 = Username.of("james-user-app2");
+    public static final Username JAMES_USER_INVALID_APP = Username.of("james-user-invalidApp");
+    public static final Username ALICE_APP1 = Username.of("alice-app1");
+
+    private static LdapGenericContainer ldapContainer = LdapGenericContainer.builder()
+        .domain(DOMAIN)
+        .password(ADMIN_PASSWORD)
+        .dockerFilePrefix("localpartLogin/")
+        .build();
+
+    @BeforeAll
+    static void setUpAll() {
+        ldapContainer.start();
+    }
+
+    @AfterAll
+    static void afterAll() {
+        ldapContainer.stop();
+    }
+
+    @Nested
+    class WhenEnableVirtualHosting {
+        @RegisterExtension
+        UsersRepositoryContract.UserRepositoryExtension extension = UsersRepositoryContract.UserRepositoryExtension.withVirtualHost();
+
+        private ReadOnlyUsersLDAPRepository usersRepository;
+
+        @BeforeEach
+        void setUp(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+            usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.empty()), testSystem.getDomainList());
+        }
+
+        @Nested
+        class LocalPartLogin {
+            @Test
+            void localPartLoginShouldFailByDefault() throws Exception {
+                assertThat(usersRepository.test(Username.of(JAMES_USER.getLocalPart()), PASSWORD))
+                    .isEqualTo(Optional.empty());
+            }
+
+            @Test
+            void localPartLoginShouldSucceedWhenValidResolveLocalPartAttributeAndValidCredential(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("uid")), testSystem.getDomainList());
+
+                assertThat(usersRepository.test(Username.of(JAMES_USER.getLocalPart()), PASSWORD))
+                    .isEqualTo(Optional.of(JAMES_USER_WITH_DOMAIN_PART));
+            }
+
+            @Test
+            void localPartLoginShouldFailWhenValidResolveLocalPartAttributeButInvalidCredential(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("uid")), testSystem.getDomainList());
+
+                assertThat(usersRepository.test(Username.of(JAMES_USER.getLocalPart()), "wrongPassword"))
+                    .isEqualTo(Optional.empty());
+            }
+
+            @Test
+            void localPartLoginShouldFailWhenInvalidResolveLocalPartAttribute(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("invalidAttribute")), testSystem.getDomainList());
+
+                assertThat(usersRepository.test(Username.of(JAMES_USER.getLocalPart()), PASSWORD))
+                    .isEqualTo(Optional.empty());
+            }
+
+            @Test
+            void getUserByLocalPartShouldReturnTranslatedUsernameWhenValidResolveLocalPartAttribute(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("uid")), testSystem.getDomainList());
+
+                assertThat(usersRepository.getUserByName(Username.of(JAMES_USER.getLocalPart())).getUserName())
+                    .isEqualTo(JAMES_USER_WITH_DOMAIN_PART);
+            }
+
+            @Test
+            void getUserByLocalPartShouldReturnNullWhenInvalidResolveLocalPartAttribute(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("invalidAttribute")), testSystem.getDomainList());
+
+                assertThat(usersRepository.getUserByName(Username.of(JAMES_USER.getLocalPart()))).isNull();
+            }
+
+            @Test
+            void assertValidUsernameShouldBeRelaxWhenResolveLocalPartAttributeEnabled(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("uid")), testSystem.getDomainList());
+
+                assertThatCode(() -> usersRepository.assertValid(Username.of(JAMES_USER.getLocalPart())))
+                    .doesNotThrowAnyException();
+            }
+
+            @Test
+            void assertValidUsernameShouldBeStrictWhenResolveLocalPartAttributeDisabled() {
+                assertThatThrownBy(() -> usersRepository.assertValid(Username.of(JAMES_USER.getLocalPart())))
+                    .isInstanceOf(InvalidUsernameException.class);
+            }
+        }
+
+        @Nested
+        class OneAppPasswordLogin {
+            @Test
+            void oneAppLoginShouldFailByDefault() throws Exception {
+                assertThat(usersRepository.test(Username.of(JAMES_USER.getLocalPart()), PASSWORD))
+                    .isEqualTo(Optional.empty());
+            }
+
+            @Test
+            void oneAppLoginShouldSucceedWhenValidResolveLocalPartAttributeAndValidCredentials(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("uid")), testSystem.getDomainList());
+
+                SoftAssertions.assertSoftly(Throwing.consumer(softly -> {
+                    softly.assertThat(usersRepository.test(JAMES_USER_APP1, PASSWORD))
+                        .isEqualTo(Optional.of(JAMES_USER_WITH_DOMAIN_PART));
+                    softly.assertThat(usersRepository.test(JAMES_USER_APP2, PASSWORD))
+                        .isEqualTo(Optional.of(JAMES_USER_WITH_DOMAIN_PART));
+                    softly.assertThat(usersRepository.test(ALICE_APP1, PASSWORD))
+                        .isEqualTo(Optional.of(ALICE_WITH_DOMAIN_PART));
+                }));
+            }
+
+            @Test
+            void oneAppLoginShouldFailWhenValidResolveLocalPartAttributeButInvalidCredentials(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("uid")), testSystem.getDomainList());
+
+                SoftAssertions.assertSoftly(Throwing.consumer(softly -> {
+                    assertThat(usersRepository.test(JAMES_USER_APP1, "wrongPassword"))
+                        .isEqualTo(Optional.empty());
+                    assertThat(usersRepository.test(JAMES_USER_APP2, "wrongPassword"))
+                        .isEqualTo(Optional.empty());
+                    assertThat(usersRepository.test(JAMES_USER_INVALID_APP, PASSWORD))
+                        .isEqualTo(Optional.empty());
+                }));
+            }
+
+            @Test
+            void oneAppLoginShouldFailWhenInvalidResolveLocalPartAttribute(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("invalidAttribute")), testSystem.getDomainList());
+
+                assertThat(usersRepository.test(JAMES_USER_APP1, PASSWORD))
+                    .isEqualTo(Optional.empty());
+            }
+
+            @Test
+            void getUserByAppIdShouldReturnTranslatedUsernameWhenValidResolveLocalPartAttribute(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("uid")), testSystem.getDomainList());
+
+                assertThat(usersRepository.getUserByName(JAMES_USER_APP1).getUserName())
+                    .isEqualTo(JAMES_USER_WITH_DOMAIN_PART);
+            }
+
+            @Test
+            void getUserByAppIdShouldReturnNullWhenInvalidResolveLocalPartAttribute(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("invalidAttribute")), testSystem.getDomainList());
+
+                assertThat(usersRepository.getUserByName(JAMES_USER_APP1)).isNull();
+            }
+
+            @Test
+            void listUsersShouldNotReturnLdapAppEntriesAsUsersWhenValidResolveLocalPartAttribute(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("uid")), testSystem.getDomainList());
+
+                assertThat(usersRepository.list())
+                    .toIterable()
+                    .containsExactlyInAnyOrder(JAMES_USER_WITH_DOMAIN_PART, ALICE_WITH_DOMAIN_PART);
+            }
+
+            @Test
+            void listUsersShouldNotReturnLdapAppEntriesAsUsersEvenWhenMissConfiguredResolveLocalPartAttribute(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("invalidAttribute")), testSystem.getDomainList());
+
+                assertThat(usersRepository.list())
+                    .toIterable()
+                    .containsExactlyInAnyOrder(JAMES_USER_WITH_DOMAIN_PART, ALICE_WITH_DOMAIN_PART);
+            }
+
+            @Test
+            void listUsersShouldNotCountLdapAppEntriesAsUsersWhenValidResolveLocalPartAttribute(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("uid")), testSystem.getDomainList());
+
+                assertThat(usersRepository.countUsers()).isEqualTo(2);
+            }
+
+            @Test
+            void listUsersShouldNotCountLdapAppEntriesAsUsersEvenWhenMissConfiguredResolveLocalPartAttribute(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("invalidAttribute")), testSystem.getDomainList());
+
+                assertThat(usersRepository.countUsers()).isEqualTo(2);
+            }
+
+            @Test
+            void assertValidUsernameShouldBeRelaxWhenResolveLocalPartAttributeEnabled(UsersRepositoryContract.TestSystem testSystem) throws Exception {
+                usersRepository = startUsersRepository(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of("uid")), testSystem.getDomainList());
+
+                assertThatCode(() -> usersRepository.assertValid(JAMES_USER_APP1))
+                    .doesNotThrowAnyException();
+            }
+
+            @Test
+            void assertValidUsernameShouldBeStrictWhenResolveLocalPartAttributeDisabled() {
+                assertThatThrownBy(() -> usersRepository.assertValid(JAMES_USER_APP1))
+                    .isInstanceOf(InvalidUsernameException.class);
+            }
+        }
+
+    }
+
+    private static ReadOnlyUsersLDAPRepository startUsersRepository(HierarchicalConfiguration<ImmutableNode> ldapRepositoryConfiguration,
+                                                                    DomainList domainList) throws Exception {
+        ReadOnlyUsersLDAPRepository ldapRepository = new ReadOnlyUsersLDAPRepository(domainList);
+        ldapRepository.configure(ldapRepositoryConfiguration);
+        ldapRepository.init();
+        return ldapRepository;
+    }
+
+    static HierarchicalConfiguration<ImmutableNode> ldapRepositoryConfigurationWithVirtualHosting(LdapGenericContainer ldapContainer, Optional<String> resolveLocalPartAttribute) {
+        return ldapRepositoryConfigurationWithVirtualHosting(ldapContainer, Optional.of(ADMIN), resolveLocalPartAttribute);
+    }
+
+    static HierarchicalConfiguration<ImmutableNode> ldapRepositoryConfigurationWithVirtualHosting(LdapGenericContainer ldapContainer, Optional<Username> administrator, Optional<String> resolveLocalPartAttribute) {
+        PropertyListConfiguration configuration = baseConfiguration(ldapContainer);
+        configuration.addProperty("[@userIdAttribute]", "mail");
+        resolveLocalPartAttribute.ifPresent(attribute -> configuration.addProperty("[@resolveLocalPartAttribute]", attribute));
+        configuration.addProperty("supportsVirtualHosting", true);
+        administrator.ifPresent(username -> configuration.addProperty("[@administratorId]", username.asString()));
+        return configuration;
+    }
+
+    private static PropertyListConfiguration baseConfiguration(LdapGenericContainer ldapContainer) {
+        PropertyListConfiguration configuration = new PropertyListConfiguration();
+        configuration.addProperty("[@ldapHost]", ldapContainer.getLdapHost());
+        configuration.addProperty("[@principal]", "cn=admin,dc=james,dc=org");
+        configuration.addProperty("[@credentials]", ADMIN_PASSWORD);
+        configuration.addProperty("[@userBase]", "ou=people,dc=james,dc=org");
+        configuration.addProperty("[@userObjectClass]", "inetOrgPerson");
+        configuration.addProperty("[@connectionTimeout]", "2000");
+        configuration.addProperty("[@readTimeout]", "2000");
+        return configuration;
+    }
+}
diff --git a/server/data/data-ldap/src/test/resources/localpartLogin/ldif-files/populate.ldif b/server/data/data-ldap/src/test/resources/localpartLogin/ldif-files/populate.ldif
new file mode 100644
index 0000000000..3dd7857487
--- /dev/null
+++ b/server/data/data-ldap/src/test/resources/localpartLogin/ldif-files/populate.ldif
@@ -0,0 +1,65 @@
+dn: ou=people, dc=james,dc=org
+ou: people
+objectClass: organizationalUnit
+
+dn: ou=empty, dc=james,dc=org
+ou: empty
+objectClass: organizationalUnit
+
+dn: ou=whatever, dc=james,dc=org
+ou: whatever
+objectClass: organizationalUnit
+
+dn: uid=james-user, ou=people, dc=james,dc=org
+objectClass: inetOrgPerson
+uid: james-user
+cn: james-user
+sn: james-user
+mail: james-user@james.org
+userPassword: secret
+description: James user
+
+dn: uid=james-user-app1, ou=people, dc=james,dc=org
+objectClass: inetOrgPerson
+uid: james-user-app1
+cn: james-user
+sn: james-user
+mail: james-user@james.org
+userPassword: secret
+description: James user App 1
+
+dn: uid=james-user-app2, ou=people, dc=james,dc=org
+objectClass: inetOrgPerson
+uid: james-user-app2
+cn: james-user
+sn: james-user
+mail: james-user@james.org
+userPassword: secret
+description: James user App 2
+
+dn: uid=alice, ou=people, dc=james,dc=org
+objectClass: inetOrgPerson
+uid: alice
+cn: alice-cn
+sn: alice-sn
+mail: alice@james.org
+userPassword: secret
+description: Alice user
+
+dn: uid=alice-app1, ou=people, dc=james,dc=org
+objectClass: inetOrgPerson
+uid: alice-app1
+cn: alice-cn
+sn: alice-sn
+mail: alice@james.org
+userPassword: secret
+description: Alice user App 1
+
+dn: uid=bob, ou=whatever, dc=james,dc=org
+objectClass: inetOrgPerson
+uid: bob
+cn: bob
+sn: bob
+mail: bob@extra.org
+userPassword: secret
+description: bob user


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org