#930 Refactor captcha managers to have a crude captcha storage class instead of inheritance
- Remove abstract captcha manager in favor of a primitive captcha code storage (composition over inheritance) - Supply player when checking captcha code for further usage (fixes open point from previous commit)
This commit is contained in:
@@ -6,7 +6,7 @@ import ch.jalu.injector.InjectorBuilder;
|
||||
import com.google.common.io.Files;
|
||||
import fr.xephi.authme.api.v3.AuthMeApi;
|
||||
import fr.xephi.authme.command.CommandHandler;
|
||||
import fr.xephi.authme.data.RegistrationCaptchaManager;
|
||||
import fr.xephi.authme.data.captcha.RegistrationCaptchaManager;
|
||||
import fr.xephi.authme.data.limbo.LimboService;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
|
||||
@@ -4,6 +4,7 @@ import ch.jalu.configme.properties.Property;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import fr.xephi.authme.data.captcha.CaptchaCodeStorage;
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtension;
|
||||
import fr.xephi.authme.initialization.HasCleanup;
|
||||
@@ -42,7 +43,7 @@ public class ClassesConsistencyTest {
|
||||
|
||||
/** Expiring structure types. */
|
||||
private static final Set<Class<?>> EXPIRING_STRUCTURES = ImmutableSet.of(
|
||||
ExpiringSet.class, ExpiringMap.class, TimedCounter.class);
|
||||
ExpiringSet.class, ExpiringMap.class, TimedCounter.class, CaptchaCodeStorage.class);
|
||||
|
||||
/** Immutable types, which are allowed to be used in non-private constants. */
|
||||
private static final Set<Class<?>> IMMUTABLE_TYPES = ImmutableSet.of(
|
||||
@@ -157,10 +158,9 @@ public class ClassesConsistencyTest {
|
||||
public void shouldImplementHasCleanup() {
|
||||
// given / when / then
|
||||
for (Class<?> clazz : ALL_CLASSES) {
|
||||
if (hasExpiringCollectionAsField(clazz)) {
|
||||
if (hasExpiringCollectionAsField(clazz) && !EXPIRING_STRUCTURES.contains(clazz)) {
|
||||
assertThat("Class '" + clazz.getSimpleName() + "' has expiring collections, should implement HasCleanup",
|
||||
HasCleanup.class.isAssignableFrom(clazz), equalTo(true));
|
||||
// System.out.println("Successful check for " + clazz);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package fr.xephi.authme.command.executable.captcha;
|
||||
|
||||
import fr.xephi.authme.data.LoginCaptchaManager;
|
||||
import fr.xephi.authme.data.RegistrationCaptchaManager;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.data.captcha.LoginCaptchaManager;
|
||||
import fr.xephi.authme.data.captcha.RegistrationCaptchaManager;
|
||||
import fr.xephi.authme.data.limbo.LimboService;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.service.CommonService;
|
||||
@@ -86,14 +86,14 @@ public class CaptchaCommandTest {
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
given(loginCaptchaManager.isCaptchaRequired(name)).willReturn(true);
|
||||
String captchaCode = "3991";
|
||||
given(loginCaptchaManager.checkCode(name, captchaCode)).willReturn(true);
|
||||
given(loginCaptchaManager.checkCode(player, captchaCode)).willReturn(true);
|
||||
|
||||
// when
|
||||
command.executeCommand(player, Collections.singletonList(captchaCode));
|
||||
|
||||
// then
|
||||
verify(loginCaptchaManager).isCaptchaRequired(name);
|
||||
verify(loginCaptchaManager).checkCode(name, captchaCode);
|
||||
verify(loginCaptchaManager).checkCode(player, captchaCode);
|
||||
verifyNoMoreInteractions(loginCaptchaManager);
|
||||
verify(commonService).send(player, MessageKey.CAPTCHA_SUCCESS);
|
||||
verify(commonService).send(player, MessageKey.LOGIN_MESSAGE);
|
||||
@@ -109,7 +109,7 @@ public class CaptchaCommandTest {
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
given(loginCaptchaManager.isCaptchaRequired(name)).willReturn(true);
|
||||
String captchaCode = "2468";
|
||||
given(loginCaptchaManager.checkCode(name, captchaCode)).willReturn(false);
|
||||
given(loginCaptchaManager.checkCode(player, captchaCode)).willReturn(false);
|
||||
String newCode = "1337";
|
||||
given(loginCaptchaManager.generateCode(name)).willReturn(newCode);
|
||||
|
||||
@@ -118,7 +118,7 @@ public class CaptchaCommandTest {
|
||||
|
||||
// then
|
||||
verify(loginCaptchaManager).isCaptchaRequired(name);
|
||||
verify(loginCaptchaManager).checkCode(name, captchaCode);
|
||||
verify(loginCaptchaManager).checkCode(player, captchaCode);
|
||||
verify(loginCaptchaManager).generateCode(name);
|
||||
verifyNoMoreInteractions(loginCaptchaManager);
|
||||
verify(commonService).send(player, MessageKey.CAPTCHA_WRONG_ERROR, newCode);
|
||||
@@ -133,13 +133,13 @@ public class CaptchaCommandTest {
|
||||
given(loginCaptchaManager.isCaptchaRequired(name)).willReturn(false);
|
||||
given(registrationCaptchaManager.isCaptchaRequired(name)).willReturn(true);
|
||||
String captchaCode = "A89Y3";
|
||||
given(registrationCaptchaManager.checkCode(name, captchaCode)).willReturn(true);
|
||||
given(registrationCaptchaManager.checkCode(player, captchaCode)).willReturn(true);
|
||||
|
||||
// when
|
||||
command.executeCommand(player, Collections.singletonList(captchaCode));
|
||||
|
||||
// then
|
||||
verify(registrationCaptchaManager).checkCode(name, captchaCode);
|
||||
verify(registrationCaptchaManager).checkCode(player, captchaCode);
|
||||
verify(loginCaptchaManager, only()).isCaptchaRequired(name);
|
||||
verify(commonService).send(player, MessageKey.CAPTCHA_SUCCESS);
|
||||
verify(commonService).send(player, MessageKey.REGISTER_MESSAGE);
|
||||
@@ -152,14 +152,14 @@ public class CaptchaCommandTest {
|
||||
Player player = mockPlayerWithName(name);
|
||||
given(registrationCaptchaManager.isCaptchaRequired(name)).willReturn(true);
|
||||
String captchaCode = "SFL3";
|
||||
given(registrationCaptchaManager.checkCode(name, captchaCode)).willReturn(false);
|
||||
given(registrationCaptchaManager.checkCode(player, captchaCode)).willReturn(false);
|
||||
given(registrationCaptchaManager.generateCode(name)).willReturn("new code");
|
||||
|
||||
// when
|
||||
command.executeCommand(player, Collections.singletonList(captchaCode));
|
||||
|
||||
// then
|
||||
verify(registrationCaptchaManager).checkCode(name, captchaCode);
|
||||
verify(registrationCaptchaManager).checkCode(player, captchaCode);
|
||||
verify(registrationCaptchaManager).generateCode(name);
|
||||
verify(commonService).send(player, MessageKey.CAPTCHA_WRONG_ERROR, "new code");
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package fr.xephi.authme.command.executable.register;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.data.RegistrationCaptchaManager;
|
||||
import fr.xephi.authme.data.captcha.RegistrationCaptchaManager;
|
||||
import fr.xephi.authme.mail.EmailService;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.process.Management;
|
||||
|
||||
+6
-3
@@ -1,9 +1,10 @@
|
||||
package fr.xephi.authme.data;
|
||||
package fr.xephi.authme.data.captcha;
|
||||
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import fr.xephi.authme.util.expiring.TimedCounter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
@@ -38,10 +39,12 @@ public class LoginCaptchaManagerTest {
|
||||
@Test
|
||||
public void shouldCreateAndCheckCaptcha() {
|
||||
// given
|
||||
String player = "Miner";
|
||||
String name = "Miner";
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
Settings settings = mockSettings(1, 4);
|
||||
LoginCaptchaManager manager = new LoginCaptchaManager(settings);
|
||||
String captchaCode = manager.getCaptchaCodeOrGenerateNew(player);
|
||||
String captchaCode = manager.getCaptchaCodeOrGenerateNew(name);
|
||||
|
||||
// when
|
||||
boolean badResult = manager.checkCode(player, "wrong_code");
|
||||
+16
-5
@@ -1,9 +1,10 @@
|
||||
package fr.xephi.authme.data;
|
||||
package fr.xephi.authme.data.captcha;
|
||||
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import fr.xephi.authme.util.expiring.ExpiringMap;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Test;
|
||||
|
||||
import static fr.xephi.authme.AuthMeMatchers.stringWithLength;
|
||||
@@ -46,8 +47,11 @@ public class RegistrationCaptchaManagerTest {
|
||||
RegistrationCaptchaManager captchaManager = new RegistrationCaptchaManager(settings);
|
||||
getCodeMap(captchaManager).put("test", captcha);
|
||||
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn("TeSt");
|
||||
|
||||
// when
|
||||
boolean isSuccessful = captchaManager.checkCode("TeSt", captcha);
|
||||
boolean isSuccessful = captchaManager.checkCode(player, captcha);
|
||||
|
||||
// then
|
||||
assertThat(isSuccessful, equalTo(true));
|
||||
@@ -72,11 +76,18 @@ public class RegistrationCaptchaManagerTest {
|
||||
assertThat(captcha1, equalTo(captcha2));
|
||||
assertThat(captcha1, stringWithLength(captchaLength));
|
||||
|
||||
// given (2)
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn("toast");
|
||||
|
||||
// when (2) / then (2)
|
||||
assertThat(captchaManager.checkCode("toast", captcha1), equalTo(true));
|
||||
assertThat(captchaManager.checkCode(player, captcha1), equalTo(true));
|
||||
}
|
||||
|
||||
private static ExpiringMap<String, String> getCodeMap(AbstractCaptchaManager captchaManager) {
|
||||
return ReflectionTestUtils.getFieldValue(AbstractCaptchaManager.class, captchaManager, "captchaCodes");
|
||||
@SuppressWarnings("unchecked")
|
||||
private static ExpiringMap<String, String> getCodeMap(RegistrationCaptchaManager captchaManager) {
|
||||
CaptchaCodeStorage captchaStorage = ReflectionTestUtils.getFieldValue(
|
||||
RegistrationCaptchaManager.class, captchaManager, "captchaCodeStorage");
|
||||
return ReflectionTestUtils.getFieldValue(CaptchaCodeStorage.class, captchaStorage, "captchaCodes");
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package fr.xephi.authme.data.limbo;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.data.RegistrationCaptchaManager;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.data.captcha.RegistrationCaptchaManager;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.message.Messages;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
|
||||
Reference in New Issue
Block a user