1. Sebastian Sdorra
  2. scm-manager

Commits

Sebastian Sdorra  committed 102e443

start implementation of permission tests for ScmRealm

  • Participants
  • Parent commits b3b219d
  • Branches default

Comments (0)

Files changed (2)

File scm-webapp/src/main/java/sonia/scm/security/ScmRealm.java

View file
  • Ignore whitespace
    * @param cacheManager
    * @param userManager
    * @param groupManager
-   * @param repositoryManager
    * @param repositoryDAO
    * @param userDAO
    * @param authenticator
   @Inject
   public ScmRealm(ScmConfiguration configuration, CacheManager cacheManager,
     UserManager userManager, GroupManager groupManager,
-    RepositoryManager repositoryManager, RepositoryDAO repositoryDAO,
-    UserDAO userDAO, AuthenticationManager authenticator,
+    RepositoryDAO repositoryDAO, UserDAO userDAO,
+    AuthenticationManager authenticator,
     Provider<HttpServletRequest> requestProvider,
     Provider<HttpServletResponse> responseProvider)
   {

File scm-webapp/src/test/java/sonia/scm/security/ScmRealmTest.java

View file
  • Ignore whitespace
 import org.apache.shiro.authc.UnknownAccountException;
 import org.apache.shiro.authc.UsernamePasswordToken;
 import org.apache.shiro.authz.AuthorizationInfo;
+import org.apache.shiro.authz.Permission;
 import org.apache.shiro.subject.PrincipalCollection;
 
 import org.junit.Test;
 
 import sonia.scm.cache.MapCacheManager;
 import sonia.scm.config.ScmConfiguration;
+import sonia.scm.group.Group;
 import sonia.scm.group.GroupManager;
 import sonia.scm.group.GroupNames;
+import sonia.scm.repository.PermissionType;
+import sonia.scm.repository.Repository;
 import sonia.scm.repository.RepositoryDAO;
-import sonia.scm.repository.RepositoryManager;
 import sonia.scm.user.User;
 import sonia.scm.user.UserDAO;
 import sonia.scm.user.UserManager;
    *
    */
   @Test
+  public void testAuthorizationAdminPermissions()
+  {
+    User trillian = createSampleUser();
+
+    trillian.setAdmin(true);
+
+    AuthorizationInfo ai = authorizationInfo(trillian);
+    Collection<Permission> permissions = ai.getObjectPermissions();
+
+    assertNotNull(permissions);
+    assertFalse(permissions.isEmpty());
+    assertEquals(1, permissions.size());
+    //J-
+    assertTrue(
+      permissions.contains(new RepositoryPermission("*", PermissionType.OWNER))
+    );
+    //J+
+  }
+
+  /**
+   * Method description
+   *
+   */
+  @Test
   public void testAuthorizationAdminRoles()
   {
     User trillian = createSampleUser();
   }
 
   /**
+   *  Method description
+   *
+   */
+  @Test
+  public void testAuthorizationDefaultUserPermissions()
+  {
+    User trillian = createSampleUser();
+
+    AuthorizationInfo ai = authorizationInfo(trillian);
+    Collection<Permission> permissions = ai.getObjectPermissions();
+
+    assertNotNull(permissions);
+    assertTrue(permissions.isEmpty());
+  }
+
+  /**
    * Method description
    *
    */
   {
     User trillian = createSampleUser();
 
-    ScmRealm realm = createRealm(trillian, ImmutableSet.of("g1", "g2"));
+    //J-
+    ScmRealm realm = createRealm(
+      trillian, 
+      ImmutableSet.of("g1", "g2"), 
+      ImmutableSet.of(new Group("xml", "g3"), new Group("xml", "g4")),
+      null
+    );
+    //J+
     AuthenticationInfo ai = realm.getAuthenticationInfo(token(trillian));
 
     assertNotNull(ai);
 
     assertNotNull(groups);
     assertFalse(groups.getCollection().isEmpty());
-    assertThat(groups, containsInAnyOrder("g1", "g2"));
+    assertEquals(4, groups.getCollection().size());
+    assertThat(groups, containsInAnyOrder("g1", "g2", "g3", "g4"));
   }
 
   /**
    */
   private ScmRealm createRealm(User user)
   {
-    return createRealm(user, null);
+    return createRealm(user, null, null, null);
   }
 
   /**
    *
    *
    * @param user
-   * @param groups
+   * @param authenticationGroups
+   * @param dbGroups
+   * @param repositories
    * @return
    */
-  private ScmRealm createRealm(User user, Collection<String> groups)
+  private ScmRealm createRealm(User user,
+    Collection<String> authenticationGroups, Collection<Group> dbGroups,
+    Collection<Repository> repositories)
   {
     UserManager userManager = mock(UserManager.class);
     GroupManager groupManager = mock(GroupManager.class);
-    RepositoryManager repositoryManager = mock(RepositoryManager.class);
+
+    if (dbGroups != null)
+    {
+      when(groupManager.getGroupsForMember(user.getId())).thenReturn(dbGroups);
+    }
+
     RepositoryDAO repositoryDAO = mock(RepositoryDAO.class);
+
+    if (repositories != null)
+    {
+      when(repositoryDAO.getAll()).thenReturn(repositories);
+    }
+
     UserDAO userDAO = mock(UserDAO.class);
 
     when(userDAO.get(user.getId())).thenReturn(user);
         eq(user.getPassword())
       ) 
     ).thenReturn( 
-      new AuthenticationResult(user, groups, AuthenticationState.SUCCESS) 
+      new AuthenticationResult(user, authenticationGroups, AuthenticationState.SUCCESS) 
     );
     
     when( 
       new MapCacheManager(),
       userManager,
       groupManager,
-      repositoryManager,
       repositoryDAO,
       userDAO,
       authManager,