From 181c1c8b5bae4a9e72ef231629abadf720a3c4c0 Mon Sep 17 00:00:00 2001 From: Michael Gerber Date: Tue, 6 Jan 2015 13:02:27 +0100 Subject: [PATCH] add error event if mail can not be send --- .../main/java/org/keycloak/events/Errors.java | 1 + .../resources/LoginActionsService.java | 5 ++- .../testsuite/forms/ResetPasswordTest.java | 41 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/events/api/src/main/java/org/keycloak/events/Errors.java b/events/api/src/main/java/org/keycloak/events/Errors.java index 1d74a7ab7a..3e8c07ceb0 100755 --- a/events/api/src/main/java/org/keycloak/events/Errors.java +++ b/events/api/src/main/java/org/keycloak/events/Errors.java @@ -40,4 +40,5 @@ public interface Errors { String USER_SESSION_NOT_FOUND = "user_session_not_found"; + String EMAIL_SEND_FAILED = "email_send_failed"; } diff --git a/services/src/main/java/org/keycloak/services/resources/LoginActionsService.java b/services/src/main/java/org/keycloak/services/resources/LoginActionsService.java index 3815317004..a9f0233a53 100755 --- a/services/src/main/java/org/keycloak/services/resources/LoginActionsService.java +++ b/services/src/main/java/org/keycloak/services/resources/LoginActionsService.java @@ -819,6 +819,8 @@ public class LoginActionsService { } else if(!user.isEnabled()) { event.user(user).error(Errors.USER_DISABLED); } else { + event.user(user); + UserSessionModel userSession = session.sessions().createUserSession(realm, user, username, clientConnection.getRemoteAddr(), "form", false); event.session(userSession); TokenManager.attachClientSession(userSession, clientSession); @@ -834,8 +836,9 @@ public class LoginActionsService { this.session.getProvider(EmailProvider.class).setRealm(realm).setUser(user).sendPasswordReset(link, expiration); - event.user(user).detail(Details.EMAIL, user.getEmail()).detail(Details.CODE_ID, clientSession.getId()).success(); + event.detail(Details.EMAIL, user.getEmail()).detail(Details.CODE_ID, clientSession.getId()).success(); } catch (EmailException e) { + event.error(Errors.EMAIL_SEND_FAILED); logger.error("Failed to send password reset email", e); return Flows.forms(this.session, realm, client, uriInfo).setError("emailSendError") .setClientSessionCode(accessCode.getCode()) diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/ResetPasswordTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/ResetPasswordTest.java index a5c25c897f..878a2e414f 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/ResetPasswordTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/ResetPasswordTest.java @@ -26,6 +26,7 @@ import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.keycloak.events.Details; +import org.keycloak.events.Errors; import org.keycloak.events.EventType; import org.keycloak.models.PasswordPolicy; import org.keycloak.models.RealmModel; @@ -257,6 +258,46 @@ public class ResetPasswordTest { } } + @Test + public void resetPasswordNoEmail() throws IOException, MessagingException, InterruptedException { + final String[] email = new String[1]; + keycloakRule.configure(new KeycloakRule.KeycloakSetup() { + @Override + public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { + UserModel user = session.users().getUserByUsername("login-test", appRealm); + email[0] = user.getEmail(); + user.setEmail(null); + + } + }); + + try { + loginPage.open(); + loginPage.resetPassword(); + + resetPasswordPage.assertCurrent(); + + resetPasswordPage.changePassword("login-test"); + + errorPage.assertCurrent(); + + Assert.assertEquals("Failed to send email, please try again later", errorPage.getError()); + + Thread.sleep(1000); + + Assert.assertEquals(0, greenMail.getReceivedMessages().length); + + events.expectRequiredAction(EventType.SEND_RESET_PASSWORD_ERROR).user(userId).detail(Details.USERNAME, "login-test").removeDetail(Details.CODE_ID).error(Errors.EMAIL_SEND_FAILED).assertEvent(); + } finally { + keycloakRule.configure(new KeycloakRule.KeycloakSetup() { + @Override + public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { + session.users().getUserByUsername("login-test", appRealm).setEmail(email[0]); + } + }); + } + } + @Test public void resetPasswordWithPasswordPolicy() throws IOException, MessagingException { keycloakRule.update(new KeycloakRule.KeycloakSetup() {