3232import org .springframework .security .authentication .BadCredentialsException ;
3333import org .springframework .security .authentication .CredentialsExpiredException ;
3434import org .springframework .security .authentication .DisabledException ;
35+ import org .springframework .security .authentication .InternalAuthenticationServiceException ;
3536import org .springframework .security .authentication .LockedException ;
3637import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
3738import org .springframework .security .core .Authentication ;
5859 * @author Rob Winch
5960 */
6061public class ActiveDirectoryLdapAuthenticationProviderTests {
62+ public static final String EXISTING_LDAP_PROVIDER = "ldap://192.168.1.200/" ;
63+ public static final String NON_EXISTING_LDAP_PROVIDER = "ldap://192.168.1.201/" ;
64+
6165 @ Rule
6266 public ExpectedException thrown = ExpectedException .none ();
6367
@@ -378,17 +382,29 @@ public void errorWithNoSubcodeIsHandledCleanly() throws Exception {
378382 }
379383
380384 @ Test (expected = org .springframework .ldap .CommunicationException .class )
381- public void nonAuthenticationExceptionIsConvertedToSpringLdapException ()
382- throws Exception {
383- provider .contextFactory = createContextFactoryThrowing (new CommunicationException (
384- msg ));
385- provider .authenticate (joe );
385+ public void nonAuthenticationExceptionIsConvertedToSpringLdapException () throws Throwable {
386+ try {
387+ provider .contextFactory = createContextFactoryThrowing (new CommunicationException (
388+ msg ));
389+ provider .authenticate (joe );
390+ } catch (InternalAuthenticationServiceException e ) {
391+ // Since GH-8418 ldap communication exception is wrapped into InternalAuthenticationServiceException.
392+ // This test is about the wrapped exception, so we throw it.
393+ throw e .getCause ();
394+ }
395+ }
396+
397+ @ Test (expected = org .springframework .security .authentication .InternalAuthenticationServiceException .class )
398+ public void connectionExceptionIsWrappedInInternalException () throws Exception {
399+ ActiveDirectoryLdapAuthenticationProvider noneReachableProvider = new ActiveDirectoryLdapAuthenticationProvider (
400+ "mydomain.eu" , NON_EXISTING_LDAP_PROVIDER , "dc=ad,dc=eu,dc=mydomain" );
401+ noneReachableProvider .doAuthentication (joe );
386402 }
387403
388404 @ Test
389405 public void rootDnProvidedSeparatelyFromDomainAlsoWorks () throws Exception {
390406 ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider (
391- "mydomain.eu" , "ldap://192.168.1.200/" , "dc=ad,dc=eu,dc=mydomain" );
407+ "mydomain.eu" , EXISTING_LDAP_PROVIDER , "dc=ad,dc=eu,dc=mydomain" );
392408 checkAuthentication ("dc=ad,dc=eu,dc=mydomain" , provider );
393409
394410 }
@@ -414,8 +430,11 @@ public void contextEnvironmentPropertiesUsed() throws Exception {
414430 provider .authenticate (joe );
415431 fail ("CommunicationException was expected with a root cause of ClassNotFoundException" );
416432 }
417- catch (org .springframework .ldap .CommunicationException expected ) {
418- assertThat (expected .getRootCause ()).isInstanceOf (ClassNotFoundException .class );
433+ catch (InternalAuthenticationServiceException expected ) {
434+ assertThat (expected .getCause ()).isInstanceOf (org .springframework .ldap .CommunicationException .class );
435+ org .springframework .ldap .CommunicationException cause =
436+ (org .springframework .ldap .CommunicationException ) expected .getCause ();
437+ assertThat (cause .getRootCause ()).isInstanceOf (ClassNotFoundException .class );
419438 }
420439 }
421440
0 commit comments