org.jboss.seam.security.Identity needs to be decoupled from system for testing purposes
---------------------------------------------------------------------------------------
Key: JBSEAM-3918
URL:
https://jira.jboss.org/jira/browse/JBSEAM-3918
Project: Seam
Issue Type: Feature Request
Components: Security
Affects Versions: 2.1.1.GA
Environment: JBoss Seam 2.1.1.GA
Reporter: Dan Hinojosa
org.jboss.seam.security.Identity cannot be used in unit testing. Unit testing being
defined as an isolated (no db, no app server) test. If I provide an identity into a test,
once I call assertTrue(identity.hasRole("xxx")); I get an understandable
NullPointerException because there is no Seam Context available.
If I may recommend that org.jboss.seam.security.Identity be either an interface or a POJO
with no dependencies as an API so TDD advocates can test their code easily. This would
also fulfill Seam's idea that everything for the end user is testable.
Example Test
@Test(groups = "unit")
public void testAuthenticateSuccessWithRoles() {
Log log = createMock(Log.class);
EntityManager entityManager = createMock(EntityManager.class);
Query query = createMock(Query.class);
Calendar createdDate = Calendar.getInstance();
Calendar updatedDate = Calendar.getInstance();
User user = new User();
user.setCreatedDate(createdDate);
user.setEmail("ricardo(a)aol.com");
user.setFirstName("Ricardo");
user.setLastName("Montalban");
user.setName("rmontalban");
user.setNotes("userNotes2");
user.setPassword("passw0rd");
user.setId(220L);
user.setUpdatedDate(updatedDate);
Role role = new Role();
role.setName("mgmt");
role.setId(665L);
Calendar createdDate2 = Calendar.getInstance();
createdDate2.set(2009, 1, 15, 12, 14, 15);
role.setCreatedDate(createdDate2);
Calendar updatedDate2 = Calendar.getInstance();
updatedDate2.set(2009, 1, 17, 13, 16, 20);
role.setUpdatedDate(updatedDate2);
role.setNotes("notes");
user.addRole(role);
role.addUser(user);
Credentials credentials = new Credentials();
credentials.setUsername("rmontalban");
credentials.setPassword("passw0rd");
Identity identity = new Identity();
log.info("authenticating #0", "rmontalban");
expect(entityManager.createQuery("SELECT u from User u " +
"WHERE u.name = #{credentials.username} AND u.password =
#{credentials.password}")).andReturn(query);
expect(query.getSingleResult()).andReturn(user);
replay(entityManager, query, log);
Authenticator authenticator = new Authenticator();
authenticator.setLog(log);
authenticator.setIdentity(identity);
authenticator.setEntityManager(entityManager);
authenticator.setCredentials(credentials);
assertTrue(authenticator.authenticate());
assertTrue(identity.hasRole("mgmt")); //NullPointerException here
verify(entityManager, query, log);
}
Output from the above test:
java.lang.NullPointerException
at org.jboss.seam.security.Identity.tryLogin(Identity.java:164)
at org.jboss.seam.security.Identity.hasRole(Identity.java:444)
at
com.manning.mymanning.AuthenticatorTest.testAuthenticateSuccessWithRoles(AuthenticatorTest.java:128)
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
https://jira.jboss.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira