#1034 Create permissions for debug sections
- Add an individual permission for each debug section (including wildcard perm) - Create test for DebugCommand - Refactor tests for the enum permission nodes to use the same abstract class - Update related project files (plugin.yml, permission docs, command docs)
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package fr.xephi.authme.permission;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Has common tests for enums implementing {@link PermissionNode}.
|
||||
*/
|
||||
public abstract class AbstractPermissionsEnumTest {
|
||||
|
||||
@Test
|
||||
public void shouldAllStartWitRequiredPrefix() {
|
||||
// given
|
||||
String requiredPrefix = getRequiredPrefix();
|
||||
|
||||
// when/then
|
||||
for (PermissionNode permission : getPermissionNodes()) {
|
||||
if (!permission.getNode().startsWith(requiredPrefix)) {
|
||||
fail("The permission '" + permission + "' does not start with the required prefix '"
|
||||
+ requiredPrefix + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveUniqueNodes() {
|
||||
// given
|
||||
Set<String> nodes = new HashSet<>();
|
||||
|
||||
// when/then
|
||||
for (PermissionNode permission : getPermissionNodes()) {
|
||||
if (!nodes.add(permission.getNode())) {
|
||||
fail("More than one enum value defines the node '" + permission.getNode() + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the permission nodes to test
|
||||
*/
|
||||
protected abstract PermissionNode[] getPermissionNodes();
|
||||
|
||||
/**
|
||||
* @return text with which all permission nodes must start with
|
||||
*/
|
||||
protected abstract String getRequiredPrefix();
|
||||
|
||||
}
|
||||
@@ -1,42 +1,18 @@
|
||||
package fr.xephi.authme.permission;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Test for {@link AdminPermission}.
|
||||
*/
|
||||
public class AdminPermissionTest {
|
||||
public class AdminPermissionTest extends AbstractPermissionsEnumTest {
|
||||
|
||||
@Test
|
||||
public void shouldStartWithAuthMeAdminPrefix() {
|
||||
// given
|
||||
String requiredPrefix = "authme.admin.";
|
||||
|
||||
// when/then
|
||||
for (AdminPermission permission : AdminPermission.values()) {
|
||||
if (!permission.getNode().startsWith(requiredPrefix)) {
|
||||
fail("The permission '" + permission + "' does not start with the required prefix '"
|
||||
+ requiredPrefix + "'");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected PermissionNode[] getPermissionNodes() {
|
||||
return AdminPermission.values();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveUniqueNodes() {
|
||||
// given
|
||||
Set<String> nodes = new HashSet<>();
|
||||
|
||||
// when/then
|
||||
for (AdminPermission permission : AdminPermission.values()) {
|
||||
if (!nodes.add(permission.getNode())) {
|
||||
fail("More than one enum value defines the node '" + permission.getNode() + "'");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected String getRequiredPrefix() {
|
||||
return "authme.admin.";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package fr.xephi.authme.permission;
|
||||
|
||||
/**
|
||||
* Test for {@link DebugSectionPermissions}.
|
||||
*/
|
||||
public class DebugSectionPermissionsTest extends AbstractPermissionsEnumTest {
|
||||
|
||||
@Override
|
||||
protected PermissionNode[] getPermissionNodes() {
|
||||
return DebugSectionPermissions.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRequiredPrefix() {
|
||||
return "authme.debug.";
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ public class PermissionConsistencyTest {
|
||||
|
||||
/** Wildcard permissions (present in plugin.yml but not in the codebase). */
|
||||
private static final Set<String> PLUGIN_YML_PERMISSIONS_WILDCARDS =
|
||||
ImmutableSet.of("authme.admin.*", "authme.player.*", "authme.player.email");
|
||||
ImmutableSet.of("authme.admin.*", "authme.player.*", "authme.player.email", "authme.debug");
|
||||
|
||||
/** Name of the fields that make up a permission entry in plugin.yml. */
|
||||
private static final Set<String> PERMISSION_FIELDS = ImmutableSet.of("description", "default", "children");
|
||||
|
||||
@@ -1,41 +1,17 @@
|
||||
package fr.xephi.authme.permission;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Test for {@link PlayerPermission}.
|
||||
*/
|
||||
public class PlayerPermissionTest {
|
||||
public class PlayerPermissionTest extends AbstractPermissionsEnumTest {
|
||||
|
||||
@Test
|
||||
public void shouldStartWithPlayerPrefix() {
|
||||
// given
|
||||
String playerBranch = "authme.player.";
|
||||
|
||||
// when/then
|
||||
for (PlayerPermission permission : PlayerPermission.values()) {
|
||||
if (!permission.getNode().startsWith(playerBranch)) {
|
||||
fail("The permission '" + permission + "' should use a node with the player-specific branch '"
|
||||
+ playerBranch + "'");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected PermissionNode[] getPermissionNodes() {
|
||||
return PlayerPermission.values();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveUniqueNodes() {
|
||||
// given
|
||||
Set<String> nodes = new HashSet<>();
|
||||
|
||||
// when/then
|
||||
for (PlayerPermission permission : PlayerPermission.values()) {
|
||||
if (!nodes.add(permission.getNode())) {
|
||||
fail("More than one enum value defines the node '" + permission.getNode() + "'");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected String getRequiredPrefix() {
|
||||
return "authme.player.";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package fr.xephi.authme.permission;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.google.common.collect.Sets.newHashSet;
|
||||
@@ -11,39 +11,32 @@ import static org.junit.Assert.fail;
|
||||
/**
|
||||
* Test for {@link PlayerStatePermission}.
|
||||
*/
|
||||
public class PlayerStatePermissionTest {
|
||||
public class PlayerStatePermissionTest extends AbstractPermissionsEnumTest {
|
||||
|
||||
@Test
|
||||
public void shouldStartWithAuthMeAdminPrefix() {
|
||||
public void shouldNotStartWithOtherPrefixes() {
|
||||
// given
|
||||
String requiredPrefix = "authme.";
|
||||
Set<String> forbiddenPrefixes = newHashSet("authme.player", "authme.admin");
|
||||
Set<String> forbiddenPrefixes = newHashSet("authme.player", "authme.admin", "authme.debug");
|
||||
|
||||
// when/then
|
||||
for (PlayerStatePermission permission : PlayerStatePermission.values()) {
|
||||
if (!permission.getNode().startsWith(requiredPrefix)) {
|
||||
fail("The permission '" + permission + "' does not start with the required prefix '"
|
||||
+ requiredPrefix + "'");
|
||||
} else if (hasAnyPrefix(permission.getNode(), forbiddenPrefixes)) {
|
||||
if (startsWithAny(permission.getNode(), forbiddenPrefixes)) {
|
||||
fail("The permission '" + permission + "' should not start with any of " + forbiddenPrefixes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveUniqueNodes() {
|
||||
// given
|
||||
Set<String> nodes = new HashSet<>();
|
||||
|
||||
// when/then
|
||||
for (PlayerStatePermission permission : PlayerStatePermission.values()) {
|
||||
if (!nodes.add(permission.getNode())) {
|
||||
fail("More than one enum value defines the node '" + permission.getNode() + "'");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected PermissionNode[] getPermissionNodes() {
|
||||
return PlayerStatePermission.values();
|
||||
}
|
||||
|
||||
private static boolean hasAnyPrefix(String node, Set<String> prefixes) {
|
||||
@Override
|
||||
protected String getRequiredPrefix() {
|
||||
return "authme.";
|
||||
}
|
||||
|
||||
private static boolean startsWithAny(String node, Collection<String> prefixes) {
|
||||
for (String prefix : prefixes) {
|
||||
if (node.startsWith(prefix)) {
|
||||
return true;
|
||||
@@ -51,5 +44,4 @@ public class PlayerStatePermissionTest {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user