Merge pull request #906 from gerbermichi/email-event

add error event if mail can not be send
This commit is contained in:
Stian Thorgersen 2015-01-06 13:45:30 +01:00
commit 6aa0cb5e3d
3 changed files with 46 additions and 1 deletions

View file

@ -40,4 +40,5 @@ public interface Errors {
String USER_SESSION_NOT_FOUND = "user_session_not_found";
String EMAIL_SEND_FAILED = "email_send_failed";
}

View file

@ -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())

View file

@ -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() {