diff --git a/server/container/guice/cassandra-ldap-guice/src/test/java/org/apache/james/CassandraLdapJamesServerTest.java b/server/container/guice/cassandra-ldap-guice/src/test/java/org/apache/james/CassandraLdapJamesServerTest.java new file mode 100644 index 00000000000..0e93c504f02 --- /dev/null +++ b/server/container/guice/cassandra-ldap-guice/src/test/java/org/apache/james/CassandraLdapJamesServerTest.java @@ -0,0 +1,75 @@ +/**************************************************************** + * 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 org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; + +import org.apache.commons.net.imap.IMAPClient; +import org.apache.james.user.ldap.LdapGenericContainer; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.RuleChain; + +import com.google.common.base.Throwables; + +public class CassandraLdapJamesServerTest extends AbstractJmapJamesServerTest { + private static final String JAMES_USER = "james-user"; + private static final String PASSWORD = "secret"; + private static final String DOMAIN = "james.org"; + private static final String ADMIN_PASSWORD = "mysecretpassword"; + + private LdapGenericContainer ldapContainer = LdapGenericContainer.builder() + .domain(DOMAIN) + .password(ADMIN_PASSWORD) + .build(); + private CassandraLdapJmapTestRule cassandraLdapJmap = CassandraLdapJmapTestRule.defaultTestRule(); + private IMAPClient imapClient = new IMAPClient(); + + @Rule + public RuleChain ruleChain = RuleChain.outerRule(ldapContainer).around(cassandraLdapJmap); + + @Override + protected JmapJamesServer createJamesServer() { + ldapContainer.start(); + return cassandraLdapJmap.jmapServer(ldapContainer.getLdapHost()); + } + + @Override + protected void clean() { + if (ldapContainer != null) { + ldapContainer.stop(); + } + + try { + imapClient.disconnect(); + } catch (IOException e) { + Throwables.propagate(e); + } + } + + @Test + public void userFromLdapShouldLoginViaImapProtocol() throws Exception { + imapClient.connect(JAMES_SERVER_HOST, IMAP_PORT); + + assertThat(imapClient.login(JAMES_USER, PASSWORD)).isTrue(); + } +} diff --git a/server/container/guice/cassandra-ldap-guice/src/test/java/org/apache/james/CassandraLdapJmapTestRule.java b/server/container/guice/cassandra-ldap-guice/src/test/java/org/apache/james/CassandraLdapJmapTestRule.java new file mode 100644 index 00000000000..a57181883f0 --- /dev/null +++ b/server/container/guice/cassandra-ldap-guice/src/test/java/org/apache/james/CassandraLdapJmapTestRule.java @@ -0,0 +1,107 @@ +/**************************************************************** + * 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 org.apache.commons.configuration.HierarchicalConfiguration; +import org.apache.commons.configuration.plist.PropertyListConfiguration; +import org.apache.james.http.jetty.ConfigurationException; +import org.apache.james.modules.TestJMAPServerModule; +import org.apache.james.utils.ConfigurationProvider; +import org.apache.james.utils.FileConfigurationProvider; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +import com.google.inject.Inject; +import com.google.inject.Module; +import com.google.inject.name.Named; +import com.google.inject.name.Names; + +public class CassandraLdapJmapTestRule implements TestRule { + private static final int LIMIT_TO_3_MESSAGES = 3; + + public static CassandraLdapJmapTestRule defaultTestRule() { + return new CassandraLdapJmapTestRule( + AggregateGuiceModuleTestRule.of(new EmbeddedElasticSearchRule(), new EmbeddedCassandraRule())); + } + + private GuiceModuleTestRule guiceModuleTestRule; + + public CassandraLdapJmapTestRule(GuiceModuleTestRule... guiceModuleTestRule) { + this.guiceModuleTestRule = + AggregateGuiceModuleTestRule + .of(guiceModuleTestRule) + .aggregate(new TempFilesystemTestRule()); + } + + public JmapJamesServer jmapServer(String ldapIp, Module... additionals) { + return new JmapJamesServer() + .combineWith(CassandraLdapJamesServerMain.cassandraLdapServerModule, + binder -> binder.bind(String.class).annotatedWith(Names.named("ldapIp")).toInstance(ldapIp)) + .overrideWith(new TestJMAPServerModule(LIMIT_TO_3_MESSAGES)) + .overrideWith(guiceModuleTestRule.getModule()) + .overrideWith(additionals) + .overrideWith(binder -> binder.bind(ConfigurationProvider.class).to(UnionConfigurationProvider.class)); + } + + @Override + public Statement apply(Statement base, Description description) { + return guiceModuleTestRule.apply(base, description); + } + + public void await() { + guiceModuleTestRule.await(); + } + + private static class UnionConfigurationProvider implements ConfigurationProvider { + private final FileConfigurationProvider fileConfigurationProvider; + private final String ldapIp; + + @Inject + public UnionConfigurationProvider(FileConfigurationProvider fileConfigurationProvider, + @Named("ldapIp") String ldapIp) { + this.fileConfigurationProvider = fileConfigurationProvider; + this.ldapIp = ldapIp; + } + + @Override + public HierarchicalConfiguration getConfiguration(String component) throws org.apache.commons.configuration.ConfigurationException { + if (component.equals("usersrepository")) { + return ldapRepositoryConfiguration(); + } + return fileConfigurationProvider.getConfiguration(component); + } + + private HierarchicalConfiguration ldapRepositoryConfiguration() throws ConfigurationException { + PropertyListConfiguration configuration = new PropertyListConfiguration(); + configuration.addProperty("[@ldapHost]", ldapIp); + configuration.addProperty("[@principal]", "cn=admin\\,dc=james\\,dc=org"); + configuration.addProperty("[@credentials]", "mysecretpassword"); + configuration.addProperty("[@userBase]", "ou=People\\,dc=james\\,dc=org"); + configuration.addProperty("[@userIdAttribute]", "uid"); + configuration.addProperty("[@userObjectClass]", "inetOrgPerson"); + configuration.addProperty("[@maxRetries]", "4"); + configuration.addProperty("[@retryStartInterval]", "0"); + configuration.addProperty("[@retryMaxInterval]", "8"); + configuration.addProperty("[@retryIntervalScale]", "1000"); + return configuration; + } + } +} diff --git a/server/container/guice/guice-common/src/test/java/org/apache/james/AbstractJmapJamesServerTest.java b/server/container/guice/guice-common/src/test/java/org/apache/james/AbstractJmapJamesServerTest.java index 860da2616ff..aff297f23a2 100644 --- a/server/container/guice/guice-common/src/test/java/org/apache/james/AbstractJmapJamesServerTest.java +++ b/server/container/guice/guice-common/src/test/java/org/apache/james/AbstractJmapJamesServerTest.java @@ -41,12 +41,14 @@ public abstract class AbstractJmapJamesServerTest { - private static final int IMAP_PORT = 1143; // You need to be root (superuser) to bind to ports under 1024. private static final int IMAP_PORT_SSL = 1993; private static final int POP3_PORT = 1110; private static final int SMTP_PORT = 1025; private static final int LMTP_PORT = 1024; + protected static final String JAMES_SERVER_HOST = "127.0.0.1"; + protected static final int IMAP_PORT = 1143; // You need to be root (superuser) to bind to ports under 1024. + private JmapJamesServer server; private SocketChannel socketChannel; @@ -71,7 +73,9 @@ public void setup() throws Exception { @After public void tearDown() throws Exception { - server.stop(); + if (server != null) { + server.stop(); + } clean(); } @@ -93,31 +97,31 @@ public void hostnameShouldBeRetrievedWhenRestarting() throws Exception { @Test public void connectIMAPServerShouldSendShabangOnConnect() throws Exception { - socketChannel.connect(new InetSocketAddress("127.0.0.1", IMAP_PORT)); + socketChannel.connect(new InetSocketAddress(JAMES_SERVER_HOST, IMAP_PORT)); assertThat(getServerConnectionResponse(socketChannel)).startsWith("* OK JAMES IMAP4rev1 Server"); } @Test public void connectOnSecondaryIMAPServerIMAPServerShouldSendShabangOnConnect() throws Exception { - socketChannel.connect(new InetSocketAddress("127.0.0.1", IMAP_PORT_SSL)); + socketChannel.connect(new InetSocketAddress(JAMES_SERVER_HOST, IMAP_PORT_SSL)); assertThat(getServerConnectionResponse(socketChannel)).startsWith("* OK JAMES IMAP4rev1 Server"); } @Test public void connectPOP3ServerShouldSendShabangOnConnect() throws Exception { - socketChannel.connect(new InetSocketAddress("127.0.0.1", POP3_PORT)); + socketChannel.connect(new InetSocketAddress(JAMES_SERVER_HOST, POP3_PORT)); assertThat(getServerConnectionResponse(socketChannel)).contains("POP3 server (JAMES POP3 Server ) ready"); } @Test public void connectSMTPServerShouldSendShabangOnConnect() throws Exception { - socketChannel.connect(new InetSocketAddress("127.0.0.1", SMTP_PORT)); + socketChannel.connect(new InetSocketAddress(JAMES_SERVER_HOST, SMTP_PORT)); assertThat(getServerConnectionResponse(socketChannel)).startsWith("220 JAMES Linagora's SMTP awesome Server"); } @Test public void connectLMTPServerShouldSendShabangOnConnect() throws Exception { - socketChannel.connect(new InetSocketAddress("127.0.0.1", LMTP_PORT)); + socketChannel.connect(new InetSocketAddress(JAMES_SERVER_HOST, LMTP_PORT)); assertThat(getServerConnectionResponse(socketChannel)).contains("LMTP Server (JAMES Protocols Server) ready"); }