JAMES-1922 Introduce LDAP usersRepository tests
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/11b9eeef Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/11b9eeef Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/11b9eeef Branch: refs/heads/master Commit: 11b9eeeffdbfd512f7b80e9407598d35a3b4f670 Parents: ed25623 Author: Antoine Duprat <[email protected]> Authored: Tue Jan 31 14:40:25 2017 +0100 Committer: Benoit Tellier <[email protected]> Committed: Wed Feb 8 09:10:24 2017 +0700 ---------------------------------------------------------------------- .../util/streams/SwarmGenericContainer.java | 5 + .../data/data-ldap-integration-testing/pom.xml | 233 +++++++++++++++++++ .../james/user/ldap/LdapGenericContainer.java | 91 ++++++++ .../ldap/ReadOnlyUsersLDAPRepositoryTest.java | 99 ++++++++ .../src/test/resources/ldif-files/Dockerfile | 3 + .../src/test/resources/ldif-files/populate.ldif | 11 + .../src/test/resources/logback.xml | 22 ++ server/pom.xml | 6 + 8 files changed, 470 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/11b9eeef/server/container/util-java8/src/test/java/org/apache/james/util/streams/SwarmGenericContainer.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/streams/SwarmGenericContainer.java b/server/container/util-java8/src/test/java/org/apache/james/util/streams/SwarmGenericContainer.java index 6bbe0af..2c70aa1 100644 --- a/server/container/util-java8/src/test/java/org/apache/james/util/streams/SwarmGenericContainer.java +++ b/server/container/util-java8/src/test/java/org/apache/james/util/streams/SwarmGenericContainer.java @@ -24,6 +24,7 @@ import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.testcontainers.containers.GenericContainer; +import org.testcontainers.images.builder.ImageFromDockerfile; import com.google.common.base.Strings; @@ -36,6 +37,10 @@ public class SwarmGenericContainer extends GenericContainer<SwarmGenericContaine super(dockerImageName); } + public SwarmGenericContainer(ImageFromDockerfile imageFromDockerfile) { + super(imageFromDockerfile); + } + public SwarmGenericContainer withAffinityToContainer() { String container = System.getenv(DOCKER_CONTAINER); if (Strings.isNullOrEmpty(container)) { http://git-wip-us.apache.org/repos/asf/james-project/blob/11b9eeef/server/data/data-ldap-integration-testing/pom.xml ---------------------------------------------------------------------- diff --git a/server/data/data-ldap-integration-testing/pom.xml b/server/data/data-ldap-integration-testing/pom.xml new file mode 100644 index 0000000..9527b91 --- /dev/null +++ b/server/data/data-ldap-integration-testing/pom.xml @@ -0,0 +1,233 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <artifactId>james-server</artifactId> + <groupId>org.apache.james</groupId> + <version>3.0.0-beta6-SNAPSHOT</version> + <relativePath>../../pom.xml</relativePath> + </parent> + + <artifactId>james-server-data-ldap-integration-testing</artifactId> + <packaging>bundle</packaging> + + <name>Apache James :: Server :: Data :: LDAP Implementation :: Integration testing</name> + + <profiles> + <profile> + <id>disable-build-for-older-jdk</id> + <activation> + <jdk>(,1.8)</jdk> + </activation> + <build> + <plugins> + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <executions> + <execution> + <id>default-jar</id> + <phase>none</phase> + </execution> + <execution> + <id>jar</id> + <phase>none</phase> + </execution> + <execution> + <id>test-jar</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <executions> + <execution> + <id>default-compile</id> + <phase>none</phase> + </execution> + <execution> + <id>default-testCompile</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <executions> + <execution> + <id>default-test</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-source-plugin</artifactId> + <executions> + <execution> + <id>attach-sources</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-install-plugin</artifactId> + <executions> + <execution> + <id>default-install</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-resources-plugin</artifactId> + <executions> + <execution> + <id>default-resources</id> + <phase>none</phase> + </execution> + <execution> + <id>default-testResources</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-site-plugin</artifactId> + <executions> + <execution> + <id>attach-descriptor</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + <profile> + <id>build-for-jdk-8</id> + <activation> + <jdk>[1.8,)</jdk> + </activation> + <dependencies> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-data-ldap</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-util-java8</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-util-java8</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <version>1.1.7</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>${assertj-3.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>testcontainers</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <configuration> + <archive> + <manifest> + <mainClass>fully.qualified.MainClass</mainClass> + </manifest> + </archive> + <descriptorRefs> + <descriptorRef>jar-with-dependencies</descriptorRef> + </descriptorRefs> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + </plugins> + </build> + </profile> + <profile> + <id>animal-sniffer-java-8</id> + <activation> + <jdk>[1.8,)</jdk> + </activation> + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>animal-sniffer-maven-plugin</artifactId> + <configuration> + <signature> + <groupId>org.codehaus.mojo.signature</groupId> + <artifactId>java18</artifactId> + <version>1.0</version> + </signature> + </configuration> + <executions> + <execution> + <id>check_java_8</id> + <phase>test</phase> + <goals> + <goal>check</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + </profiles> + +</project> http://git-wip-us.apache.org/repos/asf/james-project/blob/11b9eeef/server/data/data-ldap-integration-testing/src/test/java/org/apache/james/user/ldap/LdapGenericContainer.java ---------------------------------------------------------------------- diff --git a/server/data/data-ldap-integration-testing/src/test/java/org/apache/james/user/ldap/LdapGenericContainer.java b/server/data/data-ldap-integration-testing/src/test/java/org/apache/james/user/ldap/LdapGenericContainer.java new file mode 100644 index 0000000..89ef0b0 --- /dev/null +++ b/server/data/data-ldap-integration-testing/src/test/java/org/apache/james/user/ldap/LdapGenericContainer.java @@ -0,0 +1,91 @@ +/**************************************************************** + * 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 org.apache.james.util.streams.SwarmGenericContainer; +import org.testcontainers.images.builder.ImageFromDockerfile; + +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; + +public class LdapGenericContainer { + + public static final int DEFAULT_LDAP_PORT = 389; + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + + private String domain; + private String password; + + private Builder() { + } + + public Builder domain(String domain) { + this.domain = domain; + return this; + } + + public Builder password(String password) { + this.password = password; + return this; + } + + public LdapGenericContainer build() { + Preconditions.checkState(!Strings.isNullOrEmpty(domain), "'domain' is mandatory"); + Preconditions.checkState(!Strings.isNullOrEmpty(password), "'password' is mandatory"); + return new LdapGenericContainer(createContainer()); + } + + private SwarmGenericContainer createContainer() { + return new SwarmGenericContainer( + new ImageFromDockerfile() + .withFileFromClasspath("populate.ldif", "ldif-files/populate.ldif") + .withFileFromClasspath("Dockerfile", "ldif-files/Dockerfile")) + .withAffinityToContainer() + .withEnv("SLAPD_DOMAIN", domain) + .withEnv("SLAPD_PASSWORD", password) + .withEnv("SLAPD_CONFIG_PASSWORD", password); + } + } + + private final SwarmGenericContainer container; + + private LdapGenericContainer(SwarmGenericContainer container) { + this.container = container; + } + + public void start() { + container.start(); + } + + public void stop() { + container.stop(); + } + + public String getLdapHost() { + return "ldap://" + + container.getContainerIpAddress() + + ":" + + container.getMappedPort(LdapGenericContainer.DEFAULT_LDAP_PORT); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/11b9eeef/server/data/data-ldap-integration-testing/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java ---------------------------------------------------------------------- diff --git a/server/data/data-ldap-integration-testing/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java b/server/data/data-ldap-integration-testing/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java new file mode 100644 index 0000000..c5e1bbe --- /dev/null +++ b/server/data/data-ldap-integration-testing/src/test/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepositoryTest.java @@ -0,0 +1,99 @@ +/**************************************************************** + * 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.assertj.core.api.Assertions.assertThat; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.HierarchicalConfiguration; +import org.apache.commons.configuration.plist.PropertyListConfiguration; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ReadOnlyUsersLDAPRepositoryTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(ReadOnlyUsersLDAPRepositoryTest.class); + private static final String DOMAIN = "james.org"; + private static final String ADMIN_PASSWORD = "mysecretpassword"; + private static final String JAMES_USER = "james-user"; + private static final String UNKNOWN = "unknown"; + private static final String PASSWORD = "secret"; + private static final String BAD_PASSWORD = "badpassword"; + + private LdapGenericContainer ldapContainer; + private ReadOnlyUsersLDAPRepository ldapRepository; + + @Before + public void setup() throws Exception { + ldapContainer = LdapGenericContainer.builder() + .domain(DOMAIN) + .password(ADMIN_PASSWORD) + .build(); + ldapContainer.start(); + ldapRepository = new ReadOnlyUsersLDAPRepository(); + ldapRepository.configure(ldapRepositoryConfiguration()); + ldapRepository.setLog(LOGGER); + ldapRepository.init(); + } + + private HierarchicalConfiguration ldapRepositoryConfiguration() throws ConfigurationException { + 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("[@userIdAttribute]", "uid"); + configuration.addProperty("[@userObjectClass]", "inetOrgPerson"); + configuration.addProperty("[@maxRetries]", "4"); + configuration.addProperty("[@retryStartInterval]", "0"); + configuration.addProperty("[@retryMaxInterval]", "8"); + configuration.addProperty("[@retryIntervalScale]", "1000"); + return configuration; + } + + @After + public void tearDown() { + if (ldapContainer != null) { + ldapContainer.stop(); + } + } + + @Test + public void knownUserShouldBeAbleToLogInWhenPasswordIsCorrect() throws Exception { + assertThat(ldapRepository.test(JAMES_USER, PASSWORD)).isTrue(); + } + + @Test + public void knownUserShouldNotBeAbleToLogInWhenPasswordIsNotCorrect() throws Exception { + assertThat(ldapRepository.test(JAMES_USER, BAD_PASSWORD)).isFalse(); + } + + @Test + public void unknownUserShouldNotBeAbleToLogIn() throws Exception { + assertThat(ldapRepository.test(UNKNOWN, BAD_PASSWORD)).isFalse(); + } + + @Test + public void unknownUserShouldNotBeAbleToLogInWhenPasswordIsCorrect() throws Exception { + assertThat(ldapRepository.test(UNKNOWN, PASSWORD)).isFalse(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/11b9eeef/server/data/data-ldap-integration-testing/src/test/resources/ldif-files/Dockerfile ---------------------------------------------------------------------- diff --git a/server/data/data-ldap-integration-testing/src/test/resources/ldif-files/Dockerfile b/server/data/data-ldap-integration-testing/src/test/resources/ldif-files/Dockerfile new file mode 100644 index 0000000..d889a35 --- /dev/null +++ b/server/data/data-ldap-integration-testing/src/test/resources/ldif-files/Dockerfile @@ -0,0 +1,3 @@ +FROM dinkel/openldap:latest + +COPY populate.ldif /etc/ldap/prepopulate/prepop.ldif http://git-wip-us.apache.org/repos/asf/james-project/blob/11b9eeef/server/data/data-ldap-integration-testing/src/test/resources/ldif-files/populate.ldif ---------------------------------------------------------------------- diff --git a/server/data/data-ldap-integration-testing/src/test/resources/ldif-files/populate.ldif b/server/data/data-ldap-integration-testing/src/test/resources/ldif-files/populate.ldif new file mode 100644 index 0000000..9376a6c --- /dev/null +++ b/server/data/data-ldap-integration-testing/src/test/resources/ldif-files/populate.ldif @@ -0,0 +1,11 @@ +dn: ou=people, dc=james,dc=org +ou: people +objectClass: organizationalUnit + +dn: uid=james-user, ou=people, dc=james,dc=org +objectClass: inetOrgPerson +uid: james-user +cn: james-user +sn: james-user +userPassword: secret +description: James user http://git-wip-us.apache.org/repos/asf/james-project/blob/11b9eeef/server/data/data-ldap-integration-testing/src/test/resources/logback.xml ---------------------------------------------------------------------- diff --git a/server/data/data-ldap-integration-testing/src/test/resources/logback.xml b/server/data/data-ldap-integration-testing/src/test/resources/logback.xml new file mode 100644 index 0000000..5c69ee7 --- /dev/null +++ b/server/data/data-ldap-integration-testing/src/test/resources/logback.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="UTF-8"?> +<configuration> + + <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"> + <resetJUL>true</resetJUL> + </contextListener> + + <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern> + <immediateFlush>false</immediateFlush> + </encoder> + </appender> + + <root level="DEBUG"> + <appender-ref ref="CONSOLE" /> + </root> + + <logger name="org.testcontainers" level="INFO"/> + <logger name="org.apache.james" level="DEBUG"/> + +</configuration> http://git-wip-us.apache.org/repos/asf/james-project/blob/11b9eeef/server/pom.xml ---------------------------------------------------------------------- diff --git a/server/pom.xml b/server/pom.xml index 46687c4..bad11d8 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -89,6 +89,7 @@ <module>data/data-jmap-cassandra</module> <module>data/data-file</module> <module>data/data-ldap</module> + <module>data/data-ldap-integration-testing</module> <module>data/data-hbase</module> <module>data/data-memory</module> @@ -776,6 +777,11 @@ </dependency> <dependency> <groupId>org.apache.james</groupId> + <artifactId>james-server-data-ldap-integration-testing</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> <artifactId>james-server-protocols-lmtp</artifactId> <version>${project.version}</version> </dependency> --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
