#1190 Show settings warnings on reload also (#1384)

- Extract setting checks into their own class, called on startup and reload
This commit is contained in:
ljacqu
2017-10-28 14:15:38 +02:00
committed by GitHub
parent d40109929c
commit 04c5224e99
5 changed files with 150 additions and 28 deletions
@@ -11,6 +11,7 @@ import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.output.LogLevel;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.SettingsWarner;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
@@ -55,6 +56,9 @@ public class ReloadCommandTest {
@Mock
private CommonService commandService;
@Mock
private SettingsWarner settingsWarner;
@Mock
private SingletonStore<Reloadable> reloadableStore;
@@ -93,6 +97,7 @@ public class ReloadCommandTest {
verify(settings).reload();
verifyReloadingCalls(reloadables, dependents);
verify(commandService).send(sender, MessageKey.CONFIG_RELOAD_SUCCESS);
verify(settingsWarner).logWarningsForMisconfigurations();
}
@Test
@@ -0,0 +1,76 @@
package fr.xephi.authme.settings;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.logging.Logger;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.internal.verification.VerificationModeFactory.times;
/**
* Test for {@link SettingsWarner}.
*/
@RunWith(MockitoJUnitRunner.class)
public class SettingsWarnerTest {
@Mock
private Settings settings;
@Mock
private AuthMe authMe;
@Test
public void shouldLogWarnings() {
// given
Logger logger = TestHelper.setupLogger();
given(settings.getProperty(RestrictionSettings.FORCE_SINGLE_SESSION)).willReturn(false);
given(settings.getProperty(EmailSettings.SMTP_PORT)).willReturn(44);
given(settings.getProperty(EmailSettings.PORT25_USE_TLS)).willReturn(false);
given(settings.getProperty(PluginSettings.SESSIONS_ENABLED)).willReturn(true);
given(settings.getProperty(PluginSettings.SESSIONS_TIMEOUT)).willReturn(-5);
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.BCRYPT);
// when
createSettingsWarner().logWarningsForMisconfigurations();
// then
verify(logger, times(3)).warning(anyString());
}
@Test
public void shouldNotLogAnyWarning() {
Logger logger = TestHelper.setupLogger();
given(settings.getProperty(RestrictionSettings.FORCE_SINGLE_SESSION)).willReturn(true);
given(settings.getProperty(EmailSettings.SMTP_PORT)).willReturn(25);
given(settings.getProperty(EmailSettings.PORT25_USE_TLS)).willReturn(false);
given(settings.getProperty(PluginSettings.SESSIONS_ENABLED)).willReturn(false);
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.MD5);
// when
createSettingsWarner().logWarningsForMisconfigurations();
// then
verifyZeroInteractions(logger);
}
private SettingsWarner createSettingsWarner() {
SettingsWarner warner = new SettingsWarner();
ReflectionTestUtils.setField(warner, "settings", settings);
ReflectionTestUtils.setField(warner, "authMe", authMe);
return warner;
}
}