Skip to content

Commit

Permalink
Add more configuration for LLdapContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed Feb 19, 2025
1 parent 85697fd commit aad1ee5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class LLdapContainer extends GenericContainer<LLdapContainer> {

private static final int LDAP_PORT = 3890;

private static final int LDAPS_PORT = 6360;

private static final int UI_PORT = 17170;

public LLdapContainer(String image) {
Expand All @@ -45,11 +47,39 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
log.info("LLDAP container is ready! UI available at http://{}:{}", getHost(), getMappedPort(UI_PORT));
}

public LLdapContainer withBaseDn(String baseDn) {
withEnv("LLDAP_LDAP_BASE_DN", baseDn);
return this;
}

public LLdapContainer withUserPass(String userPass) {
withEnv("LLDAP_LDAP_USER_PASS", userPass);
return this;
}

public int getLdapPort() {
return getMappedPort(LDAP_PORT);
int port = getEnvMap().getOrDefault("LLDAP_LDAPS_OPTIONS__ENABLED", "false").equals("true")
? LDAPS_PORT
: LDAP_PORT;
return getMappedPort(port);
}

public String getLdapUrl() {
return String.format("ldap://%s:%d", getHost(), getLdapPort());
String protocol = getEnvMap().getOrDefault("LLDAP_LDAPS_OPTIONS__ENABLED", "false").equals("true")
? "ldaps"
: "ldap";
return String.format("%s://%s:%d", protocol, getHost(), getLdapPort());
}

public String getBaseDn() {
return getEnvMap().getOrDefault("LLDAP_LDAP_BASE_DN", "dc=example,dc=com");
}

public String getUser() {
return String.format("cn=admin,ou=people,%s", getBaseDn());
}

public String getUserPass() {
return getEnvMap().getOrDefault("LLDAP_LDAP_USER_PASS", "password");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,48 @@ public void test() throws LDAPException {
) {
lldap.start();
LDAPConnection connection = new LDAPConnection(lldap.getHost(), lldap.getLdapPort());
BindResult result = connection.bind("cn=admin,ou=people,dc=example,dc=com", "password");
BindResult result = connection.bind(lldap.getUser(), lldap.getUserPass());
assertThat(result).isNotNull();
}
}

@Test
public void testUsingLdapUrl() throws LDAPException {
try ( // container {
try (LLdapContainer lldap = new LLdapContainer("lldap/lldap:v0.6.1-alpine")) {
lldap.start();

LDAPURL ldapUrl = new LDAPURL(lldap.getLdapUrl());
LDAPConnection connection = new LDAPConnection(ldapUrl.getHost(), ldapUrl.getPort());
BindResult result = connection.bind(lldap.getUser(), lldap.getUserPass());
assertThat(result).isNotNull();
}
}

@Test
public void testWithCustomBaseDn() throws LDAPException {
try (
LLdapContainer lldap = new LLdapContainer("lldap/lldap:v0.6.1-alpine")
// }
.withBaseDn("dc=testcontainers,dc=org")
) {
lldap.start();

assertThat(lldap.getBaseDn()).isEqualTo("dc=testcontainers,dc=org");

LDAPURL ldapUrl = new LDAPURL(lldap.getLdapUrl());
LDAPConnection connection = new LDAPConnection(ldapUrl.getHost(), ldapUrl.getPort());
BindResult result = connection.bind(lldap.getUser(), lldap.getUserPass());
assertThat(result).isNotNull();
}
}

@Test
public void testWithCustomUserPass() throws LDAPException {
try (LLdapContainer lldap = new LLdapContainer("lldap/lldap:v0.6.1-alpine").withUserPass("adminPas$word")) {
lldap.start();

LDAPURL ldapUrl = new LDAPURL(lldap.getLdapUrl());
LDAPConnection connection = new LDAPConnection(ldapUrl.getHost(), ldapUrl.getPort());
BindResult result = connection.bind("cn=admin,ou=people,dc=example,dc=com", "password");
BindResult result = connection.bind(lldap.getUser(), lldap.getUserPass());
assertThat(result).isNotNull();
}
}
Expand Down

0 comments on commit aad1ee5

Please sign in to comment.