use of fr.xephi.authme.events.EmailChangedEvent in project AuthMeReloaded by AuthMe.
the class AsyncChangeEmailTest method shouldNotBeCaseSensitiveWhenComparingEmails.
@Test
public void shouldNotBeCaseSensitiveWhenComparingEmails() {
// given
String newEmail = "newmail@example.com";
given(player.getName()).willReturn("Debra");
given(playerCache.isAuthenticated("debra")).willReturn(true);
String oldEmail = "OLD-mail@example.org";
PlayerAuth auth = authWithMail(oldEmail);
given(playerCache.getAuth("debra")).willReturn(auth);
given(dataSource.updateEmail(auth)).willReturn(true);
given(validationService.validateEmail(newEmail)).willReturn(true);
given(validationService.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
EmailChangedEvent event = spy(new EmailChangedEvent(player, oldEmail, newEmail, false));
given(bukkitService.createAndCallEvent(any(Function.class))).willReturn(event);
// when
process.changeEmail(player, "old-mail@example.org", newEmail);
// then
verify(dataSource).updateEmail(auth);
verify(playerCache).updatePlayer(auth);
verify(service).send(player, MessageKey.EMAIL_CHANGED_SUCCESS);
}
use of fr.xephi.authme.events.EmailChangedEvent in project AuthMeReloaded by AuthMe.
the class AsyncChangeEmailTest method shouldShowErrorIfSaveFails.
@Test
public void shouldShowErrorIfSaveFails() {
// given
String newEmail = "new@mail.tld";
given(player.getName()).willReturn("Bobby");
given(playerCache.isAuthenticated("bobby")).willReturn(true);
PlayerAuth auth = authWithMail("old@mail.tld");
given(playerCache.getAuth("bobby")).willReturn(auth);
given(dataSource.updateEmail(auth)).willReturn(false);
given(validationService.validateEmail(newEmail)).willReturn(true);
given(validationService.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
EmailChangedEvent event = spy(new EmailChangedEvent(player, "old@mail.tld", newEmail, false));
given(bukkitService.createAndCallEvent(any(Function.class))).willReturn(event);
// when
process.changeEmail(player, "old@mail.tld", newEmail);
// then
verify(dataSource).updateEmail(auth);
verify(playerCache, never()).updatePlayer(auth);
verify(service).send(player, MessageKey.ERROR);
}
use of fr.xephi.authme.events.EmailChangedEvent in project AuthMeReloaded by AuthMe.
the class AsyncAddEmailTest method shouldNotAddOnCancelledEvent.
@Test
public void shouldNotAddOnCancelledEvent() {
// given
String email = "player@mail.tld";
given(player.getName()).willReturn("TestName");
given(playerCache.isAuthenticated("testname")).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getEmail()).willReturn(null);
given(playerCache.getAuth("testname")).willReturn(auth);
given(validationService.validateEmail(email)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, player)).willReturn(true);
EmailChangedEvent event = spy(new EmailChangedEvent(player, null, email, false));
event.setCancelled(true);
given(bukkitService.createAndCallEvent(any(Function.class))).willReturn(event);
// when
asyncAddEmail.addEmail(player, email);
// then
verify(service).send(player, MessageKey.EMAIL_ADD_NOT_ALLOWED);
verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
}
use of fr.xephi.authme.events.EmailChangedEvent in project AuthMeReloaded by AuthMe.
the class AsyncAddEmailTest method shouldAddEmail.
@Test
public void shouldAddEmail() {
// given
String email = "my.mail@example.org";
given(player.getName()).willReturn("testEr");
given(playerCache.isAuthenticated("tester")).willReturn(true);
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getEmail()).willReturn(null);
given(playerCache.getAuth("tester")).willReturn(auth);
given(dataSource.updateEmail(any(PlayerAuth.class))).willReturn(true);
given(validationService.validateEmail(email)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, player)).willReturn(true);
EmailChangedEvent event = spy(new EmailChangedEvent(player, null, email, false));
given(bukkitService.createAndCallEvent(any(Function.class))).willReturn(event);
// when
asyncAddEmail.addEmail(player, email);
// then
verify(dataSource).updateEmail(auth);
verify(service).send(player, MessageKey.EMAIL_ADDED_SUCCESS);
verify(auth).setEmail(email);
verify(playerCache).updatePlayer(auth);
}
use of fr.xephi.authme.events.EmailChangedEvent in project AuthMeReloaded by AuthMe.
the class AsyncChangeEmail method saveNewEmail.
/**
* Saves the new email value into the database and informs services.
*
* @param auth the player auth object
* @param player the player object
* @param oldEmail the old email value
* @param newEmail the new email value
*/
private void saveNewEmail(PlayerAuth auth, Player player, String oldEmail, String newEmail) {
EmailChangedEvent event = bukkitService.createAndCallEvent(isAsync -> new EmailChangedEvent(player, oldEmail, newEmail, isAsync));
if (event.isCancelled()) {
logger.info("Could not change email for player '" + player + "' – event was cancelled");
service.send(player, MessageKey.EMAIL_CHANGE_NOT_ALLOWED);
return;
}
auth.setEmail(newEmail);
if (dataSource.updateEmail(auth)) {
playerCache.updatePlayer(auth);
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_EMAIL, player.getName());
service.send(player, MessageKey.EMAIL_CHANGED_SUCCESS);
} else {
service.send(player, MessageKey.ERROR);
}
}
Aggregations