// 2012-06-18 johnpfeiffer
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
public class AuthgatewayServiceTest
{
private AuthgatewayService testerDefault = null;
private AuthgatewayService tester = null;
protected static final boolean DEFAULTACTIVE = true;
protected static final String DEFAULTHOST = "testhost";
protected static final int DEFAULTPORT = 389;
protected static final boolean DEFAULTSSL = false;
protected static final String DEFAULTBASEDN = "dc\\=test,dc\\=corp";
protected static final String DEFAULTUSERDN = "cn\\=testuser,ou\\=service,dc\\=test,dc\\=corp";
protected static final String DEFAULTUSERPASSWORD = "testpassword";
protected static final String DEFAULTSEARCHBASE = "ou\\=searchBase";
protected static final String DEFAULTLDAPATTRIBUTE = "sAMAccountID";
protected static final boolean DEFAULTSEARCHSUBTREE = true;
@Before
public void initializeDefault()
{
testerDefault = new AuthgatewayService( DEFAULTACTIVE , DEFAULTHOST , DEFAULTPORT , DEFAULTSSL , DEFAULTBASEDN , DEFAULTUSERDN , DEFAULTUSERPASSWORD ,
DEFAULTSEARCHBASE , DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
}
@Test
public void testAuthgatewayService()
{
assertEquals( DEFAULTACTIVE , testerDefault.isActive() );
String expected = expectedGetString( "" , DEFAULTHOST , DEFAULTPORT , DEFAULTBASEDN , DEFAULTUSERDN , DEFAULTUSERPASSWORD , DEFAULTSEARCHBASE ,
DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
assertEquals( expected , testerDefault.getString( "" ) );
String expectedHTML = expectedHTMLForm( DEFAULTHOST , DEFAULTPORT , DEFAULTBASEDN , DEFAULTUSERDN , DEFAULTUSERPASSWORD , DEFAULTSEARCHBASE ,
DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
assertEquals( expectedHTML , testerDefault.getHTMLForm() );
}
@Test
public void testAuthgatewayServiceHostNull()
{
try
{
new AuthgatewayService( DEFAULTACTIVE , null , DEFAULTPORT , DEFAULTSSL , DEFAULTBASEDN , DEFAULTUSERDN , DEFAULTUSERPASSWORD , DEFAULTSEARCHBASE ,
DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Should throw IllegalArgumentException" );
}
@Test
public void testAuthgatewayServiceHostEmpty()
{
try
{
new AuthgatewayService( DEFAULTACTIVE , "" , DEFAULTPORT , DEFAULTSSL , DEFAULTBASEDN , DEFAULTUSERDN , DEFAULTUSERPASSWORD , DEFAULTSEARCHBASE ,
DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Should throw IllegalArgumentException" );
}
@Test
public void testAuthgatewayServiceHostContainsWhitespace()
{
try
{
tester = new AuthgatewayService( DEFAULTACTIVE , "test space" , DEFAULTPORT , DEFAULTSSL , DEFAULTBASEDN , DEFAULTUSERDN , DEFAULTUSERPASSWORD ,
DEFAULTSEARCHBASE , DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Should throw IllegalArgumentException" );
}
@Test
public void testAuthgatewayServiceBaseDNNull()
{
try
{
tester = new AuthgatewayService( DEFAULTACTIVE , DEFAULTHOST , DEFAULTPORT , DEFAULTSSL , null , DEFAULTUSERDN , DEFAULTUSERPASSWORD , DEFAULTSEARCHBASE ,
DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Should throw IllegalArgumentException" );
}
@Test
public void testAuthgatewayServiceUserDNNull()
{
try
{
new AuthgatewayService( DEFAULTACTIVE , DEFAULTHOST , DEFAULTPORT , DEFAULTSSL , DEFAULTBASEDN , null , DEFAULTUSERPASSWORD , DEFAULTSEARCHBASE ,
DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Should throw IllegalArgumentException" );
}
@Test
public void testAuthgatewayServiceUserPasswordNull()
{
try
{
new AuthgatewayService( DEFAULTACTIVE , DEFAULTHOST , DEFAULTPORT , DEFAULTSSL , DEFAULTBASEDN , DEFAULTUSERDN , null , DEFAULTSEARCHBASE ,
DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Should throw IllegalArgumentException" );
}
@Test
public void testAuthgatewayServiceSearchBaseNull()
{
try
{
new AuthgatewayService( DEFAULTACTIVE , DEFAULTHOST , DEFAULTPORT , DEFAULTSSL , DEFAULTBASEDN , DEFAULTUSERDN , DEFAULTUSERPASSWORD , null ,
DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Should throw IllegalArgumentException" );
}
@Test
public void testAuthgatewayServiceLDAPAttributeNull()
{
try
{
new AuthgatewayService( DEFAULTACTIVE , DEFAULTHOST , DEFAULTPORT , DEFAULTSSL , DEFAULTBASEDN , DEFAULTUSERDN , DEFAULTUSERPASSWORD , DEFAULTSEARCHBASE ,
null , DEFAULTSEARCHSUBTREE );
}catch( IllegalArgumentException iae )
{
return;
}
fail( "Should throw IllegalArgumentException" );
}
@Test
public void testIsActive()
{
tester = new AuthgatewayService( true , DEFAULTHOST , DEFAULTPORT , DEFAULTSSL , DEFAULTBASEDN , DEFAULTUSERDN , DEFAULTUSERPASSWORD , DEFAULTSEARCHBASE ,
DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
assertTrue( testerDefault.isActive() );
tester = new AuthgatewayService( false , DEFAULTHOST , DEFAULTPORT , DEFAULTSSL , DEFAULTBASEDN , DEFAULTUSERDN , DEFAULTUSERPASSWORD , DEFAULTSEARCHBASE ,
DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
assertFalse( tester.isActive() );
}
@Test
public void testServiceAsString()
{
String expected = expectedGetString( "" , DEFAULTHOST , DEFAULTPORT , DEFAULTBASEDN , DEFAULTUSERDN , DEFAULTUSERPASSWORD , DEFAULTSEARCHBASE ,
DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
assertEquals( expected , testerDefault.getString( "" ) );
}
@Test
public void testServiceAsStringWithHeader()
{
String expected = expectedGetString( "header" , DEFAULTHOST , DEFAULTPORT , DEFAULTBASEDN , DEFAULTUSERDN , DEFAULTUSERPASSWORD , DEFAULTSEARCHBASE ,
DEFAULTLDAPATTRIBUTE , DEFAULTSEARCHSUBTREE );
assertEquals( expected , testerDefault.getString( "header" ) );
}
// TODO: Active, SSL
private String expectedGetString( String header , String host , int port , String baseDN , String userDN , String userDNPassword , String searchBase ,
String ldapUserAttribute , boolean searchSubtree )
{
String newline = System.getProperty( "line.separator" );
StringBuilder strb = new StringBuilder();
strb.append( header + AuthgatewayService.PARAMACTIVE + "=" + DEFAULTACTIVE + newline );
strb.append( header + AuthgatewayService.PARAMHOST + "=" + host + newline );
strb.append( header + AuthgatewayService.PARAMPORT + "=" + port + newline );
strb.append( header + AuthgatewayService.PARAMSSL + "=" + DEFAULTSSL + newline );
strb.append( header + AuthgatewayService.PARAMBASEDN + "=" + baseDN + newline );
strb.append( header + AuthgatewayService.PARAMUSERDN + "=" + userDN + newline );
strb.append( header + AuthgatewayService.PARAMUSERDNPASSWORD + "=" + userDNPassword + newline );
strb.append( header + AuthgatewayService.PARAMSEARCHBASE + "=" + searchBase + newline );
strb.append( header + AuthgatewayService.PARAMLDAPUSERATTRIBUTE + "=" + ldapUserAttribute + newline );
strb.append( header + AuthgatewayService.PARAMSEARCHSUBTREE + "=" + searchSubtree + newline );
return strb.toString();
}
protected String expectedHTMLForm( String host , int port , String baseDN , String userDN , String userDNPassword , String searchBase ,
String ldapUserAttribute , boolean searchSubtree )
{
StringBuilder strb = new StringBuilder();
String newline = "<br /><br />" + System.getProperty( "line.separator" );
String space = " ";
strb.append( "<label>AD/LDAP Server Address: </label>" + space + space + " <input type='text' name='" + AuthgatewayService.PARAMHOST
+ "' size='40' value='" + StringUtils.safeHTML( host ) + "' /> " + newline );
strb.append( "<label>AD/LDAP Port: </label> " );
strb.append( "<input type='radio' name='" + AuthgatewayService.PARAMPORT + "' value='389' " );
if( port == 389 )
{
strb.append( "checked='checked'" );
}
strb.append( " /> <label>389</label>" );
strb.append( "<input type='radio' name='" + AuthgatewayService.PARAMPORT + "' value='636' " );
if( port == 636 )
{
strb.append( "checked='checked'" );
}
strb.append( " /> <label> 636 </label> " + newline );
// SSL true false is handled in Controller depending if port is 389 or 636
strb.append( "<label>AD/LDAP Base DN: </label>" + space + space + "<input type='text' id='" + AuthgatewayService.PARAMBASEDN + "' name='" + AuthgatewayService.PARAMBASEDN + "' size='40' value='"
+ StringUtils.safeHTML( baseDN ) + "' /> " + newline );
strb.append( "<label>AD/LDAP Binding User DN: </label>" + space + space + "<input type='text' id='" + AuthgatewayService.PARAMUSERDN + "' name='" + AuthgatewayService.PARAMUSERDN
+ "' size='80' value='" + StringUtils.safeHTML( userDN ) + "' /> " + newline );
strb.append( "<label>AD/LDAP Binding User password: </label>" + space + space + "<input type='password' id='" + AuthgatewayService.PARAMUSERDNPASSWORD + "' name='" + AuthgatewayService.PARAMUSERDNPASSWORD
+ "' size='40' /> " + newline );
strb.append( "<label>AD/LDAP Search Base (if left empty it will use the BaseDN): </label>" + space + space + "<input type='text' id='" + AuthgatewayService.PARAMSEARCHBASE + "' name='"
+ AuthgatewayService.PARAMSEARCHBASE + "' size='80' value='" + StringUtils.safeHTML( searchBase ) + "' /> " + newline );
strb.append( "<label>Search Attribute (AD = sAMAccountName , LDAP = uid) </label> " );
strb.append( "<input type='radio' name='" + AuthgatewayService.PARAMLDAPUSERATTRIBUTE + "' value='sAMAccountName' " );
if( "sAMAccountName".equals( ldapUserAttribute ) )
{
strb.append( "checked='checked'" );
}
strb.append( " /> <label>sAMAccountName</label>" );
strb.append( "<input type='radio' name='" + AuthgatewayService.PARAMLDAPUSERATTRIBUTE + "' value='uid' " );
if( "uid".equals( ldapUserAttribute ) )
{
strb.append( "checked='checked'" );
}
strb.append( " /> <label> uid </label> " + newline );
strb.append( "<label>AD/LDAP Search Subtree: </label><input name='" + AuthgatewayService.PARAMSEARCHSUBTREE + "' type='checkbox' " );
if( searchSubtree )
{
strb.append( " checked='checked' " );
}
strb.append( " /> " + newline );
return strb.toString();
}
} // end class