Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JAMES-1927 Implement IT for cassandra-ldap guice
- Loading branch information
Quynh Nguyen
committed
Feb 9, 2017
1 parent
0a5c32b
commit 8ee5d08
Showing
3 changed files
with
193 additions
and
7 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
...ice/cassandra-ldap-guice/src/test/java/org/apache/james/CassandraLdapJamesServerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
.../guice/cassandra-ldap-guice/src/test/java/org/apache/james/CassandraLdapJmapTestRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters