#1127 Create DataSource#getEmail
This commit is contained in:
parent
cbec5427f2
commit
b99cc3bada
@ -1,8 +1,8 @@
|
|||||||
package fr.xephi.authme.command.executable.authme;
|
package fr.xephi.authme.command.executable.authme;
|
||||||
|
|
||||||
import fr.xephi.authme.command.ExecutableCommand;
|
import fr.xephi.authme.command.ExecutableCommand;
|
||||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
|
||||||
import fr.xephi.authme.datasource.DataSource;
|
import fr.xephi.authme.datasource.DataSource;
|
||||||
|
import fr.xephi.authme.datasource.DataSourceResult;
|
||||||
import fr.xephi.authme.message.MessageKey;
|
import fr.xephi.authme.message.MessageKey;
|
||||||
import fr.xephi.authme.service.CommonService;
|
import fr.xephi.authme.service.CommonService;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@ -25,11 +25,11 @@ public class GetEmailCommand implements ExecutableCommand {
|
|||||||
public void executeCommand(CommandSender sender, List<String> arguments) {
|
public void executeCommand(CommandSender sender, List<String> arguments) {
|
||||||
String playerName = arguments.isEmpty() ? sender.getName() : arguments.get(0);
|
String playerName = arguments.isEmpty() ? sender.getName() : arguments.get(0);
|
||||||
|
|
||||||
PlayerAuth auth = dataSource.getAuth(playerName);
|
DataSourceResult<String> email = dataSource.getEmail(playerName);
|
||||||
if (auth == null) {
|
if (email.playerExists()) {
|
||||||
commonService.send(sender, MessageKey.UNKNOWN_USER);
|
sender.sendMessage("[AuthMe] " + playerName + "'s email: " + email.getValue());
|
||||||
} else {
|
} else {
|
||||||
sender.sendMessage("[AuthMe] " + playerName + "'s email: " + auth.getEmail());
|
commonService.send(sender, MessageKey.UNKNOWN_USER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
package fr.xephi.authme.command.executable.authme.debug;
|
package fr.xephi.authme.command.executable.authme.debug;
|
||||||
|
|
||||||
import fr.xephi.authme.ConsoleLogger;
|
import fr.xephi.authme.ConsoleLogger;
|
||||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
|
||||||
import fr.xephi.authme.datasource.DataSource;
|
import fr.xephi.authme.datasource.DataSource;
|
||||||
|
import fr.xephi.authme.datasource.DataSourceResult;
|
||||||
import fr.xephi.authme.mail.SendMailSsl;
|
import fr.xephi.authme.mail.SendMailSsl;
|
||||||
import fr.xephi.authme.util.StringUtils;
|
import fr.xephi.authme.util.StringUtils;
|
||||||
import org.apache.commons.mail.EmailException;
|
import org.apache.commons.mail.EmailException;
|
||||||
@ -62,13 +62,13 @@ class TestEmailSender implements DebugSection {
|
|||||||
|
|
||||||
private String getEmail(CommandSender sender, List<String> arguments) {
|
private String getEmail(CommandSender sender, List<String> arguments) {
|
||||||
if (arguments.isEmpty()) {
|
if (arguments.isEmpty()) {
|
||||||
PlayerAuth auth = dataSource.getAuth(sender.getName());
|
DataSourceResult<String> emailResult = dataSource.getEmail(sender.getName());
|
||||||
if (auth == null) {
|
if (!emailResult.playerExists()) {
|
||||||
sender.sendMessage(ChatColor.RED + "Please provide an email address, "
|
sender.sendMessage(ChatColor.RED + "Please provide an email address, "
|
||||||
+ "e.g. /authme debug mail test@example.com");
|
+ "e.g. /authme debug mail test@example.com");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String email = auth.getEmail();
|
final String email = emailResult.getValue();
|
||||||
if (email == null || "your@email.com".equals(email)) {
|
if (email == null || "your@email.com".equals(email)) {
|
||||||
sender.sendMessage(ChatColor.RED + "No email set for your account!"
|
sender.sendMessage(ChatColor.RED + "No email set for your account!"
|
||||||
+ " Please use /authme debug mail <email>");
|
+ " Please use /authme debug mail <email>");
|
||||||
|
|||||||
@ -2,9 +2,9 @@ package fr.xephi.authme.command.executable.email;
|
|||||||
|
|
||||||
import fr.xephi.authme.ConsoleLogger;
|
import fr.xephi.authme.ConsoleLogger;
|
||||||
import fr.xephi.authme.command.PlayerCommand;
|
import fr.xephi.authme.command.PlayerCommand;
|
||||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
|
||||||
import fr.xephi.authme.data.auth.PlayerCache;
|
import fr.xephi.authme.data.auth.PlayerCache;
|
||||||
import fr.xephi.authme.datasource.DataSource;
|
import fr.xephi.authme.datasource.DataSource;
|
||||||
|
import fr.xephi.authme.datasource.DataSourceResult;
|
||||||
import fr.xephi.authme.mail.EmailService;
|
import fr.xephi.authme.mail.EmailService;
|
||||||
import fr.xephi.authme.message.MessageKey;
|
import fr.xephi.authme.message.MessageKey;
|
||||||
import fr.xephi.authme.service.CommonService;
|
import fr.xephi.authme.service.CommonService;
|
||||||
@ -53,13 +53,13 @@ public class RecoverEmailCommand extends PlayerCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
PlayerAuth auth = dataSource.getAuth(playerName); // TODO #1127: Create method to get email only
|
DataSourceResult<String> emailResult = dataSource.getEmail(playerName);
|
||||||
if (auth == null) {
|
if (!emailResult.playerExists()) {
|
||||||
commonService.send(player, MessageKey.USAGE_REGISTER);
|
commonService.send(player, MessageKey.USAGE_REGISTER);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final String email = auth.getEmail();
|
final String email = emailResult.getValue();
|
||||||
if (email == null || !email.equalsIgnoreCase(playerMail) || "your@email.com".equalsIgnoreCase(email)) {
|
if (email == null || !email.equalsIgnoreCase(playerMail) || "your@email.com".equalsIgnoreCase(email)) {
|
||||||
commonService.send(player, MessageKey.INVALID_EMAIL);
|
commonService.send(player, MessageKey.INVALID_EMAIL);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -23,16 +23,20 @@ import java.util.concurrent.TimeUnit;
|
|||||||
public class CacheDataSource implements DataSource {
|
public class CacheDataSource implements DataSource {
|
||||||
|
|
||||||
private final DataSource source;
|
private final DataSource source;
|
||||||
|
private final PlayerCache playerCache;
|
||||||
private final LoadingCache<String, Optional<PlayerAuth>> cachedAuths;
|
private final LoadingCache<String, Optional<PlayerAuth>> cachedAuths;
|
||||||
private final ListeningExecutorService executorService;
|
private final ListeningExecutorService executorService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for CacheDataSource.
|
* Constructor for CacheDataSource.
|
||||||
*
|
*
|
||||||
* @param src DataSource
|
* @param source the source
|
||||||
|
* @param playerCache the player cache
|
||||||
*/
|
*/
|
||||||
public CacheDataSource(DataSource src) {
|
public CacheDataSource(DataSource source, PlayerCache playerCache) {
|
||||||
source = src;
|
this.source = source;
|
||||||
|
this.playerCache = playerCache;
|
||||||
|
|
||||||
executorService = MoreExecutors.listeningDecorator(
|
executorService = MoreExecutors.listeningDecorator(
|
||||||
Executors.newCachedThreadPool(new ThreadFactoryBuilder()
|
Executors.newCachedThreadPool(new ThreadFactoryBuilder()
|
||||||
.setDaemon(true)
|
.setDaemon(true)
|
||||||
@ -168,17 +172,17 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getAllAuthsByIp(final String ip) {
|
public List<String> getAllAuthsByIp(String ip) {
|
||||||
return source.getAllAuthsByIp(ip);
|
return source.getAllAuthsByIp(ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int countAuthsByEmail(final String email) {
|
public int countAuthsByEmail(String email) {
|
||||||
return source.countAuthsByEmail(email);
|
return source.countAuthsByEmail(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void purgeRecords(final Collection<String> banned) {
|
public void purgeRecords(Collection<String> banned) {
|
||||||
source.purgeRecords(banned);
|
source.purgeRecords(banned);
|
||||||
cachedAuths.invalidateAll(banned);
|
cachedAuths.invalidateAll(banned);
|
||||||
}
|
}
|
||||||
@ -190,7 +194,7 @@ public class CacheDataSource implements DataSource {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isLogged(String user) {
|
public boolean isLogged(String user) {
|
||||||
return PlayerCache.getInstance().isAuthenticated(user);
|
return playerCache.isAuthenticated(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -223,6 +227,13 @@ public class CacheDataSource implements DataSource {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DataSourceResult<String> getEmail(String user) {
|
||||||
|
return cachedAuths.getUnchecked(user)
|
||||||
|
.map(auth -> DataSourceResult.of(auth.getEmail()))
|
||||||
|
.orElse(DataSourceResult.unknownPlayer());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PlayerAuth> getAllAuths() {
|
public List<PlayerAuth> getAllAuths() {
|
||||||
return source.getAllAuths();
|
return source.getAllAuths();
|
||||||
@ -230,6 +241,6 @@ public class CacheDataSource implements DataSource {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PlayerAuth> getLoggedPlayers() {
|
public List<PlayerAuth> getLoggedPlayers() {
|
||||||
return new ArrayList<>(PlayerCache.getInstance().getCache().values());
|
return new ArrayList<>(playerCache.getCache().values());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -189,6 +189,14 @@ public interface DataSource extends Reloadable {
|
|||||||
*/
|
*/
|
||||||
boolean updateRealName(String user, String realName);
|
boolean updateRealName(String user, String realName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the email of the user.
|
||||||
|
*
|
||||||
|
* @param user the user to retrieve an email for
|
||||||
|
* @return the email saved for the user, or null if user or email is not present
|
||||||
|
*/
|
||||||
|
DataSourceResult<String> getEmail(String user);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return all players of the database.
|
* Return all players of the database.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -0,0 +1,53 @@
|
|||||||
|
package fr.xephi.authme.datasource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps a value and allows to specify whether a value is missing or the player is not registered.
|
||||||
|
*/
|
||||||
|
public class DataSourceResult<T> {
|
||||||
|
|
||||||
|
/** Instance used when a player does not exist. */
|
||||||
|
private static final DataSourceResult UNKNOWN_PLAYER = new DataSourceResult<>(null);
|
||||||
|
private final T value;
|
||||||
|
|
||||||
|
private DataSourceResult(T value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a {@link DataSourceResult} for the given value.
|
||||||
|
*
|
||||||
|
* @param value the value to wrap
|
||||||
|
* @param <T> the value's type
|
||||||
|
* @return DataSourceResult object for the given value
|
||||||
|
*/
|
||||||
|
public static <T> DataSourceResult<T> of(T value) {
|
||||||
|
return new DataSourceResult<>(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a {@link DataSourceResult} specifying that the player does not exist.
|
||||||
|
*
|
||||||
|
* @param <T> the value type
|
||||||
|
* @return data source result for unknown player
|
||||||
|
*/
|
||||||
|
public static <T> DataSourceResult<T> unknownPlayer() {
|
||||||
|
return UNKNOWN_PLAYER;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return whether the player of the associated value exists
|
||||||
|
*/
|
||||||
|
public boolean playerExists() {
|
||||||
|
return this != UNKNOWN_PLAYER;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value. It is {@code null} if the player is unknown. It is also {@code null}
|
||||||
|
* if the player exists but does not have the value defined.
|
||||||
|
*
|
||||||
|
* @return the value, or null
|
||||||
|
*/
|
||||||
|
public T getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -396,6 +396,11 @@ public class FlatFile implements DataSource {
|
|||||||
throw new UnsupportedOperationException("Flat file no longer supported");
|
throw new UnsupportedOperationException("Flat file no longer supported");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DataSourceResult<String> getEmail(String user) {
|
||||||
|
throw new UnsupportedOperationException("Flat file no longer supported");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PlayerAuth> getAllAuths() {
|
public List<PlayerAuth> getAllAuths() {
|
||||||
BufferedReader br = null;
|
BufferedReader br = null;
|
||||||
|
|||||||
@ -879,6 +879,22 @@ public class MySQL implements DataSource {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DataSourceResult<String> getEmail(String user) {
|
||||||
|
String sql = "SELECT " + col.EMAIL + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||||
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
|
pst.setString(1, user);
|
||||||
|
try (ResultSet rs = pst.executeQuery()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
return DataSourceResult.of(rs.getString(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
logSqlException(ex);
|
||||||
|
}
|
||||||
|
return DataSourceResult.unknownPlayer();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PlayerAuth> getAllAuths() {
|
public List<PlayerAuth> getAllAuths() {
|
||||||
List<PlayerAuth> auths = new ArrayList<>();
|
List<PlayerAuth> auths = new ArrayList<>();
|
||||||
|
|||||||
@ -559,6 +559,22 @@ public class SQLite implements DataSource {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DataSourceResult<String> getEmail(String user) {
|
||||||
|
String sql = "SELECT " + col.EMAIL + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||||
|
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
|
pst.setString(1, user);
|
||||||
|
try (ResultSet rs = pst.executeQuery()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
return DataSourceResult.of(rs.getString(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
logSqlException(ex);
|
||||||
|
}
|
||||||
|
return DataSourceResult.unknownPlayer();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PlayerAuth> getAllAuths() {
|
public List<PlayerAuth> getAllAuths() {
|
||||||
List<PlayerAuth> auths = new ArrayList<>();
|
List<PlayerAuth> auths = new ArrayList<>();
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package fr.xephi.authme.initialization;
|
package fr.xephi.authme.initialization;
|
||||||
|
|
||||||
import fr.xephi.authme.ConsoleLogger;
|
import fr.xephi.authme.ConsoleLogger;
|
||||||
|
import fr.xephi.authme.data.auth.PlayerCache;
|
||||||
import fr.xephi.authme.datasource.CacheDataSource;
|
import fr.xephi.authme.datasource.CacheDataSource;
|
||||||
import fr.xephi.authme.datasource.DataSource;
|
import fr.xephi.authme.datasource.DataSource;
|
||||||
import fr.xephi.authme.datasource.DataSourceType;
|
import fr.xephi.authme.datasource.DataSourceType;
|
||||||
@ -33,6 +34,8 @@ public class DataSourceProvider implements Provider<DataSource> {
|
|||||||
private Settings settings;
|
private Settings settings;
|
||||||
@Inject
|
@Inject
|
||||||
private BukkitService bukkitService;
|
private BukkitService bukkitService;
|
||||||
|
@Inject
|
||||||
|
private PlayerCache playerCache;
|
||||||
|
|
||||||
DataSourceProvider() {
|
DataSourceProvider() {
|
||||||
}
|
}
|
||||||
@ -76,7 +79,7 @@ public class DataSourceProvider implements Provider<DataSource> {
|
|||||||
dataSource = convertFlatfileToSqlite(dataSource);
|
dataSource = convertFlatfileToSqlite(dataSource);
|
||||||
|
|
||||||
if (settings.getProperty(DatabaseSettings.USE_CACHING)) {
|
if (settings.getProperty(DatabaseSettings.USE_CACHING)) {
|
||||||
dataSource = new CacheDataSource(dataSource);
|
dataSource = new CacheDataSource(dataSource, playerCache);
|
||||||
}
|
}
|
||||||
if (DataSourceType.SQLITE.equals(dataSourceType)) {
|
if (DataSourceType.SQLITE.equals(dataSourceType)) {
|
||||||
checkDataSourceSize(dataSource, bukkitService);
|
checkDataSourceSize(dataSource, bukkitService);
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package fr.xephi.authme.command.executable.authme;
|
package fr.xephi.authme.command.executable.authme;
|
||||||
|
|
||||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
|
||||||
import fr.xephi.authme.datasource.DataSource;
|
import fr.xephi.authme.datasource.DataSource;
|
||||||
|
import fr.xephi.authme.datasource.DataSourceResult;
|
||||||
import fr.xephi.authme.message.MessageKey;
|
import fr.xephi.authme.message.MessageKey;
|
||||||
import fr.xephi.authme.service.CommonService;
|
import fr.xephi.authme.service.CommonService;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@ -38,7 +38,7 @@ public class GetEmailCommandTest {
|
|||||||
public void shouldReportUnknownUser() {
|
public void shouldReportUnknownUser() {
|
||||||
// given
|
// given
|
||||||
String user = "myTestUser";
|
String user = "myTestUser";
|
||||||
given(dataSource.getAuth(user)).willReturn(null);
|
given(dataSource.getEmail(user)).willReturn(DataSourceResult.unknownPlayer());
|
||||||
CommandSender sender = mock(CommandSender.class);
|
CommandSender sender = mock(CommandSender.class);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
@ -53,9 +53,7 @@ public class GetEmailCommandTest {
|
|||||||
// given
|
// given
|
||||||
String user = "userToView";
|
String user = "userToView";
|
||||||
String email = "user.email@example.org";
|
String email = "user.email@example.org";
|
||||||
PlayerAuth auth = mock(PlayerAuth.class);
|
given(dataSource.getEmail(user)).willReturn(DataSourceResult.of(email));
|
||||||
given(auth.getEmail()).willReturn(email);
|
|
||||||
given(dataSource.getAuth(user)).willReturn(auth);
|
|
||||||
CommandSender sender = mock(CommandSender.class);
|
CommandSender sender = mock(CommandSender.class);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
|
|||||||
@ -4,9 +4,9 @@ import ch.jalu.injector.testing.BeforeInjecting;
|
|||||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||||
import ch.jalu.injector.testing.InjectDelayed;
|
import ch.jalu.injector.testing.InjectDelayed;
|
||||||
import fr.xephi.authme.TestHelper;
|
import fr.xephi.authme.TestHelper;
|
||||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
|
||||||
import fr.xephi.authme.data.auth.PlayerCache;
|
import fr.xephi.authme.data.auth.PlayerCache;
|
||||||
import fr.xephi.authme.datasource.DataSource;
|
import fr.xephi.authme.datasource.DataSource;
|
||||||
|
import fr.xephi.authme.datasource.DataSourceResult;
|
||||||
import fr.xephi.authme.mail.EmailService;
|
import fr.xephi.authme.mail.EmailService;
|
||||||
import fr.xephi.authme.message.MessageKey;
|
import fr.xephi.authme.message.MessageKey;
|
||||||
import fr.xephi.authme.security.PasswordSecurity;
|
import fr.xephi.authme.security.PasswordSecurity;
|
||||||
@ -111,14 +111,14 @@ public class RecoverEmailCommandTest {
|
|||||||
given(sender.getName()).willReturn(name);
|
given(sender.getName()).willReturn(name);
|
||||||
given(emailService.hasAllInformation()).willReturn(true);
|
given(emailService.hasAllInformation()).willReturn(true);
|
||||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||||
given(dataSource.getAuth(name)).willReturn(null);
|
given(dataSource.getEmail(name)).willReturn(DataSourceResult.unknownPlayer());
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, Collections.singletonList("someone@example.com"));
|
command.executeCommand(sender, Collections.singletonList("someone@example.com"));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(emailService).hasAllInformation();
|
verify(emailService).hasAllInformation();
|
||||||
verify(dataSource).getAuth(name);
|
verify(dataSource).getEmail(name);
|
||||||
verifyNoMoreInteractions(dataSource);
|
verifyNoMoreInteractions(dataSource);
|
||||||
verify(commonService).send(sender, MessageKey.USAGE_REGISTER);
|
verify(commonService).send(sender, MessageKey.USAGE_REGISTER);
|
||||||
}
|
}
|
||||||
@ -131,14 +131,14 @@ public class RecoverEmailCommandTest {
|
|||||||
given(sender.getName()).willReturn(name);
|
given(sender.getName()).willReturn(name);
|
||||||
given(emailService.hasAllInformation()).willReturn(true);
|
given(emailService.hasAllInformation()).willReturn(true);
|
||||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||||
given(dataSource.getAuth(name)).willReturn(newAuthWithEmail(DEFAULT_EMAIL));
|
given(dataSource.getEmail(name)).willReturn(DataSourceResult.of(DEFAULT_EMAIL));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, Collections.singletonList(DEFAULT_EMAIL));
|
command.executeCommand(sender, Collections.singletonList(DEFAULT_EMAIL));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(emailService).hasAllInformation();
|
verify(emailService).hasAllInformation();
|
||||||
verify(dataSource).getAuth(name);
|
verify(dataSource).getEmail(name);
|
||||||
verifyNoMoreInteractions(dataSource);
|
verifyNoMoreInteractions(dataSource);
|
||||||
verify(commonService).send(sender, MessageKey.INVALID_EMAIL);
|
verify(commonService).send(sender, MessageKey.INVALID_EMAIL);
|
||||||
}
|
}
|
||||||
@ -151,14 +151,14 @@ public class RecoverEmailCommandTest {
|
|||||||
given(sender.getName()).willReturn(name);
|
given(sender.getName()).willReturn(name);
|
||||||
given(emailService.hasAllInformation()).willReturn(true);
|
given(emailService.hasAllInformation()).willReturn(true);
|
||||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||||
given(dataSource.getAuth(name)).willReturn(newAuthWithEmail("raptor@example.org"));
|
given(dataSource.getEmail(name)).willReturn(DataSourceResult.of("raptor@example.org"));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, Collections.singletonList("wrong-email@example.com"));
|
command.executeCommand(sender, Collections.singletonList("wrong-email@example.com"));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(emailService).hasAllInformation();
|
verify(emailService).hasAllInformation();
|
||||||
verify(dataSource).getAuth(name);
|
verify(dataSource).getEmail(name);
|
||||||
verifyNoMoreInteractions(dataSource);
|
verifyNoMoreInteractions(dataSource);
|
||||||
verify(commonService).send(sender, MessageKey.INVALID_EMAIL);
|
verify(commonService).send(sender, MessageKey.INVALID_EMAIL);
|
||||||
}
|
}
|
||||||
@ -173,7 +173,7 @@ public class RecoverEmailCommandTest {
|
|||||||
given(emailService.sendRecoveryCode(anyString(), anyString(), anyString())).willReturn(true);
|
given(emailService.sendRecoveryCode(anyString(), anyString(), anyString())).willReturn(true);
|
||||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||||
String email = "v@example.com";
|
String email = "v@example.com";
|
||||||
given(dataSource.getAuth(name)).willReturn(newAuthWithEmail(email));
|
given(dataSource.getEmail(name)).willReturn(DataSourceResult.of(email));
|
||||||
String code = "a94f37";
|
String code = "a94f37";
|
||||||
given(recoveryCodeService.isRecoveryCodeNeeded()).willReturn(true);
|
given(recoveryCodeService.isRecoveryCodeNeeded()).willReturn(true);
|
||||||
given(recoveryCodeService.generateCode(name)).willReturn(code);
|
given(recoveryCodeService.generateCode(name)).willReturn(code);
|
||||||
@ -183,7 +183,7 @@ public class RecoverEmailCommandTest {
|
|||||||
|
|
||||||
// then
|
// then
|
||||||
verify(emailService).hasAllInformation();
|
verify(emailService).hasAllInformation();
|
||||||
verify(dataSource).getAuth(name);
|
verify(dataSource).getEmail(name);
|
||||||
verify(recoveryService).createAndSendRecoveryCode(sender, email);
|
verify(recoveryService).createAndSendRecoveryCode(sender, email);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,8 +197,7 @@ public class RecoverEmailCommandTest {
|
|||||||
given(emailService.sendPasswordMail(anyString(), anyString(), anyString())).willReturn(true);
|
given(emailService.sendPasswordMail(anyString(), anyString(), anyString())).willReturn(true);
|
||||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||||
String email = "vulture@example.com";
|
String email = "vulture@example.com";
|
||||||
PlayerAuth auth = newAuthWithEmail(email);
|
given(dataSource.getEmail(name)).willReturn(DataSourceResult.of(email));
|
||||||
given(dataSource.getAuth(name)).willReturn(auth);
|
|
||||||
given(recoveryCodeService.isRecoveryCodeNeeded()).willReturn(false);
|
given(recoveryCodeService.isRecoveryCodeNeeded()).willReturn(false);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
@ -206,14 +205,7 @@ public class RecoverEmailCommandTest {
|
|||||||
|
|
||||||
// then
|
// then
|
||||||
verify(emailService).hasAllInformation();
|
verify(emailService).hasAllInformation();
|
||||||
verify(dataSource).getAuth(name);
|
verify(dataSource).getEmail(name);
|
||||||
verify(recoveryService).generateAndSendNewPassword(sender, email);
|
verify(recoveryService).generateAndSendNewPassword(sender, email);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static PlayerAuth newAuthWithEmail(String email) {
|
|
||||||
return PlayerAuth.builder()
|
|
||||||
.name("name")
|
|
||||||
.email(email)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,7 @@ import static org.hamcrest.Matchers.empty;
|
|||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.hasItem;
|
import static org.hamcrest.Matchers.hasItem;
|
||||||
import static org.hamcrest.Matchers.hasSize;
|
import static org.hamcrest.Matchers.hasSize;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.hamcrest.Matchers.nullValue;
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assume.assumeThat;
|
import static org.junit.Assume.assumeThat;
|
||||||
@ -383,4 +384,20 @@ public abstract class AbstractDataSourceIntegrationTest {
|
|||||||
// then
|
// then
|
||||||
assertThat(dataSource.getAllAuths(), empty());
|
assertThat(dataSource.getAllAuths(), empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldFetchEmail() {
|
||||||
|
// given
|
||||||
|
String user1 = "user";
|
||||||
|
String user2 = "Bogus";
|
||||||
|
DataSource dataSource = getDataSource();
|
||||||
|
|
||||||
|
// when
|
||||||
|
DataSourceResult<String> email1 = dataSource.getEmail(user1);
|
||||||
|
DataSourceResult<String> email2 = dataSource.getEmail(user2);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(email1.getValue(), equalTo("user@example.org"));
|
||||||
|
assertThat(email2, is(DataSourceResult.unknownPlayer()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user