package org.appfuse.webapp.action;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletResponse;
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import org.apache.cactus.WebResponse;
import org.apache.cactus.client.authentication.FormAuthentication;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.Constants;
public class LoginServletTest extends ServletTestCase {
private static ResourceBundle rb = null;
private Log log = LogFactory.getLog(LoginServletTest.class);
private LoginServlet servlet = null;
public LoginServletTest(String name) {
super(name);
rb = ResourceBundle.getBundle(this.getClass().getName());
}
public LoginServlet createInstance() throws Exception {
return new LoginServlet();
}
protected void setUp() throws Exception {
super.setUp();
servlet = new LoginServlet();
config.setInitParameter(Constants.AUTH_URL, rb.getString("authURL"));
config.setInitParameter(Constants.HTTP_PORT, rb.getString("httpPort"));
config.setInitParameter(Constants.HTTPS_PORT, rb.getString("httpsPort"));
config.setInitParameter("isSecure", rb.getString("isSecure"));
config.setInitParameter(Constants.ENC_ALGORITHM,
rb.getString("algorithm"));
servlet.init(config);
}
protected void tearDown() throws Exception {
servlet = null;
super.tearDown();
}
public void testInit() throws Exception {
String authURL = config.getInitParameter(Constants.AUTH_URL);
assertTrue((authURL != null) &&
authURL.equals(rb.getString("authURL")));
Map config = (HashMap) servlet.getServletContext().getAttribute(Constants.CONFIG);
assertEquals((String) config.get(Constants.HTTP_PORT),
rb.getString("httpPort"));
assertEquals((String) config.get(Constants.HTTPS_PORT),
rb.getString("httpsPort"));
assertEquals((Boolean) config.get(Constants.SECURE_LOGIN),
Boolean.valueOf(rb.getString("isSecure")));
assertEquals((String) config.get(Constants.ENC_ALGORITHM),
rb.getString("algorithm"));
}
public void beginFormAuthentication(WebRequest theRequest) {
theRequest.setRedirectorName("ServletRedirectorSecure");
theRequest.setAuthentication(new FormAuthentication(rb.getString("username"),
rb.getString("encryptedPassword")));
}
public void testFormAuthentication() {
assertEquals(rb.getString("username"),
request.getUserPrincipal().getName());
assertEquals(rb.getString("username"), request.getRemoteUser());
assertTrue("User not in '" + rb.getString("role") + "' role",
request.isUserInRole(rb.getString("role")));
}
public void beginExecute(WebRequest wRequest) {
wRequest.addParameter("j_username", rb.getString("username"));
wRequest.addParameter("j_password", rb.getString("password"));
}
public void testExecute() throws Exception {
servlet.execute(request, response);
}
public void endExecute(WebResponse response) {
assertTrue(response.getStatusCode() ==
HttpServletResponse.SC_MOVED_TEMPORARILY);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(LoginServletTest.class);
}
}