Add missing api methods #1304

This commit is contained in:
Gabriele C 2017-08-01 16:41:00 +02:00
parent a801f03cf2
commit 7e957cb8c7
2 changed files with 83 additions and 0 deletions

View File

@ -18,6 +18,8 @@ import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
/**
@ -138,6 +140,50 @@ public class AuthMeApi {
return null;
}
/**
* Get the last ip address of a player.
*
* @param playerName The name of the player to process
* @return String The last ip address of the player
*/
public String getLastIp(String playerName) {
PlayerAuth auth = playerCache.getAuth(playerName);
if(auth == null) {
auth = dataSource.getAuth(playerName);
}
if (auth != null) {
return auth.getIp();
}
return null;
}
/**
* Get user names by ip.
*
* @param address The ip address to process
* @return List The list of user names related to the ip address
*/
public List<String> getNamesByIp(String address) {
return dataSource.getAllAuthsByIp(address);
}
/**
* Get the last login date of a player.
*
* @param playerName The name of the player to process
* @return Date The date of the last login
*/
public Date getLastLogin(String playerName) {
PlayerAuth auth = playerCache.getAuth(playerName);
if(auth == null) {
auth = dataSource.getAuth(playerName);
}
if (auth != null) {
return new Date(auth.getLastLogin());
}
return null;
}
/**
* Return whether the player is registered.
*

View File

@ -20,6 +20,7 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@ -139,6 +140,42 @@ public class AuthMeApiTest {
assertThat(result.getPitch(), equalTo(auth.getPitch()));
}
@Test
public void shouldGetLastIp() {
// given
String name = "Gabriel";
Player player = mockPlayerWithName(name);
PlayerAuth auth = PlayerAuth.builder().name(name)
.ip("93.23.44.55")
.build();
given(playerCache.getAuth(name)).willReturn(auth);
// when
String result = api.getLastIp(player.getName());
// then
assertThat(result, not(nullValue()));
assertThat(result, equalTo("93.23.44.55"));
}
@Test
public void shouldGetLastLogin() {
// given
String name = "David";
Player player = mockPlayerWithName(name);
PlayerAuth auth = PlayerAuth.builder().name(name)
.lastLogin(1501597979)
.build();
given(playerCache.getAuth(name)).willReturn(auth);
// when
Date result = api.getLastLogin(player.getName());
// then
assertThat(result, not(nullValue()));
assertThat(result, equalTo(new Date(1501597979)));
}
@Test
public void shouldReturnNullForUnavailablePlayer() {
// given