Testing pages protected by an admin role
cdunphy Sep 3, 2009 8:03 PMI am having all kinds of fun with the powerful integration testing framework that comes with Seam. I have one problem that I am hoping to resolve.
I have the following directive in my global pages.xml file to protect access to the admin interface:
<page view-id="/admin/*">
<restrict>#{s:hasRole('admin')}</restrict>
</page>This works well and I am thus able to protect the admin interface nicely. However, it is breaking some tests. Here is one example. The test to successfully create a user works, but the test where validation should fail because of a missing field doesn't work, and it is because of the role issue. I have confirmed that if I disable the restriction in pages.xml that both tests pass.
Interestingly, the it is NOT choking on the line (in the login FacesRequest):
assert (Boolean) getValue("#{identity.hasRole('admin')}");It is choking in that it says validation passes for the processValidation() phase in the createUserFailTest1() test. That should NOT be the case as I have clearly disabled a required field. Again, if I remove the role restriction the validation fails as expected.
Any idea how I can enhance the test below so that it will work for both tests? If I can solve this one hurdle I am off to the races! This is VERY cool stuff.
// Chris
public class CreateUserTest extends DBUnitSeamTest {
static final String REAL_NAME = "Barney Purple Dinosaur";
static final String USER_NAME = "barney";
static final String EMAIL = "barney.dinosair@fake.com";
static final boolean ADMIN_FLAG = true;
@Override
protected void prepareDBUnitOperations() {
beforeTestOperations.add(new DataSetOperation("mockdata.xml"));
}
/**
* <p>
* This should fail in the process validation phase because we are omitting
* a required field.
* </p>
*
* @throws Exception
*/
@Test
public void createUserFailTest1() throws Exception {
new FacesRequest("/login.xhtml") {
@Override
protected void invokeApplication() {
setValue("#{credentials.username}",
AuthenticatorTest.TEST_ADMIN_USER_1);
setValue("#{credentials.password}",
AuthenticatorTest.TEST_ADMIN_PASSWORD_1);
invokeMethod("#{authenticator.authenticate}");
// The user has admin permissions!
assert (Boolean) getValue("#{identity.hasRole('admin')}");
}
}.run();
new FacesRequest("/admin/UserEdit.xhtml") {
EscalationRoleList escalationRoleList;
@Override
protected void processValidations() throws Exception {
escalationRoleList = (EscalationRoleList) Component
.getInstance("escalationRoleList");
validateValue("#{user.role}", escalationRoleList
.getResultList().get(0));
validateValue("#{user.admin}", ADMIN_FLAG);
validateValue("#{user.realname}", REAL_NAME);
// This should break validation
// validateValue("#{user.username}", USER_NAME);
validateValue("#{user.email}", EMAIL);
assert isValidationFailure();
}
}.run();
}
@Test
public void createUserTest() throws Exception {
new FacesRequest("/login.xhtml") {
@Override
protected void invokeApplication() {
setValue("#{credentials.username}",
AuthenticatorTest.TEST_ADMIN_USER_1);
setValue("#{credentials.password}",
AuthenticatorTest.TEST_ADMIN_PASSWORD_1);
invokeMethod("#{authenticator.authenticate}");
}
}.run();
new FacesRequest("/admin/UserEdit.xhtml") {
EscalationRoleList escalationRoleList;
@Override
protected void processValidations() throws Exception {
escalationRoleList = (EscalationRoleList) Component
.getInstance("escalationRoleList");
validateValue("#{user.role}", escalationRoleList
.getResultList().get(0));
validateValue("#{user.admin}", ADMIN_FLAG);
validateValue("#{user.realname}", REAL_NAME);
validateValue("#{user.username}", USER_NAME);
validateValue("#{user.email}", EMAIL);
assert !isValidationFailure();
}
@Override
protected void updateModelValues() throws Exception {
setValue("#{user.role}", escalationRoleList.getResultList()
.get(0));
setValue("#{user.admin}", ADMIN_FLAG);
setValue("#{user.realname}", REAL_NAME);
setValue("#{user.username}", USER_NAME);
setValue("#{user.email}", EMAIL);
}
@Override
protected void invokeApplication() {
invokeMethod("#{userHome.persist}");
}
@Override
protected void renderResponse() {
// Let's make sure that this user got saved.
assert (Boolean) getValue("#{userHome.managed}");
assert (Boolean) getValue("#{user.admin}");
assert getValue("#{user.realname}").equals(REAL_NAME);
assert getValue("#{user.username}").equals(USER_NAME);
assert getValue("#{user.role.label}").equals(
escalationRoleList.getResultList().get(0).getLabel());
}
}.run();
}
}