删除测试库

删除 wecome 增加新代码
This commit is contained in:
Sabrita 2023-02-27 18:34:06 +08:00
parent 9fd532d798
commit cfca8ccd85
345 changed files with 880 additions and 34530 deletions

View File

@ -47,7 +47,7 @@
<licenses>
<license>
<name>The GNU General Public Licence version 3 (GPLv3)</name>
<name>The GNU Public Licence version 3 (GPLv3)</name>
<url>https://www.gnu.org/licenses/gpl-3.0.html</url>
<distribution>repo</distribution>
</license>
@ -196,6 +196,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>mmdb</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
<!-- Compile and include classes -->
<plugin>

View File

@ -19,6 +19,7 @@ import fr.xephi.authme.listener.PlayerListener111;
import fr.xephi.authme.listener.PlayerListener19;
import fr.xephi.authme.listener.PlayerListener19Spigot;
import fr.xephi.authme.listener.ServerListener;
import fr.xephi.authme.mail.EmailService;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.security.crypts.Sha256;
import fr.xephi.authme.service.BackupService;
@ -28,6 +29,7 @@ import fr.xephi.authme.service.bungeecord.BungeeReceiver;
import fr.xephi.authme.service.yaml.YamlParseException;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.SettingsWarner;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.CleanupTask;
import fr.xephi.authme.task.purge.PurgeService;
@ -42,6 +44,8 @@ import org.bukkit.plugin.java.JavaPluginLoader;
import org.bukkit.scheduler.BukkitScheduler;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.function.Consumer;
import static fr.xephi.authme.service.BukkitService.TICKS_PER_MINUTE;
@ -65,6 +69,7 @@ public class AuthMe extends JavaPlugin {
private CommandHandler commandHandler;
private Settings settings;
private DataSource database;
private EmailService emailService;
private BukkitService bukkitService;
private Injector injector;
private BackupService backupService;
@ -308,6 +313,12 @@ public class AuthMe extends JavaPlugin {
onShutdownPlayerSaver.saveAllPlayers();
}
if (settings.getProperty(EmailSettings.SHOUTDOWN_MAIL)){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy'年'MM'月'dd'日' HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
emailService.sendShutDown("wujiaxin752@icloud.com",dateFormat.format(date));
}
// Do backup on stop if enabled
if (backupService != null) {
backupService.doBackup(BackupService.BackupCause.STOP);

View File

@ -5,6 +5,7 @@ import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.data.captcha.RegistrationCaptchaManager;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.mail.EmailService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.process.register.RegisterSecondaryArgument;
@ -23,6 +24,8 @@ import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import static fr.xephi.authme.process.register.RegisterSecondaryArgument.CONFIRMATION;
import static fr.xephi.authme.process.register.RegisterSecondaryArgument.EMAIL_MANDATORY;
@ -43,6 +46,9 @@ public class RegisterCommand extends PlayerCommand {
@Inject
private CommonService commonService;
@Inject
private DataSource dataSource;
@Inject
private EmailService emailService;
@ -169,6 +175,22 @@ public class RegisterCommand extends PlayerCommand {
} else if (isSecondArgValidForEmailRegistration(player, arguments)) {
management.performRegister(RegistrationMethod.EMAIL_REGISTRATION,
EmailRegisterParams.of(player, email));
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (dataSource.getAuth(player.getName()) != null) {
if (dataSource.getAuth(player.getName()).getLastLogin() == null && (dataSource.getAuth(player.getName()).getRegistrationDate() + 600000) < System.currentTimeMillis()) {
management.performUnregisterByAdmin(null, player.getName(), player);
timer.cancel();
} else if (dataSource.getAuth(player.getName()).getLastLogin() != null) {
timer.cancel();
}
} else {
timer.cancel();
}
}
}, 60000, 60000);
}
}

View File

@ -15,6 +15,8 @@ import fr.xephi.authme.util.expiring.ExpiringMap;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
@ -132,12 +134,14 @@ public class VerificationCodeManager implements SettingsDependent, HasCleanup {
*/
private void generateCode(String name) {
DataSourceValue<String> emailResult = dataSource.getEmail(name);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy'年'MM'月'dd'日' HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
if (emailResult.rowExists()) {
final String email = emailResult.getValue();
if (!Utils.isEmailEmpty(email)) {
String code = RandomStringUtils.generateNum(6); // 6 digits code
verificationCodes.put(name.toLowerCase(Locale.ROOT), code);
emailService.sendVerificationMail(name, email, code);
emailService.sendVerificationMail(name, email, code, dateFormat.format(date));
}
}
}

View File

@ -40,21 +40,7 @@ public class EmailService {
return sendMailSsl.hasAllInformation();
}
/**
* Sends an email to the user with his new password.
*
* @param name the name of the player
* @param mailAddress the player's email
* @param newPass the new password
* @return true if email could be sent, false otherwise
*/
public boolean sendPasswordMail(String name, String mailAddress, String newPass) {
if (!hasAllInformation()) {
logger.warning("Cannot perform email registration: not all email settings are complete");
return false;
}
public boolean sendNewPasswordMail(String name, String mailAddress, String newPass,String ip,String time) {
HtmlEmail email;
try {
email = sendMailSsl.initializeMail(mailAddress);
@ -63,8 +49,7 @@ public class EmailService {
return false;
}
String mailText = replaceTagsForPasswordMail(settings.getPasswordEmailMessage(), name, newPass);
// Generate an image?
String mailText = replaceTagsForPasswordMail(settings.getNewPasswordEmailMessage(), name, newPass,ip,time);
File file = null;
if (settings.getProperty(EmailSettings.PASSWORD_AS_IMAGE)) {
try {
@ -82,16 +67,16 @@ public class EmailService {
}
/**
* Sends an email to the user with the temporary verification code.
* Sends an email to the user with his new password.
*
* @param name the name of the player
* @param mailAddress the player's email
* @param code the verification code
* @param newPass the new password
* @return true if email could be sent, false otherwise
*/
public boolean sendVerificationMail(String name, String mailAddress, String code) {
public boolean sendPasswordMail(String name, String mailAddress, String newPass, String time) {
if (!hasAllInformation()) {
logger.warning("Cannot send verification email: not all email settings are complete");
logger.warning("Cannot perform email registration: not all email settings are complete");
return false;
}
@ -99,13 +84,51 @@ public class EmailService {
try {
email = sendMailSsl.initializeMail(mailAddress);
} catch (EmailException e) {
logger.logException("Failed to create verification email with the given settings:", e);
logger.logException("Failed to create email with the given settings:", e);
return false;
}
String mailText = replaceTagsForPasswordMail(settings.getPasswordEmailMessage(), name, newPass,time);
// Generate an image?
File file = null;
if (settings.getProperty(EmailSettings.PASSWORD_AS_IMAGE)) {
try {
file = generatePasswordImage(name, newPass);
mailText = embedImageIntoEmailContent(file, email, mailText);
} catch (IOException | EmailException e) {
logger.logException(
"Unable to send new password as image for email " + mailAddress + ":", e);
}
}
boolean couldSendEmail = sendMailSsl.sendEmail(mailText, email);
FileUtils.delete(file);
return couldSendEmail;
}
/**
* Sends an email to the user with the temporary verification code.
*
* @param name the name of the player
* @param mailAddress the player's email
* @param code the verification code
*/
public void sendVerificationMail(String name, String mailAddress, String code, String time) {
if (!hasAllInformation()) {
logger.warning("Cannot send verification email: not all email settings are complete");
return;
}
HtmlEmail email;
try {
email = sendMailSsl.initializeMail(mailAddress);
} catch (EmailException e) {
logger.logException("Failed to create verification email with the given settings:", e);
return;
}
String mailText = replaceTagsForVerificationEmail(settings.getVerificationEmailMessage(), name, code,
settings.getProperty(SecuritySettings.VERIFICATION_CODE_EXPIRATION_MINUTES));
return sendMailSsl.sendEmail(mailText, email);
settings.getProperty(SecuritySettings.VERIFICATION_CODE_EXPIRATION_MINUTES),time);
sendMailSsl.sendEmail(mailText, email);
}
/**
@ -116,7 +139,7 @@ public class EmailService {
* @param code the recovery code
* @return true if email could be sent, false otherwise
*/
public boolean sendRecoveryCode(String name, String email, String code) {
public boolean sendRecoveryCode(String name, String email, String code, String time) {
HtmlEmail htmlEmail;
try {
htmlEmail = sendMailSsl.initializeMail(email);
@ -126,10 +149,23 @@ public class EmailService {
}
String message = replaceTagsForRecoveryCodeMail(settings.getRecoveryCodeEmailMessage(),
name, code, settings.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID));
name, code, settings.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID),time);
return sendMailSsl.sendEmail(message, htmlEmail);
}
public void sendShutDown(String email, String time) {
HtmlEmail htmlEmail;
try {
htmlEmail = sendMailSsl.initializeMail(email);
} catch (EmailException e) {
logger.logException("Failed to create email for recovery code:", e);
return;
}
String message = replaceTagsForShutDownMail(settings.getShutdownEmailMessage(), time);
sendMailSsl.sendEmail(message, htmlEmail);
}
private File generatePasswordImage(String name, String newPass) throws IOException {
ImageGenerator gen = new ImageGenerator(newPass);
File file = new File(dataFolder, name + "_new_pass.jpg");
@ -144,26 +180,44 @@ public class EmailService {
return content.replace("<image />", "<img src=\"cid:" + tag + "\">");
}
private String replaceTagsForPasswordMail(String mailText, String name, String newPass) {
private String replaceTagsForPasswordMail(String mailText, String name, String newPass,String ip,String time) {
return mailText
.replace("<playername />", name)
.replace("<servername />", settings.getProperty(PluginSettings.SERVER_NAME))
.replace("<generatedpass />", newPass);
.replace("<generatedpass />", newPass)
.replace("<playerip />", ip)
.replace("<time />", time);
}
private String replaceTagsForVerificationEmail(String mailText, String name, String code, int minutesValid) {
private String replaceTagsForPasswordMail(String mailText, String name, String newPass, String time) {
return mailText
.replace("<playername />", name)
.replace("<servername />", settings.getProperty(PluginSettings.SERVER_NAME))
.replace("<generatedpass />", newPass)
.replace("<time />", time);
}
private String replaceTagsForVerificationEmail(String mailText, String name, String code, int minutesValid, String time) {
return mailText
.replace("<playername />", name)
.replace("<servername />", settings.getProperty(PluginSettings.SERVER_NAME))
.replace("<generatedcode />", code)
.replace("<minutesvalid />", String.valueOf(minutesValid));
.replace("<minutesvalid />", String.valueOf(minutesValid))
.replace("<time />", time);
}
private String replaceTagsForRecoveryCodeMail(String mailText, String name, String code, int hoursValid) {
private String replaceTagsForRecoveryCodeMail(String mailText, String name, String code, int hoursValid, String time) {
return mailText
.replace("<playername />", name)
.replace("<servername />", settings.getProperty(PluginSettings.SERVER_NAME))
.replace("<recoverycode />", code)
.replace("<hoursvalid />", String.valueOf(hoursValid));
.replace("<hoursvalid />", String.valueOf(hoursValid))
.replace("<time />", time);
}
private String replaceTagsForShutDownMail(String mailText, String time) {
return mailText
.replace("<servername />", settings.getProperty(PluginSettings.SERVER_NAME))
.replace("<time />", time);
}
}

View File

@ -9,11 +9,15 @@ import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.util.PlayerUtils;
import fr.xephi.authme.util.RandomStringUtils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.Date;
import static fr.xephi.authme.permission.PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS;
import static fr.xephi.authme.process.register.executors.PlayerAuthBuilderHelper.createPlayerAuth;
import static fr.xephi.authme.settings.properties.EmailSettings.RECOVERY_PASSWORD_LENGTH;
@ -64,8 +68,10 @@ class EmailRegisterExecutor implements RegistrationExecutor<EmailRegisterParams>
@Override
public void executePostPersistAction(EmailRegisterParams params) {
Player player = params.getPlayer();
boolean couldSendMail = emailService.sendPasswordMail(
player.getName(), params.getEmail(), params.getPassword());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy'年'MM'月'dd'日' HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
boolean couldSendMail = emailService.sendNewPasswordMail(
player.getName(), params.getEmail(), params.getPassword(), PlayerUtils.getPlayerIp(player), dateFormat.format(date));
if (couldSendMail) {
syncProcessManager.processSyncEmailRegister(player);
} else {

View File

@ -20,6 +20,8 @@ import org.bukkit.entity.Player;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
@ -72,9 +74,10 @@ public class PasswordRecoveryService implements Reloadable, HasCleanup {
if (!checkEmailCooldown(player)) {
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy'年'MM'月'dd'日' HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
String recoveryCode = recoveryCodeService.generateCode(player.getName());
boolean couldSendMail = emailService.sendRecoveryCode(player.getName(), email, recoveryCode);
boolean couldSendMail = emailService.sendRecoveryCode(player.getName(), email, recoveryCode, dateFormat.format(date));
if (couldSendMail) {
commonService.send(player, MessageKey.RECOVERY_CODE_SENT);
emailCooldown.add(player.getName().toLowerCase(Locale.ROOT));
@ -94,6 +97,8 @@ public class PasswordRecoveryService implements Reloadable, HasCleanup {
if (!checkEmailCooldown(player)) {
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy'年'MM'月'dd'日' HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
String name = player.getName();
String thePass = RandomStringUtils.generate(commonService.getProperty(RECOVERY_PASSWORD_LENGTH));
@ -102,7 +107,7 @@ public class PasswordRecoveryService implements Reloadable, HasCleanup {
logger.info("Generating new password for '" + name + "'");
dataSource.updatePassword(name, hashNew);
boolean couldSendMail = emailService.sendPasswordMail(name, email, thePass);
boolean couldSendMail = emailService.sendPasswordMail(name, email, thePass, dateFormat.format(date));
if (couldSendMail) {
commonService.send(player, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE);
emailCooldown.add(player.getName().toLowerCase(Locale.ROOT));

View File

@ -17,7 +17,6 @@ import fr.xephi.authme.settings.properties.ProtectionSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.PlayerUtils;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -50,6 +49,7 @@ public class ValidationService implements Reloadable {
private GeoIpService geoIpService;
private Pattern passwordRegex;
private Pattern emailRegex;
private Multimap<String, String> restrictedNames;
ValidationService() {
@ -62,6 +62,7 @@ public class ValidationService implements Reloadable {
restrictedNames = settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)
? loadNameRestrictions(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
: HashMultimap.create();
emailRegex = Utils.safePatternCompile(settings.getProperty(RestrictionSettings.ALLOWED_EMAIL_REGEX));
}
/**
@ -93,12 +94,7 @@ public class ValidationService implements Reloadable {
* @return true if the email is valid, false otherwise
*/
public boolean validateEmail(String email) {
if (Utils.isEmailEmpty(email) || !StringUtils.isInsideString('@', email)) {
return false;
}
final String emailDomain = email.split("@")[1];
return validateWhitelistAndBlacklist(
emailDomain, EmailSettings.DOMAIN_WHITELIST, EmailSettings.DOMAIN_BLACKLIST);
return emailRegex.matcher(email).matches();
}
/**

View File

@ -24,6 +24,9 @@ public class Settings extends SettingsManagerImpl {
private String passwordEmailMessage;
private String verificationEmailMessage;
private String recoveryCodeEmailMessage;
private String shutdownEmailMessage;
private String newPasswordEmailMessage;
/**
* Constructor.
@ -67,10 +70,19 @@ public class Settings extends SettingsManagerImpl {
return recoveryCodeEmailMessage;
}
public String getShutdownEmailMessage() {return shutdownEmailMessage;}
public String getNewPasswordEmailMessage() {
return newPasswordEmailMessage;
}
private void loadSettingsFromFiles() {
newPasswordEmailMessage = readFile("new_email.html");
passwordEmailMessage = readFile("email.html");
verificationEmailMessage = readFile("verification_code_email.html");
recoveryCodeEmailMessage = readFile("recovery_code_email.html");
shutdownEmailMessage = readFile("shutdown.html");
String country = readFile("GeoLite2-Country.mmdb");
}
@Override

View File

@ -74,6 +74,9 @@ public final class EmailSettings implements SettingsHolder {
@Comment("The OAuth2 token")
public static final Property<String> OAUTH2_TOKEN =
newProperty("Email.emailOauth2Token", "");
@Comment("Email notifications when the server shuts down")
public static final Property<Boolean> SHOUTDOWN_MAIL =
newProperty("Email.shutDownEmail", true);
private EmailSettings() {
}

View File

@ -165,6 +165,11 @@ public final class RestrictionSettings implements SettingsHolder {
public static final Property<String> ALLOWED_PASSWORD_REGEX =
newProperty("settings.restrictions.allowedPasswordCharacters", "[!-~]*");
@Comment("Regex syntax for allowed chars in email.")
public static final Property<String> ALLOWED_EMAIL_REGEX =
newProperty("settings.restrictions.allowedEmailCharacters", "^[A-Za-z0-9]{4,15}@(qq|163|icloud).com$");
@Comment("Force survival gamemode when player joins?")
public static final Property<Boolean> FORCE_SURVIVAL_MODE =
newProperty("settings.GameMode.ForceSurvivalMode", false);

Binary file not shown.

View File

@ -1,18 +1,120 @@
<h1>
Dear <playername />,
</h1>
<p>
This is your new AuthMe password for the server <servername />:
</p>
<p>
<generatedpass />
</p>
<image />
<p>
Do not forget to change password after login!<br />
/changepassword <generatedpass /> newPassword'
</p>
<p>
See you on <servername />!
</p>
<div class="mail">
<style>
.mail td{
font-family: "Segoe UI", sans-serif;
text-align: center;
font-size: 16px;
color: #718096;
}
.mail th, div, p, a, h1, h2, h3, h4, h5, h6{
font-family: "Segoe UI", sans-serif;
text-align: center;
color: #3d4852;
}
.mail .mail-content {
max-width: 100vw;
padding: 32px;
box-shadow: 0 15px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.25);
}
.mail div{
background-color: #ffffff;color: #718096;height: 100%;line-height: 1.4;width: 100%;
}
.mail .x_wrapper{
background-color: #edf2f7;
width:100%;
}
.mail .x_content{
width: 100%;
}
.mail .x_inner-body{
background-color: #ffffff;border-color: #e8e5ef;border-radius: 2px;border-width: 1px;margin: 0 auto;padding:0;width: 570px;
}
.mail .x_content-cell{
margin: 0 auto;padding: 0;width: 570px;line-height: 1.5em;
color: #b0adc5;font-size: 12px;
}
.mail .x_content-cell td{
max-width: 100vw;padding: 32px;
}
</style>
<div>
<table class="x_wrapper">
<tbody>
<tr>
<td>
<table class="x_content">
<tbody>
<tr>
<td style="padding: 25px 0;">
<h1 style="font-size: 20px;">密码重置邮件</h1>
</td>
</tr>
<tr>
<td>
<table class="x_inner-body">
<tbody>
<tr>
<td class="mail-content">
<h1 style="font-size: 18px;margin-bottom: 25px;">Minecraft · <servername /><br/><playername /></h1>
<hr>
<table style="width: 100%;">
<tbody>
<tr>
<td>
您正在申请的新密码为
</td>
</tr>
<tr>
<td style="font-weight: bold;">
<br/><generatedpass /></td>
</tr>
<tr>
<td>
<br/>请妥善保存,在新地址上进行登录时,需提供该密码.
<br/>若非必要,请勿更换密码,否则将对您的账户安全构成威胁.
</td>
</tr>
<tr>
<td style="text-align:right;">
<br/>
<br/>祝您游玩愉快~!
</td>
</tr>
<tr>
<td>
<small>
<br/><time />
<br/>请勿回复
</small>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="x_content-cell">
<tbody>
<tr>
<td>
<p style="color:#b0adc5;">© 2023 IrisCraft. All rights reserved.</p>
<a href="https://www.mcbbs.net/thread-1263385-1-1.html" target="_blank" style="text-decoration: none; font-size: 16px">iriscraft.work</a>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</div>

View File

@ -1,8 +1,5 @@
# AuthmeReloaded帮助文件汉化
# Translated By CH1
# -------------------------------------------------------
common:
header: '==========[ AuthMeReloaded ]=========='
header: '======================================'
optional: '可选'
hasPermission: '您拥有权限去使用这个指令'
noPermission: '您没有权限使用这个指令'

View File

@ -5,132 +5,140 @@
# Registration
registration:
disabled: '&8[&6玩家系统&8] &c目前服务器暂时禁止注册请到服务器论坛以得到更多资讯'
name_taken: '&8[&6玩家系统&8] &c此用户已经在此服务器注册过'
register_request: '&8[&6玩家系统&8] &c请输入“/register <密码> <再输入一次以确定密码>”以注册'
command_usage: '&8[&6玩家系统&8] &c正确用法“/register <密码> <再输入一次以确定密码>”'
reg_only: '&8[&6玩家系统&8] &f只允许注册过的玩家进服请到 https://example.cn 注册'
success: '&8[&6玩家系统&8] &c已成功注册'
kicked_admin_registered: '有一位管理员刚刚为您完成了注册,请重新登录'
disabled: '&c注册已被禁止'
name_taken: '&c此用户已经在此服务器注册过'
register_request: '&7请输入“/register <邮箱>”以注册'
command_usage: '&c正确用法:“/register <邮箱>”'
reg_only: '&c只允许注册过的玩家进服!'
success: '&a*** 已成功注册 ***'
# Password errors on registration
password:
match_error: '&8[&6玩家系统&8] &f密码不相同'
name_in_password: '&8[&6玩家系统&8] &f你不能使用你的名字作为密码。 '
unsafe_password: '&8[&6玩家系统&8] &f你不能使用安全性过低的密码。 '
forbidden_characters: '&4您的密码包含了非法字符可使用的字符: %valid_chars'
wrong_length: '&8[&6玩家系统&8] &c你的密码不符合要求'
match_error: '&c密码不相同'
name_in_password: '&c您不能使用您的名字作为密码。 '
unsafe_password: '&c您不能使用安全性过低的密码。 '
forbidden_characters: '&4您的密码包含了非法字符.可使用的字符: %valid_chars'
wrong_length: '&4您的密码没有达到要求!'
# Login
login:
command_usage: '&8[&6玩家系统&8] &c正确用法“/login <密码>”'
wrong_password: '&8[&6玩家系统&8] &c错误的密码'
success: '&8[&6玩家系统&8] &c已成功登录'
login_request: '&8[&6玩家系统&8] &c请输入“/login <密码>”以登录'
timeout_error: '&8[&6玩家系统&8] &f登录超时'
command_usage: '&c正确用法:“/login <密码>”'
wrong_password: '&c*** 密码错误 ***'
success: '&a*** 已成功登录 ***'
login_request: '&c请输入“/login <密码>”以登录'
timeout_error: '给您登录的时间已经过了'
# Errors
error:
denied_command: '&c您需要先通过验证才能使用该命令'
denied_chat: '&c您需要先通过验证才能聊天'
unregistered_user: '&8[&6玩家系统&8] &c此用户名还未注册过'
not_logged_in: '&8[&6玩家系统&8] &c你还未登录'
no_permission: '&8[&6玩家系统&8] &c没有权限'
unexpected_error: '&8[&6玩家系统&8] &f发现错误请联系管理员'
max_registration: '&8[&6玩家系统&8] &f你不允许再为你的IP在服务器注册更多用户了'
logged_in: '&8[&6玩家系统&8] &c你已经登陆过了'
kick_for_vip: '&8[&6玩家系统&8] &cA VIP玩家加入了已满的服务器!'
kick_unresolved_hostname: '&8[&6玩家系统&8] &c发生了一个错误: 无法解析玩家的Hostname'
tempban_max_logins: '&c由于您登录失败次数过多已被暂时禁止登录。'
denied_command: '&7您需要先通过验证才能使用该命令!'
denied_chat: '&7您需要先通过验证才能聊天!'
unregistered_user: '&c此用户名还未注册过'
not_logged_in: '&c您还未登录!'
no_permission: '&c没有权限'
unexpected_error: '&4发现错误,请联系管理员'
max_registration: '&c该地址已无法注册,请联系管理员进行注册.'
logged_in: '&c您已经登陆过了!'
kick_for_vip: '&c一个VIP玩家加入了已满的服务器!'
kick_unresolved_hostname: '&c发生了一个错误: 无法解析玩家的主机名'
tempban_max_logins: '&c由于您登录失败次数过多,已被暂时禁止登录。'
# AntiBot
antibot:
kick_antibot: '&8[&6玩家系统&8] &f验证程序已启用 !请稍等几分钟后再次进入服务器'
auto_enabled: '&8[&6玩家系统&8] &f验证程序由于大量异常连接而启用'
auto_disabled: '&8[&6玩家系统&8] &f验证程序由于异常连接减少而在 %m 分钟后停止'
kick_antibot: '连接异常,请稍后加入'
auto_enabled: '&c由于发生大量异常连接,本服将禁止连接.'
auto_disabled: '&a异常连接减少,本服将在 &a%m &a分钟后自动开放连接.'
# Unregister
unregister:
success: '&8[&6玩家系统&8] &c成功删除此用户'
command_usage: '&8[&6玩家系统&8] &c正确用法“/unregister <密码>”'
success: '&a*** 已成功注销 ***'
command_usage: '&c正确用法:“/unregister <密码>”'
# Other messages
misc:
account_not_activated: '&8[&6玩家系统&8] &f你的帐号还未激活请查看你的邮箱'
password_changed: '&8[&6玩家系统&8] &c密码已成功修改'
logout: '&8[&6玩家系统&8] &c已成功登出'
reload: '&8[&6玩家系统&8] &f配置以及数据已经重新加载完毕'
usage_change_password: '&8[&6玩家系统&8] &f正确用法“/changepassword 旧密码 新密码”'
accounts_owned_self: '您拥有 %count 个账户:'
accounts_owned_other: '玩家 %name 拥有 %count 个账户:'
account_not_activated: |-
&a一封包含密码的邮件已发送至您的收件箱.
&a请在十分钟内完成激活,否则将注销此账户.
not_activated: '&c账户未激活,请注册激活后再次尝试.'
password_changed: '&a*** 密码已修改 ***'
logout: '&a*** 已成功登出 ***'
reload: '&a配置以及数据已经重新加载完毕'
usage_change_password: '&a正确用法:“/changepassword 旧密码 新密码”'
accounts_owned_self: '您拥有 %count 个账户:'
accounts_owned_other: '玩家 %name 拥有 %count 个账户:'
# Session messages
session:
valid_session: '&8[&6玩家系统&8] &c欢迎回来已帮你自动登录到此服务器'
invalid_session: '&8[&6玩家系统&8] &f登录数据异常请等待登录结束'
valid_session: ''
invalid_session: '&c*** 请重新登录 ***'
# Error messages when joining
on_join_validation:
same_ip_online: '已有一个同IP玩家在游戏中了'
same_nick_online: '&8[&6玩家系统&8] &f同样的用户名现在在线且已经登录了'
name_length: '&8[&6玩家系统&8] &c你的用户名太短或者太长了'
characters_in_name: '&8[&6玩家系统&8] &c你的用户名包含非法字母用户名里允许的字母: %valid_chars'
kick_full_server: '&8[&6玩家系统&8] &c抱歉服务器已满!'
country_banned: '这个服务器禁止该国家登陆'
not_owner_error: '&8[&6玩家系统&8] &4警告 &c你并不是此帐户的拥有者请立即登出 '
invalid_name_case: '&8[&6玩家系统&8] &c你应该使用「%valid」而并非「%invalid」登入游戏。 '
quick_command: '&8[&6玩家系统&8] &c您发送命令的速度太快了请重新加入服务器再等待一会后再使用命令'
same_ip_online: '已有一个同IP玩家在游戏中了!'
same_nick_online: '&a同样的用户名现在在线且已经登录了!'
name_length: '&c您的用户名太短或者太长了'
characters_in_name: '&c您的用户名不符合标准.'
kick_full_server: '&c抱歉,服务器已满!'
country_banned: |-
&6[&b&lAccount Security System&6]
&c为保证您的游玩体验,请使用中国境内网络连接.
&cTo ensure your play experience,please use the Internet connection within China.
not_owner_error: |-
&6[&b&lAccount Security System&6]
&c请勿尝试登陆系统账户,系统账户受安全系统保护.
&c如果您并不知情,请更换您的用户名重新加入该服务器.
invalid_name_case: '&c您应该使用 %valid 登录服务器,当前名字: %invalid .'
quick_command: '&c您发送命令的速度太快了,请重新加入服务器等待一会后再使用命令'
# Email
email:
add_email_request: '&8[&6玩家系统&8] &c请输入“/email add <你的邮箱> <再输入一次以确认>”以添加您的邮箱到此帐号'
usage_email_add: '&8[&6玩家系统&8] &f用法: /email add <邮箱> <确认邮箱地址> '
usage_email_change: '&8[&6玩家系统&8] &f用法: /email change <旧邮箱> <新邮箱> '
new_email_invalid: '&8[&6玩家系统&8] &f新邮箱无效!'
old_email_invalid: '&8[&6玩家系统&8] &f旧邮箱无效!'
invalid: '&8[&6玩家系统&8] &f无效的邮箱'
added: '&8[&6玩家系统&8] &f邮箱已添加 !'
add_not_allowed: '&8[&6玩家系统&8] &c服务器不允许添加邮箱地址'
request_confirmation: '&8[&6玩家系统&8] &f确认你的邮箱 !'
changed: '&8[&6玩家系统&8] &f邮箱已修改 !'
change_not_allowed: '&8[&6玩家系统&8] &c服务器不允许修改邮箱地址'
email_show: '&8[&6玩家系统&8] &2您当前的电子邮件地址为 &f%email'
no_email_for_account: '&8[&6玩家系统&8] &2您当前并没有任何邮箱与该账号绑定'
already_used: '&8[&6玩家系统&8] &4邮箱已被使用'
incomplete_settings: '&8[&6玩家系统&8] 错误:必要设置未设定完成,请联系管理员'
send_failure: '&8[&6玩家系统&8] 邮件发送失败,请联系管理员'
change_password_expired: '&8[&6玩家系统&8] 您不能使用此命令更改密码'
email_cooldown_error: '&8[&6玩家系统&8] &c邮件已在几分钟前发送,您需要等待 %time 后才能再次请求发送'
add_email_request: ''
usage_email_add: '&a用法: /email add <邮箱> <确认邮箱> '
usage_email_change: '&a用法: /email change <旧邮箱> <新邮箱> '
new_email_invalid: '&c新邮箱无效!'
old_email_invalid: '&c旧邮箱无效!'
invalid: '&c无效的邮箱'
added: '&a*** 邮箱已添加 ***'
add_not_allowed: '&c服务器不允许添加电子邮箱'
request_confirmation: '&c确认您的邮箱'
changed: '&a*** 邮箱已修改 ***'
change_not_allowed: '&c服务器不允许修改邮箱地址'
email_show: '&a该账户使用的电子邮箱为: &a%email'
no_email_for_account: '&c当前并没有任何邮箱与该账号绑定'
already_used: '&c邮箱已被使用'
incomplete_settings: '&c错误: 必要设置未设定完成,请联系管理员'
send_failure: '&c邮件已被作废,请检查您的邮箱是否正常工作.'
change_password_expired: '&c您不能使用此命令更改密码'
email_cooldown_error: '&c您需要等待 %time 后才能再次请求发送'
# Password recovery by email
recovery:
forgot_password_hint: '&8[&6玩家系统&8] &c忘了你的密码请输入“/email recovery <你的邮箱>”'
command_usage: '&8[&6玩家系统&8] &f用法: /email recovery <邮箱>'
email_sent: '&8[&6玩家系统&8] &f找回密码邮件已发送 !'
forgot_password_hint: '&c忘了您的密码请输入:“/email recovery <您的邮箱>”'
command_usage: '&a用法: /email recovery <邮箱>'
email_sent: '&a找回密码邮件已发送!'
code:
code_sent: '一个用于重置您的密码的验证码已发到您的邮箱'
incorrect: '验证码不正确! 使用 /email recovery [邮箱] 以生成新的验证码'
tries_exceeded: '您已经达到输入验证码次数的最大允许次数。请使用 "/email recovery [邮箱]" 来生成一个新的'
correct: '验证码正确!'
change_password: '请使用 /email setpassword <新密码> 立即设置新的密码'
code_sent: '&a*** 已发送验证邮件 ***'
incorrect: '&a验证码不正确! 使用 /email recovery [邮箱] 以生成新的验证码'
tries_exceeded: '&a您已经达到输入验证码次数的最大允许次数.'
correct: '&a*** 验证通过 ***'
change_password: '&c请使用 /email setpassword <新密码> 立即设置新的密码'
# Captcha
captcha:
usage_captcha: '&8[&6玩家系统&8] &c正确用法/captcha %captcha_code'
wrong_captcha: '&8[&6玩家系统&8] &c错误的验证码请输入“/captcha %captcha_code”'
valid_captcha: '&8[&6玩家系统&8] &c你的验证码是有效的'
captcha_for_registration: '注册前您需要先提供验证码,请使用指令:/captcha %captcha_code'
register_captcha_valid: '&2有效的验证码您现在可以使用 /register 注册啦!'
usage_captcha: '&7请输入 /captcha %captcha_code 来验证操作.'
wrong_captcha: '&c错误的验证码.'
valid_captcha: '&a*** 验证通过 ***'
captcha_for_registration: '&7请输入 /captcha %captcha_code 来验证操作.'
register_captcha_valid: '&a验证通过, 现在可以使用 /register 注册啦!'
# Verification code
verification:
code_required: '&3这个命令非常敏感需要电子邮件验证请检查您的收件箱并遵循邮件的指导。'
command_usage: '&c使用方法 /verification <验证码>'
incorrect_code: '&c验证码错误, 请在聊天框输入 "/verification <验证码>",使用您在电子邮件中收到的验证码。'
success: '&2您的身份已经得到验证您现在可以在当前会话中执行所有命令'
already_verified: '&2您已经可以在当前会话中执行任何敏感命令'
code_expired: '&3您的验证码已失效执行另一个敏感命令以获得新的验证码'
email_needed: '&3为了验证您的身份您需要将一个电子邮件地址与您的帐户绑定'
code_required: '&a*** 已发送验证邮件 ***'
command_usage: '&c使用方法:/verification <验证码>'
incorrect_code: '&c错误的验证码.'
success: '&a*** 验证通过 ***'
already_verified: '&a您已经通过验证'
code_expired: '&c验证码已失效'
email_needed: '&c邮箱未绑定'
# Time units
time:
@ -138,20 +146,20 @@ time:
seconds: '秒'
minute: '分'
minutes: '分'
hour: '时'
hours: '时'
hour: '时'
hours: '时'
day: '天'
days: '天'
# Two-factor authentication
two_factor:
code_created: '&8[&6玩家系统&8] &a你的代码是 %code你可以使用 %url 来进行扫描'
confirmation_required: '&8[&6玩家系统&8] &3请输入 &a/2fa confirm <验证码> &3来确认双重认证'
code_required: '&8[&6玩家系统&8] &c请输入 &a/2fa code <验证码> &c来提交双重认证验证码'
already_enabled: '&8[&6玩家系统&8] &a双重认证已在您的账号上启用'
enable_error_no_code: '&8[&6玩家系统&8] &c双重认证密钥不存在或已过期请输入 &a/2fa add &c来添加'
enable_success: '&8[&6玩家系统&8] &a已成功启用双重认证'
enable_error_wrong_code: '&8[&6玩家系统&8] &c双重认证代码错误或者已经过期请重新执行 &a/2fa add'
not_enabled_error: '&8[&6玩家系统&8] &c双重认证码未在您的账号上启用请使用 &a/2fa add &c来启用'
removed_success: '&8[&6玩家系统&8] &c双重认证码已从您的账号上删除'
invalid_code: '&8[&6玩家系统&8] &c无效的验证码'
code_created: '&7您正在激活双重验证, 请打开 &a%url &7扫描二维码'
confirmation_required: '&7请输入“/totp confirm <验证码>”来确认激活双重验证'
code_required: '&c请输入“/totp code <验证码>”来提交验证码'
already_enabled: '&a双重验证已启用'
enable_error_no_code: '&c验证码丢失'
enable_success: '&a已成功启用双重验证'
enable_error_wrong_code: '&c验证码错误或者已经过期,请重新执行“/totp add”'
not_enabled_error: '&c双重验证未在您的账号上启用,请使用“/totp add”来启用'
removed_success: '&c双重验证已从您的账号上禁用'
invalid_code: '&c无效的验证码'

View File

@ -0,0 +1,126 @@
<div class="mail">
<style>
.mail td{
font-family: "Segoe UI", sans-serif;
text-align: center;
font-size: 16px;
color: #718096;
}
.mail th, div, p, a, h1, h2, h3, h4, h5, h6{
font-family: "Segoe UI", sans-serif;
text-align: center;
color: #3d4852;
}
.mail .mail-content {
max-width: 100vw;
padding: 32px;
box-shadow: 0 15px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.25);
}
.mail div{
background-color: #ffffff;color: #718096;height: 100%;line-height: 1.4;width: 100%;
}
.mail .x_wrapper{
background-color: #edf2f7;
width:100%;
}
.mail .x_content{
width: 100%;
}
.mail .x_inner-body{
background-color: #ffffff;border-color: #e8e5ef;border-radius: 2px;border-width: 1px;margin: 0 auto;padding:0;width: 570px;
}
.mail .x_content-cell{
margin: 0 auto;padding: 0;width: 570px;line-height: 1.5em;
color: #b0adc5;font-size: 12px;
}
.mail .x_content-cell td{
max-width: 100vw;padding: 32px;
}
</style>
<div>
<table class="x_wrapper">
<tbody>
<tr>
<td>
<table class="x_content">
<tbody>
<tr>
<td style="padding: 25px 0;">
<h1 style="font-size: 20px;">账户激活邮件</h1>
</td>
</tr>
<tr>
<td>
<table class="x_inner-body">
<tbody>
<tr>
<td class="mail-content">
<h1 style="font-size: 18px;margin-bottom: 25px;">Minecraft · <servername /><br/><playername /></h1>
<hr>
<table style="width: 100%;">
<tbody>
<tr>
<td>
您的账户初始密码为
</td>
</tr>
<tr>
<td style="font-weight: bold;">
<br/><generatedpass /></td>
</tr>
<tr>
<td>
<br/>已将地址(<playerip />)进行记录.
<br/>
<br/>请妥善保存,在新地址上进行登录时,需提供该密码.
<br/>若非必要,请勿更换密码,否则将对您的账户安全构成威胁.
<br/>账户所绑定的邮箱地址已被永久存储,需要更换请联系管理员.
<br/>
<br/>更换密码: /changepassword <generatedpass /> [新密码]
</td>
</tr>
<tr>
<td style="text-align:right;">
<br/>
<br/>账户将在激活后生效
<br/>欢迎您的加入~!
</td>
</tr>
<tr>
<td>
<small>
<br/><time />
<br/>请勿回复
</small>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="x_content-cell">
<tbody>
<tr>
<td>
<p style="color:#b0adc5;">© 2023 IrisCraft. All rights reserved.</p>
<a href="https://www.mcbbs.net/thread-1263385-1-1.html" target="_blank" style="text-decoration: none; font-size: 16px">iriscraft.work</a>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</div>

View File

@ -1,9 +1,119 @@
<h1>Dear <playername />,</h1>
<p>
You have requested to reset your password on <servername />. To reset it,
please use the recovery code <recoverycode />: /email code <recoverycode />.
</p>
<p>
The code expires in <hoursvalid /> hours.
</p>
<div class="mail">
<style>
.mail td{
font-family: "Segoe UI", sans-serif;
text-align: center;
font-size: 16px;
color: #718096;
}
.mail th, div, p, a, h1, h2, h3, h4, h5, h6{
font-family: "Segoe UI", sans-serif;
text-align: center;
color: #3d4852;
}
.mail .mail-content {
max-width: 100vw;
padding: 32px;
box-shadow: 0 15px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.25);
}
.mail div{
background-color: #ffffff;color: #718096;height: 100%;line-height: 1.4;width: 100%;
}
.mail .x_wrapper{
background-color: #edf2f7;
width:100%;
}
.mail .x_content{
width: 100%;
}
.mail .x_inner-body{
background-color: #ffffff;border-color: #e8e5ef;border-radius: 2px;border-width: 1px;margin: 0 auto;padding:0;width: 570px;
}
.mail .x_content-cell{
margin: 0 auto;padding: 0;width: 570px;line-height: 1.5em;
color: #b0adc5;font-size: 12px;
}
.mail .x_content-cell td{
max-width: 100vw;padding: 32px;
}
</style>
<div>
<table class="x_wrapper">
<tbody>
<tr>
<td>
<table class="x_content">
<tbody>
<tr>
<td style="padding: 25px 0;">
<h1 style="font-size: 20px;">代码验证邮件</h1>
</td>
</tr>
<tr>
<td>
<table class="x_inner-body">
<tbody>
<tr>
<td class="mail-content">
<h1 style="font-size: 18px;margin-bottom: 25px;">Minecraft · <servername /><br/><playername /></h1>
<hr>
<table style="width: 100%;">
<tbody>
<tr>
<td>
您正在申请的验证码为
</td>
</tr>
<tr>
<td style="font-weight: bold;">
<br/><recoverycode /></td>
</tr>
<tr>
<td>
<br/>使用指令: /email code <recoverycode /> 来完成验证过程.
</td>
</tr>
<tr>
<td style="text-align:right;">
<br/>
<br/>验证码将在<hoursvalid />小时后失效
</td>
</tr>
<tr>
<td>
<small>
<br/><time />
<br/>请勿回复
</small>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="x_content-cell">
<tbody>
<tr>
<td>
<p style="color:#b0adc5;">© 2023 IrisCraft. All rights reserved.</p>
<a href="https://www.mcbbs.net/thread-1263385-1-1.html" target="_blank" style="text-decoration: none; font-size: 16px">iriscraft.work</a>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</div>

View File

@ -0,0 +1,117 @@
<div class="mail">
<style>
.mail td{
font-family: "Segoe UI", sans-serif;
text-align: center;
font-size: 16px;
color: #718096;
}
.mail th, div, p, a, h1, h2, h3, h4, h5, h6{
font-family: "Segoe UI", sans-serif;
text-align: center;
color: #3d4852;
}
.mail .mail-content {
max-width: 100vw;
padding: 32px;
box-shadow: 0 15px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.25);
}
.mail div{
background-color: #ffffff;color: #718096;height: 100%;line-height: 1.4;width: 100%;
}
.mail .x_wrapper{
background-color: #edf2f7;
width:100%;
}
.mail .x_content{
width: 100%;
}
.mail .x_inner-body{
background-color: #ffffff;border-color: #e8e5ef;border-radius: 2px;border-width: 1px;margin: 0 auto;padding:0;width: 570px;
}
.mail .x_content-cell{
margin: 0 auto;padding: 0;width: 570px;line-height: 1.5em;
color: #b0adc5;font-size: 12px;
}
.mail .x_content-cell td{
max-width: 100vw;padding: 32px;
}
</style>
<div>
<table class="x_wrapper">
<tbody>
<tr>
<td>
<table class="x_content">
<tbody>
<tr>
<td style="padding: 25px 0;">
<h1 style="font-size: 20px;">服务器关闭通知</h1>
</td>
</tr>
<tr>
<td>
<table class="x_inner-body">
<tbody>
<tr>
<td class="mail-content">
<h1 style="font-size: 18px;margin-bottom: 25px;">Minecraft · <servername /></h1>
<hr>
<table style="width: 100%;">
<tbody>
<tr>
<td style="font-weight: bold;color: #da1515;">
<br/>紧急通知</td>
</tr>
<tr>
<td>
<br/>服务器当前已被关闭
<br/>
<br/>请及时检查服务器运行状态.
<br/>
</td>
</tr>
<tr>
<td style="text-align:right;">
<br/>
<br/>IrisCraft Team
</td>
</tr>
<tr>
<td>
<small>
<br/><time />
<br/>请勿回复
</small>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="x_content-cell">
<tbody>
<tr>
<td>
<p style="color:#b0adc5;">© 2023 IrisCraft. All rights reserved.</p>
<a href="https://www.mcbbs.net/thread-1263385-1-1.html" target="_blank" style="text-decoration: none; font-size: 16px">iriscraft.work</a>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</div>

View File

@ -1,20 +1,119 @@
<h1>
Dear <playername />,
</h1>
<p>
This is your temporary verification code for the server <servername />:
</p>
<p>
<generatedcode />
</p>
<p>
This code will be valid for the next <minutesvalid /> mins!<br />
Use the command
/verification <generatedcode />
to complete the verification process.
</p>
<p>
See you on <servername />!
</p>
<div class="mail">
<style>
.mail td{
font-family: "Segoe UI", sans-serif;
text-align: center;
font-size: 16px;
color: #718096;
}
.mail th, div, p, a, h1, h2, h3, h4, h5, h6{
font-family: "Segoe UI", sans-serif;
text-align: center;
color: #3d4852;
}
.mail .mail-content {
max-width: 100vw;
padding: 32px;
box-shadow: 0 15px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.25);
}
.mail div{
background-color: #ffffff;color: #718096;height: 100%;line-height: 1.4;width: 100%;
}
.mail .x_wrapper{
background-color: #edf2f7;
width:100%;
}
.mail .x_content{
width: 100%;
}
.mail .x_inner-body{
background-color: #ffffff;border-color: #e8e5ef;border-radius: 2px;border-width: 1px;margin: 0 auto;padding:0;width: 570px;
}
.mail .x_content-cell{
margin: 0 auto;padding: 0;width: 570px;line-height: 1.5em;
color: #b0adc5;font-size: 12px;
}
.mail .x_content-cell td{
max-width: 100vw;padding: 32px;
}
</style>
<div>
<table class="x_wrapper">
<tbody>
<tr>
<td>
<table class="x_content">
<tbody>
<tr>
<td style="padding: 25px 0;">
<h1 style="font-size: 20px;">代码验证邮件</h1>
</td>
</tr>
<tr>
<td>
<table class="x_inner-body">
<tbody>
<tr>
<td class="mail-content">
<h1 style="font-size: 18px;margin-bottom: 25px;">Minecraft · <servername /><br/><playername /></h1>
<hr>
<table style="width: 100%;">
<tbody>
<tr>
<td>
您正在申请的验证码为
</td>
</tr>
<tr>
<td style="font-weight: bold;">
<br/><generatedcode /></td>
</tr>
<tr>
<td>
<br/>使用指令: /verification <generatedcode /> 来完成验证过程.
</td>
</tr>
<tr>
<td style="text-align:right;">
<br/>
<br/>验证码将在30分钟后失效
</td>
</tr>
<tr>
<td>
<small>
<br/><time />
<br/>请勿回复
</small>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="x_content-cell">
<tbody>
<tr>
<td>
<p style="color:#b0adc5;">© 2023 IrisCraft. All rights reserved.</p>
<a href="https://www.mcbbs.net/thread-1263385-1-1.html" target="_blank" style="text-decoration: none; font-size: 16px">iriscraft.work</a>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</div>

View File

@ -1,145 +0,0 @@
package fr.xephi.authme;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.PropertyResource;
import ch.jalu.injector.Injector;
import ch.jalu.injector.InjectorBuilder;
import com.google.common.io.Files;
import fr.xephi.authme.api.v3.AuthMeApi;
import fr.xephi.authme.command.CommandHandler;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.listener.BlockListener;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.process.login.ProcessSyncPlayerLogin;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.bungeecord.BungeeReceiver;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.task.purge.PurgeService;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.logging.Logger;
import static fr.xephi.authme.settings.properties.AuthMeSettingsRetriever.buildConfigurationData;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Integration test verifying that all services can be initialized in {@link AuthMe}
* with the {@link Injector}.
*/
@RunWith(MockitoJUnitRunner.class)
public class AuthMeInitializationTest {
@Mock
private Server server;
@Mock
private PluginManager pluginManager;
private AuthMe authMe;
private File dataFolder;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Before
public void initAuthMe() throws IOException {
dataFolder = temporaryFolder.newFolder();
File settingsFile = new File(dataFolder, "config.yml");
given(server.getLogger()).willReturn(Logger.getAnonymousLogger());
JavaPluginLoader pluginLoader = new JavaPluginLoader(server);
Files.copy(TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "config.test.yml"), settingsFile);
// Mock / wire various Bukkit components
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
given(server.getPluginManager()).willReturn(pluginManager);
// PluginDescriptionFile is final: need to create a sample one
PluginDescriptionFile descriptionFile = new PluginDescriptionFile(
"AuthMe", "N/A", AuthMe.class.getCanonicalName());
// Initialize AuthMe
authMe = new AuthMe(pluginLoader, descriptionFile, dataFolder, null);
}
@Test
public void shouldInitializeAllServices() {
// given
PropertyReader reader = mock(PropertyReader.class);
PropertyResource resource = mock(PropertyResource.class);
given(resource.createReader()).willReturn(reader);
Settings settings = new Settings(dataFolder, resource, null, buildConfigurationData());
TestHelper.setupLogger();
Injector injector = new InjectorBuilder()
.addDefaultHandlers("fr.xephi.authme")
.create();
injector.provide(DataFolder.class, dataFolder);
injector.register(Server.class, server);
injector.register(PluginManager.class, pluginManager);
injector.register(AuthMe.class, authMe);
injector.register(Settings.class, settings);
injector.register(DataSource.class, mock(DataSource.class));
injector.register(BukkitService.class, mock(BukkitService.class));
// when
authMe.instantiateServices(injector);
authMe.registerEventListeners(injector);
// then
// Take a few samples and ensure that they are not null
assertThat(injector.getIfAvailable(AuthMeApi.class), not(nullValue()));
assertThat(injector.getIfAvailable(BlockListener.class), not(nullValue()));
assertThat(injector.getIfAvailable(BungeeReceiver.class), not(nullValue()));
assertThat(injector.getIfAvailable(BungeeSender.class), not(nullValue()));
assertThat(injector.getIfAvailable(CommandHandler.class), not(nullValue()));
assertThat(injector.getIfAvailable(Management.class), not(nullValue()));
assertThat(injector.getIfAvailable(PasswordSecurity.class), not(nullValue()));
assertThat(injector.getIfAvailable(PermissionsManager.class), not(nullValue()));
assertThat(injector.getIfAvailable(ProcessSyncPlayerLogin.class), not(nullValue()));
assertThat(injector.getIfAvailable(PurgeService.class), not(nullValue()));
}
@Test
public void shouldHandlePrematureShutdownGracefully() {
// given
BukkitScheduler scheduler = mock(BukkitScheduler.class);
given(server.getScheduler()).willReturn(scheduler);
// Make sure ConsoleLogger has no logger reference since that may happen on unexpected stops
ReflectionTestUtils.setField(ConsoleLogger.class, null, "logger", null);
// when
authMe.onDisable();
// then - no exceptions
verify(scheduler).getActiveWorkers(); // via TaskCloser
}
}

View File

@ -1,136 +0,0 @@
package fr.xephi.authme;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.security.crypts.HashedPassword;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import java.util.Objects;
/**
* Custom matchers for AuthMe entities.
*/
// Justification: Javadoc would be huge because of the many parameters
@SuppressWarnings("checkstyle:MissingJavadocMethod")
public final class AuthMeMatchers {
private AuthMeMatchers() {
}
public static Matcher<? super HashedPassword> equalToHash(String hash) {
return equalToHash(new HashedPassword(hash));
}
public static Matcher<? super HashedPassword> equalToHash(String hash, String salt) {
return equalToHash(new HashedPassword(hash, salt));
}
public static Matcher<? super HashedPassword> equalToHash(HashedPassword hash) {
return new TypeSafeMatcher<HashedPassword>() {
@Override
public boolean matchesSafely(HashedPassword item) {
return Objects.equals(hash.getHash(), item.getHash())
&& Objects.equals(hash.getSalt(), item.getSalt());
}
@Override
public void describeTo(Description description) {
String representation = "'" + hash.getHash() + "'";
if (hash.getSalt() != null) {
representation += ", '" + hash.getSalt() + "'";
}
description.appendValue("HashedPassword(" + representation + ")");
}
};
}
public static Matcher<PlayerAuth> hasAuthBasicData(String name, String realName,
String email, String lastIp) {
return new TypeSafeMatcher<PlayerAuth>() {
@Override
public boolean matchesSafely(PlayerAuth item) {
return Objects.equals(name, item.getNickname())
&& Objects.equals(realName, item.getRealName())
&& Objects.equals(email, item.getEmail())
&& Objects.equals(lastIp, item.getLastIp());
}
@Override
public void describeTo(Description description) {
description.appendValue(String.format("PlayerAuth with name %s, realname %s, email %s, lastIp %s",
name, realName, email, lastIp));
}
@Override
public void describeMismatchSafely(PlayerAuth item, Description description) {
description.appendValue(String.format("PlayerAuth with name %s, realname %s, email %s, lastIp %s",
item.getNickname(), item.getRealName(), item.getEmail(), item.getLastIp()));
}
};
}
public static Matcher<? super PlayerAuth> hasRegistrationInfo(String registrationIp, long registrationDate) {
return new TypeSafeMatcher<PlayerAuth>() {
@Override
public boolean matchesSafely(PlayerAuth item) {
return Objects.equals(registrationIp, item.getRegistrationIp())
&& Objects.equals(registrationDate, item.getRegistrationDate());
}
@Override
public void describeTo(Description description) {
description.appendValue(String.format("PlayerAuth with reg. IP %s and reg date %d",
registrationIp, registrationDate));
}
@Override
public void describeMismatchSafely(PlayerAuth item, Description description) {
description.appendValue(String.format("PlayerAuth with reg. IP %s and reg date %d",
item.getRegistrationIp(), item.getRegistrationDate()));
}
};
}
public static Matcher<? super PlayerAuth> hasAuthLocation(PlayerAuth auth) {
return hasAuthLocation(auth.getQuitLocX(), auth.getQuitLocY(), auth.getQuitLocZ(), auth.getWorld(),
auth.getYaw(), auth.getPitch());
}
public static Matcher<? super PlayerAuth> hasAuthLocation(double x, double y, double z,
String world, float yaw, float pitch) {
return new TypeSafeMatcher<PlayerAuth>() {
@Override
public boolean matchesSafely(PlayerAuth item) {
return Objects.equals(x, item.getQuitLocX())
&& Objects.equals(y, item.getQuitLocY())
&& Objects.equals(z, item.getQuitLocZ())
&& Objects.equals(world, item.getWorld())
&& Objects.equals(yaw, item.getYaw())
&& Objects.equals(pitch, item.getPitch());
}
@Override
public void describeTo(Description description) {
description.appendValue(
String.format("PlayerAuth with quit location (x: %f, y: %f, z: %f, world: %s, yaw: %f, pitch: %f)",
x, y, z, world, yaw, pitch));
}
};
}
public static Matcher<String> stringWithLength(int length) {
return new TypeSafeMatcher<String>() {
@Override
protected boolean matchesSafely(String item) {
return item != null && item.length() == length;
}
@Override
public void describeTo(Description description) {
description.appendText("String with length " + length);
}
};
}
}

View File

@ -1,165 +0,0 @@
package fr.xephi.authme;
import java.io.File;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* Collects available classes by walking through a source directory.
* <p>
* This is a naive, zero dependency collector that walks through a file directory
* and loads classes from the class loader based on the .java files it encounters.
* This is a very slow approach and should be avoided for production code.
* <p>
* For more performant approaches, see e.g. <a href="https://github.com/ronmamo/reflections">org.reflections</a>.
*/
public class ClassCollector {
private final String root;
private final String nonCodePath;
/**
* Constructor. The arguments make up the path from which the collector will start scanning.
*
* @param nonCodePath beginning of the starting path that are not Java packages, e.g. {@code src/main/java/}
* @param packagePath folders following {@code nonCodePath} that are packages, e.g. {@code com/project/app}
*/
public ClassCollector(String nonCodePath, String packagePath) {
if (!nonCodePath.endsWith("/") && !nonCodePath.endsWith("\\")) {
nonCodePath = nonCodePath.concat(File.separator);
}
this.root = nonCodePath + packagePath;
this.nonCodePath = nonCodePath;
}
/**
* Collects all classes from the parent folder and below.
*
* @return all classes
*/
public List<Class<?>> collectClasses() {
return collectClasses(x -> true);
}
/**
* Collects all classes from the parent folder and below which are of type {@link T}.
*
* @param parent the parent which classes need to extend (or be equal to) in order to be collected
* @param <T> the parent type
* @return list of matching classes
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> List<Class<? extends T>> collectClasses(Class<T> parent) {
List<Class<?>> classes = collectClasses(parent::isAssignableFrom);
return new ArrayList<>((List) classes);
}
/**
* Collects all classes from the parent folder and below which match the given predicate.
*
* @param filter the predicate classes need to satisfy in order to be collected
* @return list of matching classes
*/
public List<Class<?>> collectClasses(Predicate<Class<?>> filter) {
File rootFolder = new File(root);
List<Class<?>> collection = new ArrayList<>();
gatherClassesFromFile(rootFolder, filter, collection);
return collection;
}
/**
* Constructs an instance of all classes which are of the provided type {@code clazz}.
* This method assumes that every class has an accessible no-args constructor for creation.
*
* @param parent the parent which classes need to extend (or be equal to) in order to be instantiated
* @param <T> the parent type
* @return collection of created objects
*/
public <T> List<T> getInstancesOfType(Class<T> parent) {
return getInstancesOfType(parent, (clz) -> {
try {
return canInstantiate(clz) ? clz.newInstance() : null;
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
});
}
/**
* Constructs an instance of all classes which are of the provided type {@code clazz}
* with the provided {@code instantiator}.
*
* @param parent the parent which classes need to extend (or be equal to) in order to be instantiated
* @param instantiator function which returns an object of the given class, or null to skip the class
* @param <T> the parent type
* @return collection of created objects
*/
public <T> List<T> getInstancesOfType(Class<T> parent, Function<Class<? extends T>, T> instantiator) {
return collectClasses(parent)
.stream()
.map(instantiator)
.filter(o -> o != null)
.collect(Collectors.toList());
}
/**
* Returns whether the given class can be instantiated, i.e. if it is not abstract, an interface, etc.
*
* @param clazz the class to process
* @return true if the class can be instantiated, false otherwise
*/
public static boolean canInstantiate(Class<?> clazz) {
return clazz != null && !clazz.isEnum() && !clazz.isInterface()
&& !clazz.isArray() && !Modifier.isAbstract(clazz.getModifiers());
}
/**
* Recursively collects the classes based on the files in the directory and in its child directories.
*
* @param folder the folder to scan
* @param filter the class predicate
* @param collection collection to add classes to
*/
private void gatherClassesFromFile(File folder, Predicate<Class<?>> filter, List<Class<?>> collection) {
File[] files = folder.listFiles();
if (files == null) {
throw new IllegalStateException("Could not read files from '" + folder + "'");
}
for (File file : files) {
if (file.isDirectory()) {
gatherClassesFromFile(file, filter, collection);
} else if (file.isFile()) {
Class<?> clazz = loadTaskClassFromFile(file);
if (clazz != null && filter.test(clazz)) {
collection.add(clazz);
}
}
}
}
/**
* Loads a class from the class loader based on the given file.
*
* @param file the file whose corresponding Java class should be retrieved
* @return the corresponding class, or null if not applicable
*/
private Class<?> loadTaskClassFromFile(File file) {
if (!file.getName().endsWith(".java")) {
return null;
}
String filePath = file.getPath();
String className = filePath
.substring(nonCodePath.length(), filePath.length() - 5)
.replace(File.separator, ".");
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
}

View File

@ -1,173 +0,0 @@
package fr.xephi.authme;
import ch.jalu.configme.properties.Property;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import fr.xephi.authme.data.captcha.CaptchaCodeStorage;
import fr.xephi.authme.datasource.AbstractSqlDataSource;
import fr.xephi.authme.datasource.Columns;
import fr.xephi.authme.datasource.columnshandler.DataSourceColumn;
import fr.xephi.authme.datasource.columnshandler.PlayerAuthColumn;
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtension;
import fr.xephi.authme.initialization.HasCleanup;
import fr.xephi.authme.process.register.executors.RegistrationMethod;
import fr.xephi.authme.security.crypts.Whirlpool;
import fr.xephi.authme.util.expiring.ExpiringMap;
import fr.xephi.authme.util.expiring.ExpiringSet;
import fr.xephi.authme.util.expiring.TimedCounter;
import org.junit.Test;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
/**
* Contains consistency tests across all AuthMe classes.
*/
public class ClassesConsistencyTest {
/** Contains all production classes. */
private static final List<Class<?>> ALL_CLASSES =
new ClassCollector(TestHelper.SOURCES_FOLDER, TestHelper.PROJECT_ROOT).collectClasses();
/** Expiring structure types. */
private static final Set<Class<?>> EXPIRING_STRUCTURES = ImmutableSet.of(
ExpiringSet.class, ExpiringMap.class, TimedCounter.class, CaptchaCodeStorage.class);
/** Immutable types, which are allowed to be used in non-private constants. */
private static final Set<Class<?>> IMMUTABLE_TYPES = ImmutableSet.of(
/* JDK */
int.class, long.class, float.class, String.class, File.class, Enum.class, collectionsUnmodifiableList(),
Charset.class,
/* AuthMe */
Property.class, RegistrationMethod.class, DataSourceColumn.class, PlayerAuthColumn.class,
/* Guava */
ImmutableMap.class, ImmutableList.class);
/** Classes excluded from the field visibility test. */
private static final Set<Class<?>> CLASSES_EXCLUDED_FROM_VISIBILITY_TEST = ImmutableSet.of(
Whirlpool.class, // not our implementation, so we don't touch it
MySqlExtension.class, // has immutable protected fields used by all children
AbstractSqlDataSource.class, // protected members for inheritance
Columns.class // uses non-static String constants, which is safe
);
/**
* Checks that there aren't two classes with the same name; this is confusing and should be avoided.
*/
@Test
public void shouldNotHaveSameName() {
// given
Set<String> names = new HashSet<>();
// when / then
for (Class<?> clazz : ALL_CLASSES) {
if (!names.add(clazz.getSimpleName())) {
fail("Class with name '" + clazz.getSimpleName() + "' already encountered!");
}
}
}
/**
* Checks that fields of classes are either private or static final fields of an immutable type.
*/
@Test
public void shouldHaveNonPrivateConstantsOnly() {
// given / when
Set<String> invalidFields = ALL_CLASSES.stream()
.filter(clz -> !CLASSES_EXCLUDED_FROM_VISIBILITY_TEST.contains(clz))
.map(Class::getDeclaredFields)
.flatMap(Arrays::stream)
.filter(f -> !f.getName().contains("$"))
.filter(f -> hasIllegalFieldVisibility(f))
.map(f -> formatField(f))
.collect(Collectors.toSet());
// then
if (!invalidFields.isEmpty()) {
fail("Found " + invalidFields.size() + " fields with non-private, mutable fields:\n- "
+ String.join("\n- ", invalidFields));
}
}
private static boolean hasIllegalFieldVisibility(Field field) {
final int modifiers = field.getModifiers();
if (Modifier.isPrivate(modifiers)) {
return false;
} else if (!Modifier.isStatic(modifiers) || !Modifier.isFinal(modifiers)) {
return true;
}
// Field is non-private, static and final
Class<?> valueType;
if (Collection.class.isAssignableFrom(field.getType()) || Map.class.isAssignableFrom(field.getType())) {
// For collections/maps, need to check the actual type to ensure it's an unmodifiable implementation
Object value = ReflectionTestUtils.getFieldValue(field, null);
valueType = value.getClass();
} else {
valueType = field.getType();
}
// Field is static, final, and not private -> check that it is immutable type
return IMMUTABLE_TYPES.stream()
.noneMatch(immutableType -> immutableType.isAssignableFrom(valueType));
}
/**
* Prints out the field with its modifiers.
*
* @param field the field to format
* @return description of the field
*/
private static String formatField(Field field) {
String modifiersText = Modifier.toString(field.getModifiers());
return String.format("[%s] %s %s %s", field.getDeclaringClass().getSimpleName(), modifiersText.trim(),
field.getType().getSimpleName(), field.getName());
}
/**
* Checks that classes with expiring collections (such as {@link ExpiringMap}) implement the {@link HasCleanup}
* interface to regularly clean up expired data.
*/
@Test
public void shouldImplementHasCleanup() {
// given / when / then
for (Class<?> clazz : ALL_CLASSES) {
if (hasExpiringCollectionAsField(clazz) && !EXPIRING_STRUCTURES.contains(clazz)) {
assertThat("Class '" + clazz.getSimpleName() + "' has expiring collections, should implement HasCleanup",
HasCleanup.class.isAssignableFrom(clazz), equalTo(true));
}
}
}
private static boolean hasExpiringCollectionAsField(Class<?> clazz) {
for (Field field : clazz.getDeclaredFields()) {
if (EXPIRING_STRUCTURES.stream().anyMatch(t -> t.isAssignableFrom(field.getType()))) {
return true;
}
}
return false;
}
/**
* @return the concrete class of the unmodifiable list as returned by {@link Collections#unmodifiableList(List)}.
*/
private static Class<?> collectionsUnmodifiableList() {
return Collections.unmodifiableList(new ArrayList<>()).getClass();
}
}

View File

@ -1,44 +0,0 @@
package fr.xephi.authme;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Test;
import java.io.File;
import java.util.List;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
/**
* Consistency test for the CodeClimate configuration file.
*/
public class CodeClimateConfigTest {
private static final String CONFIG_FILE = ".codeclimate.yml";
@Test
public void shouldHaveExistingClassesInExclusions() {
// given / when
FileConfiguration configuration = YamlConfiguration.loadConfiguration(new File(CONFIG_FILE));
List<String> excludePaths = configuration.getStringList("exclude_patterns");
// then
assertThat(excludePaths, not(empty()));
removeTestsExclusionOrThrow(excludePaths);
for (String path : excludePaths) {
if (!new File(path).exists()) {
fail("Path '" + path + "' does not exist!");
}
}
}
private static void removeTestsExclusionOrThrow(List<String> excludePaths) {
boolean wasRemoved = excludePaths.removeIf("src/test/java/**/*Test.java"::equals);
assertThat("Expected an exclusion for test classes",
wasRemoved, equalTo(true));
}
}

View File

@ -1,227 +0,0 @@
package fr.xephi.authme;
import fr.xephi.authme.output.LogLevel;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.logging.Logger;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Test for {@link ConsoleLogger}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ConsoleLoggerTest {
private ConsoleLogger consoleLogger;
@Mock
private Logger logger;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private File logFile;
@Before
public void setMockLogger() throws IOException {
File folder = temporaryFolder.newFolder();
File logFile = new File(folder, "authme.log");
if (!logFile.createNewFile()) {
throw new IOException("Could not create file '" + logFile.getPath() + "'");
}
ConsoleLogger.initialize(logger, logFile);
this.logFile = logFile;
this.consoleLogger = new ConsoleLogger("test");
}
@After
public void closeFileHandlers() {
ConsoleLogger.closeFileWriter();
}
/**
* Resets the ConsoleLogger back to its defaults after running all tests. Especially important
* is that we no longer enable logging to a file as the log file we've supplied will no longer
* be around after this test class has finished.
*/
@AfterClass
public static void resetConsoleToDefault() {
ConsoleLogger.initializeSharedSettings(newSettings(false, LogLevel.INFO));
}
@Test
public void shouldLogToFile() throws IOException {
// given
Settings settings = newSettings(true, LogLevel.FINE);
ConsoleLogger.initializeSharedSettings(settings);
consoleLogger.initializeSettings(settings);
// when
consoleLogger.fine("Logging a FINE message");
consoleLogger.debug("Logging a DEBUG message");
consoleLogger.info("This is an INFO message");
// then
verify(logger, times(2)).info(anyString());
verifyNoMoreInteractions(logger);
List<String> loggedLines = Files.readAllLines(logFile.toPath(), StandardCharsets.UTF_8);
assertThat(loggedLines, hasSize(2));
assertThat(loggedLines.get(0), containsString("[FINE] Logging a FINE message"));
assertThat(loggedLines.get(1), containsString("[INFO] This is an INFO message"));
}
@Test
public void shouldNotLogToFile() {
// given
Settings settings = newSettings(false, LogLevel.DEBUG);
ConsoleLogger.initializeSharedSettings(settings);
consoleLogger.initializeSettings(settings);
// when
consoleLogger.debug("Created test");
consoleLogger.warning("Encountered a warning");
// then
verify(logger).info("[DEBUG] Created test");
verify(logger).warning("Encountered a warning");
verifyNoMoreInteractions(logger);
assertThat(logFile.length(), equalTo(0L));
}
@Test
public void shouldLogStackTraceToFile() throws IOException {
// given
Settings settings = newSettings(true, LogLevel.INFO);
ConsoleLogger.initializeSharedSettings(settings);
Exception e = new IllegalStateException("Test exception message");
// when
consoleLogger.info("Info text");
consoleLogger.debug("Debug message");
consoleLogger.fine("Fine-level message");
consoleLogger.logException("Exception occurred:", e);
// then
verify(logger).info("Info text");
verify(logger).warning("Exception occurred: [IllegalStateException]: Test exception message");
verifyNoMoreInteractions(logger);
List<String> loggedLines = Files.readAllLines(logFile.toPath(), StandardCharsets.UTF_8);
assertThat(loggedLines.size(), greaterThan(3));
assertThat(loggedLines.get(0), containsString("[INFO] Info text"));
assertThat(loggedLines.get(1),
containsString("[WARN] Exception occurred: [IllegalStateException]: Test exception message"));
// Check that we have this class' full name somewhere in the file -> stacktrace of Exception e
assertThat(String.join("", loggedLines), containsString(getClass().getCanonicalName()));
}
@Test
public void shouldSupportVariousDebugMethods() throws IOException {
// given
Settings settings = newSettings(true, LogLevel.DEBUG);
ConsoleLogger.initializeSharedSettings(settings);
consoleLogger.initializeSettings(settings);
// when
consoleLogger.debug("Got {0} entries", 17);
consoleLogger.debug("Player `{0}` is in world `{1}`", "Bobby", new WorldDummy("world"));
consoleLogger.debug("{0} quick {1} jump over {2} lazy {3} (reason: {4})", 5, "foxes", 3, "dogs", null);
consoleLogger.debug(() -> "Too little too late");
// then
verify(logger).info("[DEBUG] Got 17 entries");
verify(logger).info("[DEBUG] Player `Bobby` is in world `w[world]`");
verify(logger).info("[DEBUG] 5 quick foxes jump over 3 lazy dogs (reason: null)");
verify(logger).info("[DEBUG] Too little too late");
verifyNoMoreInteractions(logger);
List<String> loggedLines = Files.readAllLines(logFile.toPath(), StandardCharsets.UTF_8);
assertThat(loggedLines, contains(
containsString("[DEBUG] Got 17 entries"),
containsString("[DEBUG] Player `Bobby` is in world `w[world]`"),
containsString("[DEBUG] 5 quick foxes jump over 3 lazy dogs (reason: null)"),
containsString("[DEBUG] Too little too late")));
}
@Test
public void shouldCloseFileWriterDespiteExceptionOnFlush() throws IOException {
// given
FileWriter fileWriter = mock(FileWriter.class);
doThrow(new IOException("Error during flush")).when(fileWriter).flush();
ReflectionTestUtils.setField(ConsoleLogger.class, null, "fileWriter", fileWriter);
// when
ConsoleLogger.closeFileWriter();
// then
verify(fileWriter).flush();
verify(fileWriter).close();
assertThat(ReflectionTestUtils.getFieldValue(ConsoleLogger.class, null, "fileWriter"), nullValue());
}
@Test
public void shouldHandleExceptionOnFileWriterClose() throws IOException {
// given
FileWriter fileWriter = mock(FileWriter.class);
doThrow(new IOException("Cannot close")).when(fileWriter).close();
ReflectionTestUtils.setField(ConsoleLogger.class, null, "fileWriter", fileWriter);
// when
ConsoleLogger.closeFileWriter();
// then
verify(fileWriter).flush();
verify(fileWriter).close();
assertThat(ReflectionTestUtils.getFieldValue(ConsoleLogger.class, null, "fileWriter"), nullValue());
}
private static Settings newSettings(boolean logToFile, LogLevel logLevel) {
Settings settings = mock(Settings.class);
given(settings.getProperty(SecuritySettings.USE_LOGGING)).willReturn(logToFile);
given(settings.getProperty(PluginSettings.LOG_LEVEL)).willReturn(logLevel);
return settings;
}
private static final class WorldDummy {
private final String name;
WorldDummy(String name) {
this.name = name;
}
@Override
public String toString() {
return "w[" + name + "]";
}
}
}

View File

@ -1,91 +0,0 @@
package fr.xephi.authme;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static org.junit.Assert.fail;
/**
* Matcher which checks with reflection that all fields have the same value.
* This matcher considers all non-static fields until the Object parent.
*/
public final class IsEqualByReflectionMatcher<T> extends TypeSafeMatcher<T> {
private final T expected;
private IsEqualByReflectionMatcher(T expected) {
this.expected = expected;
}
/**
* Creates a matcher that checks if all fields are the same as on the {@code expected} object.
*
* @param expected the object to match
* @param <T> the object's type
* @return the matcher for the expected object
*/
public static <T> Matcher<T> hasEqualValuesOnAllFields(T expected) {
return new IsEqualByReflectionMatcher<>(expected);
}
@Override
protected boolean matchesSafely(T item) {
return assertAreFieldsEqual(item);
}
@Override
public void describeTo(Description description) {
description.appendText("parameters " + expected);
}
/**
* Checks that all fields of the given {@code item} are equal to the {@link #expected} object. Both objects must
* be exactly of the same type.
*
* @param item the object to check
* @return true if all fields are equal, false otherwise
*/
private boolean assertAreFieldsEqual(T item) {
if (expected.getClass() != item.getClass()) {
fail("Classes don't match, got " + expected.getClass().getSimpleName()
+ " and " + item.getClass().getSimpleName());
return false;
}
List<Field> fieldsToCheck = getAllFields(expected);
for (Field field : fieldsToCheck) {
Object lhsValue = ReflectionTestUtils.getFieldValue(field, expected);
Object rhsValue = ReflectionTestUtils.getFieldValue(field, item);
if (!Objects.equals(lhsValue, rhsValue)) {
fail("Field '" + field.getName() + "' does not have same value: '"
+ lhsValue + "' vs. '" + rhsValue + "'");
return false;
}
}
return true;
}
private static List<Field> getAllFields(Object object) {
List<Field> fields = new ArrayList<>();
Class<?> currentClass = object.getClass();
while (currentClass != null) {
for (Field f : currentClass.getDeclaredFields()) {
if (!Modifier.isStatic(f.getModifiers()) && !f.isSynthetic()) {
fields.add(f);
}
}
if (currentClass == Object.class) {
break;
}
currentClass = currentClass.getSuperclass();
}
return fields;
}
}

View File

@ -1,153 +0,0 @@
package fr.xephi.authme;
import ch.jalu.injector.handlers.postconstruct.PostConstructMethodInvoker;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static java.lang.String.format;
/**
* Offers reflection functionality to set up tests. Use only when absolutely necessary.
*/
public final class ReflectionTestUtils {
private ReflectionTestUtils() {
// Util class
}
/**
* Sets the field of a given object to a new value with reflection.
*
* @param clazz the class declaring the field
* @param instance the instance to modify (pass null for static fields)
* @param fieldName the field name
* @param value the value to set the field to
* @param <T> the instance type
*/
public static <T> void setField(Class<? super T> clazz, T instance, String fieldName, Object value) {
try {
Field field = getField(clazz, fieldName);
field.set(instance, value);
} catch (IllegalAccessException e) {
throw new IllegalStateException(
format("Could not set value to field '%s' for instance '%s' of class '%s'",
fieldName, instance, clazz.getName()), e);
}
}
/**
* Sets the field on the given instance to the new value.
*
* @param instance the instance to modify
* @param fieldName the field name
* @param value the value to set the field to
*/
@SuppressWarnings("unchecked")
public static void setField(Object instance, String fieldName, Object value) {
setField((Class) instance.getClass(), instance, fieldName, value);
}
private static <T> Field getField(Class<T> clazz, String fieldName) {
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return field;
} catch (NoSuchFieldException e) {
throw new IllegalStateException(format("Could not get field '%s' from class '%s'",
fieldName, clazz.getName()), e);
}
}
@SuppressWarnings("unchecked")
public static <T, V> V getFieldValue(Class<T> clazz, T instance, String fieldName) {
Field field = getField(clazz, fieldName);
return getFieldValue(field, instance);
}
/**
* Returns the value of the field on the given instance. Wraps exceptions into a runtime exception.
*
* @param field the field to read
* @param instance the instance to get the value from, null if field is static
* @param <V> type of the field
* @return value of the field
*/
@SuppressWarnings("unchecked")
public static <V> V getFieldValue(Field field, Object instance) {
field.setAccessible(true);
try {
return (V) field.get(instance);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Could not get value of field '" + field.getName() + "'", e);
}
}
/**
* Returns the method on the given class with the supplied parameter types.
*
* @param clazz the class to retrieve a method from
* @param methodName the name of the method
* @param parameterTypes the parameter types the method to retrieve has
* @return the method of the class, set to be accessible
*/
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
try {
Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
method.setAccessible(true);
return method;
} catch (NoSuchMethodException e) {
throw new IllegalStateException("Could not retrieve method '" + methodName + "' from class '"
+ clazz.getName() + "'");
}
}
/**
* Invokes the given method on the provided instance with the given parameters.
*
* @param method the method to invoke
* @param instance the instance to invoke the method on (null for static methods)
* @param parameters the parameters to pass to the method
* @param <V> return value of the method
* @return method return value
*/
@SuppressWarnings("unchecked")
public static <V> V invokeMethod(Method method, Object instance, Object... parameters) {
method.setAccessible(true);
try {
return (V) method.invoke(instance, parameters);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new IllegalStateException("Could not invoke method '" + method + "'", e);
}
}
/**
* Runs all methods annotated with {@link javax.annotation.PostConstruct} on the given instance
* (including such methods on superclasses).
*
* @param instance the instance to process
*/
public static void invokePostConstructMethods(Object instance) {
// Use the implementation of the injector to invoke all @PostConstruct methods the same way
new PostConstructMethodInvoker().postProcess(instance, null, null);
}
/**
* Creates a new instance of the given class, using a no-args constructor (which may be hidden).
*
* @param clazz the class to instantiate
* @param <T> the class' type
* @return the created instance
*/
public static <T> T newInstance(Class<T> clazz) {
try {
Constructor<T> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
} catch (ReflectiveOperationException e) {
throw new IllegalStateException("Could not invoke no-args constructor of class " + clazz, e);
}
}
}

View File

@ -1,121 +0,0 @@
package fr.xephi.authme;
import ch.jalu.configme.properties.Property;
import fr.xephi.authme.settings.Settings;
import org.bukkit.entity.Player;
import org.mockito.Mockito;
import java.io.File;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Logger;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
/**
* AuthMe test utilities.
*/
public final class TestHelper {
public static final String SOURCES_FOLDER = "src/main/java/";
public static final String TEST_SOURCES_FOLDER = "src/test/java/";
public static final String TEST_RESOURCES_FOLDER = "src/test/resources/";
public static final String PROJECT_ROOT = "/fr/xephi/authme/";
private TestHelper() {
}
/**
* Return a {@link File} to a file in the JAR's resources (main or test).
*
* @param path The absolute path to the file
* @return The project file
*/
public static File getJarFile(String path) {
URI uri = getUriOrThrow(path);
return new File(uri.getPath());
}
/**
* Return a {@link Path} to a file in the JAR's resources (main or test).
*
* @param path The absolute path to the file
* @return The Path object to the file
*/
public static Path getJarPath(String path) {
String sqlFilePath = getUriOrThrow(path).getPath();
// Windows prepends the path with a '/' or '\', which Paths cannot handle
String appropriatePath = System.getProperty("os.name").contains("indow")
? sqlFilePath.substring(1)
: sqlFilePath;
return Paths.get(appropriatePath);
}
private static URI getUriOrThrow(String path) {
URL url = TestHelper.class.getResource(path);
if (url == null) {
throw new IllegalStateException("File '" + path + "' could not be loaded");
}
try {
return new URI(url.toString());
} catch (URISyntaxException e) {
throw new IllegalStateException("File '" + path + "' cannot be converted to a URI");
}
}
/**
* Assign the necessary fields on ConsoleLogger with mocks.
*
* @return The logger mock used
*/
public static Logger setupLogger() {
Logger logger = Mockito.mock(Logger.class);
ConsoleLogger.initialize(logger, null);
return logger;
}
/**
* Set ConsoleLogger to use a new real logger.
*
* @return The real logger used by ConsoleLogger
*/
public static Logger setRealLogger() {
Logger logger = Logger.getAnonymousLogger();
ConsoleLogger.initialize(logger, null);
return logger;
}
/**
* Configures the player mock to return the given IP address.
*
* @param player the player mock
* @param ip the ip address it should return
*/
public static void mockIpAddressToPlayer(Player player, String ip) {
try {
InetAddress inetAddress = InetAddress.getByName(ip);
InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, 8093);
given(player.getAddress()).willReturn(inetSocketAddress);
} catch (UnknownHostException e) {
throw new IllegalStateException("Invalid IP address: " + ip, e);
}
}
/**
* Configures the Settings mock to return the property's default value for any given property.
*
* @param settings the settings mock
*/
@SuppressWarnings("unchecked")
public static void returnDefaultsForAllProperties(Settings settings) {
given(settings.getProperty(any(Property.class)))
.willAnswer(invocation -> ((Property<?>) invocation.getArgument(0)).getDefaultValue());
}
}

View File

@ -1,558 +0,0 @@
package fr.xephi.authme.api.v3;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.process.register.executors.ApiPasswordRegisterParams;
import fr.xephi.authme.process.register.executors.RegistrationMethod;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.GeoIpService;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.time.Instant;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;
import static fr.xephi.authme.IsEqualByReflectionMatcher.hasEqualValuesOnAllFields;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link AuthMeApi}.
*/
@RunWith(MockitoJUnitRunner.class)
public class AuthMeApiTest {
@InjectMocks
private AuthMeApi api;
@Mock
private ValidationService validationService;
@Mock
private DataSource dataSource;
@Mock
private Management management;
@Mock
private PasswordSecurity passwordSecurity;
@Mock
private PlayerCache playerCache;
@Mock
private AuthMe authMe;
@Mock
private GeoIpService geoIpService;
@Test
public void shouldReturnInstanceOrNull() {
AuthMeApi result = AuthMeApi.getInstance();
assertThat(result, sameInstance(api));
ReflectionTestUtils.setField(AuthMeApi.class, null, "singleton", null);
assertThat(AuthMeApi.getInstance(), nullValue());
}
@Test
public void shouldReturnIfPlayerIsAuthenticated() {
// given
String name = "Bobby";
Player player = mockPlayerWithName(name);
given(playerCache.isAuthenticated(name)).willReturn(true);
// when
boolean result = api.isAuthenticated(player);
// then
verify(playerCache).isAuthenticated(name);
assertThat(result, equalTo(true));
}
@Test
public void shouldReturnIfPlayerIsNpc() {
// given
Player player = mock(Player.class);
given(player.hasMetadata("NPC")).willReturn(true);
// when
boolean result = api.isNpc(player);
// then
assertThat(result, equalTo(true));
verify(player).hasMetadata("NPC");
}
@Test
public void shouldReturnIfPlayerIsUnrestricted() {
// given
String name = "Tester";
Player player = mockPlayerWithName(name);
given(validationService.isUnrestricted(name)).willReturn(true);
// when
boolean result = api.isUnrestricted(player);
// then
verify(validationService).isUnrestricted(name);
assertThat(result, equalTo(true));
}
@Test
public void shouldGetLastLocation() {
// given
String name = "Gary";
Player player = mockPlayerWithName(name);
PlayerAuth auth = PlayerAuth.builder().name(name)
.locWorld("world")
.locX(12.4)
.locY(24.6)
.locZ(-438.2)
.locYaw(3.41f)
.locPitch(0.29f)
.build();
given(playerCache.getAuth(name)).willReturn(auth);
Server server = mock(Server.class);
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
World world = mock(World.class);
given(server.getWorld(auth.getWorld())).willReturn(world);
// when
Location result = api.getLastLocation(player);
// then
assertThat(result, not(nullValue()));
assertThat(result.getX(), equalTo(auth.getQuitLocX()));
assertThat(result.getY(), equalTo(auth.getQuitLocY()));
assertThat(result.getZ(), equalTo(auth.getQuitLocZ()));
assertThat(result.getWorld(), equalTo(world));
assertThat(result.getYaw(), equalTo(auth.getYaw()));
assertThat(result.getPitch(), equalTo(auth.getPitch()));
}
@Test
public void shouldGetLastIp() {
// given
String name = "Gabriel";
Player player = mockPlayerWithName(name);
PlayerAuth auth = PlayerAuth.builder().name(name)
.lastIp("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 shouldReturnNullAsLastIpForUnknownUser() {
// given
String name = "Harrison";
given(playerCache.getAuth(name)).willReturn(null);
given(dataSource.getAuth(name)).willReturn(null);
// when
String result = api.getLastIp(name);
// then
assertThat(result, nullValue());
verify(playerCache).getAuth(name);
verify(dataSource).getAuth(name);
}
@Test
public void shouldGetLastLogin() {
// given
String name = "David";
PlayerAuth auth = PlayerAuth.builder().name(name)
.lastLogin(1501597979L)
.build();
given(playerCache.getAuth(name)).willReturn(auth);
// when
Date result = api.getLastLogin(name);
// then
assertThat(result, not(nullValue()));
assertThat(result, equalTo(new Date(1501597979L)));
}
@Test
public void shouldHandleNullLastLogin() {
// given
String name = "John";
PlayerAuth auth = PlayerAuth.builder().name(name)
.lastLogin(null)
.build();
given(dataSource.getAuth(name)).willReturn(auth);
// when
Date result = api.getLastLogin(name);
// then
assertThat(result, nullValue());
verify(dataSource).getAuth(name);
}
@Test
public void shouldGetLastLoginTime() {
// given
String name = "David";
PlayerAuth auth = PlayerAuth.builder().name(name)
.lastLogin(1501597979L)
.build();
given(playerCache.getAuth(name)).willReturn(auth);
// when
Instant result = api.getLastLoginTime(name);
// then
assertThat(result, not(nullValue()));
assertThat(result, equalTo(Instant.ofEpochMilli(1501597979L)));
}
@Test
public void testGetLastLoginMillis() {
AuthMeApi result = AuthMeApi.getInstance();
assertThat(result.getLastLoginTime("notAPlayer"), nullValue());
}
@Test
public void shouldHandleNullLastLoginTime() {
// given
String name = "John";
PlayerAuth auth = PlayerAuth.builder().name(name)
.lastLogin(null)
.build();
given(dataSource.getAuth(name)).willReturn(auth);
// when
Instant result = api.getLastLoginTime(name);
// then
assertThat(result, nullValue());
verify(dataSource).getAuth(name);
}
@Test
public void shouldReturnNullForUnavailablePlayer() {
// given
String name = "Numan";
Player player = mockPlayerWithName(name);
given(playerCache.getAuth(name)).willReturn(null);
// when
Location result = api.getLastLocation(player);
// then
assertThat(result, nullValue());
}
@Test
public void shouldCheckForRegisteredName() {
// given
String name = "toaster";
given(dataSource.isAuthAvailable(name)).willReturn(true);
// when
boolean result = api.isRegistered(name);
// then
assertThat(result, equalTo(true));
}
@Test
public void shouldCheckPassword() {
// given
String playerName = "Robert";
String password = "someSecretPhrase2983";
given(passwordSecurity.comparePassword(password, playerName)).willReturn(true);
// when
boolean result = api.checkPassword(playerName, password);
// then
verify(passwordSecurity).comparePassword(password, playerName);
assertThat(result, equalTo(true));
}
@Test
public void shouldReturnAuthNames() {
// given
String[] names = {"bobby", "peter", "elisabeth", "craig"};
List<PlayerAuth> auths = Arrays.stream(names)
.map(name -> PlayerAuth.builder().name(name).build())
.collect(Collectors.toList());
given(dataSource.getAllAuths()).willReturn(auths);
// when
List<String> result = api.getRegisteredNames();
// then
assertThat(result, contains(names));
}
@Test
public void shouldReturnAuthRealNames() {
// given
String[] names = {"Bobby", "peter", "Elisabeth", "CRAIG"};
List<PlayerAuth> auths = Arrays.stream(names)
.map(name -> PlayerAuth.builder().name(name).realName(name).build())
.collect(Collectors.toList());
given(dataSource.getAllAuths()).willReturn(auths);
// when
List<String> result = api.getRegisteredRealNames();
// then
assertThat(result, contains(names));
}
@Test
public void shouldUnregisterPlayer() {
// given
Player player = mock(Player.class);
String name = "Donald";
given(player.getName()).willReturn(name);
// when
api.forceUnregister(player);
// then
verify(management).performUnregisterByAdmin(null, name, player);
}
@Test
public void shouldUnregisterPlayerByName() {
// given
Server server = mock(Server.class);
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
String name = "tristan";
Player player = mock(Player.class);
given(server.getPlayer(name)).willReturn(player);
// when
api.forceUnregister(name);
// then
verify(management).performUnregisterByAdmin(null, name, player);
}
@Test
public void shouldChangePassword() {
// given
String name = "Bobby12";
String password = "resetPw!";
// when
api.changePassword(name, password);
// then
verify(management).performPasswordChangeAsAdmin(null, name, password);
}
@Test
public void shouldReturnAuthMeInstance() {
// given / when
AuthMe result = api.getPlugin();
// then
assertThat(result, equalTo(authMe));
}
@Test
public void shouldReturnVersion() {
// given / when
String result = api.getPluginVersion();
// then
assertThat(result, equalTo(AuthMe.getPluginVersion()));
}
@Test
public void shouldForceLogin() {
// given
Player player = mock(Player.class);
// when
api.forceLogin(player);
// then
verify(management).forceLogin(player);
}
@Test
public void shouldForceLogout() {
// given
Player player = mock(Player.class);
// when
api.forceLogout(player);
// then
verify(management).performLogout(player);
}
@Test
public void shouldForceRegister() {
// given
Player player = mock(Player.class);
String pass = "test235";
// when
api.forceRegister(player, pass);
// then
verify(management).performRegister(eq(RegistrationMethod.API_REGISTRATION),
argThat(hasEqualValuesOnAllFields(ApiPasswordRegisterParams.of(player, pass, true))));
}
@Test
public void shouldForceRegisterAndNotAutoLogin() {
// given
Player player = mock(Player.class);
String pass = "test235";
// when
api.forceRegister(player, pass, false);
// then
verify(management).performRegister(eq(RegistrationMethod.API_REGISTRATION),
argThat(hasEqualValuesOnAllFields(ApiPasswordRegisterParams.of(player, pass, false))));
}
@Test
public void shouldRegisterPlayer() {
// given
String name = "Marco";
String password = "myP4ss";
HashedPassword hashedPassword = new HashedPassword("0395872SLKDFJOWEIUTEJSD");
given(passwordSecurity.computeHash(password, name.toLowerCase(Locale.ROOT))).willReturn(hashedPassword);
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(true);
// when
boolean result = api.registerPlayer(name, password);
// 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() {
// given
String name = "jonah";
given(dataSource.isAuthAvailable(name)).willReturn(true);
// when
boolean result = api.registerPlayer(name, "pass");
// then
assertThat(result, equalTo(false));
verify(dataSource, only()).isAuthAvailable(name);
verifyNoInteractions(management, passwordSecurity);
}
@Test
public void shouldGetNamesByIp() {
// given
String ip = "123.123.123.123";
List<String> names = Arrays.asList("Morgan", "Batista", "QUINN");
given(dataSource.getAllAuthsByIp(ip)).willReturn(names);
// when
List<String> result = api.getNamesByIp(ip);
// then
assertThat(result, equalTo(names));
verify(dataSource).getAllAuthsByIp(ip);
}
@Test
public void shouldReturnGeoIpInfo() {
// given
String ip = "127.127.12.1";
given(geoIpService.getCountryCode(ip)).willReturn("XA");
given(geoIpService.getCountryName(ip)).willReturn("Syldavia");
// when
String countryCode = api.getCountryCode(ip);
String countryName = api.getCountryName(ip);
// then
assertThat(countryCode, equalTo("XA"));
assertThat(countryName, equalTo("Syldavia"));
}
@Test
public void shouldReturnAuthMePlayerInfo() {
// given
PlayerAuth auth = PlayerAuth.builder()
.name("bobb")
.realName("Bobb")
.registrationDate(1433166082000L)
.build();
given(dataSource.getAuth("bobb")).willReturn(auth);
// when
Optional<AuthMePlayer> result = api.getPlayerInfo("bobb");
// then
AuthMePlayer playerInfo = result.get();
assertThat(playerInfo.getName(), equalTo("Bobb"));
assertThat(playerInfo.getRegistrationDate(), equalTo(Instant.ofEpochMilli(1433166082000L)));
}
@Test
public void shouldReturnNullForNonExistentAuth() {
// given / when
Optional<AuthMePlayer> result = api.getPlayerInfo("doesNotExist");
// then
assertThat(result.isPresent(), equalTo(false));
verify(playerCache).getAuth("doesNotExist");
verify(dataSource).getAuth("doesNotExist");
}
private static Player mockPlayerWithName(String name) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
return player;
}
}

View File

@ -1,95 +0,0 @@
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();
}
};
}
}

View File

@ -1,84 +0,0 @@
package fr.xephi.authme.command;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static fr.xephi.authme.TestHelper.getJarFile;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
/**
* Checks that the commands declared in plugin.yml correspond
* to the ones built by the {@link CommandInitializer}.
*/
public class CommandConsistencyTest {
@Test
public void shouldHaveEqualDefinitions() {
// given
Collection<List<String>> initializedCommands = initializeCommands();
Map<String, List<String>> pluginFileLabels = getLabelsFromPluginFile();
// when / then
assertThat("number of base commands are equal in plugin.yml and CommandInitializer",
initializedCommands.size(), equalTo(pluginFileLabels.size()));
for (List<String> commandLabels : initializedCommands) {
List<String> pluginYmlLabels = pluginFileLabels.get(commandLabels.get(0));
// NB: the first label in CommandDescription needs to correspond to the key in plugin.yml
assertThat("plugin.yml contains definition for command '" + commandLabels.get(0) + "'",
pluginYmlLabels, not(nullValue()));
assertThat("plugin.yml and CommandDescription have same alternative labels for /" + commandLabels.get(0),
pluginYmlLabels, containsInAnyOrder(commandLabels.subList(1, commandLabels.size()).toArray()));
}
}
/**
* Gets the command definitions from CommandInitializer and returns the
* labels of all base commands.
*
* @return collection of all base command labels
*/
private static Collection<List<String>> initializeCommands() {
CommandInitializer initializer = new CommandInitializer();
Collection<List<String>> commandLabels = new ArrayList<>();
for (CommandDescription baseCommand : initializer.getCommands()) {
commandLabels.add(baseCommand.getLabels());
}
return commandLabels;
}
/**
* Reads plugin.yml and returns the defined commands by main label and aliases.
*
* @return collection of all labels and their aliases
*/
@SuppressWarnings("unchecked")
private static Map<String, List<String>> getLabelsFromPluginFile() {
FileConfiguration pluginFile = YamlConfiguration.loadConfiguration(getJarFile("/plugin.yml"));
MemorySection commandList = (MemorySection) pluginFile.get("commands");
Map<String, Object> commandDefinitions = commandList.getValues(false);
Map<String, List<String>> commandLabels = new HashMap<>();
for (Map.Entry<String, Object> commandDefinition : commandDefinitions.entrySet()) {
MemorySection definition = (MemorySection) commandDefinition.getValue();
List<String> alternativeLabels = definition.get("aliases") == null
? Collections.EMPTY_LIST
: (List<String>) definition.get("aliases");
commandLabels.put(commandDefinition.getKey(), alternativeLabels);
}
return commandLabels;
}
}

View File

@ -1,302 +0,0 @@
package fr.xephi.authme.command;
import ch.jalu.injector.Injector;
import ch.jalu.injector.factory.Factory;
import com.google.common.collect.Sets;
import fr.xephi.authme.command.TestCommandsUtil.TestLoginCommand;
import fr.xephi.authme.command.TestCommandsUtil.TestRegisterCommand;
import fr.xephi.authme.command.TestCommandsUtil.TestUnregisterCommand;
import fr.xephi.authme.command.help.HelpProvider;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.permission.PermissionsManager;
import org.bukkit.command.CommandSender;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static fr.xephi.authme.command.FoundResultStatus.INCORRECT_ARGUMENTS;
import static fr.xephi.authme.command.FoundResultStatus.MISSING_BASE_COMMAND;
import static fr.xephi.authme.command.FoundResultStatus.NO_PERMISSION;
import static fr.xephi.authme.command.FoundResultStatus.SUCCESS;
import static fr.xephi.authme.command.FoundResultStatus.UNKNOWN_LABEL;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link CommandHandler}.
*/
// Justification: It's more readable to use asList() everywhere in the test when we often generated two lists where one
// often consists of only one element, e.g. myMethod(asList("authme"), asList("my", "args"), ...)
@SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
@RunWith(MockitoJUnitRunner.class)
public class CommandHandlerTest {
private CommandHandler handler;
@Mock
private Factory<ExecutableCommand> commandFactory;
@Mock
private CommandMapper commandMapper;
@Mock
private PermissionsManager permissionsManager;
@Mock
private Messages messages;
@Mock
private HelpProvider helpProvider;
private Map<Class<? extends ExecutableCommand>, ExecutableCommand> mockedCommands = new HashMap<>();
@Before
@SuppressWarnings("unchecked")
public void initializeCommandMapper() {
given(commandMapper.getCommandClasses()).willReturn(Sets.newHashSet(
ExecutableCommand.class, TestLoginCommand.class, TestRegisterCommand.class, TestUnregisterCommand.class));
setInjectorToMockExecutableCommandClasses();
handler = new CommandHandler(commandFactory, commandMapper, permissionsManager, messages, helpProvider);
}
/**
* Makes the injector return a mock when {@link Injector#newInstance(Class)} is invoked
* with (a child of) ExecutableCommand.class. The mocks the injector creates are stored in {@link #mockedCommands}.
* <p>
* The {@link CommandMapper} is mocked in {@link #initializeCommandMapper()} to return certain test classes.
*/
@SuppressWarnings("unchecked")
private void setInjectorToMockExecutableCommandClasses() {
given(commandFactory.newInstance(any(Class.class))).willAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Class<?> clazz = invocation.getArgument(0);
if (ExecutableCommand.class.isAssignableFrom(clazz)) {
Class<? extends ExecutableCommand> commandClass = (Class<? extends ExecutableCommand>) clazz;
ExecutableCommand mock = mock(commandClass);
mockedCommands.put(commandClass, mock);
return mock;
}
throw new IllegalStateException("Unexpected class '" + clazz.getName()
+ "': Not a child of ExecutableCommand");
}
});
}
@Test
public void shouldCallMappedCommandWithArgs() {
// given
String bukkitLabel = "Authme";
String[] bukkitArgs = {"Login", "myPass"};
CommandSender sender = mock(CommandSender.class);
CommandDescription command = mock(CommandDescription.class);
doReturn(TestLoginCommand.class).when(command).getExecutableCommand();
given(commandMapper.mapPartsToCommand(any(CommandSender.class), anyList()))
.willReturn(new FoundCommandResult(command, asList("Authme", "Login"), asList("myPass"), 0.0, SUCCESS));
// when
handler.processCommand(sender, bukkitLabel, bukkitArgs);
// then
ExecutableCommand executableCommand = mockedCommands.get(TestLoginCommand.class);
verify(commandMapper).mapPartsToCommand(sender, asList("Authme", "Login", "myPass"));
verify(executableCommand).executeCommand(sender, asList("myPass"));
// Ensure that no error message was issued to the command sender
verify(sender, never()).sendMessage(anyString());
}
@Test
public void shouldNotCallExecutableCommandIfNoPermission() {
// given
String bukkitLabel = "unreg";
String[] bukkitArgs = {"testPlayer"};
CommandSender sender = mock(CommandSender.class);
CommandDescription command = mock(CommandDescription.class);
given(commandMapper.mapPartsToCommand(any(CommandSender.class), anyList()))
.willReturn(new FoundCommandResult(command, asList("unreg"), asList("testPlayer"), 0.0, NO_PERMISSION));
// when
handler.processCommand(sender, bukkitLabel, bukkitArgs);
// then
verify(commandMapper).mapPartsToCommand(sender, asList("unreg", "testPlayer"));
verify(command, never()).getExecutableCommand();
verify(messages).send(sender, MessageKey.NO_PERMISSION);
}
@Test
public void shouldNotCallExecutableForWrongArguments() {
// given
String bukkitLabel = "unreg";
String[] bukkitArgs = {"testPlayer"};
CommandSender sender = mock(CommandSender.class);
CommandDescription command = mock(CommandDescription.class);
given(command.getExecutableCommand()).willReturn((Class) TestUnregisterCommand.class);
given(commandMapper.mapPartsToCommand(any(CommandSender.class), anyList())).willReturn(
new FoundCommandResult(command, asList("unreg"), asList("testPlayer"), 0.0, INCORRECT_ARGUMENTS));
given(permissionsManager.hasPermission(sender, command.getPermission())).willReturn(true);
// when
handler.processCommand(sender, bukkitLabel, bukkitArgs);
// then
verify(commandMapper).mapPartsToCommand(sender, asList("unreg", "testPlayer"));
verify(sender, atLeastOnce()).sendMessage(argThat(containsString("Incorrect command arguments")));
}
@Test
public void shouldUseCustomMessageUponArgumentMismatch() {
// given
String bukkitLabel = "unreg";
String[] bukkitArgs = {"testPlayer"};
CommandSender sender = mock(CommandSender.class);
CommandDescription command = mock(CommandDescription.class);
given(command.getExecutableCommand()).willReturn((Class) TestUnregisterCommand.class);
given(mockedCommands.get(TestUnregisterCommand.class).getArgumentsMismatchMessage())
.willReturn(MessageKey.USAGE_RECOVER_EMAIL);
given(commandMapper.mapPartsToCommand(any(CommandSender.class), anyList())).willReturn(
new FoundCommandResult(command, asList("unreg"), asList("testPlayer"), 0.0, INCORRECT_ARGUMENTS));
given(permissionsManager.hasPermission(sender, command.getPermission())).willReturn(true);
// when
handler.processCommand(sender, bukkitLabel, bukkitArgs);
// then
verify(commandMapper).mapPartsToCommand(sender, asList("unreg", "testPlayer"));
verify(messages).send(sender, MessageKey.USAGE_RECOVER_EMAIL);
verify(sender, never()).sendMessage(anyString());
}
@Test
public void shouldNotCallExecutableForWrongArgumentsAndPermissionDenied() {
// given
String bukkitLabel = "unreg";
String[] bukkitArgs = {"testPlayer"};
CommandSender sender = mock(CommandSender.class);
CommandDescription command = mock(CommandDescription.class);
given(commandMapper.mapPartsToCommand(any(CommandSender.class), anyList())).willReturn(
new FoundCommandResult(command, asList("unreg"), asList("testPlayer"), 0.0, INCORRECT_ARGUMENTS));
given(permissionsManager.hasPermission(sender, command.getPermission())).willReturn(false);
// when
handler.processCommand(sender, bukkitLabel, bukkitArgs);
// then
verify(commandMapper).mapPartsToCommand(sender, asList("unreg", "testPlayer"));
verify(command, never()).getExecutableCommand();
verify(messages).send(sender, MessageKey.NO_PERMISSION);
}
@Test
public void shouldNotCallExecutableForFailedParsing() {
// given
String bukkitLabel = "unreg";
String[] bukkitArgs = {"testPlayer"};
CommandSender sender = mock(CommandSender.class);
CommandDescription command = mock(CommandDescription.class);
given(commandMapper.mapPartsToCommand(any(CommandSender.class), anyList())).willReturn(
new FoundCommandResult(command, asList("unreg"), asList("testPlayer"), 0.0, MISSING_BASE_COMMAND));
// when
handler.processCommand(sender, bukkitLabel, bukkitArgs);
// then
verify(commandMapper).mapPartsToCommand(sender, asList("unreg", "testPlayer"));
verify(command, never()).getExecutableCommand();
verify(sender).sendMessage(argThat(containsString("Failed to parse")));
}
@Test
public void shouldNotCallExecutableForUnknownLabelAndHaveSuggestion() {
// given
String bukkitLabel = "unreg";
String[] bukkitArgs = {"testPlayer"};
CommandSender sender = mock(CommandSender.class);
CommandDescription command = mock(CommandDescription.class);
given(command.getLabels()).willReturn(Collections.singletonList("test_cmd"));
given(commandMapper.mapPartsToCommand(any(CommandSender.class), anyList())).willReturn(
new FoundCommandResult(command, asList("unreg"), asList("testPlayer"), 0.01, UNKNOWN_LABEL));
// when
handler.processCommand(sender, bukkitLabel, bukkitArgs);
// then
verify(commandMapper).mapPartsToCommand(sender, asList("unreg", "testPlayer"));
verify(command, never()).getExecutableCommand();
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender, times(3)).sendMessage(captor.capture());
assertThat(captor.getAllValues().get(0), containsString("Unknown command"));
assertThat(captor.getAllValues().get(1), containsString("Did you mean"));
assertThat(captor.getAllValues().get(1), containsString("/test_cmd"));
assertThat(captor.getAllValues().get(2), containsString("Use the command"));
assertThat(captor.getAllValues().get(2), containsString("to view help"));
}
@Test
public void shouldNotCallExecutableForUnknownLabelAndNotSuggestCommand() {
// given
String bukkitLabel = "unreg";
String[] bukkitArgs = {"testPlayer"};
CommandSender sender = mock(CommandSender.class);
CommandDescription command = mock(CommandDescription.class);
given(commandMapper.mapPartsToCommand(any(CommandSender.class), anyList())).willReturn(
new FoundCommandResult(command, asList("unreg"), asList("testPlayer"), 1.0, UNKNOWN_LABEL));
// when
handler.processCommand(sender, bukkitLabel, bukkitArgs);
// then
verify(commandMapper).mapPartsToCommand(sender, asList("unreg", "testPlayer"));
verify(command, never()).getExecutableCommand();
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender, times(2)).sendMessage(captor.capture());
assertThat(captor.getAllValues().get(0), containsString("Unknown command"));
assertThat(captor.getAllValues().get(1), containsString("Use the command"));
assertThat(captor.getAllValues().get(1), containsString("to view help"));
}
@Test
public void shouldStripWhitespace() {
// given
String bukkitLabel = "AuthMe";
String[] bukkitArgs = {" ", "", "REGISTER", " ", "testArg", " "};
CommandSender sender = mock(CommandSender.class);
CommandDescription command = mock(CommandDescription.class);
doReturn(TestRegisterCommand.class).when(command).getExecutableCommand();
given(commandMapper.mapPartsToCommand(eq(sender), anyList()))
.willReturn(new FoundCommandResult(command, asList("AuthMe", "REGISTER"), asList("testArg"), 0.0, SUCCESS));
// when
handler.processCommand(sender, bukkitLabel, bukkitArgs);
// then
ExecutableCommand executableCommand = mockedCommands.get(TestRegisterCommand.class);
verify(commandMapper).mapPartsToCommand(sender, asList("AuthMe", "REGISTER", "testArg"));
verify(executableCommand).executeCommand(sender, asList("testArg"));
verify(sender, never()).sendMessage(anyString());
}
}

View File

@ -1,283 +0,0 @@
package fr.xephi.authme.command;
import fr.xephi.authme.util.StringUtils;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.regex.Pattern;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
/**
* Test for {@link CommandInitializer} to guarantee the integrity of the defined commands.
*/
public class CommandInitializerTest {
/**
* Defines the maximum allowed depths for nesting CommandDescription instances.
* Note that the depth starts at 0 (e.g. /authme), so a depth of 2 is something like /authme hello world
*/
private static int MAX_ALLOWED_DEPTH = 1;
private static Collection<CommandDescription> commands;
@BeforeClass
public static void initializeCommandCollection() {
CommandInitializer commandInitializer = new CommandInitializer();
commands = commandInitializer.getCommands();
}
@Test
public void shouldInitializeCommands() {
// given/when/then
// It obviously doesn't make sense to test much of the concrete data
// that is being initialized; we just want to guarantee with this test
// that data is indeed being initialized and we take a few "probes"
assertThat(commands, hasSize(10));
assertThat(commandsIncludeLabel(commands, "authme"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "register"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "help"), equalTo(false));
}
@Test
public void shouldNotBeNestedExcessively() {
// given
BiConsumer<CommandDescription, Integer> descriptionTester =
(command, depth) -> assertThat(depth <= MAX_ALLOWED_DEPTH, equalTo(true));
// when/then
walkThroughCommands(commands, descriptionTester);
}
/** Ensure that all children of a command stored the parent. */
@Test
public void shouldHaveConnectionBetweenParentAndChild() {
// given
BiConsumer<CommandDescription, Integer> connectionTester = new BiConsumer<CommandDescription, Integer>() {
@Override
public void accept(CommandDescription command, Integer depth) {
if (!command.getChildren().isEmpty()) {
for (CommandDescription child : command.getChildren()) {
assertThat(command.equals(child.getParent()), equalTo(true));
}
}
// Checking that the parent has the current command as child is redundant as this is how we can traverse
// the "command tree" in the first place - if we're here, it's that the parent definitely has the
// command as child.
}
};
// when/then
walkThroughCommands(commands, connectionTester);
}
@Test
public void shouldUseProperLowerCaseLabels() {
// given
final Pattern invalidPattern = Pattern.compile("\\s");
BiConsumer<CommandDescription, Integer> labelFormatTester = new BiConsumer<CommandDescription, Integer>() {
@Override
public void accept(CommandDescription command, Integer depth) {
for (String label : command.getLabels()) {
if (!label.equals(label.toLowerCase(Locale.ROOT))) {
fail("Label '" + label + "' should be lowercase");
} else if (invalidPattern.matcher(label).matches()) {
fail("Label '" + label + "' has whitespace");
}
}
}
};
// when/then
walkThroughCommands(commands, labelFormatTester);
}
@Test
public void shouldNotDefineSameLabelTwice() {
// given
final Set<String> commandMappings = new HashSet<>();
BiConsumer<CommandDescription, Integer> uniqueMappingTester = new BiConsumer<CommandDescription, Integer>() {
@Override
public void accept(CommandDescription command, Integer depth) {
int initialSize = commandMappings.size();
List<String> newMappings = getAbsoluteLabels(command);
commandMappings.addAll(newMappings);
// Set only contains unique entries, so we just check after adding all new mappings that the size
// of the Set corresponds to our expectation
assertThat("All bindings are unique for command with bindings '" + newMappings + "'",
commandMappings.size() == initialSize + newMappings.size(), equalTo(true));
}
};
// when/then
walkThroughCommands(commands, uniqueMappingTester);
}
/**
* The description should provide a very short description of the command and shouldn't end in a ".", whereas the
* detailed description should be longer and end with a period.
*/
@Test
public void shouldHaveProperDescription() {
// given
BiConsumer<CommandDescription, Integer> descriptionTester = new BiConsumer<CommandDescription, Integer>() {
@Override
public void accept(CommandDescription command, Integer depth) {
String forCommandText = " for command with labels '" + command.getLabels() + "'";
assertThat("has description" + forCommandText,
StringUtils.isBlank(command.getDescription()), equalTo(false));
assertThat("short description doesn't end in '.'" + forCommandText,
command.getDescription().endsWith("."), equalTo(false));
assertThat("has detailed description" + forCommandText,
StringUtils.isBlank(command.getDetailedDescription()), equalTo(false));
assertThat("detailed description ends in '.'" + forCommandText,
command.getDetailedDescription().endsWith("."), equalTo(true));
}
};
// when/then
walkThroughCommands(commands, descriptionTester);
}
@Test
public void shouldHaveOptionalArgumentsAfterMandatoryOnes() {
// given
BiConsumer<CommandDescription, Integer> argumentOrderTester = new BiConsumer<CommandDescription, Integer>() {
@Override
public void accept(CommandDescription command, Integer depth) {
boolean encounteredOptionalArg = false;
for (CommandArgumentDescription argument : command.getArguments()) {
if (argument.isOptional()) {
encounteredOptionalArg = true;
} else if (encounteredOptionalArg) {
fail("Mandatory arguments should come before optional ones for command with labels '"
+ command.getLabels() + "'");
}
}
}
};
// when/then
walkThroughCommands(commands, argumentOrderTester);
}
/**
* Ensure that a command with children (i.e. a base command) doesn't define any arguments. This might otherwise
* clash with the label of the child.
*/
@Test
public void shouldNotHaveArgumentsIfCommandHasChildren() {
// given
BiConsumer<CommandDescription, Integer> noArgumentForParentChecker = new BiConsumer<CommandDescription, Integer>() {
@Override
public void accept(CommandDescription command, Integer depth) {
// Fail if the command has children and has arguments at the same time
// Exception: If the parent only has one child defining the help label, it is acceptable
if (!command.getChildren().isEmpty() && !command.getArguments().isEmpty()
&& (command.getChildren().size() != 1 || !command.getChildren().get(0).hasLabel("help"))) {
fail("Parent command (labels='" + command.getLabels() + "') should not have any arguments");
}
}
};
// when/then
walkThroughCommands(commands, noArgumentForParentChecker);
}
/**
* Tests that multiple CommandDescription instances pointing to the same ExecutableCommand use the same
* count of arguments.
*/
@Test
public void shouldPointToSameExecutableCommandWithConsistentArgumentCount() {
// given
final Map<Class<? extends ExecutableCommand>, Integer> mandatoryArguments = new HashMap<>();
final Map<Class<? extends ExecutableCommand>, Integer> totalArguments = new HashMap<>();
BiConsumer<CommandDescription, Integer> argChecker = new BiConsumer<CommandDescription, Integer>() {
@Override
public void accept(CommandDescription command, Integer depth) {
testCollectionForCommand(command, CommandUtils.getMinNumberOfArguments(command), mandatoryArguments);
testCollectionForCommand(command, CommandUtils.getMaxNumberOfArguments(command), totalArguments);
}
private void testCollectionForCommand(CommandDescription command, int argCount,
Map<Class<? extends ExecutableCommand>, Integer> collection) {
final Class<? extends ExecutableCommand> clazz = command.getExecutableCommand();
Integer existingCount = collection.get(clazz);
if (existingCount == null) {
collection.put(clazz, argCount);
} else {
String commandDescription = "Command with label '" + command.getLabels().get(0) + "' and parent '"
+ (command.getParent() == null ? "null" : command.getLabels().get(0)) + "' ";
assertThat(commandDescription + "should point to " + clazz + " with arguments consistent to others",
argCount, equalTo(existingCount));
}
}
};
// when / then
walkThroughCommands(commands, argChecker);
}
// ------------
// Helper methods
// ------------
private static void walkThroughCommands(Collection<CommandDescription> commands,
BiConsumer<CommandDescription, Integer> consumer) {
walkThroughCommands(commands, consumer, 0);
}
private static void walkThroughCommands(Collection<CommandDescription> commands,
BiConsumer<CommandDescription, Integer> consumer, int depth) {
for (CommandDescription command : commands) {
consumer.accept(command, depth);
if (!command.getChildren().isEmpty()) {
walkThroughCommands(command.getChildren(), consumer, depth + 1);
}
}
}
private static boolean commandsIncludeLabel(Iterable<CommandDescription> commands, String label) {
for (CommandDescription command : commands) {
if (command.getLabels().contains(label)) {
return true;
}
}
return false;
}
/**
* Get the absolute binding that a command defines. Note: Assumes that only the passed command can have
* multiple labels; only considering the first label for all of the command's parents.
*
* @param command The command to process
*
* @return List of all bindings that lead to the command
*/
private static List<String> getAbsoluteLabels(CommandDescription command) {
CommandDescription parent = command.getParent();
String parentPath = (parent == null) ? "" : parent.getLabels().get(0) + " ";
List<String> bindings = new ArrayList<>(command.getLabels().size());
for (String label : command.getLabels()) {
bindings.add(parentPath + label);
}
return bindings;
}
}

View File

@ -1,319 +0,0 @@
package fr.xephi.authme.command;
import ch.jalu.injector.testing.BeforeInjecting;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.command.TestCommandsUtil.TestLoginCommand;
import fr.xephi.authme.command.TestCommandsUtil.TestRegisterCommand;
import fr.xephi.authme.command.TestCommandsUtil.TestUnregisterCommand;
import fr.xephi.authme.command.executable.HelpCommand;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsManager;
import org.bukkit.command.CommandSender;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import java.util.List;
import java.util.Set;
import static fr.xephi.authme.command.TestCommandsUtil.getCommandWithLabel;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Test for {@link CommandMapper}.
*/
@RunWith(DelayedInjectionRunner.class)
public class CommandMapperTest {
private static List<CommandDescription> commands;
@InjectDelayed
private CommandMapper mapper;
@Mock
private PermissionsManager permissionsManager;
@Mock
private CommandInitializer commandInitializer;
@BeforeClass
public static void setUpCommandHandler() {
commands = TestCommandsUtil.generateCommands();
}
@BeforeInjecting
public void setUpMocks() {
given(commandInitializer.getCommands()).willReturn(commands);
}
// -----------
// mapPartsToCommand() tests
// -----------
@Test
public void shouldMapPartsToLoginChildCommand() {
// given
List<String> parts = asList("authme", "login", "test1");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);
// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
// then
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "authme", "login")));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.SUCCESS));
assertThat(result.getArguments(), contains("test1"));
assertThat(result.getDifference(), equalTo(0.0));
assertThat(result.getLabels(), equalTo(parts.subList(0, 2)));
assertThat(result.getArguments(), contains(parts.get(2)));
}
@Test
public void shouldMapPartsToCommandWithNoCaseSensitivity() {
// given
List<String> parts = asList("Authme", "REG", "arg1", "arg2");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);
// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
// then
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "authme", "register")));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.SUCCESS));
assertThat(result.getLabels(), equalTo(parts.subList(0, 2)));
assertThat(result.getArguments(), contains("arg1", "arg2"));
assertThat(result.getDifference(), equalTo(0.0));
}
@Test
public void shouldRejectCommandWithTooManyArguments() {
// given
List<String> parts = asList("authme", "register", "pass123", "pass123", "pass123");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);
// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
// then
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "authme", "register")));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.INCORRECT_ARGUMENTS));
assertThat(result.getDifference(), equalTo(0.0));
assertThat(result.getLabels(), equalTo(parts.subList(0, 2)));
assertThat(result.getArguments(), equalTo(parts.subList(2, 5)));
}
@Test
public void shouldRejectCommandWithTooFewArguments() {
// given
List<String> parts = asList("authme", "Reg");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);
// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
// then
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "authme", "register")));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.INCORRECT_ARGUMENTS));
assertThat(result.getDifference(), equalTo(0.0));
assertThat(result.getLabels(), equalTo(parts));
assertThat(result.getArguments(), empty());
}
@Test
public void shouldSuggestCommandWithSimilarLabel() {
// given
List<String> parts = asList("authme", "reh", "pass123", "pass123");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);
// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
// then
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "authme", "register")));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.UNKNOWN_LABEL));
assertThat(result.getDifference() < 0.75, equalTo(true));
assertThat(result.getLabels(), equalTo(parts.subList(0, 2)));
assertThat(result.getArguments(), contains("pass123", "pass123"));
}
/** In contrast to the previous test, we test a command request with a very apart label. */
@Test
public void shouldSuggestMostSimilarCommand() {
// given
List<String> parts = asList("authme", "asdfawetawty4asdca");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);
// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
// then
assertThat(result.getCommandDescription(), not(nullValue()));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.UNKNOWN_LABEL));
assertThat(result.getDifference() > 0.75, equalTo(true));
assertThat(result.getLabels(), equalTo(parts));
assertThat(result.getArguments(), empty());
}
@Test
public void shouldHandleBaseWithWrongArguments() {
// given
List<String> parts = singletonList("unregister");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);
// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
// then
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.INCORRECT_ARGUMENTS));
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "unregister")));
assertThat(result.getDifference(), equalTo(0.0));
assertThat(result.getArguments(), empty());
assertThat(result.getLabels(), equalTo(parts));
}
@Test
public void shouldHandleUnknownBase() {
// given
List<String> parts = asList("bogus", "label1", "arg1");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);
// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
// then
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.MISSING_BASE_COMMAND));
assertThat(result.getCommandDescription(), nullValue());
}
@Test
public void shouldHandleNullInput() {
// given / when
FoundCommandResult result = mapper.mapPartsToCommand(mock(CommandSender.class), null);
// then
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.MISSING_BASE_COMMAND));
assertThat(result.getCommandDescription(), nullValue());
}
@Test
public void shouldMapToBaseWithProperArguments() {
// given
List<String> parts = asList("Unreg", "player1");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);
// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
// then
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.SUCCESS));
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "unregister")));
assertThat(result.getDifference(), equalTo(0.0));
assertThat(result.getArguments(), contains("player1"));
assertThat(result.getLabels(), contains("Unreg"));
}
@Test
public void shouldReturnChildlessBaseCommandWithArgCountError() {
// given
List<String> parts = asList("unregistER", "player1", "wrongArg");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);
// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
// then
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.INCORRECT_ARGUMENTS));
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "unregister")));
assertThat(result.getDifference(), equalTo(0.0));
assertThat(result.getArguments(), contains("player1", "wrongArg"));
assertThat(result.getLabels(), contains("unregistER"));
}
@Test
public void shouldPassCommandPathAsArgumentsToHelpCommand() {
// given
List<String> parts = asList("email", "helptest", "arg1");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), isNull())).willReturn(true);
// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
// then
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.SUCCESS));
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "email", "helptest")));
assertThat(result.getLabels(), contains("email", "helptest"));
assertThat(result.getArguments(), contains("email", "arg1"));
assertThat(result.getDifference(), equalTo(0.0));
}
@Test
public void shouldRecognizeMissingPermissionForCommand() {
// given
List<String> parts = asList("authme", "login", "test1");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(false);
// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
// then
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "authme", "login")));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.NO_PERMISSION));
assertThat(result.getArguments(), contains("test1"));
assertThat(result.getDifference(), equalTo(0.0));
assertThat(result.getLabels(), equalTo(parts.subList(0, 2)));
assertThat(result.getArguments(), contains(parts.get(2)));
}
@Test
public void shouldSupportAuthMePrefix() {
// given
List<String> parts = asList("authme:unregister", "Betty");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);
// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);
// then
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.SUCCESS));
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "unregister")));
}
@SuppressWarnings("unchecked")
@Test
public void shouldReturnExecutableCommandClasses() {
// given / when
Set<Class<? extends ExecutableCommand>> commandClasses = mapper.getCommandClasses();
// then
assertThat(commandClasses, containsInAnyOrder(ExecutableCommand.class, HelpCommand.class,
TestLoginCommand.class, TestRegisterCommand.class, TestUnregisterCommand.class));
}
}

View File

@ -1,138 +0,0 @@
package fr.xephi.authme.command;
import org.bukkit.ChatColor;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Test for {@link CommandUtils}.
*/
public class CommandUtilsTest {
private static Collection<CommandDescription> commands;
@BeforeClass
public static void setUpTestCommands() {
commands = Collections.unmodifiableCollection(TestCommandsUtil.generateCommands());
}
@Test
public void shouldReturnCommandPath() {
// given
CommandDescription base = CommandDescription.builder()
.labels("authme", "auth")
.description("Base")
.detailedDescription("Test base command.")
.executableCommand(ExecutableCommand.class)
.register();
CommandDescription command = CommandDescription.builder()
.parent(base)
.labels("help", "h", "?")
.description("Child")
.detailedDescription("Test child command.")
.executableCommand(ExecutableCommand.class)
.register();
// when
String commandPath = CommandUtils.constructCommandPath(command);
// then
assertThat(commandPath, equalTo("/authme help"));
}
@Test
public void shouldComputeMinAndMaxOnEmptyCommand() {
// given
CommandDescription command = getBuilderForArgsTest().register();
// when / then
checkArgumentCount(command, 0, 0);
}
@Test
public void shouldComputeMinAndMaxOnCommandWithMandatoryArgs() {
// given
CommandDescription command = getBuilderForArgsTest()
.withArgument("Test", "Arg description", false)
.withArgument("Test22", "Arg description 2", false)
.register();
// when / then
checkArgumentCount(command, 2, 2);
}
@Test
public void shouldComputeMinAndMaxOnCommandIncludingOptionalArgs() {
// given
CommandDescription command = getBuilderForArgsTest()
.withArgument("arg1", "Arg description", false)
.withArgument("arg2", "Arg description 2", true)
.withArgument("arg3", "Arg description 3", true)
.register();
// when / then
checkArgumentCount(command, 1, 3);
}
@Test
public void shouldFormatSimpleArgument() {
// given
CommandDescription command = TestCommandsUtil.getCommandWithLabel(commands, "authme");
List<String> labels = Collections.singletonList("authme");
// when
String result = CommandUtils.buildSyntax(command, labels);
// then
assertThat(result, equalTo(ChatColor.WHITE + "/authme" + ChatColor.YELLOW));
}
@Test
public void shouldFormatCommandWithMultipleArguments() {
// given
CommandDescription command = TestCommandsUtil.getCommandWithLabel(commands, "authme", "register");
List<String> labels = Arrays.asList("authme", "reg");
// when
String result = CommandUtils.buildSyntax(command, labels);
// then
assertThat(result, equalTo(ChatColor.WHITE + "/authme" + ChatColor.YELLOW + " reg <password> <confirmation>"));
}
@Test
public void shouldFormatCommandWithOptionalArgument() {
// given
CommandDescription command = TestCommandsUtil.getCommandWithLabel(commands, "email");
List<String> labels = Collections.singletonList("email");
// when
String result = CommandUtils.buildSyntax(command, labels);
// then
assertThat(result, equalTo(ChatColor.WHITE + "/email" + ChatColor.YELLOW + " [player]"));
}
private static void checkArgumentCount(CommandDescription command, int expectedMin, int expectedMax) {
assertThat(CommandUtils.getMinNumberOfArguments(command), equalTo(expectedMin));
assertThat(CommandUtils.getMaxNumberOfArguments(command), equalTo(expectedMax));
}
private static CommandDescription.CommandBuilder getBuilderForArgsTest() {
return CommandDescription.builder()
.labels("authme", "auth")
.description("Base")
.detailedDescription("Test base command.")
.executableCommand(ExecutableCommand.class);
}
}

View File

@ -1,81 +0,0 @@
package fr.xephi.authme.command;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link PlayerCommand}.
*/
public class PlayerCommandTest {
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = mock(BlockCommandSender.class);
PlayerCommandImpl command = new PlayerCommandImpl();
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(sender).sendMessage(argThat(containsString("only for players")));
}
@Test
public void shouldCallRunCommandForPlayer() {
// given
Player player = mock(Player.class);
List<String> arguments = Arrays.asList("arg1", "testarg2");
PlayerCommandImpl command = new PlayerCommandImpl();
// when
command.executeCommand(player, arguments);
// then
verify(player, times(1)).sendMessage("testarg2");
}
@Test
public void shouldRejectNonPlayerAndSendAlternative() {
// given
CommandSender sender = mock(CommandSender.class);
PlayerCommandWithAlt command = new PlayerCommandWithAlt();
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(sender, times(1)).sendMessage(argThat(containsString("use /authme test <command> instead")));
}
private static class PlayerCommandImpl extends PlayerCommand {
@Override
public void runCommand(Player player, List<String> arguments) {
player.sendMessage(arguments.get(1));
}
}
private static class PlayerCommandWithAlt extends PlayerCommand {
@Override
public void runCommand(Player player, List<String> arguments) {
throw new IllegalStateException("Should not be called");
}
@Override
public String getAlternativeCommand() {
return "/authme test <command>";
}
}
}

View File

@ -1,133 +0,0 @@
package fr.xephi.authme.command;
import com.google.common.collect.ImmutableList;
import fr.xephi.authme.command.executable.HelpCommand;
import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PlayerPermission;
import org.bukkit.command.CommandSender;
import java.util.Collection;
import java.util.List;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
/**
* Util class for generating and retrieving test commands.
*/
public final class TestCommandsUtil {
private TestCommandsUtil() {
}
/**
* Generate the collection of test commands.
*
* @return The generated commands
*/
public static List<CommandDescription> generateCommands() {
// Register /authme
CommandDescription authMeBase = createCommand(null, null, singletonList("authme"), ExecutableCommand.class);
// Register /authme login <password>
createCommand(PlayerPermission.LOGIN, authMeBase, singletonList("login"),
TestLoginCommand.class, newArgument("password", false));
// Register /authme register <password> <confirmation>, aliases: /authme reg, /authme r
createCommand(PlayerPermission.LOGIN, authMeBase, asList("register", "reg", "r"), TestRegisterCommand.class,
newArgument("password", false), newArgument("confirmation", false));
// Register /email [player]
CommandDescription emailBase = createCommand(null, null, singletonList("email"), ExecutableCommand.class,
newArgument("player", true));
// Register /email helptest -- use only to test for help command arguments special case
CommandDescription.builder().parent(emailBase).labels("helptest").executableCommand(HelpCommand.class)
.description("test").detailedDescription("Test.").withArgument("Query", "", false).register();
// Register /unregister <player>, alias: /unreg
CommandDescription unregisterBase = createCommand(AdminPermission.UNREGISTER, null,
asList("unregister", "unreg"), TestUnregisterCommand.class, newArgument("player", false));
return ImmutableList.of(authMeBase, emailBase, unregisterBase);
}
/**
* Retrieve the command with the given label from the collection of commands.
* Example: <code>getCommandWithLabel(commands, "authme", "reg")</code> to find the command description
* which defines the command /authme reg.
*
* @param commands The commands to search in
* @param parentLabel The parent label to search for
* @param childLabel The child label to find
* @return The matched command, or throws an exception if no command could be found
*/
public static CommandDescription getCommandWithLabel(Collection<CommandDescription> commands, String parentLabel,
String childLabel) {
CommandDescription parent = getCommandWithLabel(commands, parentLabel);
return getCommandWithLabel(parent.getChildren(), childLabel);
}
/**
* Retrieve the command with the given label from the collection of commands.
*
* @param commands The commands to search in
* @param label The label to search for
* @return The matched command, or throws an exception if no command could be found
*/
public static CommandDescription getCommandWithLabel(Collection<CommandDescription> commands, String label) {
for (CommandDescription child : commands) {
if (child.hasLabel(label)) {
return child;
}
}
throw new IllegalStateException("Could not find command with label '" + label + "'");
}
/* Shortcut command to initialize a new test command. */
private static CommandDescription createCommand(PermissionNode permission, CommandDescription parent,
List<String> labels,
Class<? extends ExecutableCommand> commandClass,
CommandArgumentDescription... arguments) {
CommandDescription.CommandBuilder command = CommandDescription.builder()
.labels(labels)
.parent(parent)
.permission(permission)
.description(labels.get(0) + " cmd")
.detailedDescription("'" + labels.get(0) + "' test command")
.executableCommand(commandClass);
if (arguments != null && arguments.length > 0) {
for (CommandArgumentDescription argument : arguments) {
command.withArgument(argument.getName(), argument.getDescription(), argument.isOptional());
}
}
return command.register();
}
/* Shortcut command to initialize a new argument description. */
private static CommandArgumentDescription newArgument(String label, boolean isOptional) {
return new CommandArgumentDescription(label, "'" + label + "' argument description", isOptional);
}
public static class TestLoginCommand implements ExecutableCommand {
@Override
public void executeCommand(CommandSender sender, List<String> arguments) {
// noop
}
}
public static class TestRegisterCommand implements ExecutableCommand {
@Override
public void executeCommand(CommandSender sender, List<String> arguments) {
// noop
}
}
public static class TestUnregisterCommand implements ExecutableCommand {
@Override
public void executeCommand(CommandSender sender, List<String> arguments) {
// noop
}
}
}

View File

@ -1,163 +0,0 @@
package fr.xephi.authme.command.executable;
import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.CommandMapper;
import fr.xephi.authme.command.FoundCommandResult;
import fr.xephi.authme.command.help.HelpProvider;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import java.util.List;
import static fr.xephi.authme.command.FoundResultStatus.INCORRECT_ARGUMENTS;
import static fr.xephi.authme.command.FoundResultStatus.MISSING_BASE_COMMAND;
import static fr.xephi.authme.command.FoundResultStatus.SUCCESS;
import static fr.xephi.authme.command.FoundResultStatus.UNKNOWN_LABEL;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_ALTERNATIVES;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_CHILDREN;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_COMMAND;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_DESCRIPTION;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link HelpCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class HelpCommandTest {
@InjectMocks
private HelpCommand command;
@Mock
private CommandMapper commandMapper;
@Mock
private HelpProvider helpProvider;
@Test
public void shouldHandleMissingBaseCommand() {
// given
List<String> arguments = asList("some", "command");
CommandSender sender = mock(CommandSender.class);
FoundCommandResult foundCommandResult = new FoundCommandResult(null, null, null, 0.0, MISSING_BASE_COMMAND);
given(commandMapper.mapPartsToCommand(sender, arguments)).willReturn(foundCommandResult);
// when
command.executeCommand(sender, arguments);
// then
verify(sender).sendMessage(argThat(containsString("Could not get base command")));
verifyNoInteractions(helpProvider);
}
@Test
public void shouldHandleWrongCommandWithSuggestion() {
// given
List<String> arguments = asList("authme", "ragister", "test");
CommandSender sender = mock(CommandSender.class);
CommandDescription description = newCommandDescription("authme", "register");
FoundCommandResult foundCommandResult = new FoundCommandResult(description, asList("authme", "ragister"),
singletonList("test"), 0.1, UNKNOWN_LABEL);
given(commandMapper.mapPartsToCommand(sender, arguments)).willReturn(foundCommandResult);
// when
command.executeCommand(sender, arguments);
// then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender).sendMessage(captor.capture());
assertThat(removeColors(captor.getValue()), containsString("Assuming /authme register"));
verify(helpProvider).outputHelp(sender, foundCommandResult, HelpProvider.ALL_OPTIONS);
}
@Test
public void shouldHandleWrongCommandWithoutSuggestion() {
List<String> arguments = asList("authme", "ragister", "test");
CommandSender sender = mock(CommandSender.class);
FoundCommandResult foundCommandResult = new FoundCommandResult(null, asList("authme", "ragister"),
singletonList("test"), 0.4, UNKNOWN_LABEL);
given(commandMapper.mapPartsToCommand(sender, arguments)).willReturn(foundCommandResult);
// when
command.executeCommand(sender, arguments);
// then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender).sendMessage(captor.capture());
assertThat(removeColors(captor.getValue()), containsString("Unknown command"));
verifyNoInteractions(helpProvider);
}
@Test
public void shouldShowChildrenOfBaseCommand() {
List<String> arguments = singletonList("authme");
CommandSender sender = mock(CommandSender.class);
CommandDescription commandDescription = mock(CommandDescription.class);
given(commandDescription.getLabelCount()).willReturn(1);
FoundCommandResult foundCommandResult = new FoundCommandResult(commandDescription, singletonList("authme"),
Collections.emptyList(), 0.0, SUCCESS);
given(commandMapper.mapPartsToCommand(sender, arguments)).willReturn(foundCommandResult);
// when
command.executeCommand(sender, arguments);
// then
verify(sender, never()).sendMessage(anyString());
verify(helpProvider).outputHelp(sender, foundCommandResult,
SHOW_DESCRIPTION | SHOW_COMMAND | SHOW_CHILDREN | SHOW_ALTERNATIVES);
}
@Test
public void shouldShowDetailedHelpForChildCommand() {
List<String> arguments = asList("authme", "getpos");
CommandSender sender = mock(CommandSender.class);
CommandDescription commandDescription = mock(CommandDescription.class);
given(commandDescription.getLabelCount()).willReturn(2);
FoundCommandResult foundCommandResult = new FoundCommandResult(commandDescription, asList("authme", "getpos"),
Collections.emptyList(), 0.0, INCORRECT_ARGUMENTS);
given(commandMapper.mapPartsToCommand(sender, arguments)).willReturn(foundCommandResult);
// when
command.executeCommand(sender, arguments);
// then
verify(sender, never()).sendMessage(anyString());
verify(helpProvider).outputHelp(sender, foundCommandResult, HelpProvider.ALL_OPTIONS);
}
private static CommandDescription newCommandDescription(String... labels) {
CommandDescription parent = null;
// iterate through the labels backwards so we can set the parent
for (String label : labels) {
CommandDescription description = mock(CommandDescription.class);
given(description.getParent()).willReturn(parent);
given(description.getLabels()).willReturn(singletonList(label));
parent = description;
}
return parent;
}
private static String removeColors(String str) {
for (ChatColor color : ChatColor.values()) {
str = str.replace(color.toString(), "");
}
return str;
}
}

View File

@ -1,178 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskAsynchronously;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Test for {@link AccountsCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class AccountsCommandTest {
@InjectMocks
private AccountsCommand command;
@Mock
private CommonService service;
@Mock
private DataSource dataSource;
@Mock
private BukkitService bukkitService;
@Test
public void shouldGetAccountsOfCurrentUser() {
// given
CommandSender sender = mock(CommandSender.class);
given(sender.getName()).willReturn("Tester");
List<String> arguments = Collections.emptyList();
given(dataSource.getAuth("tester")).willReturn(authWithIp("123.45.67.89"));
given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Arrays.asList("Toaster", "Pester"));
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
// then
String[] messages = getMessagesSentToSender(sender, 2);
assertThat(messages[0], containsString("2 accounts"));
assertThat(messages[1], containsString("Toaster, Pester"));
}
@Test
public void shouldReturnUnknownUserForNullAuth() {
// given
CommandSender sender = mock(CommandSender.class);
List<String> arguments = Collections.singletonList("SomeUser");
given(dataSource.getAuth("someuser")).willReturn(null);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
// then
verify(service).send(sender, MessageKey.UNKNOWN_USER);
verify(sender, never()).sendMessage(anyString());
}
@Test
public void shouldReturnUnregisteredMessageForEmptyAuthList() {
// given
CommandSender sender = mock(CommandSender.class);
List<String> arguments = Collections.singletonList("SomeUser");
PlayerAuth auth = authWithIp("144.56.77.88");
given(dataSource.getAuth("someuser")).willReturn(auth);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
// then
verify(service).send(sender, MessageKey.UNKNOWN_USER);
verify(sender, never()).sendMessage(anyString());
}
@Test
public void shouldReturnSingleAccountMessage() {
// given
CommandSender sender = mock(CommandSender.class);
List<String> arguments = Collections.singletonList("SomeUser");
given(dataSource.getAuth("someuser")).willReturn(authWithIp("56.78.90.123"));
given(dataSource.getAllAuthsByIp("56.78.90.123")).willReturn(Collections.singletonList("SomeUser"));
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
// then
String[] messages = getMessagesSentToSender(sender, 1);
assertThat(messages[0], containsString("single account"));
}
// -----
// Query by IP
// -----
@Test
public void shouldReturnIpUnknown() {
// given
CommandSender sender = mock(CommandSender.class);
List<String> arguments = Collections.singletonList("123.45.67.89");
given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Collections.emptyList());
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
// then
String[] messages = getMessagesSentToSender(sender, 1);
assertThat(messages[0], containsString("IP does not exist"));
}
@Test
public void shouldReturnSingleAccountForIpQuery() {
// given
CommandSender sender = mock(CommandSender.class);
List<String> arguments = Collections.singletonList("24.24.48.48");
given(dataSource.getAllAuthsByIp("24.24.48.48")).willReturn(Collections.singletonList("SomeUser"));
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
// then
String[] messages = getMessagesSentToSender(sender, 1);
assertThat(messages[0], containsString("single account"));
}
@Test
public void shouldReturnAccountListForIpQuery() {
// given
CommandSender sender = mock(CommandSender.class);
List<String> arguments = Collections.singletonList("98.76.41.122");
given(dataSource.getAllAuthsByIp("98.76.41.122")).willReturn(Arrays.asList("Tester", "Lester", "Taster"));
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, arguments);
// then
String[] messages = getMessagesSentToSender(sender, 2);
assertThat(messages[0], containsString("3 accounts"));
assertThat(messages[1], containsString("Tester, Lester, Taster"));
}
private static String[] getMessagesSentToSender(CommandSender sender, int expectedCount) {
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender, times(expectedCount)).sendMessage(captor.capture());
return captor.getAllValues().toArray(new String[expectedCount]);
}
private static PlayerAuth authWithIp(String ip) {
return PlayerAuth.builder()
.name("Test")
.lastIp(ip)
.build();
}
}

View File

@ -1,36 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.ExecutableCommand;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Test for {@link AuthMeCommand}.
*/
public class AuthMeCommandTest {
@Test
public void shouldDisplayInformation() {
// given
ExecutableCommand command = new AuthMeCommand();
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
ArgumentCaptor<String> messagesCaptor = ArgumentCaptor.forClass(String.class);
verify(sender, times(3)).sendMessage(messagesCaptor.capture());
assertThat(messagesCaptor.getAllValues().get(1), containsString("/authme help"));
assertThat(messagesCaptor.getAllValues().get(2), containsString("/authme about"));
}
}

View File

@ -1,39 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.service.BackupService;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Test for {@link BackupCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class BackupCommandTest {
@InjectMocks
private BackupCommand command;
@Mock
private BackupService backupService;
@Test
public void shouldStartBackup() {
// given
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(backupService).doBackup(BackupService.BackupCause.COMMAND, sender);
}
}

View File

@ -1,73 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Test for {@link ChangePasswordAdminCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ChangePasswordAdminCommandTest {
@InjectMocks
private ChangePasswordAdminCommand command;
@Mock
private CommonService commonService;
@Mock
private ValidationService validationService;
@Mock
private Management management;
@Test
public void shouldForwardRequestToManagement() {
// given
String name = "theUser";
String pass = "newPassword";
given(validationService.validatePassword(pass, name)).willReturn(new ValidationResult());
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Arrays.asList(name, pass));
// then
verify(validationService).validatePassword(pass, name);
verify(management).performPasswordChangeAsAdmin(sender, name, pass);
}
@Test
public void shouldSendErrorToCommandSender() {
// given
String name = "theUser";
String pass = "newPassword";
given(validationService.validatePassword(pass, name)).willReturn(
new ValidationResult(MessageKey.INVALID_PASSWORD_LENGTH, "7"));
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Arrays.asList(name, pass));
// then
verify(validationService).validatePassword(pass, name);
verify(commonService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH, "7");
verifyNoInteractions(management);
}
}

View File

@ -1,149 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import ch.jalu.injector.factory.Factory;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.datasource.converter.Converter;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.command.CommandSender;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskAsynchronously;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link ConverterCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ConverterCommandTest {
@InjectMocks
private ConverterCommand command;
@Mock
private CommonService commonService;
@Mock
private BukkitService bukkitService;
@Mock
private Factory<Converter> converterFactory;
@BeforeClass
public static void initLogger() {
TestHelper.setupLogger();
}
@Test
public void shouldHandleUnknownConversionType() {
// given
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList("invalid"));
// then
String converters = String.join(", ", ConverterCommand.CONVERTERS.keySet());
verify(sender).sendMessage(argThat(containsString(converters)));
verifyNoInteractions(commonService, converterFactory, bukkitService);
}
@Test
public void shouldHandleCommandWithNoArgs() {
// given
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
String converters = String.join(", ", ConverterCommand.CONVERTERS.keySet());
verify(sender).sendMessage(argThat(containsString(converters)));
verifyNoInteractions(commonService, converterFactory, bukkitService);
}
@Test
public void shouldHaveUniqueClassForEachConverter() {
// given
Set<Class<? extends Converter>> classes = new HashSet<>();
// when / then
for (Map.Entry<String, Class<? extends Converter>> entry : ConverterCommand.CONVERTERS.entrySet()) {
assertThat("Name is not null or empty",
StringUtils.isBlank(entry.getKey()), equalTo(false));
assertThat("Converter class is unique for each entry",
classes.add(entry.getValue()), equalTo(true));
}
}
@Test
public void shouldLaunchConverterForAllTypes() {
// given
String converterName = "rakamak";
Class<? extends Converter> converterClass = ConverterCommand.CONVERTERS.get(converterName);
Converter converter = createMockReturnedByInjector(converterClass);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, Collections.singletonList(converterName));
// then
verify(converter).execute(sender);
verifyNoMoreInteractions(converter);
verify(converterFactory).newInstance(converterClass);
verifyNoMoreInteractions(converterFactory);
}
@Test
public void shouldCatchExceptionInConverterAndInformSender() {
// given
String converterName = "vauth";
Class<? extends Converter> converterClass = ConverterCommand.CONVERTERS.get(converterName);
Converter converter = createMockReturnedByInjector(converterClass);
doThrow(IllegalStateException.class).when(converter).execute(any(CommandSender.class));
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, Collections.singletonList(converterName.toUpperCase(Locale.ROOT)));
// then
verify(converter).execute(sender);
verifyNoMoreInteractions(converter);
verify(converterFactory).newInstance(converterClass);
verifyNoMoreInteractions(converterFactory);
verify(commonService).send(sender, MessageKey.ERROR);
}
private <T extends Converter> T createMockReturnedByInjector(Class<T> clazz) {
T converter = mock(clazz);
given(converterFactory.newInstance(clazz)).willReturn(converter);
return converter;
}
}

View File

@ -1,63 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.settings.SpawnLoader;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link FirstSpawnCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class FirstSpawnCommandTest {
@InjectMocks
private FirstSpawnCommand command;
@Mock
private SpawnLoader spawnLoader;
@Test
public void shouldTeleportToFirstSpawn() {
// given
Location firstSpawn = mock(Location.class);
given(spawnLoader.getFirstSpawn()).willReturn(firstSpawn);
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.emptyList());
// then
verify(player).teleport(firstSpawn);
verify(spawnLoader, atLeastOnce()).getFirstSpawn();
}
@Test
public void shouldHandleMissingFirstSpawn() {
// given
given(spawnLoader.getFirstSpawn()).willReturn(null);
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.emptyList());
// then
verify(player).sendMessage(argThat(containsString("spawn has failed")));
verify(player, never()).teleport(any(Location.class));
}
}

View File

@ -1,133 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link ForceLoginCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ForceLoginCommandTest {
@InjectMocks
private ForceLoginCommand command;
@Mock
private Management management;
@Mock
private PermissionsManager permissionsManager;
@Mock
private BukkitService bukkitService;
@Test
public void shouldRejectOfflinePlayer() {
// given
String playerName = "Bobby";
Player player = mockPlayer(false);
given(bukkitService.getPlayerExact(playerName)).willReturn(player);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(playerName));
// then
verify(bukkitService).getPlayerExact(playerName);
verify(sender).sendMessage("Player needs to be online!");
verifyNoInteractions(management);
}
@Test
public void shouldRejectInexistentPlayer() {
// given
String playerName = "us3rname01";
given(bukkitService.getPlayerExact(playerName)).willReturn(null);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(playerName));
// then
verify(bukkitService).getPlayerExact(playerName);
verify(sender).sendMessage("Player needs to be online!");
verifyNoInteractions(management);
}
@Test
public void shouldRejectPlayerWithMissingPermission() {
// given
String playerName = "testTest";
Player player = mockPlayer(true);
given(bukkitService.getPlayerExact(playerName)).willReturn(player);
given(permissionsManager.hasPermission(player, PlayerPermission.CAN_LOGIN_BE_FORCED)).willReturn(false);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(playerName));
// then
verify(bukkitService).getPlayerExact(playerName);
verify(sender).sendMessage(argThat(containsString("You cannot force login the player")));
verifyNoInteractions(management);
}
@Test
public void shouldForceLoginPlayer() {
// given
String playerName = "tester23";
Player player = mockPlayer(true);
given(bukkitService.getPlayerExact(playerName)).willReturn(player);
given(permissionsManager.hasPermission(player, PlayerPermission.CAN_LOGIN_BE_FORCED)).willReturn(true);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(playerName));
// then
verify(bukkitService).getPlayerExact(playerName);
verify(management).forceLogin(player);
}
@Test
public void shouldForceLoginSenderSelf() {
// given
String senderName = "tester23";
Player player = mockPlayer(true);
given(bukkitService.getPlayerExact(senderName)).willReturn(player);
given(permissionsManager.hasPermission(player, PlayerPermission.CAN_LOGIN_BE_FORCED)).willReturn(true);
CommandSender sender = mock(CommandSender.class);
given(sender.getName()).willReturn(senderName);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(bukkitService).getPlayerExact(senderName);
verify(management).forceLogin(player);
}
private static Player mockPlayer(boolean isOnline) {
Player player = mock(Player.class);
given(player.isOnline()).willReturn(isOnline);
return player;
}
}

View File

@ -1,65 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import ch.jalu.datasourcecolumns.data.DataSourceValueImpl;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link GetEmailCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class GetEmailCommandTest {
@InjectMocks
private GetEmailCommand command;
@Mock
private DataSource dataSource;
@Mock
private CommonService service;
@Test
public void shouldReportUnknownUser() {
// given
String user = "myTestUser";
given(dataSource.getEmail(user)).willReturn(DataSourceValueImpl.unknownRow());
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(user));
// then
verify(service).send(sender, MessageKey.UNKNOWN_USER);
}
@Test
public void shouldReturnEmail() {
// given
String user = "userToView";
String email = "user.email@example.org";
given(dataSource.getEmail(user)).willReturn(DataSourceValueImpl.of(email));
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(user));
// then
verify(sender).sendMessage(argThat(containsString(email)));
}
}

View File

@ -1,106 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link GetIpCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class GetIpCommandTest {
@InjectMocks
private GetIpCommand command;
@Mock
private BukkitService bukkitService;
@Mock
private DataSource dataSource;
@Test
public void shouldGetIpOfPlayer() {
// given
given(bukkitService.getPlayerExact(anyString())).willReturn(null);
given(dataSource.getAuth(anyString())).willReturn(null);
CommandSender sender = mock(CommandSender.class);
String name = "Testt";
// when
command.executeCommand(sender, Collections.singletonList(name));
// then
verify(bukkitService).getPlayerExact(name);
verify(dataSource).getAuth(name);
verify(sender, only()).sendMessage(argThat(containsString("not registered")));
}
@Test
public void shouldReturnIpAddressOfPlayer() {
// given
String playerName = "charlie";
String ip = "123.34.56.88";
Player player = mockPlayer(playerName, ip);
given(bukkitService.getPlayerExact(playerName)).willReturn(player);
PlayerAuth auth = PlayerAuth.builder().name("t").lastIp("44.33.22.11").registrationIp("77.11.44.88").build();
given(dataSource.getAuth(playerName)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(playerName));
// then
verify(bukkitService).getPlayerExact(playerName);
verify(dataSource).getAuth(playerName);
verify(sender).sendMessage(argThat(both(containsString(playerName)).and(containsString(ip))));
verify(sender).sendMessage(argThat(both(containsString("44.33.22.11")).and(containsString("77.11.44.88"))));
}
@Test
public void shouldHandleUnregisteredOnlinePlayer() {
// given
String playerName = "Test";
String ip = "44.111.22.33";
Player player = mockPlayer(playerName, ip);
given(bukkitService.getPlayerExact(playerName)).willReturn(player);
given(dataSource.getAuth(anyString())).willReturn(null);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(playerName));
// then
verify(bukkitService).getPlayerExact(playerName);
verify(dataSource).getAuth(playerName);
verify(sender).sendMessage(argThat(both(containsString(playerName)).and(containsString(ip))));
verify(sender).sendMessage(argThat(containsString("not registered")));
}
private static Player mockPlayer(String name, String ip) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
TestHelper.mockIpAddressToPlayer(player, ip);
return player;
}
}

View File

@ -1,135 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import java.util.Date;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Test for {@link LastLoginCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class LastLoginCommandTest {
private static final long HOUR_IN_MSEC = 3600 * 1000;
private static final long DAY_IN_MSEC = 24 * HOUR_IN_MSEC;
@InjectMocks
private LastLoginCommand command;
@Mock
private DataSource dataSource;
@Mock
private CommonService service;
@Test
public void shouldRejectNonExistentUser() {
// given
String player = "tester";
given(dataSource.getAuth(player)).willReturn(null);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(player));
// then
verify(dataSource).getAuth(player);
verify(service).send(sender, MessageKey.UNKNOWN_USER);
}
@Test
public void shouldDisplayLastLoginOfUser() {
// given
String player = "SomePlayer";
long lastLogin = System.currentTimeMillis() -
(412 * DAY_IN_MSEC + 10 * HOUR_IN_MSEC - 9000);
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getLastLogin()).willReturn(lastLogin);
given(auth.getLastIp()).willReturn("123.45.66.77");
given(dataSource.getAuth(player)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(player));
// then
verify(dataSource).getAuth(player);
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender, times(3)).sendMessage(captor.capture());
String lastLoginString = new Date(lastLogin).toString();
assertThat(captor.getAllValues().get(0),
allOf(containsString(player), containsString(lastLoginString)));
assertThat(captor.getAllValues().get(1), containsString("412 days 9 hours"));
assertThat(captor.getAllValues().get(2), containsString("123.45.66.77"));
}
@Test
public void shouldDisplayLastLoginOfCommandSender() {
// given
String name = "CommandSender";
CommandSender sender = mock(CommandSender.class);
given(sender.getName()).willReturn(name);
long lastLogin = System.currentTimeMillis()
- (412 * DAY_IN_MSEC + 10 * HOUR_IN_MSEC - 9000);
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getLastLogin()).willReturn(lastLogin);
given(auth.getLastIp()).willReturn("123.45.66.77");
given(dataSource.getAuth(name)).willReturn(auth);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(dataSource).getAuth(name);
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender, times(3)).sendMessage(captor.capture());
String lastLoginString = new Date(lastLogin).toString();
assertThat(captor.getAllValues().get(0),
allOf(containsString(name), containsString(lastLoginString)));
assertThat(captor.getAllValues().get(1), containsString("412 days 9 hours"));
assertThat(captor.getAllValues().get(2), containsString("123.45.66.77"));
}
@Test
public void shouldHandleNullLastLoginDate() {
// given
String name = "player";
PlayerAuth auth = PlayerAuth.builder()
.name(name)
.lastIp("123.45.67.89")
.build();
given(dataSource.getAuth(name)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(name));
// then
verify(dataSource).getAuth(name);
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender, times(2)).sendMessage(captor.capture());
assertThat(captor.getAllValues().get(0), allOf(containsString(name), containsString("never")));
assertThat(captor.getAllValues().get(1), containsString("123.45.67.89"));
}
}

View File

@ -1,75 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.task.purge.PurgeService;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import static com.google.common.collect.Sets.newHashSet;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link PurgeBannedPlayersCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class PurgeBannedPlayersCommandTest {
@InjectMocks
private PurgeBannedPlayersCommand command;
@Mock
private PurgeService purgeService;
@Mock
private BukkitService bukkitService;
@Test
public void shouldForwardRequestToService() {
// given
String[] names = {"bannedPlayer", "other_banned", "evilplayer", "Someone"};
OfflinePlayer[] players = offlinePlayersWithNames(names);
given(bukkitService.getBannedPlayers()).willReturn(newHashSet(players));
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(bukkitService).getBannedPlayers();
verify(purgeService).purgePlayers(eq(sender), eq(asLowerCaseSet(names)),
argThat(arrayContainingInAnyOrder(players)));
}
private static OfflinePlayer[] offlinePlayersWithNames(String... names) {
OfflinePlayer[] players = new OfflinePlayer[names.length];
for (int i = 0; i < names.length; ++i) {
OfflinePlayer player = mock(OfflinePlayer.class);
given(player.getName()).willReturn(names[i]);
players[i] = player;
}
return players;
}
private static Set<String> asLowerCaseSet(String... items) {
Set<String> result = new HashSet<>(items.length);
for (String item : items) {
result.add(item.toLowerCase(Locale.ROOT));
}
return result;
}
}

View File

@ -1,89 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.task.purge.PurgeService;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Calendar;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link PurgeCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class PurgeCommandTest {
@InjectMocks
private PurgeCommand command;
@Mock
private PurgeService purgeService;
@Test
public void shouldHandleInvalidNumber() {
// given
String interval = "invalid";
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(interval));
// then
verify(sender).sendMessage(argThat(containsString("The value you've entered is invalid")));
verifyNoInteractions(purgeService);
}
@Test
public void shouldRejectTooSmallInterval() {
// given
String interval = "29";
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(interval));
// then
verify(sender).sendMessage(argThat(containsString("You can only purge data older than 30 days")));
verifyNoInteractions(purgeService);
}
@Test
public void shouldForwardToService() {
// given
String interval = "45";
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(interval));
// then
ArgumentCaptor<Long> captor = ArgumentCaptor.forClass(Long.class);
verify(purgeService).runPurge(eq(sender), captor.capture());
// Check the timestamp with a certain tolerance
int toleranceMillis = 100;
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -Integer.valueOf(interval));
assertIsCloseTo(captor.getValue(), calendar.getTimeInMillis(), toleranceMillis);
}
private static void assertIsCloseTo(long value1, long value2, long tolerance) {
assertThat(Math.abs(value1 - value2), not(greaterThan(tolerance)));
}
}

View File

@ -1,118 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link PurgeLastPositionCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class PurgeLastPositionCommandTest {
@InjectMocks
private PurgeLastPositionCommand command;
@Mock
private DataSource dataSource;
@Mock
private CommonService service;
@Mock
private BungeeSender bungeeSender;
@Test
public void shouldPurgeLastPosOfUser() {
// given
String player = "_Bobby";
PlayerAuth auth = mock(PlayerAuth.class);
given(dataSource.getAuth(player)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(player));
// then
verify(dataSource).getAuth(player);
verifyPositionWasReset(auth);
verify(sender).sendMessage(argThat(containsString("last position location is now reset")));
}
@Test
public void shouldPurgePositionOfCommandSender() {
// given
String player = "_Bobby";
CommandSender sender = mock(CommandSender.class);
given(sender.getName()).willReturn(player);
PlayerAuth auth = mock(PlayerAuth.class);
given(dataSource.getAuth(player)).willReturn(auth);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(dataSource).getAuth(player);
verifyPositionWasReset(auth);
verify(sender).sendMessage(argThat(containsString("position location is now reset")));
}
@Test
public void shouldHandleNonExistentUser() {
// given
String name = "invalidPlayer";
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(name));
// then
verify(dataSource).getAuth(name);
verify(service).send(sender, MessageKey.UNKNOWN_USER);
}
@Test
public void shouldResetAllLastPositions() {
// given
PlayerAuth auth1 = mock(PlayerAuth.class);
PlayerAuth auth2 = mock(PlayerAuth.class);
PlayerAuth auth3 = mock(PlayerAuth.class);
given(dataSource.getAllAuths()).willReturn(Arrays.asList(auth1, auth2, auth3));
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList("*"));
// then
verify(dataSource).getAllAuths();
verifyPositionWasReset(auth1);
verifyPositionWasReset(auth2);
verifyPositionWasReset(auth3);
verify(sender).sendMessage(argThat(containsString("last position locations are now reset")));
}
private static void verifyPositionWasReset(PlayerAuth auth) {
verify(auth).setQuitLocX(0);
verify(auth).setQuitLocY(0);
verify(auth).setQuitLocZ(0);
verify(auth).setWorld("world");
}
}

View File

@ -1,94 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.task.purge.PurgeExecutor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Locale;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskAsynchronously;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskOptionallyAsync;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link PurgePlayerCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class PurgePlayerCommandTest {
@InjectMocks
private PurgePlayerCommand command;
@Mock
private BukkitService bukkitService;
@Mock
private PurgeExecutor purgeExecutor;
@Mock
private DataSource dataSource;
@Test
public void shouldNotExecutePurgeForRegisteredPlayer() {
// given
String name = "Bobby";
given(dataSource.isAuthAvailable(name)).willReturn(true);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, singletonList(name));
// then
verify(sender).sendMessage(argThat(containsString("This player is still registered")));
verifyNoInteractions(purgeExecutor);
}
@Test
public void shouldExecutePurge() {
// given
String name = "Frank";
given(dataSource.isAuthAvailable(name)).willReturn(false);
OfflinePlayer player = mock(OfflinePlayer.class);
given(bukkitService.getOfflinePlayer(name)).willReturn(player);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, singletonList(name));
// then
verify(dataSource).isAuthAvailable(name);
verify(purgeExecutor).executePurge(singletonList(player), singletonList(name.toLowerCase(Locale.ROOT)));
}
@Test
public void shouldExecutePurgeOfRegisteredPlayer() {
// given
String name = "GhiJKlmn7";
OfflinePlayer player = mock(OfflinePlayer.class);
given(bukkitService.getOfflinePlayer(name)).willReturn(player);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, asList(name, "force"));
// then
verify(purgeExecutor).executePurge(singletonList(player), singletonList(name.toLowerCase(Locale.ROOT)));
}
}

View File

@ -1,87 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link RecentPlayersCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class RecentPlayersCommandTest {
@InjectMocks
@Spy
private RecentPlayersCommand command;
@Mock
private DataSource dataSource;
@Test
public void shouldShowRecentPlayers() {
// given
PlayerAuth auth1 = PlayerAuth.builder()
.name("hannah").realName("Hannah").lastIp("11.11.11.11")
.lastLogin(1510387755000L) // 11/11/2017 @ 8:09am
.build();
PlayerAuth auth2 = PlayerAuth.builder()
.name("matt").realName("MATT").lastIp("22.11.22.33")
.lastLogin(1510269301000L) // 11/09/2017 @ 11:15pm
.build();
doReturn(ZoneId.of("UTC")).when(command).getZoneId();
given(dataSource.getRecentlyLoggedInPlayers()).willReturn(Arrays.asList(auth1, auth2));
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(sender).sendMessage(argThat(containsString("Recently logged in players")));
verify(sender).sendMessage(argThat(equalToIgnoringCase("- Hannah (08:09 AM, 11 Nov with IP 11.11.11.11)")));
verify(sender).sendMessage(argThat(equalToIgnoringCase("- MATT (11:15 PM, 09 Nov with IP 22.11.22.33)")));
}
@Test
public void shouldHandlePlayerWithNullLastLogin() {
// given
PlayerAuth auth1 = PlayerAuth.builder()
.name("xephren").realName("Xephren").lastIp("11.11.11.11")
.lastLogin(null)
.build();
PlayerAuth auth2 = PlayerAuth.builder()
.name("silvah777").realName("silvah777").lastIp("22.11.22.33")
.lastLogin(1510269301000L) // 11/09/2017 @ 11:15pm
.build();
doReturn(ZoneId.of("UTC")).when(command).getZoneId();
given(dataSource.getRecentlyLoggedInPlayers()).willReturn(Arrays.asList(auth1, auth2));
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(sender).sendMessage(argThat(containsString("Recently logged in players")));
verify(sender).sendMessage(argThat(equalToIgnoringCase("- Xephren (never with IP 11.11.11.11)")));
verify(sender).sendMessage(argThat(equalToIgnoringCase("- silvah777 (11:15 PM, 09 Nov with IP 22.11.22.33)")));
}
}

View File

@ -1,186 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Locale;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskOptionallyAsync;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
/**
* Test for {@link RegisterAdminCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class RegisterAdminCommandTest {
@InjectMocks
private RegisterAdminCommand command;
@Mock
private PasswordSecurity passwordSecurity;
@Mock
private DataSource dataSource;
@Mock
private BukkitService bukkitService;
@Mock
private CommonService commandService;
@Mock
private ValidationService validationService;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
}
@Test
public void shouldRejectInvalidPassword() {
// given
String user = "tester";
String password = "myPassword";
given(validationService.validatePassword(password, user))
.willReturn(new ValidationResult(MessageKey.INVALID_PASSWORD_LENGTH));
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Arrays.asList(user, password));
// then
verify(validationService).validatePassword(password, user);
verify(commandService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH, new String[0]);
verify(bukkitService, never()).runTaskAsynchronously(any(Runnable.class));
}
@Test
public void shouldRejectAlreadyRegisteredAccount() {
// given
String user = "my_name55";
String password = "@some-pass@";
given(validationService.validatePassword(password, user)).willReturn(new ValidationResult());
given(dataSource.isAuthAvailable(user)).willReturn(true);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, password));
// then
verify(validationService).validatePassword(password, user);
verify(commandService).send(sender, MessageKey.NAME_ALREADY_REGISTERED);
verify(dataSource, never()).saveAuth(any(PlayerAuth.class));
}
@Test
public void shouldHandleSavingError() {
// given
String user = "test-test";
String password = "afdjhfkt";
given(validationService.validatePassword(password, user)).willReturn(new ValidationResult());
given(dataSource.isAuthAvailable(user)).willReturn(false);
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(false);
HashedPassword hashedPassword = new HashedPassword("235sdf4w5udsgf");
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, password));
// then
verify(validationService).validatePassword(password, user);
verify(commandService).send(sender, MessageKey.ERROR);
ArgumentCaptor<PlayerAuth> captor = ArgumentCaptor.forClass(PlayerAuth.class);
verify(dataSource).saveAuth(captor.capture());
assertAuthHasInfo(captor.getValue(), user, hashedPassword);
}
@Test
public void shouldRegisterOfflinePlayer() {
// given
String user = "someone";
String password = "Al1O3P49S5%";
given(validationService.validatePassword(password, user)).willReturn(new ValidationResult());
given(dataSource.isAuthAvailable(user)).willReturn(false);
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(true);
HashedPassword hashedPassword = new HashedPassword("$aea2345EW235dfsa@#R%987048");
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
given(bukkitService.getPlayerExact(user)).willReturn(null);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, password));
// then
verify(validationService).validatePassword(password, user);
verify(commandService).send(sender, MessageKey.REGISTER_SUCCESS);
ArgumentCaptor<PlayerAuth> captor = ArgumentCaptor.forClass(PlayerAuth.class);
verify(dataSource).saveAuth(captor.capture());
assertAuthHasInfo(captor.getValue(), user, hashedPassword);
}
@Test
public void shouldRegisterOnlinePlayer() {
// given
String user = "someone";
String password = "Al1O3P49S5%";
given(validationService.validatePassword(password, user)).willReturn(new ValidationResult());
given(dataSource.isAuthAvailable(user)).willReturn(false);
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(true);
HashedPassword hashedPassword = new HashedPassword("$aea2345EW235dfsa@#R%987048");
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
Player player = mock(Player.class);
given(bukkitService.getPlayerExact(user)).willReturn(player);
String kickForAdminRegister = "Admin registered you -- log in again";
given(commandService.retrieveSingleMessage(player, MessageKey.KICK_FOR_ADMIN_REGISTER)).willReturn(kickForAdminRegister);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, password));
// then
verify(validationService).validatePassword(password, user);
verify(commandService).send(sender, MessageKey.REGISTER_SUCCESS);
ArgumentCaptor<PlayerAuth> captor = ArgumentCaptor.forClass(PlayerAuth.class);
verify(dataSource).saveAuth(captor.capture());
assertAuthHasInfo(captor.getValue(), user, hashedPassword);
verify(player).kickPlayer(kickForAdminRegister);
}
private void assertAuthHasInfo(PlayerAuth auth, String name, HashedPassword hashedPassword) {
assertThat(auth.getRealName(), equalTo(name));
assertThat(auth.getNickname(), equalTo(name.toLowerCase(Locale.ROOT)));
assertThat(auth.getPassword(), equalTo(hashedPassword));
}
}

View File

@ -1,148 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import ch.jalu.injector.factory.SingletonStore;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.DataSourceType;
import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.output.LogLevel;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.SettingsWarner;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.bukkit.command.CommandSender;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link ReloadCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ReloadCommandTest {
@InjectMocks
private ReloadCommand command;
@Mock
private AuthMe authMe;
@Mock
private Settings settings;
@Mock
private DataSource dataSource;
@Mock
private CommonService commandService;
@Mock
private SettingsWarner settingsWarner;
@Mock
private SingletonStore<Reloadable> reloadableStore;
@Mock
private SingletonStore<SettingsDependent> settingsDependentStore;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
}
@Before
public void setDefaultSettings() {
// Mock properties retrieved by ConsoleLogger
given(settings.getProperty(PluginSettings.LOG_LEVEL)).willReturn(LogLevel.INFO);
given(settings.getProperty(SecuritySettings.USE_LOGGING)).willReturn(false);
}
@Test
public void shouldReload() {
// given
CommandSender sender = mock(CommandSender.class);
given(settings.getProperty(DatabaseSettings.BACKEND)).willReturn(DataSourceType.MYSQL);
given(dataSource.getType()).willReturn(DataSourceType.MYSQL);
List<Reloadable> reloadables = Arrays.asList(
mock(Reloadable.class), mock(Reloadable.class), mock(Reloadable.class));
List<SettingsDependent> dependents = Arrays.asList(
mock(SettingsDependent.class), mock(SettingsDependent.class));
given(reloadableStore.retrieveAllOfType()).willReturn(reloadables);
given(settingsDependentStore.retrieveAllOfType()).willReturn(dependents);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(settings).reload();
verifyReloadingCalls(reloadables, dependents);
verify(commandService).send(sender, MessageKey.CONFIG_RELOAD_SUCCESS);
verify(settingsWarner).logWarningsForMisconfigurations();
}
@Test
public void shouldHandleReloadError() {
// given
CommandSender sender = mock(CommandSender.class);
doThrow(IllegalStateException.class).when(reloadableStore).retrieveAllOfType();
given(settings.getProperty(DatabaseSettings.BACKEND)).willReturn(DataSourceType.MYSQL);
given(dataSource.getType()).willReturn(DataSourceType.MYSQL);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(settings).reload();
verify(reloadableStore).retrieveAllOfType();
verify(sender).sendMessage(argThat(containsString("Error occurred")));
verify(authMe).stopOrUnload();
}
@Test
public void shouldIssueWarningForChangedDataSourceSetting() {
// given
CommandSender sender = mock(CommandSender.class);
given(settings.getProperty(DatabaseSettings.BACKEND)).willReturn(DataSourceType.MYSQL);
given(dataSource.getType()).willReturn(DataSourceType.SQLITE);
given(reloadableStore.retrieveAllOfType()).willReturn(Collections.emptyList());
given(settingsDependentStore.retrieveAllOfType()).willReturn(Collections.emptyList());
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(settings).reload();
verify(reloadableStore).retrieveAllOfType();
verify(settingsDependentStore).retrieveAllOfType();
verify(sender).sendMessage(argThat(containsString("cannot change database type")));
}
private void verifyReloadingCalls(List<Reloadable> reloadables, List<SettingsDependent> dependents) {
for (Reloadable reloadable : reloadables) {
verify(reloadable).reload();
}
for (SettingsDependent dependent : dependents) {
verify(dependent).reload(settings);
}
}
}

View File

@ -1,192 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskOptionallyAsync;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Test for {@link SetEmailCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class SetEmailCommandTest {
@InjectMocks
private SetEmailCommand command;
@Mock
private DataSource dataSource;
@Mock
private CommonService commandService;
@Mock
private PlayerCache playerCache;
@Mock
private BukkitService bukkitService;
@Mock
private ValidationService validationService;
@Test
public void shouldRejectInvalidMail() {
// given
String user = "somebody";
String email = "some.test@example.org";
given(validationService.validateEmail(email)).willReturn(false);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Arrays.asList(user, email));
// then
verify(validationService).validateEmail(email);
verify(commandService).send(sender, MessageKey.INVALID_EMAIL);
verifyNoInteractions(dataSource);
}
@Test
public void shouldHandleUnknownUser() {
// given
String user = "nonexistent";
String email = "mail@example.com";
given(validationService.validateEmail(email)).willReturn(true);
given(dataSource.getAuth(user)).willReturn(null);
CommandSender sender = mock(CommandSender.class);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, email));
// then
verify(validationService).validateEmail(email);
verify(dataSource).getAuth(user);
verify(commandService).send(sender, MessageKey.UNKNOWN_USER);
verifyNoMoreInteractions(dataSource);
}
@Test
public void shouldHandleAlreadyTakenEmail() {
// given
String user = "someone";
String email = "mail@example.com";
given(validationService.validateEmail(email)).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
given(dataSource.getAuth(user)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(false);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, email));
// then
verify(validationService).validateEmail(email);
verify(dataSource).getAuth(user);
verify(validationService).isEmailFreeForRegistration(email, sender);
verify(commandService).send(sender, MessageKey.EMAIL_ALREADY_USED_ERROR);
verifyNoMoreInteractions(dataSource);
verifyNoInteractions(auth);
}
@Test
public void shouldHandlePersistenceError() {
// given
String user = "Bobby";
String email = "new-addr@example.org";
given(validationService.validateEmail(email)).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
given(dataSource.getAuth(user)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(true);
given(dataSource.updateEmail(auth)).willReturn(false);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, email));
// then
verify(validationService).validateEmail(email);
verify(dataSource).getAuth(user);
verify(validationService).isEmailFreeForRegistration(email, sender);
verify(commandService).send(sender, MessageKey.ERROR);
verify(dataSource).updateEmail(auth);
verifyNoMoreInteractions(dataSource);
}
@Test
public void shouldUpdateEmail() {
// given
String user = "Bobby";
String email = "new-addr@example.org";
given(validationService.validateEmail(email)).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
given(dataSource.getAuth(user)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(true);
given(dataSource.updateEmail(auth)).willReturn(true);
given(playerCache.getAuth(user)).willReturn(null);
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, email));
// then
verify(validationService).validateEmail(email);
verify(dataSource).getAuth(user);
verify(validationService).isEmailFreeForRegistration(email, sender);
verify(commandService).send(sender, MessageKey.EMAIL_CHANGED_SUCCESS);
verify(dataSource).updateEmail(auth);
verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
verifyNoMoreInteractions(dataSource);
}
@Test
public void shouldUpdateEmailAndPlayerCache() {
// given
String user = "Bobby";
String email = "new-addr@example.org";
given(validationService.validateEmail(email)).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
given(dataSource.getAuth(user)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(true);
given(dataSource.updateEmail(auth)).willReturn(true);
given(playerCache.getAuth(user)).willReturn(mock(PlayerAuth.class));
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, email));
// then
verify(validationService).validateEmail(email);
verify(dataSource).getAuth(user);
verify(validationService).isEmailFreeForRegistration(email, sender);
verify(commandService).send(sender, MessageKey.EMAIL_CHANGED_SUCCESS);
verify(dataSource).updateEmail(auth);
verify(playerCache).updatePlayer(auth);
verifyNoMoreInteractions(dataSource);
}
}

View File

@ -1,64 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.settings.SpawnLoader;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link SetFirstSpawnCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class SetFirstSpawnCommandTest {
@InjectMocks
private SetFirstSpawnCommand command;
@Mock
private SpawnLoader spawnLoader;
@Test
public void shouldSetFirstSpawn() {
// given
Player player = mock(Player.class);
Location location = mock(Location.class);
given(player.getLocation()).willReturn(location);
given(spawnLoader.setFirstSpawn(location)).willReturn(true);
// when
command.executeCommand(player, Collections.emptyList());
// then
verify(spawnLoader).setFirstSpawn(location);
verify(player).sendMessage(argThat(containsString("defined new first spawn")));
}
@Test
public void shouldHandleError() {
// given
Player player = mock(Player.class);
Location location = mock(Location.class);
given(player.getLocation()).willReturn(location);
given(spawnLoader.setFirstSpawn(location)).willReturn(false);
// when
command.executeCommand(player, Collections.emptyList());
// then
verify(spawnLoader).setFirstSpawn(location);
verify(player).sendMessage(argThat(containsString("has failed")));
}
}

View File

@ -1,64 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.settings.SpawnLoader;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link SetSpawnCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class SetSpawnCommandTest {
@InjectMocks
private SetSpawnCommand command;
@Mock
private SpawnLoader spawnLoader;
@Test
public void shouldSetSpawn() {
// given
Player player = mock(Player.class);
Location location = mock(Location.class);
given(player.getLocation()).willReturn(location);
given(spawnLoader.setSpawn(location)).willReturn(true);
// when
command.executeCommand(player, Collections.emptyList());
// then
verify(spawnLoader).setSpawn(location);
verify(player).sendMessage(argThat(containsString("defined new spawn")));
}
@Test
public void shouldHandleError() {
// given
Player player = mock(Player.class);
Location location = mock(Location.class);
given(player.getLocation()).willReturn(location);
given(spawnLoader.setSpawn(location)).willReturn(false);
// when
command.executeCommand(player, Collections.emptyList());
// then
verify(spawnLoader).setSpawn(location);
verify(player).sendMessage(argThat(containsString("has failed")));
}
}

View File

@ -1,64 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.settings.SpawnLoader;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link SpawnCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class SpawnCommandTest {
@InjectMocks
private SpawnCommand command;
@Mock
private SpawnLoader spawnLoader;
@Test
public void shouldTeleportToSpawn() {
// given
Location spawn = mock(Location.class);
given(spawnLoader.getSpawn()).willReturn(spawn);
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.emptyList());
// then
verify(player).teleport(spawn);
verify(spawnLoader, atLeastOnce()).getSpawn();
}
@Test
public void shouldHandleMissingSpawn() {
// given
given(spawnLoader.getSpawn()).willReturn(null);
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.emptyList());
// then
verify(player).sendMessage(argThat(containsString("Spawn has failed")));
verify(player, never()).teleport(any(Location.class));
}
}

View File

@ -1,97 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.CommandMapper;
import fr.xephi.authme.command.FoundCommandResult;
import fr.xephi.authme.command.help.HelpProvider;
import fr.xephi.authme.service.AntiBotService;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link SwitchAntiBotCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class SwitchAntiBotCommandTest {
@InjectMocks
private SwitchAntiBotCommand command;
@Mock
private AntiBotService antiBot;
@Mock
private CommandMapper commandMapper;
@Mock
private HelpProvider helpProvider;
@Test
public void shouldReturnAntiBotState() {
// given
given(antiBot.getAntiBotStatus()).willReturn(AntiBotService.AntiBotStatus.ACTIVE);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(sender).sendMessage(argThat(containsString("status: ACTIVE")));
}
@Test
public void shouldActivateAntiBot() {
// given
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList("on"));
// then
verify(antiBot).overrideAntiBotStatus(true);
verify(sender).sendMessage(argThat(containsString("enabled")));
}
@Test
public void shouldDeactivateAntiBot() {
// given
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList("Off"));
// then
verify(antiBot).overrideAntiBotStatus(false);
verify(sender).sendMessage(argThat(containsString("disabled")));
}
@Test
public void shouldShowHelpForUnknownState() {
// given
CommandSender sender = mock(CommandSender.class);
FoundCommandResult foundCommandResult = mock(FoundCommandResult.class);
given(commandMapper.mapPartsToCommand(sender, asList("authme", "antibot"))).willReturn(foundCommandResult);
// when
command.executeCommand(sender, Collections.singletonList("wrong"));
// then
verify(antiBot, never()).overrideAntiBotStatus(anyBoolean());
verify(sender).sendMessage(argThat(containsString("Invalid")));
verify(helpProvider).outputHelp(sender, foundCommandResult, HelpProvider.SHOW_ARGUMENTS);
}
}

View File

@ -1,120 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link TotpDisableAdminCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class TotpDisableAdminCommandTest {
@InjectMocks
private TotpDisableAdminCommand command;
@Mock
private DataSource dataSource;
@Mock
private Messages messages;
@Mock
private BukkitService bukkitService;
@BeforeClass
public static void initLogger() {
TestHelper.setupLogger();
}
@Test
public void shouldHandleUnknownUser() {
// given
CommandSender sender = mock(CommandSender.class);
given(dataSource.getAuth("user")).willReturn(null);
// when
command.executeCommand(sender, Collections.singletonList("user"));
// then
verify(messages).send(sender, MessageKey.UNKNOWN_USER);
verify(dataSource, only()).getAuth("user");
}
@Test
public void shouldHandleUserWithNoTotpEnabled() {
// given
CommandSender sender = mock(CommandSender.class);
PlayerAuth auth = PlayerAuth.builder()
.name("billy")
.totpKey(null)
.build();
given(dataSource.getAuth("Billy")).willReturn(auth);
// when
command.executeCommand(sender, Collections.singletonList("Billy"));
// then
verify(sender).sendMessage(argThat(containsString("'Billy' does not have two-factor auth enabled")));
verify(dataSource, only()).getAuth("Billy");
}
@Test
public void shouldRemoveTotpFromUser() {
// given
CommandSender sender = mock(CommandSender.class);
PlayerAuth auth = PlayerAuth.builder()
.name("Bobby")
.totpKey("56484998")
.build();
given(dataSource.getAuth("Bobby")).willReturn(auth);
given(dataSource.removeTotpKey("Bobby")).willReturn(true);
Player player = mock(Player.class);
given(bukkitService.getPlayerExact("Bobby")).willReturn(player);
// when
command.executeCommand(sender, Collections.singletonList("Bobby"));
// then
verify(sender).sendMessage(argThat(containsString("Disabled two-factor authentication successfully")));
verify(messages).send(player, MessageKey.TWO_FACTOR_REMOVED_SUCCESS);
}
@Test
public void shouldHandleErrorWhileRemovingTotp() {
// given
CommandSender sender = mock(CommandSender.class);
PlayerAuth auth = PlayerAuth.builder()
.name("Bobby")
.totpKey("321654")
.build();
given(dataSource.getAuth("Bobby")).willReturn(auth);
given(dataSource.removeTotpKey("Bobby")).willReturn(false);
// when
command.executeCommand(sender, Collections.singletonList("Bobby"));
// then
verify(messages).send(sender, MessageKey.ERROR);
}
}

View File

@ -1,92 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import org.bukkit.command.CommandSender;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link TotpViewStatusCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class TotpViewStatusCommandTest {
@InjectMocks
private TotpViewStatusCommand command;
@Mock
private DataSource dataSource;
@Mock
private Messages messages;
@BeforeClass
public static void initLogger() {
TestHelper.setupLogger();
}
@Test
public void shouldHandleUnknownUser() {
// given
CommandSender sender = mock(CommandSender.class);
given(dataSource.getAuth("user")).willReturn(null);
// when
command.executeCommand(sender, Collections.singletonList("user"));
// then
verify(messages).send(sender, MessageKey.UNKNOWN_USER);
verify(dataSource, only()).getAuth("user");
}
@Test
public void shouldInformForUserWithoutTotp() {
// given
CommandSender sender = mock(CommandSender.class);
PlayerAuth auth = PlayerAuth.builder()
.name("billy")
.totpKey(null)
.build();
given(dataSource.getAuth("Billy")).willReturn(auth);
// when
command.executeCommand(sender, Collections.singletonList("Billy"));
// then
verify(sender).sendMessage(argThat(containsString("'Billy' does NOT have two-factor auth enabled")));
}
@Test
public void shouldInformForUserWithTotpEnabled() {
// given
CommandSender sender = mock(CommandSender.class);
PlayerAuth auth = PlayerAuth.builder()
.name("billy")
.totpKey("92841575")
.build();
given(dataSource.getAuth("Billy")).willReturn(auth);
// when
command.executeCommand(sender, Collections.singletonList("Billy"));
// then
verify(sender).sendMessage(argThat(containsString("'Billy' has enabled two-factor authentication")));
}
}

View File

@ -1,94 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
/**
* Test for {@link UnregisterAdminCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class UnregisterAdminCommandTest {
@InjectMocks
private UnregisterAdminCommand command;
@Mock
private DataSource dataSource;
@Mock
private CommonService commandService;
@Mock
private BukkitService bukkitService;
@Mock
private Management management;
@Test
public void shouldHandleUnknownPlayer() {
// given
String user = "bobby";
given(dataSource.isAuthAvailable(user)).willReturn(false);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(user));
// then
verify(dataSource, only()).isAuthAvailable(user);
verify(commandService).send(sender, MessageKey.UNKNOWN_USER);
}
@Test
public void shouldInvokeUnregisterProcess() {
// given
String user = "personaNonGrata";
given(dataSource.isAuthAvailable(user)).willReturn(true);
Player player = mock(Player.class);
given(bukkitService.getPlayerExact(user)).willReturn(player);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(user));
// then
verify(dataSource, only()).isAuthAvailable(user);
verify(bukkitService).getPlayerExact(user);
verify(management).performUnregisterByAdmin(sender, user, player);
}
@Test
public void shouldInvokeUnregisterProcessWithNullPlayer() {
// given
String user = "personaNonGrata";
given(dataSource.isAuthAvailable(user)).willReturn(true);
given(bukkitService.getPlayerExact(user)).willReturn(null);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(user));
// then
verify(dataSource, only()).isAuthAvailable(user);
verify(bukkitService).getPlayerExact(user);
verify(management).performUnregisterByAdmin(sender, user, null);
}
}

View File

@ -1,70 +0,0 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.help.HelpMessagesService;
import fr.xephi.authme.service.HelpTranslationGenerator;
import org.bukkit.command.CommandSender;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Test for {@link UpdateHelpMessagesCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class UpdateHelpMessagesCommandTest {
@InjectMocks
private UpdateHelpMessagesCommand command;
@Mock
private HelpTranslationGenerator helpTranslationGenerator;
@Mock
private HelpMessagesService helpMessagesService;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
}
@Test
public void shouldUpdateHelpMessage() throws IOException {
// given
File updatedFile = new File("some/path/help_xx.yml");
given(helpTranslationGenerator.updateHelpFile()).willReturn(updatedFile);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(helpMessagesService).reloadMessagesFile();
verify(sender).sendMessage("Successfully updated the help file 'help_xx.yml'");
}
@Test
public void shouldCatchAndReportException() throws IOException {
// given
given(helpTranslationGenerator.updateHelpFile()).willThrow(new IOException("Couldn't do the thing"));
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(sender).sendMessage("Could not update help file: Couldn't do the thing");
verifyNoInteractions(helpMessagesService);
}
}

View File

@ -1,121 +0,0 @@
package fr.xephi.authme.command.executable.authme.debug;
import ch.jalu.injector.factory.SingletonStore;
import com.google.common.cache.LoadingCache;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.data.limbo.LimboPlayer;
import fr.xephi.authme.data.limbo.LimboService;
import fr.xephi.authme.datasource.CacheDataSource;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.initialization.HasCleanup;
import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Test for {@link DataStatistics}.
*/
@RunWith(MockitoJUnitRunner.class)
public class DataStatisticsTest {
@InjectMocks
private DataStatistics dataStatistics;
@Mock
private DataSource dataSource;
@Mock
private PlayerCache playerCache;
@Mock
private LimboService limboService;
@Mock
private SingletonStore<Object> singletonStore;
@Before
public void setUpLimboCacheMap() {
Map<String, LimboPlayer> limboMap = new HashMap<>();
limboMap.put("test", mock(LimboPlayer.class));
ReflectionTestUtils.setField(LimboService.class, limboService, "entries", limboMap);
}
@Test
public void shouldOutputStatistics() {
// given
CommandSender sender = mock(CommandSender.class);
given(singletonStore.retrieveAllOfType()).willReturn(mockListOfSize(Object.class, 7));
given(singletonStore.retrieveAllOfType(Reloadable.class)).willReturn(mockListOfSize(Reloadable.class, 4));
given(singletonStore.retrieveAllOfType(SettingsDependent.class)).willReturn(mockListOfSize(SettingsDependent.class, 3));
given(singletonStore.retrieveAllOfType(HasCleanup.class)).willReturn(mockListOfSize(HasCleanup.class, 2));
given(dataSource.getAccountsRegistered()).willReturn(219);
given(playerCache.getLogged()).willReturn(12);
// Clear any loggers that might exist and trigger the generation of two loggers
Map loggers = ReflectionTestUtils.getFieldValue(ConsoleLoggerFactory.class, null, "consoleLoggers");
loggers.clear();
ConsoleLoggerFactory.get(String.class);
ConsoleLoggerFactory.get(Integer.class);
// when
dataStatistics.execute(sender, Collections.emptyList());
// then
ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);
verify(sender, atLeastOnce()).sendMessage(stringCaptor.capture());
assertThat(stringCaptor.getAllValues(), containsInAnyOrder(
ChatColor.BLUE + "AuthMe statistics",
"Singleton Java classes: 7",
"(Reloadable: 4 / SettingsDependent: 3 / HasCleanup: 2)",
"LimboPlayers in memory: 1",
"Total players in DB: 219",
"PlayerCache size: 12 (= logged in players)",
"Total logger instances: 2"));
}
@Test
public void shouldOutputCachedDataSourceStatistics() {
// given
CacheDataSource cacheDataSource = mock(CacheDataSource.class);
LoadingCache<String, Optional<PlayerAuth>> cache = mock(LoadingCache.class);
given(cache.size()).willReturn(11L);
given(cacheDataSource.getCachedAuths()).willReturn(cache);
ReflectionTestUtils.setField(DataStatistics.class, dataStatistics, "dataSource", cacheDataSource);
CommandSender sender = mock(CommandSender.class);
// when
dataStatistics.execute(sender, Collections.emptyList());
// then
ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);
verify(sender, atLeastOnce()).sendMessage(stringCaptor.capture());
assertThat(stringCaptor.getAllValues(), hasItem("Cached PlayerAuth objects: 11"));
}
private static <T> List<T> mockListOfSize(Class<T> mockClass, int size) {
T mock = mock(mockClass);
return Collections.nCopies(size, mock);
}
}

View File

@ -1,158 +0,0 @@
package fr.xephi.authme.command.executable.authme.debug;
import ch.jalu.injector.factory.Factory;
import fr.xephi.authme.permission.DebugSectionPermissions;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsManager;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Collections.emptyList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link DebugCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class DebugCommandTest {
/**
* Number we test against if we expect an action to have been performed for each debug section.
* This is a minimum number so tests don't fail each time a new debug section is added; however,
* it should be close to the total.
*/
private static final int MIN_DEBUG_SECTIONS = 9;
@InjectMocks
private DebugCommand command;
@Mock
private Factory<DebugSection> debugSectionFactory;
@Mock
private PermissionsManager permissionsManager;
@Before
@SuppressWarnings("unchecked")
public void initFactory() {
given(debugSectionFactory.newInstance(any(Class.class))).willAnswer(
invocation -> {
Class<?> classArgument = invocation.getArgument(0);
checkArgument(DebugSection.class.isAssignableFrom(classArgument));
return spy(classArgument);
});
}
@Test
public void shouldListAllAvailableDebugSections() {
// given
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(false);
given(permissionsManager.hasPermission(sender, DebugSectionPermissions.INPUT_VALIDATOR)).willReturn(true);
given(permissionsManager.hasPermission(sender, DebugSectionPermissions.DATA_STATISTICS)).willReturn(true);
// when
command.executeCommand(sender, emptyList());
// then
verify(debugSectionFactory, atLeast(MIN_DEBUG_SECTIONS)).newInstance(any(Class.class));
verify(permissionsManager, atLeast(MIN_DEBUG_SECTIONS)).hasPermission(eq(sender), any(DebugSectionPermissions.class));
ArgumentCaptor<String> strCaptor = ArgumentCaptor.forClass(String.class);
verify(sender, times(4)).sendMessage(strCaptor.capture());
assertThat(strCaptor.getAllValues(), contains(
equalTo(ChatColor.BLUE + "AuthMe debug utils"),
equalTo("Sections available to you:"),
containsString("stats: Outputs general data statistics"),
containsString("valid: Checks if your config.yml allows a password / email")));
}
@Test
public void shouldNotListAnyDebugSection() {
// given
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(false);
// when
command.executeCommand(sender, emptyList());
// then
verify(debugSectionFactory, atLeast(MIN_DEBUG_SECTIONS)).newInstance(any(Class.class));
verify(permissionsManager, atLeast(MIN_DEBUG_SECTIONS)).hasPermission(eq(sender), any(DebugSectionPermissions.class));
ArgumentCaptor<String> strCaptor = ArgumentCaptor.forClass(String.class);
verify(sender, times(3)).sendMessage(strCaptor.capture());
assertThat(strCaptor.getAllValues(), contains(
equalTo(ChatColor.BLUE + "AuthMe debug utils"),
equalTo("Sections available to you:"),
containsString("You don't have permission to view any debug section")));
}
@Test
public void shouldRunSection() {
// given
DebugSection section = spy(InputValidator.class);
doNothing().when(section).execute(any(CommandSender.class), anyList());
// Mockito throws a runtime error if below we use the usual "given(factory.newInstance(...)).willReturn(...)"
doReturn(section).when(debugSectionFactory).newInstance(InputValidator.class);
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(sender, section.getRequiredPermission())).willReturn(true);
List<String> arguments = Arrays.asList(section.getName().toUpperCase(Locale.ROOT), "test", "toast");
// when
command.executeCommand(sender, arguments);
// then
verify(permissionsManager).hasPermission(sender, section.getRequiredPermission());
verify(section).execute(sender, Arrays.asList("test", "toast"));
}
@Test
public void shouldNotRunSectionForMissingPermission() {
// given
DebugSection section = spy(InputValidator.class);
// Mockito throws a runtime error if below we use the usual "given(factory.newInstance(...)).willReturn(...)"
doReturn(section).when(debugSectionFactory).newInstance(InputValidator.class);
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(sender, section.getRequiredPermission())).willReturn(false);
List<String> arguments = Arrays.asList(section.getName().toUpperCase(Locale.ROOT), "test");
// when
command.executeCommand(sender, arguments);
// then
verify(permissionsManager).hasPermission(sender, section.getRequiredPermission());
verify(section, never()).execute(any(CommandSender.class), anyList());
verify(sender).sendMessage(argThat(containsString("You don't have permission")));
}
}

View File

@ -1,72 +0,0 @@
package fr.xephi.authme.command.executable.authme.debug;
import fr.xephi.authme.ClassCollector;
import fr.xephi.authme.TestHelper;
import org.junit.BeforeClass;
import org.junit.Test;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
/**
* Consistency tests for {@link DebugSection} implementors.
*/
public class DebugSectionConsistencyTest {
private static List<Class<?>> debugClasses;
private static List<DebugSection> debugSections;
@BeforeClass
public static void collectClasses() {
// TODO ljacqu 20171021: Improve ClassCollector (pass pkg by class, improve #getInstancesOfType's instantiation)
ClassCollector classCollector = new ClassCollector(
TestHelper.SOURCES_FOLDER, TestHelper.PROJECT_ROOT + "command/executable/authme/debug");
debugClasses = classCollector.collectClasses();
debugSections = classCollector.getInstancesOfType(DebugSection.class, clz -> instantiate(clz));
}
@Test
public void shouldAllBePackagePrivate() {
for (Class<?> clazz : debugClasses) {
if (clazz != DebugCommand.class) {
assertThat(clazz + " should be package-private",
Modifier.isPublic(clazz.getModifiers()), equalTo(false));
}
}
}
@Test
public void shouldHaveDifferentSubcommandName() throws IllegalAccessException, InstantiationException {
Set<String> names = new HashSet<>();
for (DebugSection debugSection : debugSections) {
if (!names.add(debugSection.getName())) {
fail("Encountered name '" + debugSection.getName() + "' a second time in " + debugSection.getClass());
}
}
}
@Test
public void shouldAllHaveDescription() {
for (DebugSection debugSection : debugSections) {
assertThat("Description of '" + debugSection.getClass() + "' may not be null",
debugSection.getDescription(), not(nullValue()));
}
}
private static DebugSection instantiate(Class<? extends DebugSection> clazz) {
try {
return ClassCollector.canInstantiate(clazz) ? clazz.newInstance() : null;
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
}

View File

@ -1,125 +0,0 @@
package fr.xephi.authme.command.executable.authme.debug;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.data.limbo.LimboPlayer;
import fr.xephi.authme.data.limbo.LimboService;
import fr.xephi.authme.datasource.CacheDataSource;
import fr.xephi.authme.datasource.DataSource;
import org.bukkit.Location;
import org.junit.Before;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
/**
* Test for {@link DebugSectionUtils}.
*/
public class DebugSectionUtilsTest {
@Before
public void initMockLogger() {
TestHelper.setupLogger();
}
@Test
public void shouldFormatLocation() {
// given / when
String result = DebugSectionUtils.formatLocation(0.0, 10.248592, -18934.2349023, "Main");
// then
assertThat(result, equalTo("(0, 10.25, -18934.23) in 'Main'"));
}
@Test
public void shouldHandleNullWorld() {
// given
Location location = new Location(null, 3.7777, 2.14156, 1);
// when
String result = DebugSectionUtils.formatLocation(location);
// then
assertThat(result, equalTo("(3.78, 2.14, 1) in 'null'"));
}
@Test
public void shouldHandleNullLocation() {
// given / when / then
assertThat(DebugSectionUtils.formatLocation(null), equalTo("null"));
}
@Test
public void shouldFetchMapInLimboService() {
// given
LimboService limboService = mock(LimboService.class);
Map<String, LimboPlayer> limboMap = new HashMap<>();
ReflectionTestUtils.setField(LimboService.class, limboService, "entries", limboMap);
// when
Map map = DebugSectionUtils.applyToLimboPlayersMap(limboService, Function.identity());
// then
assertThat(map, sameInstance(limboMap));
}
@Test
public void shouldHandleErrorGracefully() {
// given
LimboService limboService = mock(LimboService.class);
Map<String, LimboPlayer> limboMap = new HashMap<>();
ReflectionTestUtils.setField(LimboService.class, limboService, "entries", limboMap);
// when
Object result = DebugSectionUtils.applyToLimboPlayersMap(limboService, map -> {
throw new IllegalStateException();
});
// then
assertThat(result, nullValue());
}
@Test
public void shouldReturnSameDataSourceInstance() {
// given
DataSource dataSource = mock(DataSource.class);
// when
DataSource result = DebugSectionUtils.unwrapSourceFromCacheDataSource(dataSource);
// then
assertThat(result, equalTo(dataSource));
}
@Test
public void shouldUnwrapCacheDataSource() {
// given
DataSource source = mock(DataSource.class);
PlayerCache playerCache = mock(PlayerCache.class);
CacheDataSource cacheDataSource = new CacheDataSource(source, playerCache);
// when
DataSource result = DebugSectionUtils.unwrapSourceFromCacheDataSource(cacheDataSource);
// then
assertThat(result, equalTo(source));
}
@Test
public void shouldCastOrReturnNull() {
// given / when / then
assertThat(DebugSectionUtils.castToTypeOrNull("test", String.class), equalTo("test"));
assertThat(DebugSectionUtils.castToTypeOrNull("test", Integer.class), nullValue());
assertThat(DebugSectionUtils.castToTypeOrNull(5, String.class), nullValue());
assertThat(DebugSectionUtils.castToTypeOrNull(5, Integer.class), equalTo(5));
}
}

View File

@ -1,96 +0,0 @@
package fr.xephi.authme.command.executable.authme.debug;
import fr.xephi.authme.ClassCollector;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link HasPermissionChecker}.
*/
@RunWith(MockitoJUnitRunner.class)
public class HasPermissionCheckerTest {
@InjectMocks
private HasPermissionChecker hasPermissionChecker;
@Mock
private PermissionsManager permissionsManager;
@Mock
private BukkitService bukkitService;
@Test
public void shouldListAllPermissionNodeClasses() {
// given
List<Class<? extends PermissionNode>> permissionClasses =
new ClassCollector(TestHelper.SOURCES_FOLDER, TestHelper.PROJECT_ROOT)
.collectClasses(PermissionNode.class).stream()
.filter(clz -> !clz.isInterface())
.collect(Collectors.toList());
// when / then
assertThat(HasPermissionChecker.PERMISSION_NODE_CLASSES, containsInAnyOrder(permissionClasses.toArray()));
}
@Test
public void shouldShowUsageInfo() {
// given
CommandSender sender = mock(CommandSender.class);
// when
hasPermissionChecker.execute(sender, emptyList());
// then
ArgumentCaptor<String> msgCaptor = ArgumentCaptor.forClass(String.class);
verify(sender, atLeast(2)).sendMessage(msgCaptor.capture());
assertThat(
msgCaptor.getAllValues().stream().anyMatch(msg -> msg.contains("/authme debug perm bobby my.perm.node")),
equalTo(true));
}
@Test
public void shouldShowSuccessfulTestWithRegularPlayer() {
// given
String name = "Chuck";
Player player = mock(Player.class);
given(bukkitService.getPlayerExact(name)).willReturn(player);
PermissionNode permission = AdminPermission.CHANGE_EMAIL;
given(permissionsManager.hasPermission(player, permission)).willReturn(true);
CommandSender sender = mock(CommandSender.class);
// when
hasPermissionChecker.execute(sender, asList(name, permission.getNode()));
// then
verify(bukkitService).getPlayerExact(name);
verify(permissionsManager).hasPermission(player, permission);
verify(sender).sendMessage(argThat(containsString("Success: player '" + player.getName()
+ "' has permission '" + permission.getNode() + "'")));
}
}

View File

@ -1,69 +0,0 @@
package fr.xephi.authme.command.executable.authme.debug;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
/**
* Consistency test for {@link MySqlDefaultChanger.Columns} enum.
*/
public class MySqlDefaultChangerColumnsTest {
@Test
public void shouldAllHaveDifferentNameProperty() {
// given
Set<String> properties = new HashSet<>();
// when / then
for (MySqlDefaultChanger.Columns col : MySqlDefaultChanger.Columns.values()) {
if (!properties.add(col.getColumnNameProperty().getPath())) {
fail("Column '" + col + "' has a column name property path that was already encountered: "
+ col.getColumnNameProperty().getPath());
}
}
}
@Test
public void shouldHaveMatchingNullableAndNotNullDefinition() {
for (MySqlDefaultChanger.Columns col : MySqlDefaultChanger.Columns.values()) {
verifyHasCorrespondingColumnDefinitions(col);
}
}
@Test
public void shouldHaveMatchingDefaultValueInNotNullDefinition() {
for (MySqlDefaultChanger.Columns col : MySqlDefaultChanger.Columns.values()) {
verifyHasSameDefaultValueInNotNullDefinition(col);
}
}
private void verifyHasCorrespondingColumnDefinitions(MySqlDefaultChanger.Columns column) {
// given / when
String nullable = column.getNullableDefinition();
String notNull = column.getNotNullDefinition();
// then
String expectedNotNull = nullable + " NOT NULL DEFAULT ";
assertThat(column.name(), notNull.startsWith(expectedNotNull), equalTo(true));
// Check that `notNull` length is bigger because we expect a value after DEFAULT
assertThat(column.name(), notNull.length() > expectedNotNull.length(), equalTo(true));
}
private void verifyHasSameDefaultValueInNotNullDefinition(MySqlDefaultChanger.Columns column) {
// given / when
String notNull = column.getNotNullDefinition();
Object defaultValue = column.getDefaultValue();
// then
String defaultValueAsString = String.valueOf(defaultValue);
if (!notNull.endsWith("DEFAULT " + defaultValueAsString)
&& !notNull.endsWith("DEFAULT '" + defaultValueAsString + "'")) {
fail("Expected '" + column + "' not-null definition to contain DEFAULT " + defaultValueAsString);
}
}
}

View File

@ -1,98 +0,0 @@
package fr.xephi.authme.command.executable.authme.debug;
import com.zaxxer.hikari.HikariDataSource;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.CacheDataSource;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.MySQL;
import fr.xephi.authme.datasource.SqlDataSourceTestUtil;
import fr.xephi.authme.settings.Settings;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.sql.Connection;
import java.sql.SQLException;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Test for {@link MySqlDefaultChanger}.
*/
@RunWith(MockitoJUnitRunner.class)
public class MySqlDefaultChangerTest {
@Mock
private Settings settings;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
}
@Test
public void shouldReturnMySqlConnection() throws SQLException {
// given
Settings settings = mock(Settings.class);
TestHelper.returnDefaultsForAllProperties(settings);
HikariDataSource dataSource = mock(HikariDataSource.class);
Connection connection = mock(Connection.class);
given(dataSource.getConnection()).willReturn(connection);
MySQL mySQL = SqlDataSourceTestUtil.createMySql(settings, dataSource);
MySqlDefaultChanger defaultChanger = createDefaultChanger(mySQL);
// when
Connection result = defaultChanger.getConnection(mySQL);
// then
assertThat(result, equalTo(connection));
verify(dataSource).getConnection();
}
@Test
public void shouldSetMySqlFieldOnInitialization() {
// given
MySQL mySql = mock(MySQL.class);
MySqlDefaultChanger defaultChanger = createDefaultChanger(mySql);
// when
defaultChanger.setMySqlField();
// then
assertThat(ReflectionTestUtils.getFieldValue(MySqlDefaultChanger.class, defaultChanger, "mySql"),
sameInstance(mySql));
}
@Test
public void shouldLeaveMySqlFieldToNullOnInitialization() {
// given
DataSource dataSource = mock(DataSource.class);
PlayerCache playerCache = mock(PlayerCache.class);
CacheDataSource cacheDataSource = new CacheDataSource(dataSource, playerCache);
MySqlDefaultChanger defaultChanger = createDefaultChanger(cacheDataSource);
// when
defaultChanger.setMySqlField();
// then
assertThat(ReflectionTestUtils.getFieldValue(MySqlDefaultChanger.class, defaultChanger, "mySql"),
nullValue());
}
private MySqlDefaultChanger createDefaultChanger(DataSource dataSource) {
MySqlDefaultChanger defaultChanger = new MySqlDefaultChanger();
ReflectionTestUtils.setField(defaultChanger, "dataSource", dataSource);
ReflectionTestUtils.setField(defaultChanger, "settings", settings);
return defaultChanger;
}
}

View File

@ -1,106 +0,0 @@
package fr.xephi.authme.command.executable.authme.debug;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link PlayerAuthViewer}.
*/
@RunWith(MockitoJUnitRunner.class)
public class PlayerAuthViewerTest {
@InjectMocks
private PlayerAuthViewer authViewer;
@Mock
private DataSource dataSource;
@Test
public void shouldMakeExample() {
// given
CommandSender sender = mock(CommandSender.class);
// when
authViewer.execute(sender, Collections.emptyList());
// then
verify(sender).sendMessage(argThat(containsString("Example: /authme debug db Bobby")));
}
@Test
public void shouldHandleMissingPlayer() {
// given
CommandSender sender = mock(CommandSender.class);
// when
authViewer.execute(sender, Collections.singletonList("bogus"));
// then
verify(dataSource).getAuth("bogus");
verify(sender).sendMessage(argThat(containsString("No record exists for 'bogus'")));
}
@Test
public void shouldDisplayAuthInfo() {
// given
CommandSender sender = mock(CommandSender.class);
PlayerAuth auth = PlayerAuth.builder().name("george").realName("George")
.password("abcdefghijkl", "mnopqrst")
.lastIp("127.1.2.7").registrationDate(1111140000000L)
.totpKey("SECRET1321")
.build();
given(dataSource.getAuth("George")).willReturn(auth);
// when
authViewer.execute(sender, Collections.singletonList("George"));
// then
ArgumentCaptor<String> textCaptor = ArgumentCaptor.forClass(String.class);
verify(sender, atLeastOnce()).sendMessage(textCaptor.capture());
assertThat(textCaptor.getAllValues(), hasItem(containsString("Player george / George")));
assertThat(textCaptor.getAllValues(), hasItem(containsString("Registration: 2005-03-18T")));
assertThat(textCaptor.getAllValues(), hasItem(containsString("Hash / salt (partial): 'abcdef...' / 'mnop...'")));
assertThat(textCaptor.getAllValues(), hasItem(containsString("TOTP code (partial): 'SEC...'")));
}
@Test
public void shouldHandleCornerCases() {
// given
CommandSender sender = mock(CommandSender.class);
PlayerAuth auth = PlayerAuth.builder().name("tar")
.password("abcd", null)
.lastIp("127.1.2.7").registrationDate(0L)
.build();
given(dataSource.getAuth("Tar")).willReturn(auth);
// when
authViewer.execute(sender, Collections.singletonList("Tar"));
// then
ArgumentCaptor<String> textCaptor = ArgumentCaptor.forClass(String.class);
verify(sender, atLeastOnce()).sendMessage(textCaptor.capture());
assertThat(textCaptor.getAllValues(), hasItem(containsString("Player tar / Player")));
assertThat(textCaptor.getAllValues(), hasItem(containsString("Registration: Not available (0)")));
assertThat(textCaptor.getAllValues(), hasItem(containsString("Last login: Not available (null)")));
assertThat(textCaptor.getAllValues(), hasItem(containsString("Hash / salt (partial): 'ab...' / ''")));
assertThat(textCaptor.getAllValues(), hasItem(containsString("TOTP code (partial): ''")));
}
}

View File

@ -1,190 +0,0 @@
package fr.xephi.authme.command.executable.captcha;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.data.captcha.LoginCaptchaManager;
import fr.xephi.authme.data.captcha.RegistrationCaptchaManager;
import fr.xephi.authme.data.limbo.LimboService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Test for {@link CaptchaCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class CaptchaCommandTest {
@InjectMocks
private CaptchaCommand command;
@Mock
private LoginCaptchaManager loginCaptchaManager;
@Mock
private RegistrationCaptchaManager registrationCaptchaManager;
@Mock
private PlayerCache playerCache;
@Mock
private CommonService commonService;
@Mock
private LimboService limboService;
@Mock
private DataSource dataSource;
@Test
public void shouldDetectIfPlayerIsLoggedIn() {
// given
String name = "creeper011";
Player player = mockPlayerWithName(name);
given(playerCache.isAuthenticated(name)).willReturn(true);
// when
command.executeCommand(player, Collections.singletonList("123"));
// then
verify(commonService).send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
}
@Test
public void shouldShowLoginUsageIfCaptchaIsNotRequired() {
// given
String name = "bobby";
Player player = mockPlayerWithName(name);
given(playerCache.isAuthenticated(name)).willReturn(false);
given(loginCaptchaManager.isCaptchaRequired(name)).willReturn(false);
given(dataSource.isAuthAvailable(name)).willReturn(true);
// when
command.executeCommand(player, Collections.singletonList("1234"));
// then
verify(commonService).send(player, MessageKey.USAGE_LOGIN);
verify(loginCaptchaManager).isCaptchaRequired(name);
verifyNoMoreInteractions(loginCaptchaManager, registrationCaptchaManager);
}
@Test
public void shouldHandleCorrectCaptchaInput() {
// given
String name = "smith";
Player player = mockPlayerWithName(name);
given(playerCache.isAuthenticated(name)).willReturn(false);
given(loginCaptchaManager.isCaptchaRequired(name)).willReturn(true);
String captchaCode = "3991";
given(loginCaptchaManager.checkCode(player, captchaCode)).willReturn(true);
// when
command.executeCommand(player, Collections.singletonList(captchaCode));
// then
verify(loginCaptchaManager).isCaptchaRequired(name);
verify(loginCaptchaManager).checkCode(player, captchaCode);
verifyNoMoreInteractions(loginCaptchaManager);
verify(commonService).send(player, MessageKey.CAPTCHA_SUCCESS);
verify(commonService).send(player, MessageKey.LOGIN_MESSAGE);
verify(limboService).unmuteMessageTask(player);
verifyNoMoreInteractions(commonService);
}
@Test
public void shouldHandleWrongCaptchaInput() {
// given
String name = "smith";
Player player = mockPlayerWithName(name);
given(playerCache.isAuthenticated(name)).willReturn(false);
given(loginCaptchaManager.isCaptchaRequired(name)).willReturn(true);
String captchaCode = "2468";
given(loginCaptchaManager.checkCode(player, captchaCode)).willReturn(false);
String newCode = "1337";
given(loginCaptchaManager.getCaptchaCodeOrGenerateNew(name)).willReturn(newCode);
// when
command.executeCommand(player, Collections.singletonList(captchaCode));
// then
verify(loginCaptchaManager).isCaptchaRequired(name);
verify(loginCaptchaManager).checkCode(player, captchaCode);
verify(loginCaptchaManager).getCaptchaCodeOrGenerateNew(name);
verifyNoMoreInteractions(loginCaptchaManager);
verify(commonService).send(player, MessageKey.CAPTCHA_WRONG_ERROR, newCode);
verifyNoMoreInteractions(commonService);
}
@Test
public void shouldVerifyWithRegisterCaptchaManager() {
// given
String name = "john";
Player player = mockPlayerWithName(name);
given(loginCaptchaManager.isCaptchaRequired(name)).willReturn(false);
given(registrationCaptchaManager.isCaptchaRequired(name)).willReturn(true);
String captchaCode = "A89Y3";
given(registrationCaptchaManager.checkCode(player, captchaCode)).willReturn(true);
// when
command.executeCommand(player, Collections.singletonList(captchaCode));
// then
verify(registrationCaptchaManager).checkCode(player, captchaCode);
verify(loginCaptchaManager, only()).isCaptchaRequired(name);
verify(commonService).send(player, MessageKey.REGISTER_CAPTCHA_SUCCESS);
verify(commonService).send(player, MessageKey.REGISTER_MESSAGE);
}
@Test
public void shouldHandleFailedRegisterCaptcha() {
// given
String name = "asfd";
Player player = mockPlayerWithName(name);
given(registrationCaptchaManager.isCaptchaRequired(name)).willReturn(true);
String captchaCode = "SFL3";
given(registrationCaptchaManager.checkCode(player, captchaCode)).willReturn(false);
given(registrationCaptchaManager.getCaptchaCodeOrGenerateNew(name)).willReturn("new code");
// when
command.executeCommand(player, Collections.singletonList(captchaCode));
// then
verify(registrationCaptchaManager).checkCode(player, captchaCode);
verify(registrationCaptchaManager).getCaptchaCodeOrGenerateNew(name);
verify(commonService).send(player, MessageKey.CAPTCHA_WRONG_ERROR, "new code");
}
@Test
public void shouldShowRegisterUsageWhenRegistrationCaptchaIsSolved() {
// given
String name = "alice";
Player player = mockPlayerWithName(name);
given(registrationCaptchaManager.isCaptchaRequired(name)).willReturn(false);
// when
command.executeCommand(player, Collections.singletonList("test"));
// then
verify(registrationCaptchaManager, only()).isCaptchaRequired(name);
verify(commonService).send(player, MessageKey.USAGE_REGISTER);
}
private static Player mockPlayerWithName(String name) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
return player;
}
}

View File

@ -1,130 +0,0 @@
package fr.xephi.authme.command.executable.changepassword;
import fr.xephi.authme.data.VerificationCodeManager;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link ChangePasswordCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ChangePasswordCommandTest {
@InjectMocks
private ChangePasswordCommand command;
@Mock
private CommonService commonService;
@Mock
private PlayerCache playerCache;
@Mock
private VerificationCodeManager codeManager;
@Mock
private ValidationService validationService;
@Mock
private Management management;
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = mock(BlockCommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(sender).sendMessage(argThat(containsString("use /authme password <playername> <password> instead")));
}
@Test
public void shouldRejectNotLoggedInPlayer() {
// given
CommandSender sender = initPlayerWithName("name", false);
// when
command.executeCommand(sender, Arrays.asList("pass", "pass"));
// then
verify(commonService).send(sender, MessageKey.NOT_LOGGED_IN);
}
@Test
public void shouldRejectInvalidPassword() {
// given
Player sender = initPlayerWithName("abc12", true);
String password = "newPW";
given(validationService.validatePassword(password, "abc12")).willReturn(new ValidationResult(MessageKey.INVALID_PASSWORD_LENGTH));
given(codeManager.isVerificationRequired(sender)).willReturn(false);
// when
command.executeCommand(sender, Arrays.asList("tester", password));
// then
verify(validationService).validatePassword(password, "abc12");
verify(commonService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH, new String[0]);
verify(codeManager).isVerificationRequired(sender);
}
@Test
public void shouldForwardTheDataForValidPassword() {
// given
String oldPass = "oldpass";
String newPass = "abc123";
Player player = initPlayerWithName("parker", true);
given(validationService.validatePassword("abc123", "parker")).willReturn(new ValidationResult());
given(codeManager.isVerificationRequired(player)).willReturn(false);
// when
command.executeCommand(player, Arrays.asList(oldPass, newPass));
// then
verify(validationService).validatePassword(newPass, "parker");
verify(commonService, never()).send(eq(player), any(MessageKey.class));
verify(management).performPasswordChange(player, oldPass, newPass);
verify(codeManager).isVerificationRequired(player);
}
@Test
public void shouldDefineArgumentMismatchMessage() {
// given / when / then
assertThat(command.getArgumentsMismatchMessage(), equalTo(MessageKey.USAGE_CHANGE_PASSWORD));
}
private Player initPlayerWithName(String name, boolean loggedIn) {
Player player = mock(Player.class);
when(player.getName()).thenReturn(name);
when(playerCache.isAuthenticated(name)).thenReturn(loggedIn);
return player;
}
}

View File

@ -1,83 +0,0 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Collections;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Test for {@link AddEmailCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class AddEmailCommandTest {
@InjectMocks
private AddEmailCommand command;
@Mock
private CommonService commandService;
@Mock
private Management management;
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = mock(BlockCommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verifyNoInteractions(management);
}
@Test
public void shouldForwardData() {
// given
Player sender = mock(Player.class);
String email = "mail@example";
// when
command.executeCommand(sender, Arrays.asList(email, email));
// then
verify(management).performAddEmail(sender, email);
}
@Test
public void shouldFailForConfirmationMismatch() {
// given
Player sender = mock(Player.class);
String email = "asdfasdf@example.com";
// when
command.executeCommand(sender, Arrays.asList(email, "wrongConf"));
// then
verifyNoInteractions(management);
verify(commandService).send(sender, MessageKey.CONFIRM_EMAIL_MESSAGE);
}
@Test
public void shouldDefineArgumentMismatchMessage() {
// given / when / then
assertThat(command.getArgumentsMismatchMessage(), equalTo(MessageKey.USAGE_ADD_EMAIL));
}
}

View File

@ -1,99 +0,0 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.data.VerificationCodeManager;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Collections;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
/**
* Test for {@link ChangeEmailCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ChangeEmailCommandTest {
@InjectMocks
private ChangeEmailCommand command;
@Mock
private Management management;
@Mock
private CommonService commonService;
@Mock
private VerificationCodeManager codeManager;
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = mock(BlockCommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verifyNoInteractions(management);
}
@Test
public void shouldStopIfVerificationIsRequired() {
// given
String name = "Testeroni";
Player player = initPlayerWithName(name);
given(codeManager.isVerificationRequired(player)).willReturn(true);
// when
command.executeCommand(player, Arrays.asList("mail@example.org", "otherMail@example.com"));
// then
verify(codeManager).codeExistOrGenerateNew(name);
verify(commonService).send(player, MessageKey.VERIFICATION_CODE_REQUIRED);
verifyNoInteractions(management);
}
@Test
public void shouldForwardData() {
// given
Player sender = initPlayerWithName("AmATest");
given(codeManager.isVerificationRequired(sender)).willReturn(false);
// when
command.executeCommand(sender, Arrays.asList("new.mail@example.org", "old_mail@example.org"));
// then
verify(management).performChangeEmail(sender, "new.mail@example.org", "old_mail@example.org");
verify(codeManager).isVerificationRequired(sender);
}
@Test
public void shouldDefineArgumentMismatchMessage() {
// given / when / then
assertThat(command.getArgumentsMismatchMessage(), equalTo(MessageKey.USAGE_CHANGE_EMAIL));
}
private Player initPlayerWithName(String name) {
Player player = mock(Player.class);
when(player.getName()).thenReturn(name);
return player;
}
}

View File

@ -1,49 +0,0 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.CommandMapper;
import fr.xephi.authme.command.FoundCommandResult;
import fr.xephi.authme.command.help.HelpProvider;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Test for {@link EmailBaseCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class EmailBaseCommandTest {
@InjectMocks
private EmailBaseCommand command;
@Mock
private HelpProvider helpProvider;
@Mock
private CommandMapper commandMapper;
@Test
public void shouldDisplayHelp() {
// given
CommandSender sender = mock(CommandSender.class);
FoundCommandResult result = mock(FoundCommandResult.class);
given(commandMapper.mapPartsToCommand(eq(sender), anyList())).willReturn(result);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(commandMapper).mapPartsToCommand(sender, Collections.singletonList("email"));
verify(helpProvider).outputHelp(sender, result, HelpProvider.SHOW_CHILDREN);
}
}

View File

@ -1,108 +0,0 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.PasswordRecoveryService;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Tests for {@link EmailSetPasswordCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class EmailSetPasswordCommandTest {
@InjectMocks
private EmailSetPasswordCommand command;
@Mock
private DataSource dataSource;
@Mock
private CommonService commonService;
@Mock
private PasswordRecoveryService recoveryService;
@Mock
private PasswordSecurity passwordSecurity;
@Mock
private ValidationService validationService;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
}
@Test
public void shouldChangePassword() {
// given
Player player = mock(Player.class);
String name = "Jerry";
given(player.getName()).willReturn(name);
given(recoveryService.canChangePassword(player)).willReturn(true);
HashedPassword hashedPassword = passwordSecurity.computeHash("abc123", name);
given(passwordSecurity.computeHash("abc123", name)).willReturn(hashedPassword);
given(validationService.validatePassword("abc123", name))
.willReturn(new ValidationService.ValidationResult());
// when
command.runCommand(player, Collections.singletonList("abc123"));
// then
verify(validationService).validatePassword("abc123", name);
verify(dataSource).updatePassword(name, hashedPassword);
verify(recoveryService).removeFromSuccessfulRecovery(player);
verify(commonService).send(player, MessageKey.PASSWORD_CHANGED_SUCCESS);
}
@Test
public void shouldRejectInvalidPassword() {
// given
Player player = mock(Player.class);
String name = "Morgan";
given(player.getName()).willReturn(name);
String password = "newPW";
given(validationService.validatePassword(password, name))
.willReturn(new ValidationService.ValidationResult(MessageKey.INVALID_PASSWORD_LENGTH));
given(recoveryService.canChangePassword(player)).willReturn(true);
// when
command.executeCommand(player, Collections.singletonList(password));
// then
verify(validationService).validatePassword(password, name);
verify(commonService).send(player, MessageKey.INVALID_PASSWORD_LENGTH, new String[0]);
}
@Test
public void shouldDoNothingCantChangePass() {
// given
Player player = mock(Player.class);
// when
command.runCommand(player, Collections.singletonList("abc123"));
// then
verifyNoInteractions(validationService, dataSource);
verify(commonService).send(player, MessageKey.CHANGE_PASSWORD_EXPIRED);
}
}

View File

@ -1,92 +0,0 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.PasswordRecoveryService;
import fr.xephi.authme.service.RecoveryCodeService;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Tests for {@link ProcessCodeCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ProcessCodeCommandTest {
@InjectMocks
private ProcessCodeCommand command;
@Mock
private CommonService commonService;
@Mock
private RecoveryCodeService codeService;
@Mock
private PasswordRecoveryService recoveryService;
@Test
public void shouldSendErrorForInvalidRecoveryCode() {
// given
String name = "Vultur3";
Player sender = mock(Player.class);
given(sender.getName()).willReturn(name);
given(codeService.hasTriesLeft(name)).willReturn(true);
given(codeService.isCodeValid(name, "bogus")).willReturn(false);
given(codeService.getTriesLeft(name)).willReturn(2);
// when
command.executeCommand(sender, Collections.singletonList("bogus"));
// then
verify(commonService).send(sender, MessageKey.INCORRECT_RECOVERY_CODE, "2");
verifyNoMoreInteractions(recoveryService);
}
@Test
public void shouldSendErrorForNoMoreTries() {
// given
String name = "BobbY";
Player sender = mock(Player.class);
given(sender.getName()).willReturn(name);
given(codeService.hasTriesLeft(name)).willReturn(false);
// when
command.executeCommand(sender, Collections.singletonList("bogus"));
// then
verify(commonService).send(sender, MessageKey.RECOVERY_TRIES_EXCEEDED);
verify(codeService).removeCode(name);
verifyNoMoreInteractions(recoveryService);
}
@Test
public void shouldProcessCorrectCode() {
// given
String name = "Dwight";
String code = "chickenDinner";
Player sender = mock(Player.class);
given(sender.getName()).willReturn(name);
given(codeService.hasTriesLeft(name)).willReturn(true);
given(codeService.isCodeValid(name, code)).willReturn(true);
// when
command.runCommand(sender, Collections.singletonList(code));
// then
verify(commonService).send(sender, MessageKey.RECOVERY_CODE_CORRECT);
verify(recoveryService).addSuccessfulRecovery(sender);
verify(codeService).removeCode(name);
}
}

View File

@ -1,227 +0,0 @@
package fr.xephi.authme.command.executable.email;
import ch.jalu.datasourcecolumns.data.DataSourceValueImpl;
import ch.jalu.injector.testing.BeforeInjecting;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.mail.EmailService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.PasswordRecoveryService;
import fr.xephi.authme.service.RecoveryCodeService;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import java.util.Collections;
import java.util.Locale;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskAsynchronously;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Test for {@link RecoverEmailCommand}.
*/
@RunWith(DelayedInjectionRunner.class)
public class RecoverEmailCommandTest {
private static final String DEFAULT_EMAIL = "your@email.com";
@InjectDelayed
private RecoverEmailCommand command;
@Mock
private PasswordSecurity passwordSecurity;
@Mock
private CommonService commonService;
@Mock
private DataSource dataSource;
@Mock
private PlayerCache playerCache;
@Mock
private EmailService emailService;
@Mock
private PasswordRecoveryService recoveryService;
@Mock
private RecoveryCodeService recoveryCodeService;
@Mock
private BukkitService bukkitService;
@BeforeClass
public static void initLogger() {
TestHelper.setupLogger();
}
@BeforeInjecting
public void initSettings() {
given(commonService.getProperty(SecuritySettings.EMAIL_RECOVERY_COOLDOWN_SECONDS)).willReturn(40);
}
@Test
public void shouldHandleMissingMailProperties() {
// given
given(emailService.hasAllInformation()).willReturn(false);
Player sender = mock(Player.class);
// when
command.executeCommand(sender, Collections.singletonList("some@email.tld"));
// then
verify(commonService).send(sender, MessageKey.INCOMPLETE_EMAIL_SETTINGS);
verifyNoInteractions(dataSource, passwordSecurity);
}
@Test
public void shouldShowErrorForAuthenticatedUser() {
// given
String name = "Bobby";
Player sender = mock(Player.class);
given(sender.getName()).willReturn(name);
given(emailService.hasAllInformation()).willReturn(true);
given(playerCache.isAuthenticated(name)).willReturn(true);
// when
command.executeCommand(sender, Collections.singletonList("bobby@example.org"));
// then
verify(emailService).hasAllInformation();
verifyNoInteractions(dataSource);
verify(commonService).send(sender, MessageKey.ALREADY_LOGGED_IN_ERROR);
}
@Test
public void shouldShowRegisterMessageForUnregisteredPlayer() {
// given
String name = "Player123";
Player sender = mock(Player.class);
given(sender.getName()).willReturn(name);
given(emailService.hasAllInformation()).willReturn(true);
given(playerCache.isAuthenticated(name)).willReturn(false);
given(dataSource.getEmail(name)).willReturn(DataSourceValueImpl.unknownRow());
// when
command.executeCommand(sender, Collections.singletonList("someone@example.com"));
// then
verify(emailService).hasAllInformation();
verify(dataSource).getEmail(name);
verifyNoMoreInteractions(dataSource);
verify(commonService).send(sender, MessageKey.USAGE_REGISTER);
}
@Test
public void shouldHandleDefaultEmail() {
// given
String name = "Tract0r";
Player sender = mock(Player.class);
given(sender.getName()).willReturn(name);
given(emailService.hasAllInformation()).willReturn(true);
given(playerCache.isAuthenticated(name)).willReturn(false);
given(dataSource.getEmail(name)).willReturn(DataSourceValueImpl.of(DEFAULT_EMAIL));
// when
command.executeCommand(sender, Collections.singletonList(DEFAULT_EMAIL));
// then
verify(emailService).hasAllInformation();
verify(dataSource).getEmail(name);
verifyNoMoreInteractions(dataSource);
verify(commonService).send(sender, MessageKey.INVALID_EMAIL);
}
@Test
public void shouldHandleInvalidEmailInput() {
// given
String name = "Rapt0r";
Player sender = mock(Player.class);
given(sender.getName()).willReturn(name);
given(emailService.hasAllInformation()).willReturn(true);
given(playerCache.isAuthenticated(name)).willReturn(false);
given(dataSource.getEmail(name)).willReturn(DataSourceValueImpl.of("raptor@example.org"));
// when
command.executeCommand(sender, Collections.singletonList("wrong-email@example.com"));
// then
verify(emailService).hasAllInformation();
verify(dataSource).getEmail(name);
verifyNoMoreInteractions(dataSource);
verify(commonService).send(sender, MessageKey.INVALID_EMAIL);
}
@Test
public void shouldGenerateRecoveryCode() {
// given
String name = "Vultur3";
Player sender = mock(Player.class);
given(sender.getName()).willReturn(name);
given(emailService.hasAllInformation()).willReturn(true);
given(emailService.sendRecoveryCode(anyString(), anyString(), anyString())).willReturn(true);
given(playerCache.isAuthenticated(name)).willReturn(false);
String email = "v@example.com";
given(dataSource.getEmail(name)).willReturn(DataSourceValueImpl.of(email));
String code = "a94f37";
given(recoveryCodeService.isRecoveryCodeNeeded()).willReturn(true);
given(recoveryCodeService.generateCode(name)).willReturn(code);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, Collections.singletonList(email.toUpperCase(Locale.ROOT)));
// then
verify(emailService).hasAllInformation();
verify(dataSource).getEmail(name);
verify(recoveryService).createAndSendRecoveryCode(sender, email);
}
@Test
public void shouldGenerateNewPasswordWithoutRecoveryCode() {
// given
String name = "Vultur3";
Player sender = mock(Player.class);
given(sender.getName()).willReturn(name);
given(emailService.hasAllInformation()).willReturn(true);
given(emailService.sendPasswordMail(anyString(), anyString(), anyString())).willReturn(true);
given(playerCache.isAuthenticated(name)).willReturn(false);
String email = "vulture@example.com";
given(dataSource.getEmail(name)).willReturn(DataSourceValueImpl.of(email));
given(recoveryCodeService.isRecoveryCodeNeeded()).willReturn(false);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, Collections.singletonList(email));
// then
verify(emailService).hasAllInformation();
verify(dataSource).getEmail(name);
verify(recoveryService).generateAndSendNewPassword(sender, email);
}
@Test
public void shouldDefineArgumentMismatchMessage() {
// given / when / then
assertThat(command.getArgumentsMismatchMessage(), equalTo(MessageKey.USAGE_RECOVER_EMAIL));
}
}

View File

@ -1,95 +0,0 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Test for {@link ShowEmailCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ShowEmailCommandTest {
private static final String CURRENT_EMAIL = "my.email@example.com";
private static final String USERNAME = "name";
@InjectMocks
private ShowEmailCommand command;
@Mock
private CommonService commonService;
@Mock
private PlayerCache playerCache;
@Test
public void shouldShowCurrentEmailMessage() {
// given
Player sender = mock(Player.class);
given(sender.getName()).willReturn(USERNAME);
given(playerCache.getAuth(USERNAME)).willReturn(newAuthWithEmail(CURRENT_EMAIL));
given(commonService.getProperty(SecuritySettings.USE_EMAIL_MASKING)).willReturn(false);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(commonService).send(sender, MessageKey.EMAIL_SHOW, CURRENT_EMAIL);
}
@Test
public void shouldShowHiddenEmailMessage() {
// given
Player sender = mock(Player.class);
given(sender.getName()).willReturn(USERNAME);
given(playerCache.getAuth(USERNAME)).willReturn(newAuthWithEmail(CURRENT_EMAIL));
given(commonService.getProperty(SecuritySettings.USE_EMAIL_MASKING)).willReturn(true);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(commonService).send(sender, MessageKey.EMAIL_SHOW, "my.***@***mple.com");
}
@Test
public void shouldReturnNoEmailMessage() {
// given
Player sender = mock(Player.class);
given(sender.getName()).willReturn(USERNAME);
given(playerCache.getAuth(USERNAME)).willReturn(newAuthWithNoEmail());
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(commonService).send(sender, MessageKey.SHOW_NO_EMAIL);
}
private static PlayerAuth newAuthWithEmail(String email) {
return PlayerAuth.builder()
.name(USERNAME)
.email(email)
.build();
}
private static PlayerAuth newAuthWithNoEmail() {
return PlayerAuth.builder()
.name(USERNAME)
.build();
}
}

View File

@ -1,67 +0,0 @@
package fr.xephi.authme.command.executable.login;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link LoginCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class LoginCommandTest {
@InjectMocks
private LoginCommand command;
@Mock
private Management management;
@Test
public void shouldStopIfSenderIsNotAPlayer() {
// given
CommandSender sender = mock(BlockCommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verifyNoInteractions(management);
verify(sender).sendMessage(argThat(containsString("/authme forcelogin <player>")));
}
@Test
public void shouldCallManagementForPlayerCaller() {
// given
Player sender = mock(Player.class);
// when
command.executeCommand(sender, Collections.singletonList("password"));
// then
verify(management).performLogin(sender, "password" );
}
@Test
public void shouldDefineArgumentMismatchMessage() {
// given / when / then
assertThat(command.getArgumentsMismatchMessage(), equalTo(MessageKey.USAGE_LOGIN));
}
}

View File

@ -1,60 +0,0 @@
package fr.xephi.authme.command.executable.logout;
import fr.xephi.authme.process.Management;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link LogoutCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class LogoutCommandTest {
@InjectMocks
private LogoutCommand command;
@Mock
private Management management;
@Test
public void shouldStopIfSenderIsNotAPlayer() {
// given
CommandSender sender = mock(BlockCommandSender.class);
// when
command.executeCommand(sender, new ArrayList<String>());
// then
verifyNoInteractions(management);
verify(sender).sendMessage(argThat(containsString("only for players")));
}
@Test
public void shouldCallManagementForPlayerCaller() {
// given
Player sender = mock(Player.class);
// when
command.executeCommand(sender, Collections.singletonList("password"));
// then
verify(management).performLogout(sender);
}
}

View File

@ -1,332 +0,0 @@
package fr.xephi.authme.command.executable.register;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.captcha.RegistrationCaptchaManager;
import fr.xephi.authme.mail.EmailService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.process.register.RegisterSecondaryArgument;
import fr.xephi.authme.process.register.RegistrationType;
import fr.xephi.authme.process.register.executors.EmailRegisterParams;
import fr.xephi.authme.process.register.executors.PasswordRegisterParams;
import fr.xephi.authme.process.register.executors.RegistrationMethod;
import fr.xephi.authme.process.register.executors.TwoFactorRegisterParams;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Collections;
import static fr.xephi.authme.IsEqualByReflectionMatcher.hasEqualValuesOnAllFields;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link RegisterCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class RegisterCommandTest {
@InjectMocks
private RegisterCommand command;
@Mock
private CommonService commonService;
@Mock
private Management management;
@Mock
private EmailService emailService;
@Mock
private ValidationService validationService;
@Mock
private RegistrationCaptchaManager registrationCaptchaManager;
@BeforeClass
public static void setup() {
TestHelper.setupLogger();
}
@Before
public void linkMocksAndProvideSettingDefaults() {
given(commonService.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.BCRYPT);
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.PASSWORD);
given(commonService.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT)).willReturn(RegisterSecondaryArgument.NONE);
}
@Test
public void shouldNotRunForNonPlayerSender() {
// given
CommandSender sender = mock(BlockCommandSender.class);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(sender).sendMessage(argThat(containsString("Player only!")));
verifyNoInteractions(management, emailService);
}
@Test
public void shouldForwardToManagementForTwoFactor() {
// given
given(commonService.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.TWO_FACTOR);
Player player = mockPlayerWithName("test2");
// when
command.executeCommand(player, Collections.emptyList());
// then
verify(registrationCaptchaManager).isCaptchaRequired("test2");
verify(management).performRegister(eq(RegistrationMethod.TWO_FACTOR_REGISTRATION),
argThat(hasEqualValuesOnAllFields(TwoFactorRegisterParams.of(player))));
verifyNoInteractions(emailService);
}
@Test
public void shouldReturnErrorForEmptyArguments() {
// given
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.emptyList());
// then
verify(commonService).send(player, MessageKey.USAGE_REGISTER);
verifyNoInteractions(management, emailService);
}
@Test
public void shouldReturnErrorForMissingConfirmation() {
// given
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.PASSWORD);
given(commonService.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT)).willReturn(RegisterSecondaryArgument.CONFIRMATION);
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.singletonList("arrrr"));
// then
verify(commonService).send(player, MessageKey.USAGE_REGISTER);
verifyNoInteractions(management, emailService);
}
@Test
public void shouldReturnErrorForMissingEmailConfirmation() {
// given
given(emailService.hasAllInformation()).willReturn(true);
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.EMAIL);
given(commonService.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT)).willReturn(RegisterSecondaryArgument.EMAIL_MANDATORY);
given(validationService.validateEmail(anyString())).willReturn(true);
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.singletonList("test@example.org"));
// then
verify(commonService).send(player, MessageKey.USAGE_REGISTER);
verifyNoInteractions(management);
}
@Test
public void shouldThrowErrorForMissingEmailConfiguration() {
// given
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.EMAIL);
given(emailService.hasAllInformation()).willReturn(false);
Player player = mock(Player.class);
// when
command.executeCommand(player, Collections.singletonList("myMail@example.tld"));
// then
verify(commonService).send(player, MessageKey.INCOMPLETE_EMAIL_SETTINGS);
verify(emailService).hasAllInformation();
verifyNoInteractions(management);
}
@Test
public void shouldRejectInvalidEmail() {
// given
String playerMail = "player@example.org";
given(validationService.validateEmail(playerMail)).willReturn(false);
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.EMAIL);
given(emailService.hasAllInformation()).willReturn(true);
Player player = mock(Player.class);
// when
command.executeCommand(player, Arrays.asList(playerMail, playerMail));
// then
verify(validationService).validateEmail(playerMail);
verify(commonService).send(player, MessageKey.INVALID_EMAIL);
verifyNoInteractions(management);
}
@Test
public void shouldRejectInvalidEmailConfirmation() {
// given
String playerMail = "bobber@bobby.org";
given(validationService.validateEmail(playerMail)).willReturn(true);
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.EMAIL);
given(commonService.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT)).willReturn(RegisterSecondaryArgument.CONFIRMATION);
given(emailService.hasAllInformation()).willReturn(true);
Player player = mock(Player.class);
// when
command.executeCommand(player, Arrays.asList(playerMail, "invalid"));
// then
verify(commonService).send(player, MessageKey.USAGE_REGISTER);
verify(emailService).hasAllInformation();
verifyNoInteractions(management);
}
@Test
public void shouldPerformEmailRegistration() {
// given
String playerMail = "asfd@lakjgre.lds";
given(validationService.validateEmail(playerMail)).willReturn(true);
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.EMAIL);
given(commonService.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT)).willReturn(RegisterSecondaryArgument.CONFIRMATION);
given(emailService.hasAllInformation()).willReturn(true);
Player player = mockPlayerWithName("brett");
// when
command.executeCommand(player, Arrays.asList(playerMail, playerMail));
// then
verify(registrationCaptchaManager).isCaptchaRequired("brett");
verify(validationService).validateEmail(playerMail);
verify(emailService).hasAllInformation();
verify(management).performRegister(eq(RegistrationMethod.EMAIL_REGISTRATION),
argThat(hasEqualValuesOnAllFields(EmailRegisterParams.of(player, playerMail))));
}
@Test
public void shouldRejectInvalidPasswordConfirmation() {
// given
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.PASSWORD);
given(commonService.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT)).willReturn(RegisterSecondaryArgument.CONFIRMATION);
Player player = mock(Player.class);
// when
command.executeCommand(player, Arrays.asList("myPass", "mypass"));
// then
verify(commonService).send(player, MessageKey.PASSWORD_MATCH_ERROR);
verifyNoInteractions(management, emailService);
}
@Test
public void shouldPerformPasswordRegistration() {
// given
Player player = mockPlayerWithName("newPlayer");
// when
command.executeCommand(player, Collections.singletonList("myPass"));
// then
verify(registrationCaptchaManager).isCaptchaRequired("newPlayer");
verify(management).performRegister(eq(RegistrationMethod.PASSWORD_REGISTRATION),
argThat(hasEqualValuesOnAllFields(PasswordRegisterParams.of(player, "myPass", null))));
}
@Test
public void shouldPerformMailValidationForPasswordWithEmail() {
// given
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.PASSWORD);
given(commonService.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT)).willReturn(RegisterSecondaryArgument.EMAIL_MANDATORY);
String email = "email@example.org";
given(validationService.validateEmail(email)).willReturn(true);
Player player = mock(Player.class);
// when
command.executeCommand(player, Arrays.asList("myPass", email));
// then
verify(validationService).validateEmail(email);
verify(management).performRegister(eq(RegistrationMethod.PASSWORD_REGISTRATION),
argThat(hasEqualValuesOnAllFields(PasswordRegisterParams.of(player, "myPass", email))));
}
@Test
public void shouldStopForInvalidEmail() {
// given
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.PASSWORD);
given(commonService.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT)).willReturn(RegisterSecondaryArgument.EMAIL_OPTIONAL);
String email = "email@example.org";
given(validationService.validateEmail(email)).willReturn(false);
Player player = mockPlayerWithName("Waaa");
// when
command.executeCommand(player, Arrays.asList("myPass", email));
// then
verify(registrationCaptchaManager).isCaptchaRequired("Waaa");
verify(validationService).validateEmail(email);
verify(commonService).send(player, MessageKey.INVALID_EMAIL);
verifyNoInteractions(management);
}
@Test
public void shouldPerformNormalPasswordRegisterForOneArgument() {
// given
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.PASSWORD);
given(commonService.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT)).willReturn(RegisterSecondaryArgument.EMAIL_OPTIONAL);
Player player = mockPlayerWithName("Doa");
// when
command.executeCommand(player, Collections.singletonList("myPass"));
// then
verify(registrationCaptchaManager).isCaptchaRequired("Doa");
verify(management).performRegister(eq(RegistrationMethod.PASSWORD_REGISTRATION),
argThat(hasEqualValuesOnAllFields(PasswordRegisterParams.of(player, "myPass", null))));
}
@Test
public void shouldRequestCaptcha() {
// given
given(registrationCaptchaManager.isCaptchaRequired(anyString())).willReturn(true);
String name = "Brian";
Player player = mockPlayerWithName(name);
String captcha = "AB923C";
given(registrationCaptchaManager.getCaptchaCodeOrGenerateNew(name)).willReturn(captcha);
// when
command.executeCommand(player, Arrays.asList("myPass", "myPass"));
// then
verify(registrationCaptchaManager).isCaptchaRequired(name);
verify(commonService).send(player, MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED, captcha);
verifyNoInteractions(management, validationService);
}
private static Player mockPlayerWithName(String name) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
return player;
}
}

View File

@ -1,93 +0,0 @@
package fr.xephi.authme.command.executable.totp;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.security.totp.GenerateTotpService;
import fr.xephi.authme.security.totp.TotpAuthenticator.TotpGenerationResult;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Test for {@link AddTotpCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class AddTotpCommandTest {
@InjectMocks
private AddTotpCommand addTotpCommand;
@Mock
private GenerateTotpService generateTotpService;
@Mock
private PlayerCache playerCache;
@Mock
private Messages messages;
@Test
public void shouldHandleNonLoggedInUser() {
// given
Player player = mockPlayerWithName("bob");
given(playerCache.getAuth("bob")).willReturn(null);
// when
addTotpCommand.runCommand(player, Collections.emptyList());
// then
verify(messages).send(player, MessageKey.NOT_LOGGED_IN);
verifyNoInteractions(generateTotpService);
}
@Test
public void shouldNotAddCodeForAlreadyExistingTotp() {
// given
Player player = mockPlayerWithName("arend");
PlayerAuth auth = PlayerAuth.builder().name("arend")
.totpKey("TOTP2345").build();
given(playerCache.getAuth("arend")).willReturn(auth);
// when
addTotpCommand.runCommand(player, Collections.emptyList());
// then
verify(messages).send(player, MessageKey.TWO_FACTOR_ALREADY_ENABLED);
verifyNoInteractions(generateTotpService);
}
@Test
public void shouldGenerateTotpCode() {
// given
Player player = mockPlayerWithName("charles");
PlayerAuth auth = PlayerAuth.builder().name("charles").build();
given(playerCache.getAuth("charles")).willReturn(auth);
TotpGenerationResult generationResult = new TotpGenerationResult(
"777Key214", "http://example.org/qr-code/link");
given(generateTotpService.generateTotpKey(player)).willReturn(generationResult);
// when
addTotpCommand.runCommand(player, Collections.emptyList());
// then
verify(messages).send(player, MessageKey.TWO_FACTOR_CREATE, generationResult.getTotpKey(), generationResult.getAuthenticatorQrCodeUrl());
verify(messages).send(player, MessageKey.TWO_FACTOR_CREATE_CONFIRMATION_REQUIRED);
}
private static Player mockPlayerWithName(String name) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
return player;
}
}

View File

@ -1,158 +0,0 @@
package fr.xephi.authme.command.executable.totp;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.security.totp.GenerateTotpService;
import fr.xephi.authme.security.totp.TotpAuthenticator.TotpGenerationResult;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Test for {@link ConfirmTotpCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ConfirmTotpCommandTest {
@InjectMocks
private ConfirmTotpCommand command;
@Mock
private GenerateTotpService generateTotpService;
@Mock
private DataSource dataSource;
@Mock
private PlayerCache playerCache;
@Mock
private Messages messages;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
}
@Test
public void shouldAddTotpCodeToUserAfterSuccessfulConfirmation() {
// given
Player player = mock(Player.class);
String playerName = "George";
given(player.getName()).willReturn(playerName);
PlayerAuth auth = PlayerAuth.builder().name(playerName).build();
given(playerCache.getAuth(playerName)).willReturn(auth);
String generatedTotpKey = "totp-key";
given(generateTotpService.getGeneratedTotpKey(player)).willReturn(new TotpGenerationResult(generatedTotpKey, "url-not-relevant"));
String totpCode = "954321";
given(generateTotpService.isTotpCodeCorrectForGeneratedTotpKey(player, totpCode)).willReturn(true);
given(dataSource.setTotpKey(anyString(), anyString())).willReturn(true);
// when
command.runCommand(player, Collections.singletonList(totpCode));
// then
verify(generateTotpService).isTotpCodeCorrectForGeneratedTotpKey(player, totpCode);
verify(generateTotpService).removeGenerateTotpKey(player);
verify(dataSource).setTotpKey(playerName, generatedTotpKey);
verify(playerCache).updatePlayer(auth);
verify(messages).send(player, MessageKey.TWO_FACTOR_ENABLE_SUCCESS);
assertThat(auth.getTotpKey(), equalTo(generatedTotpKey));
}
@Test
public void shouldHandleWrongTotpCode() {
// given
Player player = mock(Player.class);
String playerName = "George";
given(player.getName()).willReturn(playerName);
PlayerAuth auth = PlayerAuth.builder().name(playerName).build();
given(playerCache.getAuth(playerName)).willReturn(auth);
given(generateTotpService.getGeneratedTotpKey(player)).willReturn(new TotpGenerationResult("totp-key", "url-not-relevant"));
String totpCode = "754321";
given(generateTotpService.isTotpCodeCorrectForGeneratedTotpKey(player, totpCode)).willReturn(false);
// when
command.runCommand(player, Collections.singletonList(totpCode));
// then
verify(generateTotpService).isTotpCodeCorrectForGeneratedTotpKey(player, totpCode);
verify(generateTotpService, never()).removeGenerateTotpKey(any(Player.class));
verify(playerCache, only()).getAuth(playerName);
verify(messages).send(player, MessageKey.TWO_FACTOR_ENABLE_ERROR_WRONG_CODE);
verifyNoInteractions(dataSource);
}
@Test
public void shouldHandleMissingTotpKey() {
// given
Player player = mock(Player.class);
String playerName = "George";
given(player.getName()).willReturn(playerName);
PlayerAuth auth = PlayerAuth.builder().name(playerName).build();
given(playerCache.getAuth(playerName)).willReturn(auth);
given(generateTotpService.getGeneratedTotpKey(player)).willReturn(null);
// when
command.runCommand(player, Collections.singletonList("871634"));
// then
verify(generateTotpService, only()).getGeneratedTotpKey(player);
verify(playerCache, only()).getAuth(playerName);
verify(messages).send(player, MessageKey.TWO_FACTOR_ENABLE_ERROR_NO_CODE);
verifyNoInteractions(dataSource);
}
@Test
public void shouldStopForAlreadyExistingTotpKeyOnAccount() {
// given
Player player = mock(Player.class);
String playerName = "George";
given(player.getName()).willReturn(playerName);
PlayerAuth auth = PlayerAuth.builder().name(playerName).totpKey("A987234").build();
given(playerCache.getAuth(playerName)).willReturn(auth);
// when
command.runCommand(player, Collections.singletonList("871634"));
// then
verify(playerCache, only()).getAuth(playerName);
verifyNoInteractions(generateTotpService, dataSource);
verify(messages).send(player, MessageKey.TWO_FACTOR_ALREADY_ENABLED);
}
@Test
public void shouldHandleMissingAuthAccount() {
// given
Player player = mock(Player.class);
String playerName = "George";
given(player.getName()).willReturn(playerName);
given(playerCache.getAuth(playerName)).willReturn(null);
// when
command.runCommand(player, Collections.singletonList("984685"));
// then
verify(playerCache, only()).getAuth(playerName);
verifyNoInteractions(generateTotpService, dataSource);
verify(messages).send(player, MessageKey.NOT_LOGGED_IN);
}
}

View File

@ -1,149 +0,0 @@
package fr.xephi.authme.command.executable.totp;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.security.totp.TotpAuthenticator;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Test for {@link RemoveTotpCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class RemoveTotpCommandTest {
@InjectMocks
private RemoveTotpCommand command;
@Mock
private DataSource dataSource;
@Mock
private PlayerCache playerCache;
@Mock
private TotpAuthenticator totpAuthenticator;
@Mock
private Messages messages;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
}
@Test
public void shouldRemoveTotpKey() {
// given
String name = "aws";
PlayerAuth auth = PlayerAuth.builder().name(name).totpKey("some-totp-key").build();
given(playerCache.getAuth(name)).willReturn(auth);
String inputCode = "93847";
given(totpAuthenticator.checkCode(auth, inputCode)).willReturn(true);
given(dataSource.removeTotpKey(name)).willReturn(true);
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
// when
command.runCommand(player, singletonList(inputCode));
// then
verify(dataSource).removeTotpKey(name);
verify(messages, only()).send(player, MessageKey.TWO_FACTOR_REMOVED_SUCCESS);
verify(playerCache).updatePlayer(auth);
assertThat(auth.getTotpKey(), nullValue());
}
@Test
public void shouldHandleDatabaseError() {
// given
String name = "aws";
PlayerAuth auth = PlayerAuth.builder().name(name).totpKey("some-totp-key").build();
given(playerCache.getAuth(name)).willReturn(auth);
String inputCode = "93847";
given(totpAuthenticator.checkCode(auth, inputCode)).willReturn(true);
given(dataSource.removeTotpKey(name)).willReturn(false);
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
// when
command.runCommand(player, singletonList(inputCode));
// then
verify(dataSource).removeTotpKey(name);
verify(messages, only()).send(player, MessageKey.ERROR);
verify(playerCache, only()).getAuth(name);
}
@Test
public void shouldHandleInvalidCode() {
// given
String name = "cesar";
PlayerAuth auth = PlayerAuth.builder().name(name).totpKey("some-totp-key").build();
given(playerCache.getAuth(name)).willReturn(auth);
String inputCode = "93847";
given(totpAuthenticator.checkCode(auth, inputCode)).willReturn(false);
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
// when
command.runCommand(player, singletonList(inputCode));
// then
verifyNoInteractions(dataSource);
verify(messages, only()).send(player, MessageKey.TWO_FACTOR_INVALID_CODE);
verify(playerCache, only()).getAuth(name);
}
@Test
public void shouldHandleUserWithoutTotpKey() {
// given
String name = "cesar";
PlayerAuth auth = PlayerAuth.builder().name(name).build();
given(playerCache.getAuth(name)).willReturn(auth);
String inputCode = "654684";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
// when
command.runCommand(player, singletonList(inputCode));
// then
verifyNoInteractions(dataSource, totpAuthenticator);
verify(messages, only()).send(player, MessageKey.TWO_FACTOR_NOT_ENABLED_ERROR);
verify(playerCache, only()).getAuth(name);
}
@Test
public void shouldHandleNonLoggedInUser() {
// given
String name = "cesar";
given(playerCache.getAuth(name)).willReturn(null);
String inputCode = "654684";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
// when
command.runCommand(player, singletonList(inputCode));
// then
verifyNoInteractions(dataSource, totpAuthenticator);
verify(messages, only()).send(player, MessageKey.NOT_LOGGED_IN);
verify(playerCache, only()).getAuth(name);
}
}

View File

@ -1,47 +0,0 @@
package fr.xephi.authme.command.executable.totp;
import fr.xephi.authme.command.CommandMapper;
import fr.xephi.authme.command.FoundCommandResult;
import fr.xephi.authme.command.help.HelpProvider;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Test for {@link TotpBaseCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class TotpBaseCommandTest {
@InjectMocks
private TotpBaseCommand command;
@Mock
private CommandMapper mapper;
@Mock
private HelpProvider helpProvider;
@Test
public void shouldOutputHelp() {
// given
CommandSender sender = mock(CommandSender.class);
FoundCommandResult mappingResult = mock(FoundCommandResult.class);
given(mapper.mapPartsToCommand(sender, Collections.singletonList("totp"))).willReturn(mappingResult);
// when
command.executeCommand(sender, Collections.emptyList());
// then
verify(mapper).mapPartsToCommand(sender, Collections.singletonList("totp"));
verify(helpProvider).outputHelp(sender, mappingResult, HelpProvider.SHOW_CHILDREN);
}
}

View File

@ -1,122 +0,0 @@
package fr.xephi.authme.command.executable.unregister;
import fr.xephi.authme.data.VerificationCodeManager;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.CommonService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link UnregisterCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class UnregisterCommandTest {
@InjectMocks
private UnregisterCommand command;
@Mock
private Management management;
@Mock
private CommonService commonService;
@Mock
private PlayerCache playerCache;
@Mock
private VerificationCodeManager codeManager;
@Test
public void shouldCatchUnauthenticatedUser() {
// given
String password = "mySecret123";
String name = "player77";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
given(playerCache.isAuthenticated(name)).willReturn(false);
// when
command.executeCommand(player, Collections.singletonList(password));
// then
verify(playerCache).isAuthenticated(name);
verify(commonService).send(player, MessageKey.NOT_LOGGED_IN);
verifyNoInteractions(management);
}
@Test
public void shouldStopForMissingVerificationCode() {
// given
String name = "asldjf";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
given(playerCache.isAuthenticated(name)).willReturn(true);
given(codeManager.isVerificationRequired(player)).willReturn(true);
// when
command.executeCommand(player, Collections.singletonList("blergh"));
// then
verify(playerCache).isAuthenticated(name);
verify(codeManager).codeExistOrGenerateNew(name);
verify(commonService).send(player, MessageKey.VERIFICATION_CODE_REQUIRED);
verifyNoInteractions(management);
}
@Test
public void shouldForwardDataToAsyncTask() {
// given
String password = "p@ssw0rD";
String name = "jas0n_";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
given(playerCache.isAuthenticated(name)).willReturn(true);
given(codeManager.isVerificationRequired(player)).willReturn(false);
// when
command.executeCommand(player, Collections.singletonList(password));
// then
verify(playerCache).isAuthenticated(name);
verify(management).performUnregister(player, password);
verify(codeManager).isVerificationRequired(player);
}
@Test
public void shouldStopIfSenderIsNotPlayer() {
// given
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList("password"));
// then
verifyNoInteractions(playerCache, management);
verify(sender).sendMessage(argThat(containsString("/authme unregister <player>")));
}
@Test
public void shouldDefineArgumentMismatchMessage() {
// given / when / then
assertThat(command.getArgumentsMismatchMessage(), equalTo(MessageKey.USAGE_UNREGISTER));
}
}

View File

@ -1,168 +0,0 @@
package fr.xephi.authme.command.executable.verification;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.VerificationCodeManager;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Test for {@link VerificationCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class VerificationCommandTest {
@InjectMocks
private VerificationCommand command;
@Mock
private CommonService commonService;
@Mock
private VerificationCodeManager codeManager;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
}
@Test
public void shouldDetectIfMailHasASetup() {
// given
String name = "Alligator";
Player player = mockPlayerWithName(name);
given(codeManager.canSendMail()).willReturn(false);
// when
command.executeCommand(player, Collections.singletonList("code"));
// then
verify(commonService).send(player, MessageKey.INCOMPLETE_EMAIL_SETTINGS);
}
@Test
public void shouldRequireAndAcceptCode() {
// given
String name = "Duck";
String code = "123932";
Player player = mockPlayerWithName(name);
given(codeManager.canSendMail()).willReturn(true);
given(codeManager.isVerificationRequired(player)).willReturn(true);
given(codeManager.isCodeRequired(name)).willReturn(true);
given(codeManager.checkCode(name, code)).willReturn(true);
// when
command.executeCommand(player, Collections.singletonList(code));
// then
verify(codeManager).isVerificationRequired(player);
verify(codeManager).isCodeRequired(name);
verify(codeManager).checkCode(name, code);
verify(commonService).send(player, MessageKey.VERIFICATION_CODE_VERIFIED);
}
@Test
public void shouldRejectCode() {
// given
String name = "Spider";
String code = "98345222"; // more than 6 digits
Player player = mockPlayerWithName(name);
given(codeManager.canSendMail()).willReturn(true);
given(codeManager.isVerificationRequired(player)).willReturn(true);
given(codeManager.isCodeRequired(name)).willReturn(true);
given(codeManager.checkCode(name, code)).willReturn(false);
// when
command.executeCommand(player, Collections.singletonList(code));
// then
verify(codeManager).isVerificationRequired(player);
verify(codeManager).isCodeRequired(name);
verify(codeManager).checkCode(name, code);
verify(commonService).send(player, MessageKey.INCORRECT_VERIFICATION_CODE);
}
@Test
public void shouldRejectVerificationDueToExpiration() {
// given
String name = "Dog";
String code = "131552";
Player player = mockPlayerWithName(name);
given(codeManager.canSendMail()).willReturn(true);
given(codeManager.isVerificationRequired(player)).willReturn(true);
given(codeManager.isCodeRequired(name)).willReturn(false);
// when
command.executeCommand(player, Collections.singletonList(code));
// then
verify(codeManager).isVerificationRequired(player);
verify(codeManager).isCodeRequired(name);
verify(commonService).send(player, MessageKey.VERIFICATION_CODE_EXPIRED);
}
@Test
public void shouldRejectVerificationDueToVerifiedIdentity() {
// given
String name = "Cow";
String code = "973583";
Player player = mockPlayerWithName(name);
given(codeManager.canSendMail()).willReturn(true);
given(codeManager.isVerificationRequired(player)).willReturn(false);
given(codeManager.hasEmail(name)).willReturn(true);
// when
command.executeCommand(player, Collections.singletonList(code));
// then
verify(codeManager).isVerificationRequired(player);
verify(codeManager).hasEmail(name);
verify(commonService).send(player, MessageKey.VERIFICATION_CODE_ALREADY_VERIFIED);
}
@Test
public void shouldRejectVerificationDueToUndefinedEmail() {
// given
String name = "Frog";
String code = "774543";
Player player = mockPlayerWithName(name);
given(codeManager.canSendMail()).willReturn(true);
given(codeManager.isVerificationRequired(player)).willReturn(false);
given(codeManager.hasEmail(name)).willReturn(false);
// when
command.executeCommand(player, Collections.singletonList(code));
// then
verify(codeManager).isVerificationRequired(player);
verify(codeManager).hasEmail(name);
verify(commonService).send(player, MessageKey.VERIFICATION_CODE_EMAIL_NEEDED);
verify(commonService).send(player, MessageKey.ADD_EMAIL_MESSAGE);
}
@Test
public void shouldDefineArgumentMismatchMessage() {
// given / when / then
assertThat(command.getArgumentsMismatchMessage(), equalTo(MessageKey.USAGE_VERIFICATION_CODE));
}
private static Player mockPlayerWithName(String name) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
return player;
}
}

View File

@ -1,39 +0,0 @@
package fr.xephi.authme.command.help;
import fr.xephi.authme.util.StringUtils;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
/**
* Test for enums {@link HelpMessage} and {@link HelpSection}.
*/
public class HelpMessageAndHelpSectionConsistencyTest {
@Test
public void shouldHaveUniqueNonEmptyKeys() {
// given
Set<String> keys = new HashSet<>();
// when / then
for (HelpMessage message : HelpMessage.values()) {
assertThat("Key for message '" + message + "' is empty",
StringUtils.isBlank(message.getKey()), equalTo(false));
if (!keys.add(message.getKey())) {
fail("Key for message '" + message + "' is already used elsewhere");
}
}
for (HelpSection section : HelpSection.values()) {
assertThat("Key for section '" + section + "' is empty",
StringUtils.isBlank(section.getKey()), equalTo(false));
if (!keys.add(section.getKey())) {
fail("Key for section '" + section + "' is already used elsewhere");
}
}
}
}

View File

@ -1,97 +0,0 @@
package fr.xephi.authme.command.help;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.CommandInitializer;
import fr.xephi.authme.message.MessagePathHelper;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Test;
import java.io.File;
import java.util.Collection;
import java.util.List;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertThat;
/**
* Tests that /messages/help_en.yml contains texts that correspond
* to the texts provided in the CommandDescription.
*/
public class HelpMessagesConsistencyTest {
private static final File DEFAULT_MESSAGES_FILE =
TestHelper.getJarFile("/" + MessagePathHelper.createHelpMessageFilePath(MessagePathHelper.DEFAULT_LANGUAGE));
@Test
public void shouldHaveIdenticalTexts() {
// given
CommandDescription description = getAuthMeRegisterDescription();
FileConfiguration configuration = YamlConfiguration.loadConfiguration(DEFAULT_MESSAGES_FILE);
final String path = "commands.authme.register.";
// when / then
assertThat(configuration.get(path + "description"), equalTo(description.getDescription()));
assertThat(configuration.get(path + "detailedDescription"), equalTo(description.getDetailedDescription()));
assertThat(configuration.get(path + "arg1.label"), equalTo(description.getArguments().get(0).getName()));
assertThat(configuration.get(path + "arg1.description"), equalTo(description.getArguments().get(0).getDescription()));
assertThat(configuration.get(path + "arg2.label"), equalTo(description.getArguments().get(1).getName()));
assertThat(configuration.get(path + "arg2.description"), equalTo(description.getArguments().get(1).getDescription()));
}
/**
* Since CommandInitializer contains all descriptions for commands in English, the help_en.yml file
* only contains an entry for one command as to provide an example.
*/
@Test
public void shouldOnlyHaveDescriptionForOneCommand() {
// given
FileConfiguration configuration = YamlConfiguration.loadConfiguration(DEFAULT_MESSAGES_FILE);
// when
Object commands = configuration.get("commands");
// then
assertThat(commands, instanceOf(MemorySection.class));
assertThat(((MemorySection) commands).getKeys(false), contains("authme"));
}
@Test
public void shouldHaveEntryForEachHelpMessageKey() {
// given
PropertyReader reader = new YamlFileReader(DEFAULT_MESSAGES_FILE);
// when / then
for (HelpMessage message : HelpMessage.values()) {
assertThat("Default configuration should have entry for message '" + message + "'",
reader.contains(message.getKey()), equalTo(true));
}
for (HelpSection section : HelpSection.values()) {
assertThat("Default configuration should have entry for section '" + section + "'",
reader.contains(section.getKey()), equalTo(true));
}
}
/**
* @return the CommandDescription object for the {@code /authme register} command.
*/
private static CommandDescription getAuthMeRegisterDescription() {
Collection<CommandDescription> commands = new CommandInitializer().getCommands();
List<CommandDescription> children = commands.stream()
.filter(command -> command.getLabels().contains("authme"))
.map(CommandDescription::getChildren)
.findFirst().get();
return children
.stream()
.filter(child -> child.getLabels().contains("register"))
.findFirst().get();
}
}

View File

@ -1,160 +0,0 @@
package fr.xephi.authme.command.help;
import com.google.common.io.Files;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.TestCommandsUtil;
import fr.xephi.authme.message.AbstractMessageFileHandler;
import fr.xephi.authme.message.HelpMessagesFileHandler;
import fr.xephi.authme.message.MessagePathHelper;
import fr.xephi.authme.permission.DefaultPermission;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import static fr.xephi.authme.command.TestCommandsUtil.getCommandWithLabel;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Test for {@link HelpMessagesService}.
*/
public class HelpMessagesServiceTest {
private static final String TEST_FILE = TestHelper.PROJECT_ROOT + "command/help/help_test.yml";
private static final Collection<CommandDescription> COMMANDS = TestCommandsUtil.generateCommands();
private HelpMessagesService helpMessagesService;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private File dataFolder;
@Before
public void initializeHandler() throws IOException {
dataFolder = temporaryFolder.newFolder();
new File(dataFolder, "messages").mkdirs();
File messagesFile = new File(dataFolder, MessagePathHelper.createHelpMessageFilePath("test"));
Files.copy(TestHelper.getJarFile(TEST_FILE), messagesFile);
HelpMessagesFileHandler helpMessagesFileHandler = createMessagesFileHandler();
helpMessagesService = new HelpMessagesService(helpMessagesFileHandler);
}
@Test
public void shouldReturnLocalizedCommand() {
// given
CommandDescription command = getCommandWithLabel(COMMANDS, "authme", "register");
// when
CommandDescription localCommand = helpMessagesService.buildLocalizedDescription(command);
// then
assertThat(localCommand.getDescription(), equalTo("Registration"));
assertThat(localCommand.getDetailedDescription(), equalTo("Registers the player"));
assertThat(localCommand.getExecutableCommand(), equalTo(command.getExecutableCommand()));
assertThat(localCommand.getPermission(), equalTo(command.getPermission()));
assertThat(localCommand.getArguments(), hasSize(2));
assertThat(localCommand.getArguments().get(0).getName(), equalTo("password"));
assertThat(localCommand.getArguments().get(0).getDescription(), equalTo("The password"));
assertThat(localCommand.getArguments().get(1).getName(), equalTo("confirmation"));
assertThat(localCommand.getArguments().get(1).getDescription(), equalTo("The confirmation"));
}
@Test
public void shouldReturnLocalizedCommandWithDefaults() {
// given
CommandDescription command = getCommandWithLabel(COMMANDS, "authme", "login");
// when
CommandDescription localCommand = helpMessagesService.buildLocalizedDescription(command);
// then
assertThat(localCommand.getDescription(), equalTo("Logging in"));
assertThat(localCommand.getDetailedDescription(), equalTo("'login' test command"));
assertThat(localCommand.getArguments(), hasSize(1));
assertThat(localCommand.getArguments().get(0).getName(), equalTo("user password"));
assertThat(localCommand.getArguments().get(0).getDescription(), equalTo("'password' argument description"));
}
@Test
public void shouldReturnSameCommandForNoLocalization() {
// given
CommandDescription command = getCommandWithLabel(COMMANDS, "email");
// when
CommandDescription localCommand = helpMessagesService.buildLocalizedDescription(command);
// then
assertThat(localCommand, sameInstance(command));
}
@Test
public void shouldKeepChildrenInLocalCommand() {
// given
CommandDescription command = getCommandWithLabel(COMMANDS, "authme");
// when
CommandDescription localCommand = helpMessagesService.buildLocalizedDescription(command);
// then
assertThat(localCommand.getChildren(), equalTo(command.getChildren()));
assertThat(localCommand.getDescription(), equalTo("authme cmd"));
assertThat(localCommand.getDetailedDescription(), equalTo("Main command"));
}
@Test
public void shouldGetTranslationsForSectionAndMessage() {
// given / when / then
assertThat(helpMessagesService.getMessage(DefaultPermission.OP_ONLY), equalTo("only op"));
assertThat(helpMessagesService.getMessage(HelpMessage.RESULT), equalTo("res."));
assertThat(helpMessagesService.getMessage(HelpSection.ARGUMENTS), equalTo("arg."));
}
@Test
public void shouldGetLocalCommandDescription() {
// given
CommandDescription command = getCommandWithLabel(COMMANDS, "authme", "register");
// when
String description = helpMessagesService.getDescription(command);
// then
assertThat(description, equalTo("Registration"));
}
@Test
public void shouldFallbackToDescriptionOnCommandObject() {
// given
CommandDescription command = getCommandWithLabel(COMMANDS, "unregister");
// when
String description = helpMessagesService.getDescription(command);
// then
assertThat(description, equalTo(command.getDescription()));
}
private HelpMessagesFileHandler createMessagesFileHandler() {
Settings settings = mock(Settings.class);
given(settings.getProperty(PluginSettings.MESSAGES_LANGUAGE)).willReturn("test");
HelpMessagesFileHandler messagesFileHandler = ReflectionTestUtils.newInstance(HelpMessagesFileHandler.class);
ReflectionTestUtils.setField(AbstractMessageFileHandler.class, messagesFileHandler, "settings", settings);
ReflectionTestUtils.setField(AbstractMessageFileHandler.class, messagesFileHandler, "dataFolder", dataFolder);
ReflectionTestUtils.invokePostConstructMethods(messagesFileHandler);
return messagesFileHandler;
}
}

View File

@ -1,466 +0,0 @@
package fr.xephi.authme.command.help;
import ch.jalu.injector.testing.BeforeInjecting;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.FoundCommandResult;
import fr.xephi.authme.command.FoundResultStatus;
import fr.xephi.authme.command.TestCommandsUtil;
import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.DefaultPermission;
import fr.xephi.authme.permission.PermissionsManager;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.internal.stubbing.answers.ReturnsArgumentAt;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import static fr.xephi.authme.command.TestCommandsUtil.getCommandWithLabel;
import static fr.xephi.authme.command.help.HelpProvider.ALL_OPTIONS;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_ALTERNATIVES;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_ARGUMENTS;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_CHILDREN;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_COMMAND;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_DESCRIPTION;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_LONG_DESCRIPTION;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_PERMISSIONS;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
/**
* Test for {@link HelpProvider}.
*/
@RunWith(DelayedInjectionRunner.class)
public class HelpProviderTest {
private static Collection<CommandDescription> commands;
@InjectDelayed
private HelpProvider helpProvider;
@Mock
private PermissionsManager permissionsManager;
@Mock
private HelpMessagesService helpMessagesService;
@Mock
private CommandSender sender;
@BeforeClass
public static void setUpCommands() {
commands = TestCommandsUtil.generateCommands();
}
@BeforeInjecting
public void setInitialSettings() {
setDefaultHelpMessages(helpMessagesService);
}
@Test
public void shouldShowLongDescription() {
// given
CommandDescription command = getCommandWithLabel(commands, "authme", "login");
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "login"));
// when
helpProvider.outputHelp(sender, result, SHOW_COMMAND | SHOW_LONG_DESCRIPTION | SHOW_DESCRIPTION);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(5));
assertThat(lines.get(0), containsString("Header"));
assertThat(lines.get(1), containsString("Command: /authme login <password>"));
assertThat(lines.get(2), containsString("Short description: login cmd"));
assertThat(lines.get(3), equalTo("Detailed description:"));
assertThat(lines.get(4), containsString("'login' test command"));
}
@Test
public void shouldShowArguments() {
// given
CommandDescription command = getCommandWithLabel(commands, "authme", "register");
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "reg"));
// when
helpProvider.outputHelp(sender, result, SHOW_ARGUMENTS);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(4));
assertThat(lines.get(0), containsString("Header"));
assertThat(lines.get(1), equalTo("Arguments:"));
assertThat(lines.get(2), containsString("password: 'password' argument description"));
assertThat(lines.get(3), containsString("confirmation: 'confirmation' argument description"));
}
@Test
public void shouldShowSpecifyIfArgumentIsOptional() {
// given
CommandDescription command = getCommandWithLabel(commands, "email");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("email"));
// when
helpProvider.outputHelp(sender, result, SHOW_ARGUMENTS);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(3));
assertThat(lines.get(2), containsString("player: 'player' argument description (Optional)"));
}
/** Verifies that the "Arguments:" line is not shown if the command has no arguments. */
@Test
public void shouldNotShowAnythingIfCommandHasNoArguments() {
// given
CommandDescription command = getCommandWithLabel(commands, "authme");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
// when
helpProvider.outputHelp(sender, result, SHOW_ARGUMENTS);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(1)); // only has the help banner
}
@Test
public void shouldShowAndEvaluatePermissions() {
// given
CommandDescription command = getCommandWithLabel(commands, "unregister");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("unreg"));
given(sender.isOp()).willReturn(true);
given(permissionsManager.hasPermission(sender, AdminPermission.UNREGISTER)).willReturn(true);
given(permissionsManager.hasPermission(sender, command.getPermission())).willReturn(true);
// when
helpProvider.outputHelp(sender, result, SHOW_PERMISSIONS);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(5));
assertThat(lines.get(1), containsString("Permissions:"));
assertThat(lines.get(2),
containsString(AdminPermission.UNREGISTER.getNode() + " (Has permission)"));
assertThat(lines.get(3), containsString("Default: Op only (Has permission)"));
assertThat(lines.get(4), containsString("Result: Has permission"));
}
@Test
public void shouldShowAndEvaluateForbiddenPermissions() {
// given
CommandDescription command = getCommandWithLabel(commands, "unregister");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("unregister"));
given(sender.isOp()).willReturn(false);
given(permissionsManager.hasPermission(sender, AdminPermission.UNREGISTER)).willReturn(false);
given(permissionsManager.hasPermission(sender, command.getPermission())).willReturn(false);
// when
helpProvider.outputHelp(sender, result, SHOW_PERMISSIONS);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(5));
assertThat(lines.get(1), containsString("Permissions:"));
assertThat(lines.get(2),
containsString(AdminPermission.UNREGISTER.getNode() + " (No permission)"));
assertThat(lines.get(3), containsString("Default: Op only (No permission)"));
assertThat(lines.get(4), containsString("Result: No permission"));
}
@Test
public void shouldNotShowAnythingForEmptyPermissions() {
// given
CommandDescription command = getCommandWithLabel(commands, "authme");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
// when
helpProvider.outputHelp(sender, result, SHOW_PERMISSIONS);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(1));
}
@Test
public void shouldNotShowAnythingForNullPermissionsOnCommand() {
// given
CommandDescription command = mock(CommandDescription.class);
given(command.getPermission()).willReturn(null);
given(command.getLabels()).willReturn(Collections.singletonList("test"));
FoundCommandResult result = newFoundResult(command, Collections.singletonList("test"));
// when
helpProvider.outputHelp(sender, result, SHOW_PERMISSIONS);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(1));
}
@Test
public void shouldShowAlternatives() {
// given
CommandDescription command = getCommandWithLabel(commands, "authme", "register");
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "reg"));
// when
helpProvider.outputHelp(sender, result, SHOW_ALTERNATIVES);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(4));
assertThat(lines.get(1), containsString("Alternatives:"));
assertThat(lines.get(2), containsString("/authme register <password> <confirmation>"));
assertThat(lines.get(3), containsString("/authme r <password> <confirmation>"));
}
@Test
public void shouldNotShowAnythingIfHasNoAlternatives() {
// given
CommandDescription command = getCommandWithLabel(commands, "authme", "login");
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "login"));
// when
helpProvider.outputHelp(sender, result, SHOW_ALTERNATIVES);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(1));
}
@Test
public void shouldShowChildren() {
// given
CommandDescription command = getCommandWithLabel(commands, "authme");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
given(helpMessagesService.getDescription(getCommandWithLabel(commands, "authme", "login")))
.willReturn("Command for login [localized]");
given(helpMessagesService.getDescription(getCommandWithLabel(commands, "authme", "register")))
.willReturn("Registration command [localized]");
// when
helpProvider.outputHelp(sender, result, SHOW_CHILDREN);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(4));
assertThat(lines.get(1), equalTo("Children:"));
assertThat(lines.get(2), equalTo(" /authme login: Command for login [localized]"));
assertThat(lines.get(3), equalTo(" /authme register: Registration command [localized]"));
}
@Test
public void shouldNotShowCommandsTitleForCommandWithNoChildren() {
// given
CommandDescription command = getCommandWithLabel(commands, "authme", "register");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
// when
helpProvider.outputHelp(sender, result, SHOW_CHILDREN);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(1));
}
@Test
public void shouldHandleUnboundFoundCommandResult() {
// given
FoundCommandResult result = new FoundCommandResult(null, Arrays.asList("authme", "test"),
Collections.emptyList(), 0.0, FoundResultStatus.UNKNOWN_LABEL);
// when
helpProvider.outputHelp(sender, result, ALL_OPTIONS);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(1));
assertThat(lines.get(0), containsString("Failed to retrieve any help information"));
}
/**
* Since command parts may be mapped to a command description with labels that don't completely correspond to it,
* (e.g. suggest "register command" for /authme ragister), we need to check the labels and construct a correct list
*/
@Test
public void shouldShowCommandSyntaxWithCorrectLabels() {
// given
CommandDescription command = getCommandWithLabel(commands, "authme", "register");
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "ragister"));
// when
helpProvider.outputHelp(sender, result, SHOW_COMMAND);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(2));
assertThat(lines.get(0), containsString("Header"));
assertThat(lines.get(1), containsString("Command: /authme register <password> <confirmation>"));
}
@Test
public void shouldRetainCorrectLabels() {
// given
List<String> labels = Arrays.asList("authme", "reg");
CommandDescription command = getCommandWithLabel(commands, "authme", "register");
// when
List<String> result = HelpProvider.filterCorrectLabels(command, labels);
// then
assertThat(result, equalTo(labels));
}
@Test
public void shouldReplaceIncorrectLabels() {
// given
List<String> labels = Arrays.asList("authme", "wrong");
CommandDescription command = getCommandWithLabel(commands, "authme", "register");
// when
List<String> result = HelpProvider.filterCorrectLabels(command, labels);
// then
assertThat(result, contains("authme", "register"));
}
@Test
public void shouldDisableSectionsWithEmptyTranslations() {
// given
given(helpMessagesService.getMessage(HelpSection.DETAILED_DESCRIPTION)).willReturn("");
given(helpMessagesService.getMessage(HelpSection.ALTERNATIVES)).willReturn("");
given(helpMessagesService.getMessage(HelpSection.PERMISSIONS)).willReturn("");
CommandDescription command = getCommandWithLabel(commands, "authme", "register");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
// when
helpProvider.outputHelp(sender, result, ALL_OPTIONS);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(6));
assertThat(lines.get(0), equalTo("Header"));
assertThat(lines.get(1), equalTo("Command: /authme register <password> <confirmation>"));
assertThat(lines.get(2), equalTo("Short description: register cmd"));
assertThat(lines.get(3), equalTo("Arguments:"));
assertThat(lines.get(4), containsString("'password' argument description"));
assertThat(lines.get(5), containsString("'confirmation' argument description"));
}
@Test
public void shouldNotReturnAnythingForAllDisabledSections() {
// given
given(helpMessagesService.getMessage(HelpSection.COMMAND)).willReturn("");
given(helpMessagesService.getMessage(HelpSection.ALTERNATIVES)).willReturn("");
given(helpMessagesService.getMessage(HelpSection.PERMISSIONS)).willReturn("");
CommandDescription command = getCommandWithLabel(commands, "authme", "register");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
// when
helpProvider.outputHelp(sender, result, SHOW_COMMAND | SHOW_ALTERNATIVES | SHOW_PERMISSIONS);
// then
verify(sender, never()).sendMessage(anyString());
}
@Test
public void shouldSkipEmptyHeader() {
// given
given(helpMessagesService.getMessage(HelpMessage.HEADER)).willReturn("");
CommandDescription command = getCommandWithLabel(commands, "authme", "register");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
// when
helpProvider.outputHelp(sender, result, SHOW_COMMAND);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(1));
assertThat(lines.get(0), equalTo("Command: /authme register <password> <confirmation>"));
}
@Test
public void shouldShowAlternativesForRootCommand() {
// given
CommandDescription command = getCommandWithLabel(commands, "unregister");
FoundCommandResult result = newFoundResult(command, Collections.singletonList("unreg"));
// when
helpProvider.outputHelp(sender, result, SHOW_COMMAND | SHOW_ALTERNATIVES);
// then
List<String> lines = getLines(sender);
assertThat(lines, hasSize(4));
assertThat(lines.get(0), equalTo("Header"));
assertThat(lines.get(1), equalTo("Command: /unreg <player>"));
assertThat(lines.get(2), equalTo("Alternatives:"));
assertThat(lines.get(3), equalTo(" /unregister <player>"));
}
/**
* Generate an instance of {@link FoundCommandResult} with the given command and labels. All other fields aren't
* retrieved by {@link HelpProvider} and so are initialized to default values for the tests.
*
* @param command The command description
* @param labels The labels of the command (as in a real use case, they do not have to be correct)
* @return The generated FoundCommandResult object
*/
private static FoundCommandResult newFoundResult(CommandDescription command, List<String> labels) {
return new FoundCommandResult(command, labels, Collections.emptyList(), 0.0, FoundResultStatus.SUCCESS);
}
private static String removeColors(String str) {
for (ChatColor color : ChatColor.values()) {
str = str.replace(color.toString(), "");
}
return str;
}
private static List<String> getLines(CommandSender sender) {
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender, atLeastOnce()).sendMessage(captor.capture());
return captor.getAllValues().stream().map(s -> removeColors(s)).collect(Collectors.toList());
}
private static void setDefaultHelpMessages(HelpMessagesService helpMessagesService) {
given(helpMessagesService.buildLocalizedDescription(any(CommandDescription.class)))
.willAnswer(new ReturnsArgumentAt(0));
for (HelpMessage key : HelpMessage.values()) {
String text = key.name().replace("_", " ").toLowerCase(Locale.ROOT);
given(helpMessagesService.getMessage(key))
.willReturn(text.substring(0, 1).toUpperCase(Locale.ROOT) + text.substring(1));
}
for (DefaultPermission permission : DefaultPermission.values()) {
String text = permission.name().replace("_", " ").toLowerCase(Locale.ROOT);
given(helpMessagesService.getMessage(permission))
.willReturn(text.substring(0, 1).toUpperCase(Locale.ROOT) + text.substring(1));
}
for (HelpSection section : HelpSection.values()) {
String text = section.name().replace("_", " ").toLowerCase(Locale.ROOT);
given(helpMessagesService.getMessage(section))
.willReturn(text.substring(0, 1).toUpperCase(Locale.ROOT) + text.substring(1));
}
}
}

View File

@ -1,80 +0,0 @@
package fr.xephi.authme.data;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ProtectionSettings;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Test for {@link QuickCommandsProtectionManager}.
*/
@RunWith(MockitoJUnitRunner.class)
public class QuickCommandsProtectionManagerTest {
@Mock
private Settings settings;
@Mock
private PermissionsManager permissionsManager;
@Test
public void shouldAllowCommand() {
// given
String playername = "PlayerName";
Player player = mockPlayerWithName(playername);
given(settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS)).willReturn(0);
given(permissionsManager.hasPermission(player, PlayerPermission.QUICK_COMMANDS_PROTECTION)).willReturn(true);
String name = "TestName";
QuickCommandsProtectionManager qcpm = createQuickCommandsProtectioneManager();
qcpm.processJoin(player);
// when
boolean test1 = qcpm.isAllowed(name);
boolean test2 = qcpm.isAllowed(playername);
// then
assertThat(test1, equalTo(true));
assertThat(test2, equalTo(true));
}
@Test
public void shouldDenyCommand() {
// given
String name = "TestName1";
Player player = mockPlayerWithName(name);
given(settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS)).willReturn(5000);
QuickCommandsProtectionManager qcpm = createQuickCommandsProtectioneManager();
given(permissionsManager.hasPermission(player, PlayerPermission.QUICK_COMMANDS_PROTECTION)).willReturn(true);
qcpm.processJoin(player);
// when
boolean test = qcpm.isAllowed(name);
// then
assertThat(test, equalTo(false));
}
private QuickCommandsProtectionManager createQuickCommandsProtectioneManager() {
return new QuickCommandsProtectionManager(settings, permissionsManager);
}
private static Player mockPlayerWithName(String name) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
return player;
}
}

View File

@ -1,265 +0,0 @@
package fr.xephi.authme.data;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.expiring.TimedCounter;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTask;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Test for {@link TempbanManager}.
*/
@RunWith(MockitoJUnitRunner.class)
public class TempbanManagerTest {
private static final long DATE_TOLERANCE_MILLISECONDS = 200L;
private static final long TEST_EXPIRATION_THRESHOLD = 120_000L;
@Mock
private BukkitService bukkitService;
@Mock
private Messages messages;
@Test
public void shouldAddCounts() {
// given
Settings settings = mockSettings(3, 60, "");
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
String address = "192.168.1.1";
// when
manager.increaseCount(address, "Bob");
manager.increaseCount(address, "Todd");
// then
assertThat(manager.shouldTempban(address), equalTo(false));
assertHasCount(manager, address, "Bob", 1);
assertHasCount(manager, address, "Todd", 1);
manager.increaseCount(address, "Bob");
assertThat(manager.shouldTempban(address), equalTo(true));
assertThat(manager.shouldTempban("10.0.0.1"), equalTo(false));
}
@Test
public void shouldIncreaseAndResetCount() {
// given
String address = "192.168.1.2";
Settings settings = mockSettings(3, 60, "");
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
// when
manager.increaseCount(address, "test");
manager.increaseCount(address, "test");
manager.increaseCount(address, "test");
// then
assertThat(manager.shouldTempban(address), equalTo(true));
assertHasCount(manager, address, "test", 3);
// when 2
manager.resetCount(address, "test");
// then 2
assertThat(manager.shouldTempban(address), equalTo(false));
assertHasNoEntries(manager, address);
}
@Test
public void shouldNotIncreaseCountForDisabledTempban() {
// given
String address = "192.168.1.3";
Settings settings = mockSettings(1, 5, "");
given(settings.getProperty(SecuritySettings.TEMPBAN_ON_MAX_LOGINS)).willReturn(false);
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
// when
manager.increaseCount(address, "username");
// then
assertThat(manager.shouldTempban(address), equalTo(false));
assertHasNoEntries(manager, address);
}
@Test
public void shouldNotCheckCountIfTempbanIsDisabled() {
// given
String address = "192.168.1.4";
Settings settings = mockSettings(1, 5, "");
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
given(settings.getProperty(SecuritySettings.TEMPBAN_ON_MAX_LOGINS)).willReturn(false);
// when
manager.increaseCount(address, "username");
// assumptions
assertThat(manager.shouldTempban(address), equalTo(true));
assertHasCount(manager, address, "username", 1);
// end assumptions
manager.reload(settings);
boolean result = manager.shouldTempban(address);
// then
assertThat(result, equalTo(false));
}
@Test
public void shouldNotIssueBanIfDisabled() {
// given
Settings settings = mockSettings(0, 0, "");
given(settings.getProperty(SecuritySettings.TEMPBAN_ON_MAX_LOGINS)).willReturn(false);
Player player = mock(Player.class);
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
// when
manager.tempbanPlayer(player);
// then
verifyNoInteractions(player, bukkitService);
}
@Test
public void shouldBanPlayerIp() {
// given
Player player = mock(Player.class);
String ip = "123.45.67.89";
TestHelper.mockIpAddressToPlayer(player, ip);
String banReason = "IP ban too many logins";
given(messages.retrieveSingle(player, MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason);
Settings settings = mockSettings(2, 100, "");
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
setBukkitServiceToScheduleSyncDelayedTask(bukkitService);
// when
manager.tempbanPlayer(player);
// then
verify(player).kickPlayer(banReason);
ArgumentCaptor<Date> captor = ArgumentCaptor.forClass(Date.class);
verify(bukkitService).banIp(eq(ip), eq(banReason), captor.capture(), eq("AuthMe"));
// Compute the expected expiration date and check that the actual date is within the difference tolerance
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 100);
long expectedExpiration = cal.getTime().getTime();
assertThat(Math.abs(captor.getValue().getTime() - expectedExpiration), lessThan(DATE_TOLERANCE_MILLISECONDS));
}
@Test
public void shouldBanPlayerIpCustom() {
// given
Player player = mock(Player.class);
given(player.getName()).willReturn("Bob");
String ip = "143.45.77.89";
TestHelper.mockIpAddressToPlayer(player, ip);
String banCommand = "banip %ip% 15d IP ban too many logins";
Settings settings = mockSettings(2, 100, banCommand);
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
setBukkitServiceToScheduleSyncDelayedTask(bukkitService);
// when
manager.tempbanPlayer(player);
// then
verify(bukkitService).dispatchConsoleCommand(banCommand.replace("%ip%", ip));
}
@Test
public void shouldResetCountAfterBan() {
// given
Player player = mock(Player.class);
String ip = "22.44.66.88";
TestHelper.mockIpAddressToPlayer(player, ip);
String banReason = "kick msg";
given(messages.retrieveSingle(player, MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason);
Settings settings = mockSettings(10, 60, "");
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
manager.increaseCount(ip, "user");
manager.increaseCount(ip, "name2");
manager.increaseCount(ip, "user");
setBukkitServiceToScheduleSyncDelayedTask(bukkitService);
// when
manager.tempbanPlayer(player);
// then
verify(player).kickPlayer(banReason);
assertHasNoEntries(manager, ip);
}
@Test
public void shouldPerformCleanup() {
// given
Map<String, TimedCounter<String>> counts = new HashMap<>();
TimedCounter<String> counter1 = mockCounter();
given(counter1.isEmpty()).willReturn(true);
counts.put("11.11.11.11", counter1);
TimedCounter<String> counter2 = mockCounter();
given(counter2.isEmpty()).willReturn(false);
counts.put("33.33.33.33", counter2);
TempbanManager manager = new TempbanManager(bukkitService, messages, mockSettings(3, 10, ""));
ReflectionTestUtils.setField(TempbanManager.class, manager, "ipLoginFailureCounts", counts);
// when
manager.performCleanup();
// then
verify(counter1).removeExpiredEntries();
verify(counter2).removeExpiredEntries();
assertThat(counts.keySet(), contains("33.33.33.33"));
}
private static Settings mockSettings(int maxTries, int tempbanLength, String customCommand) {
Settings settings = mock(Settings.class);
given(settings.getProperty(SecuritySettings.TEMPBAN_ON_MAX_LOGINS)).willReturn(true);
given(settings.getProperty(SecuritySettings.MAX_LOGIN_TEMPBAN)).willReturn(maxTries);
given(settings.getProperty(SecuritySettings.TEMPBAN_LENGTH)).willReturn(tempbanLength);
given(settings.getProperty(SecuritySettings.TEMPBAN_MINUTES_BEFORE_RESET))
.willReturn((int) TEST_EXPIRATION_THRESHOLD / 60_000);
given(settings.getProperty(SecuritySettings.TEMPBAN_CUSTOM_COMMAND)).willReturn(customCommand);
return settings;
}
private static void assertHasNoEntries(TempbanManager manager, String address) {
Map<String, TimedCounter<String>> playerCounts = ReflectionTestUtils
.getFieldValue(TempbanManager.class, manager, "ipLoginFailureCounts");
TimedCounter<String> counter = playerCounts.get(address);
assertThat(counter == null || counter.isEmpty(), equalTo(true));
}
private static void assertHasCount(TempbanManager manager, String address, String name, int count) {
Map<String, TimedCounter<String>> playerCounts = ReflectionTestUtils
.getFieldValue(TempbanManager.class, manager, "ipLoginFailureCounts");
assertThat(playerCounts.get(address).get(name), equalTo(count));
}
@SuppressWarnings("unchecked")
private static <T> TimedCounter<T> mockCounter() {
return mock(TimedCounter.class);
}
}

View File

@ -1,171 +0,0 @@
package fr.xephi.authme.data;
import ch.jalu.datasourcecolumns.data.DataSourceValueImpl;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.mail.EmailService;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Test for {@link VerificationCodeManager}.
*/
@RunWith(MockitoJUnitRunner.class)
public class VerificationCodeManagerTest {
@Mock
private Settings settings;
@Mock
private DataSource dataSource;
@Mock
private EmailService emailService;
@Mock
private PermissionsManager permissionsManager;
@Before
public void setUpBasicBehavior() {
given(emailService.hasAllInformation()).willReturn(true);
given(settings.getProperty(SecuritySettings.VERIFICATION_CODE_EXPIRATION_MINUTES)).willReturn(1);
}
@Test
public void shouldRequireVerification() {
// given
String name1 = "ILoveTests";
Player player1 = mockPlayerWithName(name1);
given(dataSource.getEmail(name1)).willReturn(DataSourceValueImpl.of("ilovetests@test.com"));
given(permissionsManager.hasPermission(player1, PlayerPermission.VERIFICATION_CODE)).willReturn(true);
String name2 = "StillLovingTests";
Player player2 = mockPlayerWithName(name2);
VerificationCodeManager codeManager = createCodeManager();
codeManager.verify(name2);
// when
boolean test1 = codeManager.isVerificationRequired(player1);
boolean test2 = codeManager.isVerificationRequired(player2);
// then
assertThat(test1, equalTo(true));
assertThat(test2, equalTo(false));
verify(dataSource, only()).getEmail(name1);
verify(permissionsManager, only()).hasPermission(player1, PlayerPermission.VERIFICATION_CODE);
}
@Test
public void shouldNotRequireVerificationIfEmailSettingsAreIncomplete() {
// given
given(emailService.hasAllInformation()).willReturn(false);
VerificationCodeManager codeManager = createCodeManager();
Player player = mock(Player.class);
// when
boolean result = codeManager.isVerificationRequired(player);
// then
assertThat(result, equalTo(false));
verifyNoInteractions(permissionsManager, dataSource);
}
@Test
public void shouldNotRequireVerificationForMissingPermission() {
// given
Player player = mockPlayerWithName("ILoveTests");
given(permissionsManager.hasPermission(player, PlayerPermission.VERIFICATION_CODE)).willReturn(false);
VerificationCodeManager codeManager = createCodeManager();
// when
boolean result = codeManager.isVerificationRequired(player);
// then
assertThat(result, equalTo(false));
verify(permissionsManager).hasPermission(player, PlayerPermission.VERIFICATION_CODE);
verifyNoInteractions(dataSource);
}
@Test
public void shouldGenerateCode() {
// given
String player = "ILoveTests";
String email = "ilovetests@test.com";
given(dataSource.getEmail(player)).willReturn(DataSourceValueImpl.of(email));
VerificationCodeManager codeManager1 = createCodeManager();
VerificationCodeManager codeManager2 = createCodeManager();
codeManager2.codeExistOrGenerateNew(player);
// when
boolean test1 = codeManager1.hasCode(player);
boolean test2 = codeManager2.hasCode(player);
// then
assertThat(test1, equalTo(false));
assertThat(test2, equalTo(true));
}
@Test
public void shouldRequireCode() {
// given
String player = "ILoveTests";
String email = "ilovetests@test.com";
given(dataSource.getEmail(player)).willReturn(DataSourceValueImpl.of(email));
VerificationCodeManager codeManager1 = createCodeManager();
VerificationCodeManager codeManager2 = createCodeManager();
codeManager2.codeExistOrGenerateNew(player);
// when
boolean test1 = codeManager1.isCodeRequired(player);
boolean test2 = codeManager2.isCodeRequired(player);
// then
assertThat(test1, equalTo(false));
assertThat(test2, equalTo(true));
}
@Test
public void shouldVerifyCode() {
// given
String player = "ILoveTests";
String code = "193458";
String email = "ilovetests@test.com";
given(dataSource.getEmail(player)).willReturn(DataSourceValueImpl.of(email));
VerificationCodeManager codeManager1 = createCodeManager();
VerificationCodeManager codeManager2 = createCodeManager();
codeManager1.codeExistOrGenerateNew(player);
// when
boolean test1 = codeManager1.checkCode(player, code);
boolean test2 = codeManager2.checkCode(player, code);
// then
assertThat(test1, equalTo(false));
assertThat(test2, equalTo(false));
}
private VerificationCodeManager createCodeManager() {
return new VerificationCodeManager(settings, dataSource, emailService, permissionsManager);
}
private static Player mockPlayerWithName(String name) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
return player;
}
}

View File

@ -1,69 +0,0 @@
package fr.xephi.authme.data.auth;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.fail;
/**
* Test for {@link PlayerAuth} and its builder.
*/
public class PlayerAuthTest {
@Test
public void shouldRemoveDatabaseDefaults() {
// given / when
PlayerAuth auth = PlayerAuth.builder()
.name("Bobby")
.lastLogin(0L)
.lastIp("127.0.0.1")
.email("your@email.com")
.build();
// then
assertThat(auth.getNickname(), equalTo("bobby"));
assertThat(auth.getLastLogin(), nullValue());
// Note ljacqu 20171020: Although 127.0.0.1 is the default value, we need to keep it because it might
// legitimately be the resolved IP of a player
assertThat(auth.getLastIp(), equalTo("127.0.0.1"));
assertThat(auth.getEmail(), nullValue());
}
@Test
public void shouldThrowForMissingName() {
try {
// given / when
PlayerAuth.builder()
.email("test@example.org")
.groupId(3)
.build();
// then
fail("Expected exception to be thrown");
} catch (NullPointerException e) {
// all good
}
}
@Test
public void shouldCreatePlayerAuthWithNullValues() {
// given / when
PlayerAuth auth = PlayerAuth.builder()
.name("Charlie")
.email(null)
.lastLogin(null)
.lastIp(null)
.groupId(19)
.locPitch(123.004f)
.build();
// then
assertThat(auth.getEmail(), nullValue());
assertThat(auth.getLastLogin(), nullValue());
assertThat(auth.getLastIp(), nullValue());
assertThat(auth.getGroupId(), equalTo(19));
assertThat(auth.getPitch(), equalTo(123.004f));
}
}

View File

@ -1,174 +0,0 @@
package fr.xephi.authme.data.captcha;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.expiring.TimedCounter;
import org.bukkit.entity.Player;
import org.junit.Test;
import java.util.Locale;
import static fr.xephi.authme.AuthMeMatchers.stringWithLength;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Test for {@link LoginCaptchaManager}.
*/
public class LoginCaptchaManagerTest {
@Test
public void shouldAddCounts() {
// given
Settings settings = mockSettings(3, 4);
LoginCaptchaManager manager = new LoginCaptchaManager(settings);
String player = "tester";
// when
for (int i = 0; i < 2; ++i) {
manager.increaseLoginFailureCount(player);
}
// then
assertThat(manager.isCaptchaRequired(player), equalTo(false));
manager.increaseLoginFailureCount(player);
assertThat(manager.isCaptchaRequired(player.toUpperCase(Locale.ROOT)), equalTo(true));
assertThat(manager.isCaptchaRequired("otherPlayer"), equalTo(false));
}
@Test
public void shouldCreateAndCheckCaptcha() {
// given
String name = "Miner";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
Settings settings = mockSettings(1, 4);
LoginCaptchaManager manager = new LoginCaptchaManager(settings);
String captchaCode = manager.getCaptchaCodeOrGenerateNew(name);
// when
boolean result = manager.checkCode(player, captchaCode);
// then
assertThat(captchaCode, stringWithLength(4));
assertThat(result, equalTo(true));
// Supplying correct code should clear the entry, and a code should be invalid if no entry is present
assertThat(manager.checkCode(player, "bogus"), equalTo(false));
}
@Test
public void shouldGenerateNewCodeOnFailure() {
// given
String name = "Tarheel";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
Settings settings = mockSettings(1, 9);
LoginCaptchaManager manager = new LoginCaptchaManager(settings);
String captchaCode = manager.getCaptchaCodeOrGenerateNew(name);
// when
boolean result = manager.checkCode(player, "wrongcode");
// then
assertThat(captchaCode, stringWithLength(9));
assertThat(result, equalTo(false));
assertThat(manager.getCaptchaCodeOrGenerateNew(name), not(equalTo(captchaCode)));
}
@Test
public void shouldHaveSameCodeAfterGeneration() {
// given
String player = "Tester";
Settings settings = mockSettings(1, 5);
LoginCaptchaManager manager = new LoginCaptchaManager(settings);
// when
String code1 = manager.getCaptchaCodeOrGenerateNew(player);
String code2 = manager.getCaptchaCodeOrGenerateNew(player);
String code3 = manager.getCaptchaCodeOrGenerateNew(player);
// then
assertThat(code1.length(), equalTo(5));
assertThat(code2, equalTo(code1));
assertThat(code3, equalTo(code1));
}
@Test
public void shouldIncreaseAndResetCount() {
// given
String player = "plaYer";
Settings settings = mockSettings(2, 3);
LoginCaptchaManager manager = new LoginCaptchaManager(settings);
// when
manager.increaseLoginFailureCount(player);
manager.increaseLoginFailureCount(player);
// then
assertThat(manager.isCaptchaRequired(player), equalTo(true));
assertHasCount(manager, player, 2);
// when 2
manager.resetLoginFailureCount(player);
// then 2
assertThat(manager.isCaptchaRequired(player), equalTo(false));
assertHasCount(manager, player, 0);
}
@Test
public void shouldNotIncreaseCountForDisabledCaptcha() {
// given
String player = "someone_";
Settings settings = mockSettings(1, 3);
given(settings.getProperty(SecuritySettings.ENABLE_LOGIN_FAILURE_CAPTCHA)).willReturn(false);
LoginCaptchaManager manager = new LoginCaptchaManager(settings);
// when
manager.increaseLoginFailureCount(player);
// then
assertThat(manager.isCaptchaRequired(player), equalTo(false));
assertHasCount(manager, player, 0);
}
@Test
public void shouldNotCheckCountIfCaptchaIsDisabled() {
// given
String player = "Robert001";
Settings settings = mockSettings(1, 5);
LoginCaptchaManager manager = new LoginCaptchaManager(settings);
given(settings.getProperty(SecuritySettings.ENABLE_LOGIN_FAILURE_CAPTCHA)).willReturn(false);
// when
manager.increaseLoginFailureCount(player);
// assumptions
assertThat(manager.isCaptchaRequired(player), equalTo(true));
assertHasCount(manager, player, 1);
// end assumptions
manager.reload(settings);
boolean result = manager.isCaptchaRequired(player);
// then
assertThat(result, equalTo(false));
}
private static Settings mockSettings(int maxTries, int captchaLength) {
Settings settings = mock(Settings.class);
given(settings.getProperty(SecuritySettings.ENABLE_LOGIN_FAILURE_CAPTCHA)).willReturn(true);
given(settings.getProperty(SecuritySettings.MAX_LOGIN_TRIES_BEFORE_CAPTCHA)).willReturn(maxTries);
given(settings.getProperty(SecuritySettings.CAPTCHA_LENGTH)).willReturn(captchaLength);
given(settings.getProperty(SecuritySettings.CAPTCHA_COUNT_MINUTES_BEFORE_RESET)).willReturn(30);
return settings;
}
private static void assertHasCount(LoginCaptchaManager manager, String player, Integer count) {
TimedCounter<String> playerCounts = ReflectionTestUtils
.getFieldValue(LoginCaptchaManager.class, manager, "playerCounts");
assertThat(playerCounts.get(player.toLowerCase(Locale.ROOT)), equalTo(count));
}
}

View File

@ -1,93 +0,0 @@
package fr.xephi.authme.data.captcha;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.expiring.ExpiringMap;
import org.bukkit.entity.Player;
import org.junit.Test;
import static fr.xephi.authme.AuthMeMatchers.stringWithLength;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Test for {@link RegistrationCaptchaManager}.
*/
public class RegistrationCaptchaManagerTest {
@Test
public void shouldBeDisabled() {
// given
Settings settings = mock(Settings.class);
// Return false first time, and true after that
given(settings.getProperty(SecuritySettings.ENABLE_CAPTCHA_FOR_REGISTRATION))
.willReturn(false).willReturn(true);
given(settings.getProperty(SecuritySettings.CAPTCHA_LENGTH)).willReturn(12);
// when
RegistrationCaptchaManager captchaManager1 = new RegistrationCaptchaManager(settings);
RegistrationCaptchaManager captchaManager2 = new RegistrationCaptchaManager(settings);
// then
assertThat(captchaManager1.isCaptchaRequired("bob"), equalTo(false));
assertThat(captchaManager2.isCaptchaRequired("bob"), equalTo(true));
}
@Test
public void shouldVerifyCodeSuccessfully() {
// given
Settings settings = mock(Settings.class);
given(settings.getProperty(SecuritySettings.ENABLE_CAPTCHA_FOR_REGISTRATION)).willReturn(true);
given(settings.getProperty(SecuritySettings.CAPTCHA_LENGTH)).willReturn(12);
String captcha = "abc3";
RegistrationCaptchaManager captchaManager = new RegistrationCaptchaManager(settings);
getCodeMap(captchaManager).put("test", captcha);
Player player = mock(Player.class);
given(player.getName()).willReturn("TeSt");
// when
boolean isSuccessful = captchaManager.checkCode(player, captcha);
// then
assertThat(isSuccessful, equalTo(true));
assertThat(getCodeMap(captchaManager).isEmpty(), equalTo(true));
assertThat(captchaManager.isCaptchaRequired("test"), equalTo(false));
}
@Test
public void shouldGenerateAndRetrieveCode() {
// given
Settings settings = mock(Settings.class);
given(settings.getProperty(SecuritySettings.ENABLE_CAPTCHA_FOR_REGISTRATION)).willReturn(true);
int captchaLength = 9;
given(settings.getProperty(SecuritySettings.CAPTCHA_LENGTH)).willReturn(captchaLength);
RegistrationCaptchaManager captchaManager = new RegistrationCaptchaManager(settings);
// when
String captcha1 = captchaManager.getCaptchaCodeOrGenerateNew("toast");
String captcha2 = captchaManager.getCaptchaCodeOrGenerateNew("Toast");
// then
assertThat(captcha1, equalTo(captcha2));
assertThat(captcha1, stringWithLength(captchaLength));
// given (2)
Player player = mock(Player.class);
given(player.getName()).willReturn("toast");
// when (2) / then (2)
assertThat(captchaManager.checkCode(player, captcha1), equalTo(true));
}
@SuppressWarnings("unchecked")
private static ExpiringMap<String, String> getCodeMap(RegistrationCaptchaManager captchaManager) {
CaptchaCodeStorage captchaStorage = ReflectionTestUtils.getFieldValue(
RegistrationCaptchaManager.class, captchaManager, "captchaCodeStorage");
return ReflectionTestUtils.getFieldValue(CaptchaCodeStorage.class, captchaStorage, "captchaCodes");
}
}

Some files were not shown because too many files have changed in this diff Show More