32
32
import org .springframework .security .authentication .BadCredentialsException ;
33
33
import org .springframework .security .authentication .CredentialsExpiredException ;
34
34
import org .springframework .security .authentication .DisabledException ;
35
+ import org .springframework .security .authentication .InternalAuthenticationServiceException ;
35
36
import org .springframework .security .authentication .LockedException ;
36
37
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
37
38
import org .springframework .security .core .Authentication ;
58
59
* @author Rob Winch
59
60
*/
60
61
public 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
+
61
65
@ Rule
62
66
public ExpectedException thrown = ExpectedException .none ();
63
67
@@ -378,17 +382,29 @@ public void errorWithNoSubcodeIsHandledCleanly() throws Exception {
378
382
}
379
383
380
384
@ 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 );
386
402
}
387
403
388
404
@ Test
389
405
public void rootDnProvidedSeparatelyFromDomainAlsoWorks () throws Exception {
390
406
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" );
392
408
checkAuthentication ("dc=ad,dc=eu,dc=mydomain" , provider );
393
409
394
410
}
@@ -414,8 +430,11 @@ public void contextEnvironmentPropertiesUsed() throws Exception {
414
430
provider .authenticate (joe );
415
431
fail ("CommunicationException was expected with a root cause of ClassNotFoundException" );
416
432
}
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 );
419
438
}
420
439
}
421
440
0 commit comments