-
Notifications
You must be signed in to change notification settings - Fork 559
#1448 Create AuthMePlayer to get player data from API with #2000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b46f63a
#1448 Create AuthMePlayer to get player data from API with
ljacqu 1df38a9
#1448 Add tests for new API method & AuthMePlayer
ljacqu 239281b
#1448 Create AuthMePlayer to get player data from API with
ljacqu 79dfd05
#1448 Add comment that AuthMePlayer data does not update itself
ljacqu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package fr.xephi.authme.api.v3; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| /** | ||
| * Read-only player info exposed in the AuthMe API. The data in this object is copied from the | ||
| * database and not updated afterwards. As such, it may become outdated if the player data changes | ||
| * in AuthMe. | ||
| * | ||
| * @see AuthMeApi#getPlayerInfo | ||
| */ | ||
| public interface AuthMePlayer { | ||
|
|
||
| /** | ||
| * @return the case-sensitive name of the player, e.g. "thePlayer3030" - never null | ||
| */ | ||
| String getName(); | ||
|
|
||
| /** | ||
| * Returns the UUID of the player as given by the server (may be offline UUID or not). | ||
| * The UUID is not present if AuthMe is configured not to store the UUID or if the data is not | ||
| * present (e.g. older record). | ||
| * | ||
| * @return player uuid, or empty optional if not available | ||
| */ | ||
| Optional<UUID> getUuid(); | ||
|
|
||
| /** | ||
| * Returns the email address associated with this player, or an empty optional if not available. | ||
| * | ||
| * @return player's email or empty optional | ||
| */ | ||
| Optional<String> getEmail(); | ||
|
|
||
| /** | ||
| * @return the registration date of the player's account - never null | ||
| */ | ||
| Instant getRegistrationDate(); | ||
|
|
||
| /** | ||
| * Returns the IP address with which the player's account was registered. Returns an empty optional | ||
| * for older accounts, or if the account was registered by someone else (e.g. by an admin). | ||
| * | ||
| * @return the ip address used during the registration of the account, or empty optional | ||
| */ | ||
| Optional<String> getRegistrationIpAddress(); | ||
|
|
||
| /** | ||
| * Returns the last login date of the player. An empty optional is returned if the player never logged in. | ||
| * | ||
| * @return date the player last logged in successfully, or empty optional if not applicable | ||
| */ | ||
| Optional<Instant> getLastLoginDate(); | ||
|
|
||
| /** | ||
| * Returns the IP address the player last logged in with successfully. Returns an empty optional if the | ||
| * player never logged in. | ||
| * | ||
| * @return ip address the player last logged in with successfully, or empty optional if not applicable | ||
| */ | ||
| Optional<String> getLastLoginIpAddress(); | ||
|
|
||
| } | ||
93 changes: 93 additions & 0 deletions
93
src/main/java/fr/xephi/authme/api/v3/AuthMePlayerImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package fr.xephi.authme.api.v3; | ||
|
|
||
| import fr.xephi.authme.data.auth.PlayerAuth; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| /** | ||
| * Implementation of {@link AuthMePlayer}. This implementation is not part of the API and | ||
| * may have breaking changes in subsequent releases. | ||
| */ | ||
| class AuthMePlayerImpl implements AuthMePlayer { | ||
|
|
||
| private String name; | ||
| private UUID uuid; | ||
| private String email; | ||
|
|
||
| private Instant registrationDate; | ||
| private String registrationIpAddress; | ||
|
|
||
| private Instant lastLoginDate; | ||
| private String lastLoginIpAddress; | ||
|
|
||
| AuthMePlayerImpl() { | ||
| } | ||
|
|
||
| /** | ||
| * Maps the given player auth to an AuthMePlayer instance. Returns an empty optional if | ||
| * the player auth is null. | ||
| * | ||
| * @param playerAuth the player auth or null | ||
| * @return the mapped player auth, or empty optional if the argument was null | ||
| */ | ||
| static Optional<AuthMePlayer> fromPlayerAuth(PlayerAuth playerAuth) { | ||
| if (playerAuth == null) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| AuthMePlayerImpl authMeUser = new AuthMePlayerImpl(); | ||
| authMeUser.name = playerAuth.getRealName(); | ||
| authMeUser.uuid = playerAuth.getUuid(); | ||
| authMeUser.email = nullIfDefault(playerAuth.getEmail(), PlayerAuth.DB_EMAIL_DEFAULT); | ||
| Long lastLoginMillis = nullIfDefault(playerAuth.getLastLogin(), PlayerAuth.DB_LAST_LOGIN_DEFAULT); | ||
| authMeUser.registrationDate = toInstant(playerAuth.getRegistrationDate()); | ||
| authMeUser.registrationIpAddress = playerAuth.getRegistrationIp(); | ||
| authMeUser.lastLoginDate = toInstant(lastLoginMillis); | ||
| authMeUser.lastLoginIpAddress = nullIfDefault(playerAuth.getLastIp(), PlayerAuth.DB_LAST_IP_DEFAULT); | ||
| return Optional.of(authMeUser); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public Optional<UUID> getUuid() { | ||
| return Optional.ofNullable(uuid); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<String> getEmail() { | ||
| return Optional.ofNullable(email); | ||
| } | ||
|
|
||
| @Override | ||
| public Instant getRegistrationDate() { | ||
| return registrationDate; | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<String> getRegistrationIpAddress() { | ||
| return Optional.ofNullable(registrationIpAddress); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Instant> getLastLoginDate() { | ||
| return Optional.ofNullable( lastLoginDate); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<String> getLastLoginIpAddress() { | ||
| return Optional.ofNullable(lastLoginIpAddress); | ||
| } | ||
|
|
||
| private static Instant toInstant(Long epochMillis) { | ||
| return epochMillis == null ? null : Instant.ofEpochMilli(epochMillis); | ||
| } | ||
|
|
||
| private static <T> T nullIfDefault(T value, T defaultValue) { | ||
| return defaultValue.equals(value) ? null : value; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/test/java/fr/xephi/authme/api/v3/AuthMePlayerImplTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package fr.xephi.authme.api.v3; | ||
|
|
||
| import fr.xephi.authme.data.auth.PlayerAuth; | ||
| import org.hamcrest.Description; | ||
| import org.hamcrest.Matcher; | ||
| import org.hamcrest.TypeSafeMatcher; | ||
| import org.junit.Test; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
||
| /** | ||
| * Test for {@link AuthMePlayerImpl}. | ||
| */ | ||
| public class AuthMePlayerImplTest { | ||
|
|
||
| @Test | ||
| public void shouldMapNullWithoutError() { | ||
| // given / when / then | ||
| assertThat(AuthMePlayerImpl.fromPlayerAuth(null), emptyOptional()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldMapFromPlayerAuth() { | ||
| // given | ||
| PlayerAuth auth = PlayerAuth.builder() | ||
| .name("victor") | ||
| .realName("Victor") | ||
| .email("vic@example.com") | ||
| .registrationDate(1480075661000L) | ||
| .registrationIp("124.125.126.127") | ||
| .lastLogin(1542675632000L) | ||
| .lastIp("62.63.64.65") | ||
| .uuid(UUID.fromString("deadbeef-2417-4653-9026-feedbabeface")) | ||
| .build(); | ||
|
|
||
| // when | ||
| Optional<AuthMePlayer> result = AuthMePlayerImpl.fromPlayerAuth(auth); | ||
|
|
||
| // then | ||
| AuthMePlayer playerInfo = result.get(); | ||
| assertThat(playerInfo.getName(), equalTo("Victor")); | ||
| assertThat(playerInfo.getUuid().get(), equalTo(auth.getUuid())); | ||
| assertThat(playerInfo.getEmail().get(), equalTo(auth.getEmail())); | ||
| assertThat(playerInfo.getRegistrationDate(), equalTo(Instant.ofEpochMilli(auth.getRegistrationDate()))); | ||
| assertThat(playerInfo.getRegistrationIpAddress().get(), equalTo(auth.getRegistrationIp())); | ||
| assertThat(playerInfo.getLastLoginDate().get(), equalTo(Instant.ofEpochMilli(auth.getLastLogin()))); | ||
| assertThat(playerInfo.getLastLoginIpAddress().get(), equalTo(auth.getLastIp())); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldHandleNullAndDefaultValues() { | ||
| // given | ||
| PlayerAuth auth = PlayerAuth.builder() | ||
| .name("victor") | ||
| .realName("Victor") | ||
| .email("your@email.com") // DB default | ||
| .registrationDate(1480075661000L) | ||
| .lastLogin(0L) // DB default | ||
| .lastIp("127.0.0.1") // DB default | ||
| .build(); | ||
|
|
||
| // when | ||
| Optional<AuthMePlayer> result = AuthMePlayerImpl.fromPlayerAuth(auth); | ||
|
|
||
| // then | ||
| AuthMePlayer playerInfo = result.get(); | ||
| assertThat(playerInfo.getName(), equalTo("Victor")); | ||
| assertThat(playerInfo.getUuid(), emptyOptional()); | ||
| assertThat(playerInfo.getEmail(), emptyOptional()); | ||
| assertThat(playerInfo.getRegistrationDate(), equalTo(Instant.ofEpochMilli(auth.getRegistrationDate()))); | ||
| assertThat(playerInfo.getRegistrationIpAddress(), emptyOptional()); | ||
| assertThat(playerInfo.getLastLoginDate(), emptyOptional()); | ||
| assertThat(playerInfo.getLastLoginIpAddress(), emptyOptional()); | ||
| } | ||
|
|
||
| private static <T> Matcher<Optional<T>> emptyOptional() { | ||
| return new TypeSafeMatcher<Optional<T>>() { | ||
|
|
||
| @Override | ||
| public void describeTo(Description description) { | ||
| description.appendText("an empty optional"); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean matchesSafely(Optional<T> item) { | ||
| return !item.isPresent(); | ||
| } | ||
| }; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One comment would have been enough :P