Skip to content

Make ldap integration tests independent #7231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions ldap/spring-security-ldap.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ dependencies {
}

integrationTest {
include('**/ApacheDSServerIntegrationTests.class',
'**/ApacheDSEmbeddedLdifTests.class',
'**/LdapUserDetailsManagerModifyPasswordTests.class')
// exclude('**/OpenLDAPIntegrationTestSuite.class')
maxParallelForks = 1
}

// Runs a server for running the integration tests against (from an IDE, for example)
task(ldapServer, dependsOn: 'integrationTestClasses', type: JavaExec) {
classpath = sourceSets.integrationTest.runtimeClasspath
main = 'org.springframework.security.ldap.ApacheDSServerIntegrationTests'
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.security.ldap;

import javax.annotation.PreDestroy;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.ContextSource;
import org.springframework.security.ldap.server.ApacheDSContainer;

/**
* @author Eddú Meléndez
*/
@Configuration
public class ApacheDsContainerConfig {

private ApacheDSContainer container;

@Bean
ApacheDSContainer ldapContainer() throws Exception {
this.container = new ApacheDSContainer("dc=springframework,dc=org",
"classpath:test-server.ldif");
return this.container;
}

@Bean
ContextSource contextSource() throws Exception {
return new DefaultSpringSecurityContextSource("ldap://127.0.0.1:"
+ ldapContainer().getPort() + "/dc=springframework,dc=org");
}

@PreDestroy
void shutdown() {
this.container.stop();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,13 +24,24 @@
import javax.naming.directory.DirContext;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.AuthenticationException;
import org.springframework.ldap.core.support.AbstractContextSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

/**
* @author Luke Taylor
* @author Eddú Meléndez
*/
public class DefaultSpringSecurityContextSourceTests extends AbstractLdapIntegrationTests {
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ApacheDsContainerConfig.class)
public class DefaultSpringSecurityContextSourceTests {

@Autowired
private DefaultSpringSecurityContextSource contextSource;

@Test
public void instantiationSucceedsWithExpectedProperties() {
Expand Down Expand Up @@ -76,7 +87,7 @@ public void cantBindWithWrongPasswordImmediatelyAfterSuccessfulBind()
throws Exception {
DirContext ctx = null;
try {
ctx = getContextSource().getContext(
ctx = this.contextSource.getContext(
"uid=Bob,ou=people,dc=springframework,dc=org", "bobspassword");
}
catch (Exception e) {
Expand All @@ -86,16 +97,16 @@ public void cantBindWithWrongPasswordImmediatelyAfterSuccessfulBind()
ctx.close();
// com.sun.jndi.ldap.LdapPoolManager.showStats(System.out);
// Now get it gain, with wrong password. Should fail.
ctx = getContextSource().getContext(
ctx = this.contextSource.getContext(
"uid=Bob,ou=people,dc=springframework,dc=org", "wrongpassword");
ctx.close();
}

@Test
public void serverUrlWithSpacesIsSupported() throws Exception {
DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(
"ldap://127.0.0.1:" + ApacheDSServerIntegrationTests.getServerPort()
+ "/ou=space%20cadets,dc=springframework,dc=org");
this.contextSource.getUrls()[0]
+ "ou=space%20cadets,dc=springframework,dc=org");
contextSource.afterPropertiesSet();
contextSource.getContext(
"uid=space cadet,ou=space cadets,dc=springframework,dc=org",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,35 @@
import javax.naming.directory.SearchResult;

import org.junit.*;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.UncategorizedLdapException;
import org.springframework.ldap.core.ContextExecutor;
import org.springframework.security.crypto.codec.Utf8;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

/**
* @author Luke Taylor
* @author Edd� Mel�ndez
*/
public class SpringSecurityLdapTemplateITests extends AbstractLdapIntegrationTests {
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ApacheDsContainerConfig.class)
public class SpringSecurityLdapTemplateITests {
// ~ Instance fields
// ================================================================================================

@Autowired
private DefaultSpringSecurityContextSource contextSource;
private SpringSecurityLdapTemplate template;

// ~ Methods
// ========================================================================================================

@Before
public void setUp() throws Exception {
template = new SpringSecurityLdapTemplate(getContextSource());
template = new SpringSecurityLdapTemplate(this.contextSource);
}

@Test
Expand Down Expand Up @@ -187,8 +197,7 @@ public void roleSearchWithEscapedCharacterSucceeds() throws Exception {
public void nonSpringLdapSearchCodeTestMethod() throws Exception {
java.util.Hashtable<String, String> env = new java.util.Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:"
+ ApacheDSServerIntegrationTests.getServerPort());
env.put(Context.PROVIDER_URL, this.contextSource.getUrls()[0]);
env.put(Context.SECURITY_PRINCIPAL, "");
env.put(Context.SECURITY_CREDENTIALS, "");

Expand Down
Loading