mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-17 22:28:13 +01:00
Add missing tests for API package
This commit is contained in:
parent
b687c68d46
commit
2c6181d150
@ -62,13 +62,8 @@ public class NewAPI {
|
||||
* @return The API object, or null if the AuthMe plugin is not enabled or not fully initialized yet
|
||||
*/
|
||||
public static NewAPI getInstance() {
|
||||
if (singleton != null) {
|
||||
return singleton;
|
||||
}
|
||||
// NewAPI is initialized in AuthMe#onEnable -> if singleton is null,
|
||||
// it means AuthMe isn't initialized (yet)
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the plugin instance.
|
||||
@ -168,10 +163,10 @@ public class NewAPI {
|
||||
*/
|
||||
public boolean registerPlayer(String playerName, String password) {
|
||||
String name = playerName.toLowerCase();
|
||||
HashedPassword result = passwordSecurity.computeHash(password, name);
|
||||
if (isRegistered(name)) {
|
||||
return false;
|
||||
}
|
||||
HashedPassword result = passwordSecurity.computeHash(password, name);
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name(name)
|
||||
.password(result)
|
||||
|
@ -62,13 +62,8 @@ public class AuthMeApi {
|
||||
* @return The AuthMeApi object, or null if the AuthMe plugin is not enabled or not fully initialized yet
|
||||
*/
|
||||
public static AuthMeApi getInstance() {
|
||||
if (singleton != null) {
|
||||
return singleton;
|
||||
}
|
||||
// AuthMeApi is initialized in AuthMe#onEnable -> if singleton is null,
|
||||
// it means AuthMe isn't initialized (yet)
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the plugin instance.
|
||||
@ -144,7 +139,7 @@ public class AuthMeApi {
|
||||
*/
|
||||
public String getLastIp(String playerName) {
|
||||
PlayerAuth auth = playerCache.getAuth(playerName);
|
||||
if(auth == null) {
|
||||
if (auth == null) {
|
||||
auth = dataSource.getAuth(playerName);
|
||||
}
|
||||
if (auth != null) {
|
||||
@ -212,10 +207,10 @@ public class AuthMeApi {
|
||||
*/
|
||||
public boolean registerPlayer(String playerName, String password) {
|
||||
String name = playerName.toLowerCase();
|
||||
HashedPassword result = passwordSecurity.computeHash(password, name);
|
||||
if (isRegistered(name)) {
|
||||
return false;
|
||||
}
|
||||
HashedPassword result = passwordSecurity.computeHash(password, name);
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name(name)
|
||||
.password(result)
|
||||
|
@ -1,12 +1,15 @@
|
||||
package fr.xephi.authme.api;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.api.v3.AuthMeApi;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.process.Management;
|
||||
import fr.xephi.authme.process.register.executors.ApiPasswordRegisterParams;
|
||||
import fr.xephi.authme.process.register.executors.RegistrationMethod;
|
||||
import fr.xephi.authme.security.PasswordSecurity;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
import fr.xephi.authme.service.ValidationService;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
@ -15,6 +18,7 @@ import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
@ -23,15 +27,21 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static fr.xephi.authme.IsEqualByReflectionMatcher.isEqualTo;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.only;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
|
||||
|
||||
/**
|
||||
* Test for {@link fr.xephi.authme.api.NewAPI}.
|
||||
@ -52,14 +62,16 @@ public class NewAPITest {
|
||||
private PasswordSecurity passwordSecurity;
|
||||
@Mock
|
||||
private PlayerCache playerCache;
|
||||
@Mock
|
||||
private AuthMe authMe;
|
||||
|
||||
@Test
|
||||
public void shouldReturnInstanceOrNull() {
|
||||
NewAPI result = NewAPI.getInstance();
|
||||
assertThat(result, sameInstance(api));
|
||||
|
||||
ReflectionTestUtils.setField(AuthMeApi.class, null, "singleton", null);
|
||||
assertThat(AuthMeApi.getInstance(), nullValue());
|
||||
ReflectionTestUtils.setField(NewAPI.class, null, "singleton", null);
|
||||
assertThat(NewAPI.getInstance(), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -240,6 +252,113 @@ public class NewAPITest {
|
||||
verify(management).performUnregisterByAdmin(null, name, player);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnAuthMeInstance() {
|
||||
// given / when
|
||||
AuthMe result = api.getPlugin();
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(authMe));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnVersion() {
|
||||
// given / when
|
||||
String result = api.getPluginVersion();
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(AuthMe.getPluginVersion()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldForceLogin() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
api.forceLogin(player);
|
||||
|
||||
// then
|
||||
verify(management).forceLogin(player);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldForceLogout() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
api.forceLogout(player);
|
||||
|
||||
// then
|
||||
verify(management).performLogout(player);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldForceRegister() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
String pass = "test235";
|
||||
|
||||
// when
|
||||
api.forceRegister(player, pass);
|
||||
|
||||
// then
|
||||
verify(management).performRegister(eq(RegistrationMethod.API_REGISTRATION),
|
||||
argThat(isEqualTo(ApiPasswordRegisterParams.of(player, pass, true))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldForceRegisterAndNotAutoLogin() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
String pass = "test235";
|
||||
|
||||
// when
|
||||
api.forceRegister(player, pass, false);
|
||||
|
||||
// then
|
||||
verify(management).performRegister(eq(RegistrationMethod.API_REGISTRATION),
|
||||
argThat(isEqualTo(ApiPasswordRegisterParams.of(player, pass, false))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRegisterPlayer() {
|
||||
// given
|
||||
String name = "Marco";
|
||||
String password = "myP4ss";
|
||||
HashedPassword hashedPassword = new HashedPassword("0395872SLKDFJOWEIUTEJSD");
|
||||
given(passwordSecurity.computeHash(password, name.toLowerCase())).willReturn(hashedPassword);
|
||||
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(true);
|
||||
|
||||
// when
|
||||
boolean result = api.registerPlayer(name, password);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
verify(passwordSecurity).computeHash(password, name.toLowerCase());
|
||||
ArgumentCaptor<PlayerAuth> authCaptor = ArgumentCaptor.forClass(PlayerAuth.class);
|
||||
verify(dataSource).saveAuth(authCaptor.capture());
|
||||
assertThat(authCaptor.getValue().getNickname(), equalTo(name.toLowerCase()));
|
||||
assertThat(authCaptor.getValue().getRealName(), equalTo(name));
|
||||
assertThat(authCaptor.getValue().getPassword(), equalTo(hashedPassword));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotRegisterAlreadyRegisteredPlayer() {
|
||||
// given
|
||||
String name = "jonah";
|
||||
given(dataSource.isAuthAvailable(name)).willReturn(true);
|
||||
|
||||
// when
|
||||
boolean result = api.registerPlayer(name, "pass");
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
verify(dataSource, only()).isAuthAvailable(name);
|
||||
verifyZeroInteractions(management, passwordSecurity);
|
||||
}
|
||||
|
||||
private static Player mockPlayerWithName(String name) {
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
|
@ -1,11 +1,16 @@
|
||||
package fr.xephi.authme.api.v3;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.process.Management;
|
||||
import fr.xephi.authme.process.register.executors.ApiPasswordRegisterParams;
|
||||
import fr.xephi.authme.process.register.executors.RegistrationMethod;
|
||||
import fr.xephi.authme.security.PasswordSecurity;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
import fr.xephi.authme.service.GeoIpService;
|
||||
import fr.xephi.authme.service.ValidationService;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
@ -14,6 +19,7 @@ import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
@ -23,15 +29,21 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static fr.xephi.authme.IsEqualByReflectionMatcher.isEqualTo;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.only;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
|
||||
|
||||
/**
|
||||
* Test for {@link AuthMeApi}.
|
||||
@ -52,6 +64,10 @@ public class AuthMeApiTest {
|
||||
private PasswordSecurity passwordSecurity;
|
||||
@Mock
|
||||
private PlayerCache playerCache;
|
||||
@Mock
|
||||
private AuthMe authMe;
|
||||
@Mock
|
||||
private GeoIpService geoIpService;
|
||||
|
||||
@Test
|
||||
public void shouldReturnInstanceOrNull() {
|
||||
@ -156,6 +172,22 @@ public class AuthMeApiTest {
|
||||
assertThat(result, equalTo("93.23.44.55"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullAsLastIpForUnknownUser() {
|
||||
// given
|
||||
String name = "Harrison";
|
||||
given(playerCache.getAuth(name)).willReturn(null);
|
||||
given(dataSource.getAuth(name)).willReturn(null);
|
||||
|
||||
// when
|
||||
String result = api.getLastIp(name);
|
||||
|
||||
// then
|
||||
assertThat(result, nullValue());
|
||||
verify(playerCache).getAuth(name);
|
||||
verify(dataSource).getAuth(name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetLastLogin() {
|
||||
// given
|
||||
@ -307,6 +339,144 @@ public class AuthMeApiTest {
|
||||
verify(management).performPasswordChangeAsAdmin(null, name, password);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnAuthMeInstance() {
|
||||
// given / when
|
||||
AuthMe result = api.getPlugin();
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(authMe));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnVersion() {
|
||||
// given / when
|
||||
String result = api.getPluginVersion();
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(AuthMe.getPluginVersion()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldForceLogin() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
api.forceLogin(player);
|
||||
|
||||
// then
|
||||
verify(management).forceLogin(player);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldForceLogout() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
api.forceLogout(player);
|
||||
|
||||
// then
|
||||
verify(management).performLogout(player);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldForceRegister() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
String pass = "test235";
|
||||
|
||||
// when
|
||||
api.forceRegister(player, pass);
|
||||
|
||||
// then
|
||||
verify(management).performRegister(eq(RegistrationMethod.API_REGISTRATION),
|
||||
argThat(isEqualTo(ApiPasswordRegisterParams.of(player, pass, true))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldForceRegisterAndNotAutoLogin() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
String pass = "test235";
|
||||
|
||||
// when
|
||||
api.forceRegister(player, pass, false);
|
||||
|
||||
// then
|
||||
verify(management).performRegister(eq(RegistrationMethod.API_REGISTRATION),
|
||||
argThat(isEqualTo(ApiPasswordRegisterParams.of(player, pass, false))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRegisterPlayer() {
|
||||
// given
|
||||
String name = "Marco";
|
||||
String password = "myP4ss";
|
||||
HashedPassword hashedPassword = new HashedPassword("0395872SLKDFJOWEIUTEJSD");
|
||||
given(passwordSecurity.computeHash(password, name.toLowerCase())).willReturn(hashedPassword);
|
||||
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(true);
|
||||
|
||||
// when
|
||||
boolean result = api.registerPlayer(name, password);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
verify(passwordSecurity).computeHash(password, name.toLowerCase());
|
||||
ArgumentCaptor<PlayerAuth> authCaptor = ArgumentCaptor.forClass(PlayerAuth.class);
|
||||
verify(dataSource).saveAuth(authCaptor.capture());
|
||||
assertThat(authCaptor.getValue().getNickname(), equalTo(name.toLowerCase()));
|
||||
assertThat(authCaptor.getValue().getRealName(), equalTo(name));
|
||||
assertThat(authCaptor.getValue().getPassword(), equalTo(hashedPassword));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotRegisterAlreadyRegisteredPlayer() {
|
||||
// given
|
||||
String name = "jonah";
|
||||
given(dataSource.isAuthAvailable(name)).willReturn(true);
|
||||
|
||||
// when
|
||||
boolean result = api.registerPlayer(name, "pass");
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
verify(dataSource, only()).isAuthAvailable(name);
|
||||
verifyZeroInteractions(management, passwordSecurity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetNamesByIp() {
|
||||
// given
|
||||
String ip = "123.123.123.123";
|
||||
List<String> names = Arrays.asList("Morgan", "Batista", "QUINN");
|
||||
given(dataSource.getAllAuthsByIp(ip)).willReturn(names);
|
||||
|
||||
// when
|
||||
List<String> result = api.getNamesByIp(ip);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(names));
|
||||
verify(dataSource).getAllAuthsByIp(ip);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnGeoIpInfo() {
|
||||
// given
|
||||
String ip = "127.127.12.1";
|
||||
given(geoIpService.getCountryCode(ip)).willReturn("XA");
|
||||
given(geoIpService.getCountryName(ip)).willReturn("Syldavia");
|
||||
|
||||
// when
|
||||
String countryCode = api.getCountryCode(ip);
|
||||
String countryName = api.getCountryName(ip);
|
||||
|
||||
// then
|
||||
assertThat(countryCode, equalTo("XA"));
|
||||
assertThat(countryName, equalTo("Syldavia"));
|
||||
}
|
||||
|
||||
private static Player mockPlayerWithName(String name) {
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
|
Loading…
Reference in New Issue
Block a user