From b54bb251218279b865bb9f8f094bad500dadc72e Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Wed, 14 Oct 2015 14:22:10 +0200 Subject: [PATCH] KEYCLOAK-1930 --- .../java/org/keycloak/events/EventType.java | 1 + .../testsuite/admin/AbstractClientTest.java | 3 + .../keycloak/testsuite/admin/UserTest.java | 78 +++++++++++++++++++ .../testsuite/forms/ResetPasswordTest.java | 2 +- .../testsuite/rule/GreenMailRule.java | 11 +++ 5 files changed, 94 insertions(+), 1 deletion(-) diff --git a/events/api/src/main/java/org/keycloak/events/EventType.java b/events/api/src/main/java/org/keycloak/events/EventType.java index ac93a4f7a9..0a8b3aecee 100755 --- a/events/api/src/main/java/org/keycloak/events/EventType.java +++ b/events/api/src/main/java/org/keycloak/events/EventType.java @@ -70,6 +70,7 @@ public enum EventType { CUSTOM_REQUIRED_ACTION(true), CUSTOM_REQUIRED_ACTION_ERROR(true), EXECUTE_ACTIONS(true), + EXECUTE_ACTIONS_ERROR(true), CLIENT_INFO(false), CLIENT_INFO_ERROR(false), diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AbstractClientTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AbstractClientTest.java index cfc9270b3d..e3d6a5713a 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AbstractClientTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/AbstractClientTest.java @@ -13,6 +13,8 @@ import org.keycloak.representations.idm.IdentityProviderRepresentation; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.services.managers.RealmManager; import org.keycloak.testsuite.rule.KeycloakRule; +import org.keycloak.testsuite.rule.WebResource; +import org.openqa.selenium.WebDriver; import java.util.Arrays; import java.util.Collections; @@ -41,6 +43,7 @@ public abstract class AbstractClientTest { public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { RealmModel testRealm = manager.createRealm(REALM_NAME); testRealm.setEnabled(true); + testRealm.setAccessCodeLifespanUserAction(600); KeycloakModelUtils.generateRealmKeys(testRealm); } }); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/UserTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/UserTest.java index de1b2ca3cc..f262f34bd8 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/UserTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/UserTest.java @@ -1,18 +1,37 @@ package org.keycloak.testsuite.admin; import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.keycloak.admin.client.resource.IdentityProviderResource; import org.keycloak.admin.client.resource.UserResource; +import org.keycloak.models.RealmModel; +import org.keycloak.models.UserModel; +import org.keycloak.models.utils.KeycloakModelUtils; import org.keycloak.representations.idm.ErrorRepresentation; import org.keycloak.representations.idm.FederatedIdentityRepresentation; import org.keycloak.representations.idm.IdentityProviderRepresentation; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.UserRepresentation; +import org.keycloak.services.managers.RealmManager; +import org.keycloak.testsuite.forms.ResetPasswordTest; +import org.keycloak.testsuite.pages.LoginPasswordResetPage; +import org.keycloak.testsuite.pages.LoginPasswordUpdatePage; +import org.keycloak.testsuite.rule.GreenMailRule; +import org.keycloak.testsuite.rule.KeycloakRule; +import org.keycloak.testsuite.rule.WebResource; +import org.keycloak.testsuite.rule.WebRule; +import org.openqa.selenium.WebDriver; +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; +import javax.ws.rs.BadRequestException; import javax.ws.rs.ClientErrorException; import javax.ws.rs.core.Response; +import java.io.IOException; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -27,6 +46,31 @@ import static org.junit.Assert.fail; */ public class UserTest extends AbstractClientTest { + @Rule + public WebRule webRule = new WebRule(this); + + @Rule + public GreenMailRule greenMail = new GreenMailRule(); + + @WebResource + protected LoginPasswordUpdatePage passwordUpdatePage; + + @WebResource + protected WebDriver driver; + + @Before + public void before() { + super.before(); + + keycloakRule.configure(new KeycloakRule.KeycloakSetup() { + @Override + public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { + RealmModel testRealm = manager.getRealm(REALM_NAME); + greenMail.configureRealm(testRealm); + } + }); + } + public String createUser() { return createUser("user1", "user1@localhost"); } @@ -396,6 +440,40 @@ public class UserTest extends AbstractClientTest { } } + @Test + public void sendResetPasswordEmailSuccess() throws IOException, MessagingException { + UserRepresentation userRep = new UserRepresentation(); + userRep.setEnabled(true); + userRep.setUsername("user1"); + userRep.setEmail("user1@test.com"); + Response response = realm.users().create(userRep); + String id = ApiUtil.getCreatedId(response); + response.close(); + UserResource user = realm.users().get(id); + List actions = new LinkedList<>(); + actions.add(UserModel.RequiredAction.UPDATE_PASSWORD.name()); + user.executeActionsEmail("account", actions); + + Assert.assertEquals(1, greenMail.getReceivedMessages().length); + + MimeMessage message = greenMail.getReceivedMessages()[0]; + + String link = ResetPasswordTest.getPasswordResetEmailLink(message); + + driver.navigate().to(link); + + assertTrue(passwordUpdatePage.isCurrent()); + + passwordUpdatePage.changePassword("new-pass", "new-pass"); + + assertEquals("Your account has been updated.", driver.getTitle()); + + driver.navigate().to(link); + + assertEquals("We're sorry...", driver.getTitle()); + } + + @Test public void sendVerifyEmail() { UserRepresentation userRep = new UserRepresentation(); 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 04b0c65769..88d6f3deea 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 @@ -571,7 +571,7 @@ public class ResetPasswordTest { } } - private String getPasswordResetEmailLink(MimeMessage message) throws IOException, MessagingException { + public static String getPasswordResetEmailLink(MimeMessage message) throws IOException, MessagingException { Multipart multipart = (Multipart) message.getContent(); final String textContentType = multipart.getBodyPart(0).getContentType(); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/GreenMailRule.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/GreenMailRule.java index bd52a59c7c..21a35a649e 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/GreenMailRule.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/GreenMailRule.java @@ -24,10 +24,13 @@ package org.keycloak.testsuite.rule; import com.icegreen.greenmail.util.GreenMail; import com.icegreen.greenmail.util.ServerSetup; import org.junit.rules.ExternalResource; +import org.keycloak.models.RealmModel; import javax.mail.internet.MimeMessage; import java.lang.Thread.UncaughtExceptionHandler; import java.net.SocketException; +import java.util.HashMap; +import java.util.Map; /** * @author Stian Thorgersen @@ -63,6 +66,14 @@ public class GreenMailRule extends ExternalResource { } } + public void configureRealm(RealmModel realm) { + Map config = new HashMap<>(); + config.put("from", "auto@keycloak.org"); + config.put("host", "localhost"); + config.put("port", "3025"); + realm.setSmtpConfig(config); + } + public MimeMessage[] getReceivedMessages() { return greenMail.getReceivedMessages(); }