Search in sources :

Example 6 with EmailChangedEvent

use of fr.xephi.authme.events.EmailChangedEvent in project AuthMeReloaded by AuthMe.

the class AsyncChangeEmailTest method shouldNotChangeOnCancelledEvent.

@Test
public void shouldNotChangeOnCancelledEvent() {
    // given
    String newEmail = "new@example.com";
    String oldEmail = "old@example.com";
    given(player.getName()).willReturn("Username");
    given(playerCache.isAuthenticated("username")).willReturn(true);
    PlayerAuth auth = authWithMail(oldEmail);
    given(playerCache.getAuth("username")).willReturn(auth);
    given(validationService.validateEmail(newEmail)).willReturn(true);
    given(validationService.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
    EmailChangedEvent event = spy(new EmailChangedEvent(player, oldEmail, newEmail, false));
    event.setCancelled(true);
    given(bukkitService.createAndCallEvent(any(Function.class))).willReturn(event);
    // when
    process.changeEmail(player, oldEmail, newEmail);
    // then
    verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
    verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
    verify(service).send(player, MessageKey.EMAIL_CHANGE_NOT_ALLOWED);
}
Also used : Function(java.util.function.Function) PlayerAuth(fr.xephi.authme.data.auth.PlayerAuth) EmailChangedEvent(fr.xephi.authme.events.EmailChangedEvent) Test(org.junit.Test)

Example 7 with EmailChangedEvent

use of fr.xephi.authme.events.EmailChangedEvent in project AuthMeReloaded by AuthMe.

the class AsyncChangeEmailTest method shouldChangeEmail.

@Test
public void shouldChangeEmail() {
    // 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(true);
    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).updatePlayer(auth);
    verify(service).send(player, MessageKey.EMAIL_CHANGED_SUCCESS);
}
Also used : Function(java.util.function.Function) PlayerAuth(fr.xephi.authme.data.auth.PlayerAuth) EmailChangedEvent(fr.xephi.authme.events.EmailChangedEvent) Test(org.junit.Test)

Example 8 with EmailChangedEvent

use of fr.xephi.authme.events.EmailChangedEvent in project AuthMeReloaded by AuthMe.

the class AsyncAddEmailTest method shouldReturnErrorWhenMailCannotBeSaved.

@Test
public void shouldReturnErrorWhenMailCannotBeSaved() {
    // 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(false);
    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.ERROR);
}
Also used : Function(java.util.function.Function) PlayerAuth(fr.xephi.authme.data.auth.PlayerAuth) EmailChangedEvent(fr.xephi.authme.events.EmailChangedEvent) Test(org.junit.Test)

Example 9 with EmailChangedEvent

use of fr.xephi.authme.events.EmailChangedEvent in project AuthMeReloaded by AuthMe.

the class AsyncAddEmail method addEmail.

/**
 * Handles the request to add the given email to the player's account.
 *
 * @param player the player to add the email to
 * @param email the email to add
 */
public void addEmail(Player player, String email) {
    String playerName = player.getName().toLowerCase();
    if (playerCache.isAuthenticated(playerName)) {
        PlayerAuth auth = playerCache.getAuth(playerName);
        final String currentEmail = auth.getEmail();
        if (!Utils.isEmailEmpty(currentEmail)) {
            service.send(player, MessageKey.USAGE_CHANGE_EMAIL);
        } else if (!validationService.validateEmail(email)) {
            service.send(player, MessageKey.INVALID_EMAIL);
        } else if (!validationService.isEmailFreeForRegistration(email, player)) {
            service.send(player, MessageKey.EMAIL_ALREADY_USED_ERROR);
        } else {
            EmailChangedEvent event = bukkitService.createAndCallEvent(isAsync -> new EmailChangedEvent(player, null, email, isAsync));
            if (event.isCancelled()) {
                logger.info("Could not add email to player '" + player + "' – event was cancelled");
                service.send(player, MessageKey.EMAIL_ADD_NOT_ALLOWED);
                return;
            }
            auth.setEmail(email);
            if (dataSource.updateEmail(auth)) {
                playerCache.updatePlayer(auth);
                bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_EMAIL, playerName);
                service.send(player, MessageKey.EMAIL_ADDED_SUCCESS);
            } else {
                logger.warning("Could not save email for player '" + player + "'");
                service.send(player, MessageKey.ERROR);
            }
        }
    } else {
        sendUnloggedMessage(player);
    }
}
Also used : PlayerAuth(fr.xephi.authme.data.auth.PlayerAuth) EmailChangedEvent(fr.xephi.authme.events.EmailChangedEvent)

Aggregations

EmailChangedEvent (fr.xephi.authme.events.EmailChangedEvent)9 PlayerAuth (fr.xephi.authme.data.auth.PlayerAuth)8 Function (java.util.function.Function)7 Test (org.junit.Test)7