Perhaps we can add a "roles" property to the MockRequest then we can do something like this:

// Setup remote user by a new MockPrincipal class
Principal p = new MockPrincipal("test");
container.getRequest().setPrincipal(p);

// Setup roles
container.getRequest().setRoles("admin", "user");

And then isUserInRoles implementation can be simple:

  public boolean isUserInRoles(String role) {
    if (roles.contains(role)) {
      return true;
    } else {
      return false;
    }
  }


Or MockPrincipal can expose a method to set its own roles:

  public boolean isUserInRoles(String role) {
    if (getUserPrincipal().getRoles().contains(role)) {
      return true;
    } else {
      return false;
    }
  }

What do you think?

bob


Bob Schellink wrote:
Hi Sven,

You should be able to set the REMOTE_USER with:

  container.getRequest().addHeader("REMOTE_USER", "admin");

This won't get you past the unimplemented isUserInRole however. You could provide a custom MockRequest and set that on the Container before starting it e.g:

  container.setRequest(new MyMockRequest());
  container.start();

where MyMockRequest somehow implements isUserInRole. Haven't really though of a way to implement isUserInRole but maybe we can expose a way to setup Roles and Principals for the container which isUserInRole can test against?

kind regards

bob

Sven Pfeiffer wrote:

I'd like to use click-mock for testing my click web-application.

Several times I need to get the username and role from the request, which AFAIK are set by the SecurityRealm.

For example I display the users name on the top of the page using context.getRequest().getUserPrincipal().getName()

For security reasons I check the users role in onSecurityCheck() using
context.getRequest().isUserInRole(<requiredRole>)

Is the a way to inject Username/Role with the mock API?
In the moment I am trying the following, which results in a NPEX

MockContainer container = new MockContainer("C:/dev/projects/clock-mock-test/src/main/webapp");
container.start();
container.setParameter(Form.FORM_NAME, "filter");

//setting parameters for a FormTable
container.setParameter("filterForm_column_0", "entryDate");
container.setParameter("filterForm_operator_0", Operators.AFTER.name());
container.setParameter("filterForm_value_0", "01.07.2009");

//Thats where I get the NPEX
MyTestPage page = (MyTestPage) container.testPage(MyTestPage.class);

//The page forward to itself
assertTrue(MyTestPage.class.getName().equals(container.getForwardPageClass().getName()));

container.stop();

In the MockRequest sources I saw that
isUserInRole(String role) is not implemented yet and getUserPrincipal returns a Principal with name getHeader("REMOTE_USER"), how can I set REMOTE_USER?


Thanks in advance
SVen




Reply via email to