@Test(expected=IllegalArgumentException.class) public void testSetUserDnTemplateEmpty() { realm.setUserDnTemplate(" "); }
@Test(expected=IllegalArgumentException.class) public void testSetUserDnTemplateWithoutToken() { realm.setUserDnTemplate("uid=,ou=users,dc=mycompany,dc=com"); }
@Test(expected=IllegalArgumentException.class) public void testSetUserDnTemplateNull() { realm.setUserDnTemplate(null); }
@Test public void testUserDnTemplate() { String template = "uid={0},ou=users,dc=mycompany,dc=com"; realm.setUserDnTemplate(template); assertEquals(template, realm.getUserDnTemplate()); }
@Test public void testUserDnTemplateSubstitution() throws NamingException { realm.setUserDnTemplate("uid={0},ou=users,dc=mycompany,dc=com"); LdapContextFactory factory = createMock(LdapContextFactory.class); realm.setContextFactory(factory); Object expectedPrincipal = "uid=jsmith,ou=users,dc=mycompany,dc=com"; expect(factory.getLdapContext(eq(expectedPrincipal), isA(Object.class))).andReturn(createNiceMock(LdapContext.class)); replay(factory); realm.getAuthenticationInfo(new UsernamePasswordToken("jsmith", "secret") ); verify(factory); }
@Test(expected= AuthenticationException.class) public void testGetAuthenticationInfoNamingException() throws NamingException { realm.setUserDnTemplate("uid={0},ou=users,dc=mycompany,dc=com"); LdapContextFactory factory = createMock(LdapContextFactory.class); realm.setContextFactory(factory); expect(factory.getLdapContext(isA(Object.class), isA(Object.class))) .andThrow(new NamingException("Communication error.")); replay(factory); realm.getAuthenticationInfo(new UsernamePasswordToken("jsmith", "secret") ); }
/** * This test simulates that if a non-String principal (i.e. not a username) is passed as the LDAP principal, that * it is not altered into a User DN and is passed as-is. This will allow principals to be things like X.509 * certificates as well instead of only strings. * * @throws NamingException not thrown */ @Test public void testGetAuthenticationInfoNonSimpleToken() throws NamingException { realm.setUserDnTemplate("uid={0},ou=users,dc=mycompany,dc=com"); LdapContextFactory factory = createMock(LdapContextFactory.class); realm.setContextFactory(factory); final UUID userId = UUID.randomUUID(); //ensure the userId is passed as-is: expect(factory.getLdapContext(eq(userId), isA(Object.class))).andReturn(createNiceMock(LdapContext.class)); replay(factory); realm.getAuthenticationInfo(new AuthenticationToken() { public Object getPrincipal() { return userId; } public Object getCredentials() { return "secret"; } }); verify(factory); }
@Test(expected= AuthenticationException.class) public void testGetAuthenticationInfoNamingAuthenticationException() throws NamingException { realm.setUserDnTemplate("uid={0},ou=users,dc=mycompany,dc=com"); LdapContextFactory factory = createMock(LdapContextFactory.class); realm.setContextFactory(factory); expect(factory.getLdapContext(isA(Object.class), isA(Object.class))) .andThrow(new javax.naming.AuthenticationException("LDAP Authentication failed.")); replay(factory); realm.getAuthenticationInfo(new UsernamePasswordToken("jsmith", "secret") ); }