Seam 1.2.1.GA Integration Testing (identity.login)
mhaubrich May 8, 2007 1:55 PMHi,
I have a question about the integration testing of the login process with the 1.2.1 seam version and I hope someone can help me here as I've been trying around for quite some time now on my own to no result...
Ok... I have the following JSF view: (extract)
<h:form rendered="#{not identity.loggedIn}"> #{messages['login.form']} <br /> <span class="errors"> <h:messages globalOnly="true" /> </span> <table cellspacing="2" cellpadding="2" border="0" > <tr> <td>#{messages['login.login']}:</td><td><h:inputText value="#{identity.username}" id="login" /></td> </tr> <tr> <td>#{messages['login.pwd']}:</td><td><h:inputSecret value="#{identity.password}" id="password" /></td> </tr> <tr> <td colspan="2" align="right"><h:commandButton value="#{messages['login.button']}" action="#{identity.login}" /></td> </tr> </table> </h:form>
In components.xml I have defined my authentication method as follows:
<security:identity authenticate-method="#{authenticator.authenticate}"/>
My Authenticator interface and AuthenticatorAction class look like so:
import org.jboss.annotation.ejb.Local; @Local public interface Authenticator { public boolean authenticate(); }
import java.io.Serializable; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.JndiName; import org.jboss.seam.annotations.Logger; import org.jboss.seam.annotations.Name; import org.jboss.seam.contexts.Context; import org.jboss.seam.log.Log; import org.jboss.seam.security.Identity; @Name("authenticator") public class AuthenticatorAction implements Authenticator, Serializable { private static final long serialVersionUID = 4836686434984261542L; private static final String USER_VAR = "currentUser"; @In private Context sessionContext; @Logger private Log log; /** * validate the username and password */ public boolean authenticate() { User user = ServiceLocator.instance() // find user in database .getLoginService() .authenticate(Identity.instance().getUsername(), Identity.instance().getPassword()); if(user==null) { log.debug("invalid login!"); return false; } Identity.instance().addRole("user"); if(user.isAdmin()) // add admin role if the user is an admin Identity.instance().addRole("admin"); sessionContext.set(USER_VAR, user); // save in session context log.debug("login successful: #0", user); return true; } }
And finally my test class:
import static org.testng.AssertJUnit.*; import org.jboss.seam.log.Log; import org.jboss.seam.log.Logging; import org.jboss.seam.mock.SeamTest; import org.testng.annotations.Configuration; import org.testng.annotations.Test; public class AuthenticatorActionTest extends SeamTest { @Test public void testAuthenticate() throws Exception { new FacesRequest() { @Override protected void updateModelValues() throws Exception { assertFalse("session should be valid", isSessionInvalid()); // assert that the session is NOT invalid setValue("#{identity.username}", "test"); // inject the login values setValue("#{identity.password}", "test"); } @Override protected void invokeApplication() { invokeMethod("#{identity.login}"); } @Override protected void renderResponse() { assertTrue("identity.loggedIn", getValue("#{identity.loggedIn}").equals(true)); // make sure that we are logged in assertEquals("view id", "/home.xhtml", getViewId() ); // assert that the home-page is being shown } }.run(); } }
Now when I run the Test it fails at the
assertTrue("identity.loggedIn", getValue("#{identity.loggedIn}").equals(true));part !
Am I missing something here ?!
Thanks for you help !
Maarten