#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
-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