javax.naming.AuthenticationNotSupportedException Java Examples

The following examples show how to use javax.naming.AuthenticationNotSupportedException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LDAPAuthorityTest.java    From athenz with Apache License 2.0 4 votes vote down vote up
@Test
public void testLDAPAuthorityConnection() throws NamingException {

    setProperties();
    ldapAuthority = new LDAPAuthority();
    ldapAuthority.initialize();
    errMsg = new StringBuilder();
    // naming exception
    principal = ldapAuthority.authenticate("Basic dGVzdHVzZXI6dGVzdHB3ZA==", "", "", errMsg);
    assertNull(principal);

    //authentication exception - wrong username password combination
    errMsg = new StringBuilder();
    ldapAuthority = mock(LDAPAuthority.class);
    doCallRealMethod().when(ldapAuthority).initialize();
    ldapAuthority.initialize();
    when(ldapAuthority.getDirContext("cn=testuser,dc=example,dc=com", "wrongpwd")).thenThrow(new AuthenticationException());
    when(ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg)).thenCallRealMethod();
    principal = ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg);
    assertNull(principal);

    //authentication not supported exception
    errMsg = new StringBuilder();
    ldapAuthority = mock(LDAPAuthority.class);
    doCallRealMethod().when(ldapAuthority).initialize();
    ldapAuthority.initialize();
    when(ldapAuthority.getDirContext("cn=testuser,dc=example,dc=com", "wrongpwd")).thenThrow(new AuthenticationNotSupportedException());
    when(ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg)).thenCallRealMethod();
    principal = ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg);
    assertNull(principal);

    //success case
    errMsg = new StringBuilder();
    ldapAuthority = mock(LDAPAuthority.class);
    doCallRealMethod().when(ldapAuthority).initialize();
    doCallRealMethod().when(ldapAuthority).getDomain();
    doCallRealMethod().when(ldapAuthority).getSimplePrincipal("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "testuser");
    ldapAuthority.initialize();

    when(ldapAuthority.getDirContext("cn=testuser,dc=example,dc=com", "wrongpwd")).thenReturn(new InitialDirContext());
    when(ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg)).thenCallRealMethod();
    when(ldapAuthority.authenticate("Basic dGVzdHVzZXIK", "", "", errMsg)).thenCallRealMethod();
    principal = ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg);
    assertNotNull(principal);
    assertEquals(principal.getName(), "testuser");
    assertEquals(principal.getDomain(), "user");
    assertEquals(principal.getCredentials(), "Basic dGVzdHVzZXI6d3Jvbmdwd2Q=");
    assertEquals(principal.getUnsignedCredentials(), "testuser");

    // pass credentials without password component

    principal = ldapAuthority.authenticate("Basic dGVzdHVzZXIK", "", "", errMsg);
    assertNull(principal);

    //null principal s returned from function
    System.setProperty(baseDNProp,"dc=example,dc=com");
    System.setProperty(portNumberProp,"389");
    errMsg = new StringBuilder();
    ldapAuthority = mock(LDAPAuthority.class);
    doCallRealMethod().when(ldapAuthority).initialize();
    doCallRealMethod().when(ldapAuthority).getDomain();
    when(ldapAuthority.getSimplePrincipal("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "testuser")).thenReturn(null);
    ldapAuthority.initialize();
    when(ldapAuthority.getDirContext("cn=testuser,dc=example,dc=com", "wrongpwd")).thenReturn(new InitialDirContext());
    when(ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg)).thenCallRealMethod();
    principal = ldapAuthority.authenticate("Basic dGVzdHVzZXI6d3Jvbmdwd2Q=", "", "", errMsg);
    assertNull(principal);

    resetProperties();
}