From 7e957cb8c704ca4cac13f2b023662f13cb6b1f8b Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Tue, 1 Aug 2017 16:41:00 +0200 Subject: [PATCH] Add missing api methods #1304 --- .../fr/xephi/authme/api/v3/AuthMeApi.java | 46 +++++++++++++++++++ .../fr/xephi/authme/api/v3/AuthMeApiTest.java | 37 +++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/src/main/java/fr/xephi/authme/api/v3/AuthMeApi.java b/src/main/java/fr/xephi/authme/api/v3/AuthMeApi.java index 2e5ad98c2..a291852db 100644 --- a/src/main/java/fr/xephi/authme/api/v3/AuthMeApi.java +++ b/src/main/java/fr/xephi/authme/api/v3/AuthMeApi.java @@ -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 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. * diff --git a/src/test/java/fr/xephi/authme/api/v3/AuthMeApiTest.java b/src/test/java/fr/xephi/authme/api/v3/AuthMeApiTest.java index 818d9f9f2..0fadd3f19 100644 --- a/src/test/java/fr/xephi/authme/api/v3/AuthMeApiTest.java +++ b/src/test/java/fr/xephi/authme/api/v3/AuthMeApiTest.java @@ -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