Merge branch 'master' of https://github.com/AuthMe/AuthMeReloaded into limbo
# Conflicts: # src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java # src/test/java/fr/xephi/authme/settings/SettingsConsistencyTest.java
This commit is contained in:
@@ -4,24 +4,30 @@ import ch.jalu.injector.testing.BeforeInjecting;
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import com.google.common.base.Strings;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.permission.PlayerStatePermission;
|
||||
import fr.xephi.authme.service.ValidationService.ValidationResult;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.EmailSettings;
|
||||
import fr.xephi.authme.settings.properties.ProtectionSettings;
|
||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import fr.xephi.authme.service.ValidationService.ValidationResult;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
@@ -55,6 +61,7 @@ public class ValidationServiceTest {
|
||||
.willReturn(asList("unsafe", "other-unsafe"));
|
||||
given(settings.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).willReturn(3);
|
||||
given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(asList("name01", "npc"));
|
||||
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -115,8 +122,8 @@ public class ValidationServiceTest {
|
||||
@Test
|
||||
public void shouldAcceptEmailWithEmptyLists() {
|
||||
// given
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.emptyList());
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.emptyList());
|
||||
|
||||
// when
|
||||
boolean result = validationService.validateEmail("test@example.org");
|
||||
@@ -130,7 +137,7 @@ public class ValidationServiceTest {
|
||||
// given
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST))
|
||||
.willReturn(asList("domain.tld", "example.com"));
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.emptyList());
|
||||
|
||||
// when
|
||||
boolean result = validationService.validateEmail("TesT@Example.com");
|
||||
@@ -144,7 +151,7 @@ public class ValidationServiceTest {
|
||||
// given
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST))
|
||||
.willReturn(asList("domain.tld", "example.com"));
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.emptyList());
|
||||
|
||||
// when
|
||||
boolean result = validationService.validateEmail("email@other-domain.abc");
|
||||
@@ -156,7 +163,7 @@ public class ValidationServiceTest {
|
||||
@Test
|
||||
public void shouldAcceptEmailNotInBlacklist() {
|
||||
// given
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.emptyList());
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST))
|
||||
.willReturn(asList("Example.org", "a-test-name.tld"));
|
||||
|
||||
@@ -170,7 +177,7 @@ public class ValidationServiceTest {
|
||||
@Test
|
||||
public void shouldRejectEmailInBlacklist() {
|
||||
// given
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.emptyList());
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST))
|
||||
.willReturn(asList("Example.org", "a-test-name.tld"));
|
||||
|
||||
@@ -263,8 +270,8 @@ public class ValidationServiceTest {
|
||||
@Test
|
||||
public void shouldNotInvokeGeoLiteApiIfCountryListsAreEmpty() {
|
||||
// given
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.emptyList());
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.emptyList());
|
||||
|
||||
// when
|
||||
boolean result = validationService.isCountryAdmitted("addr");
|
||||
@@ -278,7 +285,7 @@ public class ValidationServiceTest {
|
||||
public void shouldAcceptCountryInWhitelist() {
|
||||
// given
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(asList("ch", "it"));
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.emptyList());
|
||||
String ip = "127.0.0.1";
|
||||
given(geoIpService.getCountryCode(ip)).willReturn("CH");
|
||||
|
||||
@@ -294,7 +301,7 @@ public class ValidationServiceTest {
|
||||
public void shouldRejectCountryMissingFromWhitelist() {
|
||||
// given
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(asList("ch", "it"));
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.emptyList());
|
||||
String ip = "123.45.67.89";
|
||||
given(geoIpService.getCountryCode(ip)).willReturn("BR");
|
||||
|
||||
@@ -309,7 +316,7 @@ public class ValidationServiceTest {
|
||||
@Test
|
||||
public void shouldAcceptCountryAbsentFromBlacklist() {
|
||||
// given
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.emptyList());
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(asList("ch", "it"));
|
||||
String ip = "127.0.0.1";
|
||||
given(geoIpService.getCountryCode(ip)).willReturn("BR");
|
||||
@@ -325,7 +332,7 @@ public class ValidationServiceTest {
|
||||
@Test
|
||||
public void shouldRejectCountryInBlacklist() {
|
||||
// given
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.emptyList());
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(asList("ch", "it"));
|
||||
String ip = "123.45.67.89";
|
||||
given(geoIpService.getCountryCode(ip)).willReturn("IT");
|
||||
@@ -338,6 +345,54 @@ public class ValidationServiceTest {
|
||||
verify(geoIpService).getCountryCode(ip);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCheckNameRestrictions() {
|
||||
// given
|
||||
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true);
|
||||
given(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
|
||||
.willReturn(Arrays.asList("Bobby;127.0.0.4", "Tamara;32.24.16.8"));
|
||||
validationService.reload();
|
||||
|
||||
Player bobby = mockPlayer("bobby", "127.0.0.4");
|
||||
Player tamara = mockPlayer("taMARA", "8.8.8.8");
|
||||
Player notRestricted = mockPlayer("notRestricted", "0.0.0.0");
|
||||
|
||||
// when
|
||||
boolean isBobbyAdmitted = validationService.fulfillsNameRestrictions(bobby);
|
||||
boolean isTamaraAdmitted = validationService.fulfillsNameRestrictions(tamara);
|
||||
boolean isNotRestrictedAdmitted = validationService.fulfillsNameRestrictions(notRestricted);
|
||||
|
||||
// then
|
||||
assertThat(isBobbyAdmitted, equalTo(true));
|
||||
assertThat(isTamaraAdmitted, equalTo(false));
|
||||
assertThat(isNotRestrictedAdmitted, equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLogWarningForInvalidRestrictionRule() {
|
||||
// given
|
||||
Logger logger = TestHelper.setupLogger();
|
||||
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true);
|
||||
given(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
|
||||
.willReturn(Arrays.asList("Bobby;127.0.0.4", "Tamara;"));
|
||||
|
||||
// when
|
||||
validationService.reload();
|
||||
|
||||
// then
|
||||
ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(logger).warning(stringCaptor.capture());
|
||||
assertThat(stringCaptor.getValue(), containsString("Tamara;"));
|
||||
}
|
||||
|
||||
private static Player mockPlayer(String name, String ip) {
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
given(player.getAddress().getHostName()).willReturn("--");
|
||||
return player;
|
||||
}
|
||||
|
||||
private static void assertErrorEquals(ValidationResult validationResult, MessageKey messageKey, String... args) {
|
||||
assertThat(validationResult.hasError(), equalTo(true));
|
||||
assertThat(validationResult.getMessageKey(), equalTo(messageKey));
|
||||
|
||||
@@ -3,16 +3,23 @@ package fr.xephi.authme.settings;
|
||||
import ch.jalu.configme.SectionComments;
|
||||
import ch.jalu.configme.SettingsHolder;
|
||||
import ch.jalu.configme.configurationdata.ConfigurationData;
|
||||
import ch.jalu.configme.properties.EnumProperty;
|
||||
import ch.jalu.configme.properties.Property;
|
||||
import com.google.common.collect.ImmutableSetMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import fr.xephi.authme.ClassCollector;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.datasource.DataSourceType;
|
||||
import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever;
|
||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -20,6 +27,7 @@ import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static fr.xephi.authme.ReflectionTestUtils.getFieldValue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
@@ -32,6 +40,17 @@ public class SettingsConsistencyTest {
|
||||
*/
|
||||
private static final int MAX_COMMENT_LENGTH = 90;
|
||||
|
||||
/**
|
||||
* Exclusions for the enum in comments check. Use {@link Exclude#ALL}
|
||||
* to skip an entire property from being checked.
|
||||
*/
|
||||
private static final Multimap<Property<?>, Enum<?>> EXCLUDED_ENUMS =
|
||||
ImmutableSetMultimap.<Property<?>, Enum<?>>builder()
|
||||
.put(DatabaseSettings.BACKEND, DataSourceType.FILE)
|
||||
.put(SecuritySettings.PASSWORD_HASH, Exclude.ALL)
|
||||
.put(SecuritySettings.LEGACY_HASHES, Exclude.ALL)
|
||||
.build();
|
||||
|
||||
private static ConfigurationData configurationData;
|
||||
|
||||
@BeforeClass
|
||||
@@ -132,4 +151,63 @@ public class SettingsConsistencyTest {
|
||||
.flatMap(Arrays::stream)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that enum properties have all possible enum values listed in their comment
|
||||
* so the user knows which values are available.
|
||||
*/
|
||||
@Test
|
||||
public void shouldMentionAllEnumValues() {
|
||||
// given
|
||||
Map<Property<?>, Enum<?>> invalidEnumProperties = new HashMap<>();
|
||||
|
||||
for (Property<?> property : configurationData.getProperties()) {
|
||||
// when
|
||||
Class<? extends Enum<?>> enumClass = getEnumClass(property);
|
||||
if (enumClass != null) {
|
||||
String comments = String.join("\n", configurationData.getCommentsForSection(property.getPath()));
|
||||
Arrays.stream(enumClass.getEnumConstants())
|
||||
.filter(e -> !comments.contains(e.name()) && !isExcluded(property, e))
|
||||
.findFirst()
|
||||
.ifPresent(e -> invalidEnumProperties.put(property, e));
|
||||
}
|
||||
}
|
||||
|
||||
// then
|
||||
if (!invalidEnumProperties.isEmpty()) {
|
||||
String invalidEnums = invalidEnumProperties.entrySet().stream()
|
||||
.map(e -> e.getKey() + " does not mention " + e.getValue() + " and possibly others")
|
||||
.collect(Collectors.joining("\n- "));
|
||||
|
||||
fail("Found enum properties that do not list all entries in the comments:\n- " + invalidEnums);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the enum class the property holds values for, if applicable.
|
||||
*
|
||||
* @param property the property to get the enum class from
|
||||
* @return the enum class, or null if not available
|
||||
*/
|
||||
private static Class<? extends Enum<?>> getEnumClass(Property<?> property) {
|
||||
if (property instanceof EnumProperty<?>) {
|
||||
return getFieldValue(EnumProperty.class, (EnumProperty<?>) property, "clazz");
|
||||
} else if (property instanceof EnumSetProperty<?>) {
|
||||
return getFieldValue(EnumSetProperty.class, (EnumSetProperty<?>) property, "enumClass");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean isExcluded(Property<?> property, Enum<?> enumValue) {
|
||||
return EXCLUDED_ENUMS.get(property).contains(Exclude.ALL)
|
||||
|| EXCLUDED_ENUMS.get(property).contains(enumValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dummy enum to specify in the exclusion that all enum values
|
||||
* should be skipped. See its usages.
|
||||
*/
|
||||
private enum Exclude {
|
||||
ALL
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,4 +95,14 @@ public class StringUtilsTest {
|
||||
public void shouldHaveHiddenConstructor() {
|
||||
TestHelper.validateHasOnlyPrivateEmptyConstructor(StringUtils.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCheckIfHasNeedleInWord() {
|
||||
// given/when/then
|
||||
assertThat(StringUtils.isInsideString('@', "@hello"), equalTo(false));
|
||||
assertThat(StringUtils.isInsideString('?', "absent"), equalTo(false));
|
||||
assertThat(StringUtils.isInsideString('-', "abcd-"), equalTo(false));
|
||||
assertThat(StringUtils.isInsideString('@', "hello@example"), equalTo(true));
|
||||
assertThat(StringUtils.isInsideString('@', "D@Z"), equalTo(true));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user