mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 23:45:44 +01:00
Enhance unit test for PermissionUtil to cover more group permission scenarios.
Reduce duplication of code in Permission constructors.
This commit is contained in:
@@ -69,6 +69,7 @@ public class Permission implements Serializable
|
|||||||
*/
|
*/
|
||||||
public Permission(String name)
|
public Permission(String name)
|
||||||
{
|
{
|
||||||
|
this();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,7 +82,7 @@ public class Permission implements Serializable
|
|||||||
*/
|
*/
|
||||||
public Permission(String name, PermissionType type)
|
public Permission(String name, PermissionType type)
|
||||||
{
|
{
|
||||||
this.name = name;
|
this(name);
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,14 +91,13 @@ public class Permission implements Serializable
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param name
|
* @param name
|
||||||
* @param groupPermission
|
|
||||||
* @param type
|
* @param type
|
||||||
|
* @param groupPermission
|
||||||
*/
|
*/
|
||||||
public Permission(String name, boolean groupPermission, PermissionType type)
|
public Permission(String name, PermissionType type, boolean groupPermission)
|
||||||
{
|
{
|
||||||
this.name = name;
|
this(name, type);
|
||||||
this.groupPermission = groupPermission;
|
this.groupPermission = groupPermission;
|
||||||
this.type = type;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|||||||
@@ -118,21 +118,44 @@ public class PermissionUtilTest
|
|||||||
@Test
|
@Test
|
||||||
public void testGroupPermissions()
|
public void testGroupPermissions()
|
||||||
{
|
{
|
||||||
WebSecurityContext context = mockGroupCtx(new User("dent", "Arthur Dent",
|
WebSecurityContext dent = mockGroupCtx(new User("dent", "Arthur Dent",
|
||||||
"arthur.dent@hitchhiker.com"));
|
"arthur.dent@hitchhiker.com"),
|
||||||
|
"devel", "qa");
|
||||||
|
WebSecurityContext ford = mockGroupCtx(new User("ford", "Ford Prefect",
|
||||||
|
"ford.prefect@hitchhiker.com"), "devel");
|
||||||
|
WebSecurityContext zaphod = mockGroupCtx(new User("zaphod",
|
||||||
|
"Zaphod Beeblebrox",
|
||||||
|
"zaphod.beeblebrox@hitchhiker.com"), "qa");
|
||||||
|
WebSecurityContext trillian = mockGroupCtx(new User("trillian",
|
||||||
|
"Trillian Astra",
|
||||||
|
"trillian.astra@hitchhiker.com"));
|
||||||
Repository r = new Repository();
|
Repository r = new Repository();
|
||||||
|
|
||||||
r.setPermissions(
|
r.setPermissions(
|
||||||
new ArrayList<Permission>(
|
new ArrayList<Permission>(
|
||||||
Arrays.asList(
|
Arrays.asList(
|
||||||
new Permission("dent"),
|
new Permission("dent"),
|
||||||
new Permission("devel", true, PermissionType.READ),
|
new Permission("devel", PermissionType.WRITE, true),
|
||||||
new Permission("qa", true, PermissionType.WRITE))));
|
new Permission("qa", PermissionType.READ, true))));
|
||||||
assertTrue(PermissionUtil.hasPermission(r, context, PermissionType.READ));
|
// member of both devel and qa
|
||||||
assertTrue(PermissionUtil.hasPermission(r, context, PermissionType.WRITE));
|
assertTrue(PermissionUtil.hasPermission(r, dent, PermissionType.READ));
|
||||||
assertFalse(PermissionUtil.hasPermission(r, context, PermissionType.OWNER));
|
assertTrue(PermissionUtil.hasPermission(r, dent, PermissionType.WRITE));
|
||||||
|
assertFalse(PermissionUtil.hasPermission(r, dent, PermissionType.OWNER));
|
||||||
|
// now, additionally the owner
|
||||||
r.getPermissions().add(new Permission("dent", PermissionType.OWNER));
|
r.getPermissions().add(new Permission("dent", PermissionType.OWNER));
|
||||||
assertTrue(PermissionUtil.hasPermission(r, context, PermissionType.OWNER));
|
assertTrue(PermissionUtil.hasPermission(r, dent, PermissionType.OWNER));
|
||||||
|
// member of just devel
|
||||||
|
assertTrue(PermissionUtil.hasPermission(r, ford, PermissionType.READ));
|
||||||
|
assertTrue(PermissionUtil.hasPermission(r, ford, PermissionType.WRITE));
|
||||||
|
assertFalse(PermissionUtil.hasPermission(r, ford, PermissionType.OWNER));
|
||||||
|
// member of just qa
|
||||||
|
assertTrue(PermissionUtil.hasPermission(r, zaphod, PermissionType.READ));
|
||||||
|
assertFalse(PermissionUtil.hasPermission(r, zaphod, PermissionType.WRITE));
|
||||||
|
assertFalse(PermissionUtil.hasPermission(r, zaphod, PermissionType.OWNER));
|
||||||
|
// member of no groups
|
||||||
|
assertFalse(PermissionUtil.hasPermission(r, trillian, PermissionType.READ));
|
||||||
|
assertFalse(PermissionUtil.hasPermission(r, trillian, PermissionType.WRITE));
|
||||||
|
assertFalse(PermissionUtil.hasPermission(r, trillian, PermissionType.OWNER));
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
@@ -197,14 +220,12 @@ public class PermissionUtilTest
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private WebSecurityContext mockGroupCtx(User user)
|
private WebSecurityContext mockGroupCtx(User user, String... groups)
|
||||||
{
|
{
|
||||||
WebSecurityContext context = mockCtx(user);
|
WebSecurityContext context = mockCtx(user);
|
||||||
Set<String> groups = new HashSet<String>();
|
|
||||||
|
Set<String> groupSet = new HashSet<String>(Arrays.asList(groups));
|
||||||
groups.add("devel");
|
when(context.getGroups()).thenReturn(groupSet);
|
||||||
groups.add("qa");
|
|
||||||
when(context.getGroups()).thenReturn(groups);
|
|
||||||
|
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user