Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions authme-core/src/test/java/fr/xephi/authme/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.mockito.Mockito;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
Expand Down Expand Up @@ -119,4 +120,21 @@ public static void returnDefaultsForAllProperties(Settings settings) {
given(settings.getProperty(any(Property.class)))
.willAnswer(invocation -> ((Property<?>) invocation.getArgument(0)).getDefaultValue());
}

/**
* Creates a file with the given name in the provided folder. Throws an exception if the file
* could not be created.
*
* @param folder the folder to create the file in
* @param filename the name of the file to create
* @return the created file
*/
public static File createFile(File folder, String filename) throws IOException {
File file = new File(folder, filename);
boolean created = file.createNewFile();
if (!created) {
throw new IllegalStateException("Could not create file '" + filename + "' in " + folder);
}
return file;
}
}
86 changes: 41 additions & 45 deletions authme-core/src/test/java/fr/xephi/authme/api/v3/AuthMeApiTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package fr.xephi.authme.api.v3;

import org.mockito.quality.Strictness;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.api.extension.ExtendWith;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.data.auth.PlayerAuth;
Expand All @@ -22,10 +18,11 @@
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.Instant;
import java.util.Arrays;
Expand Down Expand Up @@ -55,8 +52,7 @@
* Test for {@link AuthMeApi}.
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
public class AuthMeApiTest {
class AuthMeApiTest {

@InjectMocks
private AuthMeApi api;
Expand All @@ -75,11 +71,9 @@ public class AuthMeApiTest {
private AuthMe authMe;
@Mock
private GeoIpService geoIpService;
@Captor
private ArgumentCaptor<PlayerAuth> authCaptor;

@Test
public void shouldReturnInstanceOrNull() {
void shouldReturnInstanceOrNull() {
AuthMeApi result = AuthMeApi.getInstance();
assertThat(result, sameInstance(api));

Expand All @@ -88,7 +82,7 @@ public void shouldReturnInstanceOrNull() {
}

@Test
public void shouldReturnIfPlayerIsAuthenticated() {
void shouldReturnIfPlayerIsAuthenticated() {
// given
String name = "Bobby";
Player player = mockPlayerWithName(name);
Expand All @@ -103,7 +97,7 @@ public void shouldReturnIfPlayerIsAuthenticated() {
}

@Test
public void shouldReturnIfPlayerIsNpc() {
void shouldReturnIfPlayerIsNpc() {
// given
Player player = mock(Player.class);
given(player.hasMetadata("NPC")).willReturn(true);
Expand All @@ -117,7 +111,7 @@ public void shouldReturnIfPlayerIsNpc() {
}

@Test
public void shouldReturnIfPlayerIsUnrestricted() {
void shouldReturnIfPlayerIsUnrestricted() {
// given
String name = "Tester";
Player player = mockPlayerWithName(name);
Expand All @@ -132,7 +126,7 @@ public void shouldReturnIfPlayerIsUnrestricted() {
}

@Test
public void shouldGetLastLocation() {
void shouldGetLastLocation() {
// given
String name = "Gary";
Player player = mockPlayerWithName(name);
Expand Down Expand Up @@ -164,7 +158,7 @@ public void shouldGetLastLocation() {
}

@Test
public void shouldGetLastIp() {
void shouldGetLastIp() {
// given
String name = "Gabriel";
Player player = mockPlayerWithName(name);
Expand All @@ -182,7 +176,7 @@ public void shouldGetLastIp() {
}

@Test
public void shouldReturnNullAsLastIpForUnknownUser() {
void shouldReturnNullAsLastIpForUnknownUser() {
// given
String name = "Harrison";
given(playerCache.getAuth(name)).willReturn(null);
Expand All @@ -198,7 +192,7 @@ public void shouldReturnNullAsLastIpForUnknownUser() {
}

@Test
public void shouldGetLastLogin() {
void shouldGetLastLogin() {
// given
String name = "David";
PlayerAuth auth = PlayerAuth.builder().name(name)
Expand All @@ -215,7 +209,7 @@ public void shouldGetLastLogin() {
}

@Test
public void shouldHandleNullLastLogin() {
void shouldHandleNullLastLogin() {
// given
String name = "John";
PlayerAuth auth = PlayerAuth.builder().name(name)
Expand All @@ -232,7 +226,7 @@ public void shouldHandleNullLastLogin() {
}

@Test
public void shouldGetLastLoginTime() {
void shouldGetLastLoginTime() {
// given
String name = "David";
PlayerAuth auth = PlayerAuth.builder().name(name)
Expand All @@ -249,13 +243,16 @@ public void shouldGetLastLoginTime() {
}

@Test
public void testGetLastLoginMillis() {
AuthMeApi result = AuthMeApi.getInstance();
assertThat(result.getLastLoginTime("notAPlayer"), nullValue());
void shouldReturnNullLastLoginForUnknownPlayer() {
// given / when
Instant result = api.getLastLoginTime("notAPlayer");

// then
assertThat(result, nullValue());
}

@Test
public void shouldHandleNullLastLoginTime() {
void shouldHandleNullLastLoginTime() {
// given
String name = "John";
PlayerAuth auth = PlayerAuth.builder().name(name)
Expand All @@ -272,7 +269,7 @@ public void shouldHandleNullLastLoginTime() {
}

@Test
public void shouldReturnNullForUnavailablePlayer() {
void shouldReturnNullForUnavailablePlayer() {
// given
String name = "Numan";
Player player = mockPlayerWithName(name);
Expand All @@ -286,7 +283,7 @@ public void shouldReturnNullForUnavailablePlayer() {
}

@Test
public void shouldCheckForRegisteredName() {
void shouldCheckForRegisteredName() {
// given
String name = "toaster";
given(dataSource.isAuthAvailable(name)).willReturn(true);
Expand All @@ -299,7 +296,7 @@ public void shouldCheckForRegisteredName() {
}

@Test
public void shouldCheckPassword() {
void shouldCheckPassword() {
// given
String playerName = "Robert";
String password = "someSecretPhrase2983";
Expand All @@ -314,7 +311,7 @@ public void shouldCheckPassword() {
}

@Test
public void shouldReturnAuthNames() {
void shouldReturnAuthNames() {
// given
String[] names = {"bobby", "peter", "elisabeth", "craig"};
List<PlayerAuth> auths = Arrays.stream(names)
Expand All @@ -330,7 +327,7 @@ public void shouldReturnAuthNames() {
}

@Test
public void shouldReturnAuthRealNames() {
void shouldReturnAuthRealNames() {
// given
String[] names = {"Bobby", "peter", "Elisabeth", "CRAIG"};
List<PlayerAuth> auths = Arrays.stream(names)
Expand All @@ -346,7 +343,7 @@ public void shouldReturnAuthRealNames() {
}

@Test
public void shouldUnregisterPlayer() {
void shouldUnregisterPlayer() {
// given
Player player = mock(Player.class);
String name = "Donald";
Expand All @@ -360,7 +357,7 @@ public void shouldUnregisterPlayer() {
}

@Test
public void shouldUnregisterPlayerByName() {
void shouldUnregisterPlayerByName() {
// given
Server server = mock(Server.class);
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
Expand All @@ -376,7 +373,7 @@ public void shouldUnregisterPlayerByName() {
}

@Test
public void shouldChangePassword() {
void shouldChangePassword() {
// given
String name = "Bobby12";
String password = "resetPw!";
Expand All @@ -389,7 +386,7 @@ public void shouldChangePassword() {
}

@Test
public void shouldReturnAuthMeInstance() {
void shouldReturnAuthMeInstance() {
// given / when
AuthMe result = api.getPlugin();

Expand All @@ -398,7 +395,7 @@ public void shouldReturnAuthMeInstance() {
}

@Test
public void shouldReturnVersion() {
void shouldReturnVersion() {
// given / when
String result = api.getPluginVersion();

Expand All @@ -407,7 +404,7 @@ public void shouldReturnVersion() {
}

@Test
public void shouldForceLogin() {
void shouldForceLogin() {
// given
Player player = mock(Player.class);

Expand All @@ -419,7 +416,7 @@ public void shouldForceLogin() {
}

@Test
public void shouldForceLogout() {
void shouldForceLogout() {
// given
Player player = mock(Player.class);

Expand All @@ -431,7 +428,7 @@ public void shouldForceLogout() {
}

@Test
public void shouldForceRegister() {
void shouldForceRegister() {
// given
Player player = mock(Player.class);
String pass = "test235";
Expand All @@ -445,7 +442,7 @@ public void shouldForceRegister() {
}

@Test
public void shouldForceRegisterAndNotAutoLogin() {
void shouldForceRegisterAndNotAutoLogin() {
// given
Player player = mock(Player.class);
String pass = "test235";
Expand All @@ -459,7 +456,7 @@ public void shouldForceRegisterAndNotAutoLogin() {
}

@Test
public void shouldRegisterPlayer() {
void shouldRegisterPlayer() {
// given
String name = "Marco";
String password = "myP4ss";
Expand All @@ -473,14 +470,15 @@ public void shouldRegisterPlayer() {
// then
assertThat(result, equalTo(true));
verify(passwordSecurity).computeHash(password, name.toLowerCase(Locale.ROOT));
ArgumentCaptor<PlayerAuth> authCaptor = ArgumentCaptor.forClass(PlayerAuth.class);
verify(dataSource).saveAuth(authCaptor.capture());
assertThat(authCaptor.getValue().getNickname(), equalTo(name.toLowerCase(Locale.ROOT)));
assertThat(authCaptor.getValue().getRealName(), equalTo(name));
assertThat(authCaptor.getValue().getPassword(), equalTo(hashedPassword));
}

@Test
public void shouldNotRegisterAlreadyRegisteredPlayer() {
void shouldNotRegisterAlreadyRegisteredPlayer() {
// given
String name = "jonah";
given(dataSource.isAuthAvailable(name)).willReturn(true);
Expand All @@ -495,7 +493,7 @@ public void shouldNotRegisterAlreadyRegisteredPlayer() {
}

@Test
public void shouldGetNamesByIp() {
void shouldGetNamesByIp() {
// given
String ip = "123.123.123.123";
List<String> names = Arrays.asList("Morgan", "Batista", "QUINN");
Expand All @@ -510,7 +508,7 @@ public void shouldGetNamesByIp() {
}

@Test
public void shouldReturnGeoIpInfo() {
void shouldReturnGeoIpInfo() {
// given
String ip = "127.127.12.1";
given(geoIpService.getCountryCode(ip)).willReturn("XA");
Expand All @@ -526,7 +524,7 @@ public void shouldReturnGeoIpInfo() {
}

@Test
public void shouldReturnAuthMePlayerInfo() {
void shouldReturnAuthMePlayerInfo() {
// given
PlayerAuth auth = PlayerAuth.builder()
.name("bobb")
Expand All @@ -545,7 +543,7 @@ public void shouldReturnAuthMePlayerInfo() {
}

@Test
public void shouldReturnNullForNonExistentAuth() {
void shouldReturnNullForNonExistentAuth() {
// given / when
Optional<AuthMePlayer> result = api.getPlayerInfo("doesNotExist");

Expand All @@ -561,5 +559,3 @@ private static Player mockPlayerWithName(String name) {
return player;
}
}


Loading
Loading