Uploaded image for project: 'Click'
  1. Click
  2. CLK-585

Implement mock support for Principal and Roles

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.1.0
    • 2.2.0
    • mock
    • None

    Description

      Currently there is no way to provide a Principal (user) and Roles for mock tests.

      Sven Pfeiffer provided the following impl on the mainlig list which can be added to the Mock API:

      ====================

      public class AuthenticatedMockRequest extends MockRequest {

      MockPrincipal userPrincipal;

      public AuthenticatedMockRequest(MockPrincipal userPrincipal)

      { this.userPrincipal = userPrincipal; }

      @Override
      public boolean isUserInRole(String name)

      { return userPrincipal.getRoles().contains(name); }

      @Override
      public Principal getUserPrincipal()

      { return userPrincipal; }

      }
      and

      MockPrincipal:
      public class MockPrincipal implements Principal {

      private String name;
      private List<String> roles = new ArrayList<String>();

      public MockPrincipal(String userName)

      { this.name = userName; }

      public MockPrincipal(String userName, List<String> roles)

      { this.name = userName; this.roles = roles; }

      @Override
      public String getName()

      { return name; }

      public List<String> getRoles()

      { return roles; }

      public void setRoles(String... newRoles) {
      roles.clear();
      for (String role : newRoles)

      { roles.add(role); }

      }
      }

      And I added the following lines to my testcase:

      MockPrincipal principal = new MockPrincipal("admin");
      principal.setRoles("admin", "user");
      container.setRequest(new AuthenticatedMockRequest(principal));

      Attachments

        Activity

          People

            sabob Bob Schellink
            sabob Bob Schellink
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: