#1035 Migrate other accounts config from config.yml to commands.yml

This commit is contained in:
ljacqu
2018-01-21 18:58:20 +01:00
parent 456693ea59
commit 761ee2f05b
10 changed files with 158 additions and 40 deletions
@@ -97,6 +97,24 @@ public class SettingsMigrationServiceTest {
assertThat(migrationService.returnedValues, contains(true, false));
}
@Test
public void shouldKeepOldOtherAccountsSettings() throws IOException {
// given
File dataFolder = temporaryFolder.newFolder();
File configFile = new File(dataFolder, "config.yml");
Files.copy(getJarFile(OLD_CONFIG_FILE), configFile);
PropertyResource resource = new YamlFileResource(configFile);
SettingsMigrationService migrationService = new SettingsMigrationService(dataFolder);
// when
migrationService.performMigrations(resource, AuthMeSettingsRetriever.buildConfigurationData().getProperties());
// then
assertThat(migrationService.hasOldOtherAccountsCommand(), equalTo(true));
assertThat(migrationService.getOldOtherAccountsCommand(), equalTo("msg admin %playername% has a lot of accounts!"));
assertThat(migrationService.getOldOtherAccountsCommandThreshold(), equalTo(5));
}
private void verifyHasUpToDateSettings(Settings settings, File dataFolder) throws IOException {
assertThat(settings.getProperty(ALLOWED_NICKNAME_CHARACTERS), equalTo(ALLOWED_NICKNAME_CHARACTERS.getDefaultValue()));
assertThat(settings.getProperty(DELAY_JOIN_MESSAGE), equalTo(true));
@@ -6,17 +6,26 @@ import ch.jalu.configme.configurationdata.ConfigurationDataBuilder;
import ch.jalu.configme.resource.PropertyResource;
import ch.jalu.configme.resource.YamlFileResource;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.SettingsMigrationService;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static com.google.common.collect.Sets.newHashSet;
import static org.hamcrest.Matchers.aMapWithSize;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
/**
* Test for {@link CommandMigrationService}.
@@ -27,6 +36,9 @@ public class CommandMigrationServiceTest {
@InjectMocks
private CommandMigrationService commandMigrationService;
@Mock
private SettingsMigrationService settingsMigrationService;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
@@ -90,4 +102,43 @@ public class CommandMigrationServiceTest {
// when / then
assertThat(CommandMigrationService.COMMAND_CONFIG_PROPERTIES, containsInAnyOrder(properties));
}
@Test
public void shouldMigrateOldOtherAccountsCommand() {
// given
given(settingsMigrationService.hasOldOtherAccountsCommand()).willReturn(true);
given(settingsMigrationService.getOldOtherAccountsCommand())
.willReturn("helpop %playername% (%playerip%) has other accounts!");
given(settingsMigrationService.getOldOtherAccountsCommandThreshold()).willReturn(3);
File commandFile = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "settings/commandconfig/commands.complete.yml");
PropertyResource resource = new YamlFileResource(commandFile);
// when
commandMigrationService.checkAndMigrate(
resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties());
// then
Map<String, OnLoginCommand> onLoginCommands = CommandSettingsHolder.COMMANDS.getValue(resource).getOnLogin();
assertThat(onLoginCommands, aMapWithSize(6)); // 5 in the file + the newly migrated on
OnLoginCommand newCommand = getUnknownOnLoginCommand(onLoginCommands);
assertThat(newCommand.getCommand(), equalTo("helpop %p (%ip) has other accounts!"));
assertThat(newCommand.getExecutor(), equalTo(Executor.CONSOLE));
assertThat(newCommand.getIfNumberOfAccountsAtLeast().get(), equalTo(3));
assertThat(newCommand.getIfNumberOfAccountsLessThan().isPresent(), equalTo(false));
}
/*
* Returns the command under onLogin from commands.complete.yml that isn't present in the beginning.
*/
private static OnLoginCommand getUnknownOnLoginCommand(Map<String, OnLoginCommand> onLoginCommands) {
Set<String> knownKeys = newHashSet("welcome", "show_motd", "display_list", "warn_for_alts", "log_suspicious_user");
List<String> unknownKeys = onLoginCommands.keySet().stream()
.filter(key -> !knownKeys.contains(key))
.collect(Collectors.toList());
if (unknownKeys.size() == 1) {
return onLoginCommands.get(unknownKeys.get(0));
} else {
throw new IllegalStateException("Expected 1 unknown key but found " + unknownKeys.size() + ": " + unknownKeys);
}
}
}
@@ -4,17 +4,20 @@ import ch.jalu.configme.configurationdata.ConfigurationDataBuilder;
import ch.jalu.configme.resource.PropertyResource;
import ch.jalu.configme.resource.YamlFileResource;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.SettingsMigrationService;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.io.File;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
/**
* Tests that commands.yml is well-formed.
@@ -25,6 +28,9 @@ public class CommandYmlConsistencyTest {
@InjectMocks
private CommandMigrationService commandMigrationService;
@Mock
private SettingsMigrationService settingsMigrationService;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@@ -40,5 +46,6 @@ public class CommandYmlConsistencyTest {
// then
assertThat(result, equalTo(false));
verify(settingsMigrationService).hasOldOtherAccountsCommand();
}
}
@@ -154,6 +154,11 @@ settings:
noTeleport: false
# Regex sintax for allowed Chars in passwords.
allowedPasswordCharacters: '[\x21-\x7E]*'
# Command to run when a user has more accounts than the configured threshold.
# Available variables: %playername%, %playerip%
otherAccountsCmd: 'msg admin %playername% has a lot of accounts!'
# Threshold of the other accounts command, a value less than 2 means disabled.
otherAccountsCmdThreshold: 5
GameMode:
# ForceSurvivalMode to player when join ?
ForceSurvivalMode: false