Merge pull request #2753 from ssilvert/migrate-actions

KEYCLOAK-2905 Migrate actions package from old testsuite
This commit is contained in:
Stian Thorgersen 2016-04-28 06:43:55 +02:00
commit 231e049771
12 changed files with 283 additions and 326 deletions

View file

@ -77,6 +77,11 @@ public class AssertEvents implements TestRule {
return event; return event;
} }
public void assertEmpty() {
EventRepresentation event = fetchNextEvent();
Assert.assertNull("Empty event queue expected, but there is " + event, event);
}
public void clear() { public void clear() {
Response res = context.testingClient.testing().clearQueue(); Response res = context.testingClient.testing().clearQueue();
try { try {

View file

@ -0,0 +1,64 @@
/*
* Copyright 2016 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @author tags. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.keycloak.testsuite.actions;
import java.util.LinkedList;
import java.util.List;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RequiredActionProviderRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.testsuite.util.UserBuilder;
/**
*
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
public class ActionUtil {
public static UserRepresentation findUserInRealmRep(RealmRepresentation testRealm, String username) {
for (UserRepresentation rep : testRealm.getUsers()) {
if (rep.getUsername().equals(username)) return rep;
}
return null;
}
public static UserRepresentation findUserWithAdminClient(Keycloak adminClient, String username) {
return adminClient.realm("test").users().search(username, null, null, null, 0, 1).get(0);
}
public static void addRequiredActionForUser(RealmRepresentation testRealm, String userName, String action) {
UserRepresentation user = findUserInRealmRep(testRealm, userName);
UserBuilder.edit(user).requiredAction(action);
}
public static void addRequiredActionForRealm(RealmRepresentation testRealm, String providerId) {
List<RequiredActionProviderRepresentation> requiredActions = testRealm.getRequiredActions();
if (requiredActions == null) requiredActions = new LinkedList();
RequiredActionProviderRepresentation action = new RequiredActionProviderRepresentation();
action.setAlias(providerId);
action.setProviderId(providerId);
action.setEnabled(true);
action.setDefaultAction(true);
requiredActions.add(action);
testRealm.setRequiredActions(requiredActions);
}
}

View file

@ -18,38 +18,31 @@ package org.keycloak.testsuite.actions;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.keycloak.common.util.KeycloakUriBuilder; import org.keycloak.common.util.KeycloakUriBuilder;
import org.keycloak.events.Details; import org.keycloak.events.Details;
import org.keycloak.events.Errors; import org.keycloak.events.Errors;
import org.keycloak.events.Event;
import org.keycloak.events.EventType; import org.keycloak.events.EventType;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.testsuite.AssertEvents; import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.MailUtil;
import org.keycloak.testsuite.OAuthClient;
import org.keycloak.testsuite.pages.AppPage; import org.keycloak.testsuite.pages.AppPage;
import org.keycloak.testsuite.pages.AppPage.RequestType; import org.keycloak.testsuite.pages.AppPage.RequestType;
import org.keycloak.testsuite.pages.InfoPage; import org.keycloak.testsuite.pages.InfoPage;
import org.keycloak.testsuite.pages.LoginPage; import org.keycloak.testsuite.pages.LoginPage;
import org.keycloak.testsuite.pages.RegisterPage; import org.keycloak.testsuite.pages.RegisterPage;
import org.keycloak.testsuite.pages.VerifyEmailPage; import org.keycloak.testsuite.pages.VerifyEmailPage;
import org.keycloak.testsuite.rule.GreenMailRule;
import org.keycloak.testsuite.rule.KeycloakRule;
import org.keycloak.testsuite.rule.KeycloakRule.KeycloakSetup;
import org.keycloak.testsuite.rule.WebResource;
import org.keycloak.testsuite.rule.WebRule;
import org.openqa.selenium.WebDriver;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.mail.Multipart; import javax.mail.Multipart;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import java.io.IOException; import java.io.IOException;
import org.jboss.arquillian.graphene.page.Page;
import org.keycloak.representations.idm.EventRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.TestRealmKeycloakTest;
import org.keycloak.testsuite.util.GreenMailRule;
import org.keycloak.testsuite.util.MailUtils;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -57,55 +50,38 @@ import static org.junit.Assert.assertTrue;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/ */
public class RequiredActionEmailVerificationTest { public class RequiredActionEmailVerificationTest extends TestRealmKeycloakTest {
@ClassRule
public static KeycloakRule keycloakRule = new KeycloakRule();
@Rule @Rule
public AssertEvents events = new AssertEvents(keycloakRule); public AssertEvents events = new AssertEvents(this);
@Rule
public WebRule webRule = new WebRule(this);
@Rule @Rule
public GreenMailRule greenMail = new GreenMailRule(); public GreenMailRule greenMail = new GreenMailRule();
@WebResource @Page
protected WebDriver driver;
@WebResource
protected OAuthClient oauth;
@WebResource
protected AppPage appPage; protected AppPage appPage;
@WebResource @Page
protected LoginPage loginPage; protected LoginPage loginPage;
@WebResource @Page
protected VerifyEmailPage verifyEmailPage; protected VerifyEmailPage verifyEmailPage;
@WebResource @Page
protected RegisterPage registerPage; protected RegisterPage registerPage;
@WebResource @Page
protected InfoPage infoPage; protected InfoPage infoPage;
@Override
public void configureTestRealm(RealmRepresentation testRealm) {
testRealm.setVerifyEmail(Boolean.TRUE);
ActionUtil.findUserInRealmRep(testRealm, "test-user@localhost").setEmailVerified(Boolean.FALSE);
}
@Before @Before
public void before() { public void before() {
oauth.state("mystate"); // have to set this as keycloak validates that state is sent oauth.state("mystate"); // have to set this as keycloak validates that state is sent
keycloakRule.configure(new KeycloakSetup() {
@Override
public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) {
appRealm.setVerifyEmail(true);
UserModel user = manager.getSession().users().getUserByUsername("test-user@localhost", appRealm);
user.setEmailVerified(false);
}
});
} }
@Test @Test
@ -118,11 +94,11 @@ public class RequiredActionEmailVerificationTest {
Assert.assertEquals(1, greenMail.getReceivedMessages().length); Assert.assertEquals(1, greenMail.getReceivedMessages().length);
MimeMessage message = greenMail.getReceivedMessages()[0]; MimeMessage message = greenMail.getReceivedMessages()[0];
String verificationUrl = getPasswordResetEmailLink(message); String verificationUrl = getPasswordResetEmailLink(message);
AssertEvents.ExpectedEvent emailEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost"); AssertEvents.ExpectedEvent emailEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost");
Event sendEvent = emailEvent.assertEvent(); EventRepresentation sendEvent = emailEvent.assertEvent();
String sessionId = sendEvent.getSessionId(); String sessionId = sendEvent.getSessionId();
String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID); String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID);
@ -152,7 +128,7 @@ public class RequiredActionEmailVerificationTest {
MimeMessage message = greenMail.getReceivedMessages()[0]; MimeMessage message = greenMail.getReceivedMessages()[0];
Event sendEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).user(userId).detail("username", "verifyemail").detail("email", "email@mail.com").assertEvent(); EventRepresentation sendEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).user(userId).detail("username", "verifyemail").detail("email", "email@mail.com").assertEvent();
String sessionId = sendEvent.getSessionId(); String sessionId = sendEvent.getSessionId();
String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID); String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID);
@ -177,7 +153,7 @@ public class RequiredActionEmailVerificationTest {
Assert.assertEquals(1, greenMail.getReceivedMessages().length); Assert.assertEquals(1, greenMail.getReceivedMessages().length);
Event sendEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost").assertEvent(); EventRepresentation sendEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost").assertEvent();
String sessionId = sendEvent.getSessionId(); String sessionId = sendEvent.getSessionId();
String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID); String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID);
@ -215,7 +191,7 @@ public class RequiredActionEmailVerificationTest {
String verificationUrl = getPasswordResetEmailLink(message); String verificationUrl = getPasswordResetEmailLink(message);
AssertEvents.ExpectedEvent emailEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost"); AssertEvents.ExpectedEvent emailEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost");
Event sendEvent = emailEvent.assertEvent(); EventRepresentation sendEvent = emailEvent.assertEvent();
String sessionId = sendEvent.getSessionId(); String sessionId = sendEvent.getSessionId();
String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID); String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID);
@ -247,7 +223,7 @@ public class RequiredActionEmailVerificationTest {
String keyInsteadCodeURL = resendEmailLink.replace("code=", "key="); String keyInsteadCodeURL = resendEmailLink.replace("code=", "key=");
AssertEvents.ExpectedEvent emailEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost"); AssertEvents.ExpectedEvent emailEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost");
Event sendEvent = emailEvent.assertEvent(); EventRepresentation sendEvent = emailEvent.assertEvent();
String sessionId = sendEvent.getSessionId(); String sessionId = sendEvent.getSessionId();
String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID); String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID);
@ -271,24 +247,24 @@ public class RequiredActionEmailVerificationTest {
.detail(Details.CODE_ID, mailCodeId) .detail(Details.CODE_ID, mailCodeId)
.assertEvent(); .assertEvent();
} }
public static String getPasswordResetEmailLink(MimeMessage message) throws IOException, MessagingException { public static String getPasswordResetEmailLink(MimeMessage message) throws IOException, MessagingException {
Multipart multipart = (Multipart) message.getContent(); Multipart multipart = (Multipart) message.getContent();
final String textContentType = multipart.getBodyPart(0).getContentType(); final String textContentType = multipart.getBodyPart(0).getContentType();
assertEquals("text/plain; charset=UTF-8", textContentType); assertEquals("text/plain; charset=UTF-8", textContentType);
final String textBody = (String) multipart.getBodyPart(0).getContent(); final String textBody = (String) multipart.getBodyPart(0).getContent();
final String textChangePwdUrl = MailUtil.getLink(textBody); final String textChangePwdUrl = MailUtils.getLink(textBody);
final String htmlContentType = multipart.getBodyPart(1).getContentType(); final String htmlContentType = multipart.getBodyPart(1).getContentType();
assertEquals("text/html; charset=UTF-8", htmlContentType); assertEquals("text/html; charset=UTF-8", htmlContentType);
final String htmlBody = (String) multipart.getBodyPart(1).getContent(); final String htmlBody = (String) multipart.getBodyPart(1).getContent();
final String htmlChangePwdUrl = MailUtil.getLink(htmlBody); final String htmlChangePwdUrl = MailUtils.getLink(htmlBody);
assertEquals(htmlChangePwdUrl, textChangePwdUrl); assertEquals(htmlChangePwdUrl, textChangePwdUrl);
return htmlChangePwdUrl; return htmlChangePwdUrl;

View file

@ -16,65 +16,43 @@
*/ */
package org.keycloak.testsuite.actions; package org.keycloak.testsuite.actions;
import org.jboss.arquillian.graphene.page.Page;
import org.junit.Assert; import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.keycloak.events.Details; import org.keycloak.events.Details;
import org.keycloak.events.EventType; import org.keycloak.events.EventType;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.UserModel.RequiredAction; import org.keycloak.models.UserModel.RequiredAction;
import org.keycloak.services.managers.RealmManager; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.AssertEvents; import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.OAuthClient; import org.keycloak.testsuite.TestRealmKeycloakTest;
import org.keycloak.testsuite.pages.*; import org.keycloak.testsuite.pages.*;
import org.keycloak.testsuite.pages.AppPage.RequestType; import org.keycloak.testsuite.pages.AppPage.RequestType;
import org.keycloak.testsuite.rule.KeycloakRule;
import org.keycloak.testsuite.rule.KeycloakRule.KeycloakSetup;
import org.keycloak.testsuite.rule.WebResource;
import org.keycloak.testsuite.rule.WebRule;
import org.openqa.selenium.WebDriver;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/ */
public class RequiredActionMultipleActionsTest { public class RequiredActionMultipleActionsTest extends TestRealmKeycloakTest {
@ClassRule @Override
public static KeycloakRule keycloakRule = new KeycloakRule(new KeycloakSetup() { public void configureTestRealm(RealmRepresentation testRealm) {
ActionUtil.addRequiredActionForUser(testRealm, "test-user@localhost", RequiredAction.UPDATE_PROFILE.name());
@Override ActionUtil.addRequiredActionForUser(testRealm, "test-user@localhost", RequiredAction.UPDATE_PASSWORD.name());
public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) { }
UserModel user = manager.getSession().users().getUserByUsername("test-user@localhost", appRealm);
user.addRequiredAction(RequiredAction.UPDATE_PROFILE);
user.addRequiredAction(RequiredAction.UPDATE_PASSWORD);
}
});
@Rule @Rule
public WebRule webRule = new WebRule(this); public AssertEvents events = new AssertEvents(this);
@Rule @Page
public AssertEvents events = new AssertEvents(keycloakRule);
@WebResource
protected WebDriver driver;
@WebResource
protected OAuthClient oauth;
@WebResource
protected AppPage appPage; protected AppPage appPage;
@WebResource @Page
protected LoginPage loginPage; protected LoginPage loginPage;
@WebResource @Page
protected LoginPasswordUpdatePage changePasswordPage; protected LoginPasswordUpdatePage changePasswordPage;
@WebResource @Page
protected LoginUpdateProfileEditUsernameAllowedPage updateProfilePage; protected LoginUpdateProfileEditUsernameAllowedPage updateProfilePage;
@Test @Test

View file

@ -16,70 +16,47 @@
*/ */
package org.keycloak.testsuite.actions; package org.keycloak.testsuite.actions;
import org.jboss.arquillian.graphene.page.Page;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.keycloak.events.Event;
import org.keycloak.events.EventType; import org.keycloak.events.EventType;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.UserModel.RequiredAction; import org.keycloak.models.UserModel.RequiredAction;
import org.keycloak.services.managers.RealmManager; import org.keycloak.representations.idm.EventRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.AssertEvents; import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.OAuthClient; import org.keycloak.testsuite.TestRealmKeycloakTest;
import org.keycloak.testsuite.pages.AppPage; import org.keycloak.testsuite.pages.AppPage;
import org.keycloak.testsuite.pages.AppPage.RequestType; import org.keycloak.testsuite.pages.AppPage.RequestType;
import org.keycloak.testsuite.pages.LoginPage; import org.keycloak.testsuite.pages.LoginPage;
import org.keycloak.testsuite.pages.LoginPasswordUpdatePage; import org.keycloak.testsuite.pages.LoginPasswordUpdatePage;
import org.keycloak.testsuite.rule.GreenMailRule; import org.keycloak.testsuite.util.GreenMailRule;
import org.keycloak.testsuite.rule.KeycloakRule;
import org.keycloak.testsuite.rule.KeycloakRule.KeycloakSetup;
import org.keycloak.testsuite.rule.WebResource;
import org.keycloak.testsuite.rule.WebRule;
import org.openqa.selenium.WebDriver;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/ */
public class RequiredActionResetPasswordTest { public class RequiredActionResetPasswordTest extends TestRealmKeycloakTest {
@ClassRule @Override
public static KeycloakRule keycloakRule = new KeycloakRule(new KeycloakSetup() { public void configureTestRealm(RealmRepresentation testRealm) {
testRealm.setResetPasswordAllowed(Boolean.TRUE);
@Override ActionUtil.addRequiredActionForUser(testRealm, "test-user@localhost", RequiredAction.UPDATE_PASSWORD.name());
public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) { }
appRealm.setResetPasswordAllowed(true);
UserModel user = manager.getSession().users().getUserByUsername("test-user@localhost", appRealm);
user.addRequiredAction(RequiredAction.UPDATE_PASSWORD);
}
});
@Rule @Rule
public WebRule webRule = new WebRule(this); public AssertEvents events = new AssertEvents(this);
@Rule
public AssertEvents events = new AssertEvents(keycloakRule);
@Rule @Rule
public GreenMailRule greenMail = new GreenMailRule(); public GreenMailRule greenMail = new GreenMailRule();
@WebResource @Page
protected WebDriver driver;
@WebResource
protected OAuthClient oauth;
@WebResource
protected AppPage appPage; protected AppPage appPage;
@WebResource @Page
protected LoginPage loginPage; protected LoginPage loginPage;
@WebResource @Page
protected LoginPasswordUpdatePage changePasswordPage; protected LoginPasswordUpdatePage changePasswordPage;
@Before @Before
@ -100,7 +77,7 @@ public class RequiredActionResetPasswordTest {
Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());
Event loginEvent = events.expectLogin().session(sessionId).assertEvent(); EventRepresentation loginEvent = events.expectLogin().session(sessionId).assertEvent();
oauth.openLogout(); oauth.openLogout();

View file

@ -16,24 +16,26 @@
*/ */
package org.keycloak.testsuite.actions; package org.keycloak.testsuite.actions;
import java.util.LinkedList;
import java.util.List;
import org.jboss.arquillian.graphene.page.Page;
import org.junit.Assert; import org.junit.Assert;
import org.junit.ClassRule; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.keycloak.events.Details; import org.keycloak.events.Details;
import org.keycloak.events.Event;
import org.keycloak.events.EventType; import org.keycloak.events.EventType;
import org.keycloak.models.OTPPolicy; import org.keycloak.models.AuthenticationExecutionModel;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RequiredActionProviderModel;
import org.keycloak.models.UserCredentialModel; import org.keycloak.models.UserCredentialModel;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import org.keycloak.models.utils.HmacOTP; import org.keycloak.models.utils.HmacOTP;
import org.keycloak.models.utils.TimeBasedOTP; import org.keycloak.models.utils.TimeBasedOTP;
import org.keycloak.representations.idm.CredentialRepresentation; import org.keycloak.representations.idm.AuthenticationExecutionInfoRepresentation;
import org.keycloak.services.managers.RealmManager; import org.keycloak.representations.idm.EventRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RequiredActionProviderRepresentation;
import org.keycloak.testsuite.AssertEvents; import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.OAuthClient; import org.keycloak.testsuite.TestRealmKeycloakTest;
import org.keycloak.testsuite.pages.AccountTotpPage; import org.keycloak.testsuite.pages.AccountTotpPage;
import org.keycloak.testsuite.pages.AppPage; import org.keycloak.testsuite.pages.AppPage;
import org.keycloak.testsuite.pages.AppPage.RequestType; import org.keycloak.testsuite.pages.AppPage.RequestType;
@ -41,64 +43,59 @@ import org.keycloak.testsuite.pages.LoginConfigTotpPage;
import org.keycloak.testsuite.pages.LoginPage; import org.keycloak.testsuite.pages.LoginPage;
import org.keycloak.testsuite.pages.LoginTotpPage; import org.keycloak.testsuite.pages.LoginTotpPage;
import org.keycloak.testsuite.pages.RegisterPage; import org.keycloak.testsuite.pages.RegisterPage;
import org.keycloak.testsuite.rule.KeycloakRule; import org.keycloak.testsuite.util.RealmBuilder;
import org.keycloak.testsuite.rule.KeycloakRule.KeycloakSetup;
import org.keycloak.testsuite.rule.WebResource;
import org.keycloak.testsuite.rule.WebRule;
import org.keycloak.utils.CredentialHelper;
import org.openqa.selenium.WebDriver;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/ */
public class RequiredActionTotpSetupTest { public class RequiredActionTotpSetupTest extends TestRealmKeycloakTest {
private static OTPPolicy originalPolicy; @Override
public void configureTestRealm(RealmRepresentation testRealm) {
RequiredActionProviderRepresentation requiredAction = new RequiredActionProviderRepresentation();
requiredAction.setAlias(UserModel.RequiredAction.CONFIGURE_TOTP.name());
requiredAction.setProviderId(UserModel.RequiredAction.CONFIGURE_TOTP.name());
requiredAction.setName("Configure Totp");
requiredAction.setEnabled(true);
requiredAction.setDefaultAction(true);
@ClassRule List<RequiredActionProviderRepresentation> requiredActions = new LinkedList<>();
public static KeycloakRule keycloakRule = new KeycloakRule(new KeycloakSetup() { requiredActions.add(requiredAction);
testRealm.setRequiredActions(requiredActions);
testRealm.setResetPasswordAllowed(Boolean.TRUE);
}
@Override @Before
public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) { public void setOTPAuthRequired() {
CredentialHelper.setRequiredCredential(manager.getSession(), CredentialRepresentation.TOTP, appRealm); for (AuthenticationExecutionInfoRepresentation execution : adminClient.realm("test").flows().getExecutions("browser")) {
//appRealm.addRequiredCredential(CredentialRepresentation.TOTP); String providerId = execution.getProviderId();
RequiredActionProviderModel requiredAction = appRealm.getRequiredActionProviderByAlias(UserModel.RequiredAction.CONFIGURE_TOTP.name()); if ("auth-otp-form".equals(providerId)) {
requiredAction.setDefaultAction(true); execution.setRequirement(AuthenticationExecutionModel.Requirement.REQUIRED.name());
appRealm.updateRequiredActionProvider(requiredAction); adminClient.realm("test").flows().updateExecutions("browser", execution);
appRealm.setResetPasswordAllowed(true); }
originalPolicy = appRealm.getOTPPolicy();
} }
}
});
@Rule @Rule
public AssertEvents events = new AssertEvents(keycloakRule); public AssertEvents events = new AssertEvents(this);
@Rule @Page
public WebRule webRule = new WebRule(this);
@WebResource
protected WebDriver driver;
@WebResource
protected AppPage appPage; protected AppPage appPage;
@WebResource @Page
protected LoginPage loginPage; protected LoginPage loginPage;
@WebResource @Page
protected LoginTotpPage loginTotpPage; protected LoginTotpPage loginTotpPage;
@WebResource @Page
protected LoginConfigTotpPage totpPage; protected LoginConfigTotpPage totpPage;
@WebResource @Page
protected AccountTotpPage accountTotpPage; protected AccountTotpPage accountTotpPage;
@WebResource @Page
protected OAuthClient oauth;
@WebResource
protected RegisterPage registerPage; protected RegisterPage registerPage;
protected TimeBasedOTP totp = new TimeBasedOTP(); protected TimeBasedOTP totp = new TimeBasedOTP();
@ -111,7 +108,7 @@ public class RequiredActionTotpSetupTest {
String userId = events.expectRegister("setupTotp", "email@mail.com").assertEvent().getUserId(); String userId = events.expectRegister("setupTotp", "email@mail.com").assertEvent().getUserId();
totpPage.assertCurrent(); Assert.assertTrue(totpPage.isCurrent());
totpPage.configure(totp.generateTOTP(totpPage.getTotpSecret())); totpPage.configure(totp.generateTOTP(totpPage.getTotpSecret()));
@ -137,7 +134,7 @@ public class RequiredActionTotpSetupTest {
Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());
Event loginEvent = events.expectLogin().session(sessionId).assertEvent(); EventRepresentation loginEvent = events.expectLogin().session(sessionId).assertEvent();
oauth.openLogout(); oauth.openLogout();
@ -175,7 +172,7 @@ public class RequiredActionTotpSetupTest {
events.expectRequiredAction(EventType.UPDATE_TOTP).user(userId).detail(Details.USERNAME, "setuptotp2").assertEvent(); events.expectRequiredAction(EventType.UPDATE_TOTP).user(userId).detail(Details.USERNAME, "setuptotp2").assertEvent();
Event loginEvent = events.expectLogin().user(userId).detail(Details.USERNAME, "setuptotp2").assertEvent(); EventRepresentation loginEvent = events.expectLogin().user(userId).detail(Details.USERNAME, "setuptotp2").assertEvent();
// Logout // Logout
oauth.openLogout(); oauth.openLogout();
@ -227,21 +224,15 @@ public class RequiredActionTotpSetupTest {
@Test @Test
public void setupOtpPolicyChangedTotp8Digits() { public void setupOtpPolicyChangedTotp8Digits() {
// set policy to 8 digits // set policy to 8 digits
keycloakRule.update(new KeycloakRule.KeycloakSetup() { RealmRepresentation realmRep = adminClient.realm("test").toRepresentation();
RealmBuilder.edit(realmRep)
@Override .otpLookAheadWindow(1)
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { .otpDigits(8)
OTPPolicy newPolicy = new OTPPolicy(); .otpPeriod(30)
newPolicy.setLookAheadWindow(1); .otpType(UserCredentialModel.TOTP)
newPolicy.setDigits(8); .otpAlgorithm(HmacOTP.HMAC_SHA1)
newPolicy.setPeriod(30); .otpInitialCounter(0);
newPolicy.setType(UserCredentialModel.TOTP); adminClient.realm("test").update(realmRep);
newPolicy.setAlgorithm(HmacOTP.HMAC_SHA1);
newPolicy.setInitialCounter(0);
appRealm.setOTPPolicy(newPolicy);
}
});
loginPage.open(); loginPage.open();
@ -258,7 +249,7 @@ public class RequiredActionTotpSetupTest {
Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());
Event loginEvent = events.expectLogin().session(sessionId).assertEvent(); EventRepresentation loginEvent = events.expectLogin().session(sessionId).assertEvent();
oauth.openLogout(); oauth.openLogout();
@ -274,35 +265,19 @@ public class RequiredActionTotpSetupTest {
Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());
events.expectLogin().assertEvent(); events.expectLogin().assertEvent();
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.setOTPPolicy(originalPolicy);
}
});
} }
@Test @Test
public void setupOtpPolicyChangedHotp() { public void setupOtpPolicyChangedHotp() {
keycloakRule.update(new KeycloakRule.KeycloakSetup() { RealmRepresentation realmRep = adminClient.realm("test").toRepresentation();
RealmBuilder.edit(realmRep)
@Override .otpLookAheadWindow(0)
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { .otpDigits(6)
OTPPolicy newPolicy = new OTPPolicy(); .otpPeriod(30)
newPolicy.setLookAheadWindow(0); .otpType(UserCredentialModel.HOTP)
newPolicy.setDigits(6); .otpAlgorithm(HmacOTP.HMAC_SHA1)
newPolicy.setPeriod(30); .otpInitialCounter(0);
newPolicy.setType(UserCredentialModel.HOTP); adminClient.realm("test").update(realmRep);
newPolicy.setAlgorithm(HmacOTP.HMAC_SHA1);
newPolicy.setInitialCounter(0);
appRealm.setOTPPolicy(newPolicy);
}
});
loginPage.open(); loginPage.open();
@ -319,7 +294,7 @@ public class RequiredActionTotpSetupTest {
Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());
Event loginEvent = events.expectLogin().session(sessionId).assertEvent(); EventRepresentation loginEvent = events.expectLogin().session(sessionId).assertEvent();
oauth.openLogout(); oauth.openLogout();
@ -338,22 +313,15 @@ public class RequiredActionTotpSetupTest {
events.expectLogout(null).session(AssertEvents.isUUID()).assertEvent(); events.expectLogout(null).session(AssertEvents.isUUID()).assertEvent();
// test lookAheadWindow // test lookAheadWindow
realmRep = adminClient.realm("test").toRepresentation();
keycloakRule.update(new KeycloakRule.KeycloakSetup() { RealmBuilder.edit(realmRep)
.otpLookAheadWindow(5)
@Override .otpDigits(6)
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { .otpPeriod(30)
OTPPolicy newPolicy = new OTPPolicy(); .otpType(UserCredentialModel.HOTP)
newPolicy.setLookAheadWindow(5); .otpAlgorithm(HmacOTP.HMAC_SHA1)
newPolicy.setDigits(6); .otpInitialCounter(0);
newPolicy.setPeriod(30); adminClient.realm("test").update(realmRep);
newPolicy.setType(UserCredentialModel.HOTP);
newPolicy.setAlgorithm(HmacOTP.HMAC_SHA1);
newPolicy.setInitialCounter(0);
appRealm.setOTPPolicy(newPolicy);
}
});
loginPage.open(); loginPage.open();
@ -365,23 +333,6 @@ public class RequiredActionTotpSetupTest {
Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());
events.expectLogin().assertEvent(); events.expectLogin().assertEvent();
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.setOTPPolicy(originalPolicy);
}
});
} }
} }

View file

@ -16,60 +16,41 @@
*/ */
package org.keycloak.testsuite.actions; package org.keycloak.testsuite.actions;
import org.jboss.arquillian.graphene.page.Page;
import org.junit.*; import org.junit.*;
import org.keycloak.events.Details; import org.keycloak.events.Details;
import org.keycloak.events.EventType; import org.keycloak.events.EventType;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.testsuite.AssertEvents; import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.TestRealmKeycloakTest;
import org.keycloak.testsuite.pages.AppPage; import org.keycloak.testsuite.pages.AppPage;
import org.keycloak.testsuite.pages.AppPage.RequestType; import org.keycloak.testsuite.pages.AppPage.RequestType;
import org.keycloak.testsuite.pages.LoginPage; import org.keycloak.testsuite.pages.LoginPage;
import org.keycloak.testsuite.pages.LoginUpdateProfileEditUsernameAllowedPage; import org.keycloak.testsuite.pages.LoginUpdateProfileEditUsernameAllowedPage;
import org.keycloak.testsuite.rule.KeycloakRule;
import org.keycloak.testsuite.rule.WebResource;
import org.keycloak.testsuite.rule.WebRule;
import org.openqa.selenium.WebDriver;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/ */
public class RequiredActionUpdateProfileTest { public class RequiredActionUpdateProfileTest extends TestRealmKeycloakTest {
@ClassRule
public static KeycloakRule keycloakRule = new KeycloakRule();
@Rule @Rule
public WebRule webRule = new WebRule(this); public AssertEvents events = new AssertEvents(this);
@Rule @Page
public AssertEvents events = new AssertEvents(keycloakRule);
@WebResource
protected WebDriver driver;
@WebResource
protected AppPage appPage; protected AppPage appPage;
@WebResource @Page
protected LoginPage loginPage; protected LoginPage loginPage;
@WebResource @Page
protected LoginUpdateProfileEditUsernameAllowedPage updateProfilePage; protected LoginUpdateProfileEditUsernameAllowedPage updateProfilePage;
@Before @Override
public void before() { public void configureTestRealm(RealmRepresentation testRealm) {
keycloakRule.configure(new KeycloakRule.KeycloakSetup() { ActionUtil.addRequiredActionForUser(testRealm, "test-user@localhost", UserModel.RequiredAction.UPDATE_PROFILE.name());
@Override ActionUtil.addRequiredActionForUser(testRealm, "john-doh@localhost", UserModel.RequiredAction.UPDATE_PROFILE.name());
public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) {
UserModel user = manager.getSession().users().getUserByUsername("test-user@localhost", appRealm);
user.addRequiredAction(UserModel.RequiredAction.UPDATE_PROFILE);
UserModel anotherUser = manager.getSession().users().getUserByEmail("john-doh@localhost", appRealm);
anotherUser.addRequiredAction(UserModel.RequiredAction.UPDATE_PROFILE);
}
});
} }
@Test @Test
@ -90,7 +71,7 @@ public class RequiredActionUpdateProfileTest {
events.expectLogin().session(sessionId).assertEvent(); events.expectLogin().session(sessionId).assertEvent();
// assert user is really updated in persistent store // assert user is really updated in persistent store
UserRepresentation user = keycloakRule.getUser("test", "test-user@localhost"); UserRepresentation user = ActionUtil.findUserWithAdminClient(adminClient, "test-user@localhost");
Assert.assertEquals("New first", user.getFirstName()); Assert.assertEquals("New first", user.getFirstName());
Assert.assertEquals("New last", user.getLastName()); Assert.assertEquals("New last", user.getLastName());
Assert.assertEquals("new@email.com", user.getEmail()); Assert.assertEquals("new@email.com", user.getEmail());
@ -103,7 +84,7 @@ public class RequiredActionUpdateProfileTest {
loginPage.login("john-doh@localhost", "password"); loginPage.login("john-doh@localhost", "password");
String userId = keycloakRule.getUser("test", "john-doh@localhost").getId(); String userId = ActionUtil.findUserWithAdminClient(adminClient, "john-doh@localhost").getId();
updateProfilePage.assertCurrent(); updateProfilePage.assertCurrent();
@ -124,7 +105,7 @@ public class RequiredActionUpdateProfileTest {
events.expectLogin().detail(Details.USERNAME, "john-doh@localhost").user(userId).session(sessionId).assertEvent(); events.expectLogin().detail(Details.USERNAME, "john-doh@localhost").user(userId).session(sessionId).assertEvent();
// assert user is really updated in persistent store // assert user is really updated in persistent store
UserRepresentation user = keycloakRule.getUser("test", "new"); UserRepresentation user = ActionUtil.findUserWithAdminClient(adminClient, "new");
Assert.assertEquals("New first", user.getFirstName()); Assert.assertEquals("New first", user.getFirstName());
Assert.assertEquals("New last", user.getLastName()); Assert.assertEquals("New last", user.getLastName());
Assert.assertEquals("john-doh@localhost", user.getEmail()); Assert.assertEquals("john-doh@localhost", user.getEmail());

View file

@ -17,68 +17,55 @@
package org.keycloak.testsuite.actions; package org.keycloak.testsuite.actions;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.keycloak.authentication.requiredactions.TermsAndConditions; import org.keycloak.authentication.requiredactions.TermsAndConditions;
import org.keycloak.events.Details; import org.keycloak.events.Details;
import org.keycloak.events.Errors; import org.keycloak.events.Errors;
import org.keycloak.events.EventType; import org.keycloak.events.EventType;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.testsuite.AssertEvents; import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.pages.AppPage; import org.keycloak.testsuite.pages.AppPage;
import org.keycloak.testsuite.pages.AppPage.RequestType; import org.keycloak.testsuite.pages.AppPage.RequestType;
import org.keycloak.testsuite.pages.LoginPage; import org.keycloak.testsuite.pages.LoginPage;
import org.keycloak.testsuite.pages.TermsAndConditionsPage; import org.keycloak.testsuite.pages.TermsAndConditionsPage;
import org.keycloak.testsuite.rule.KeycloakRule;
import org.keycloak.testsuite.rule.WebResource;
import org.keycloak.testsuite.rule.WebRule;
import org.openqa.selenium.WebDriver;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.jboss.arquillian.graphene.page.Page;
import org.junit.Before;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.TestRealmKeycloakTest;
import org.keycloak.testsuite.util.UserBuilder;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/ */
public class TermsAndConditionsTest { public class TermsAndConditionsTest extends TestRealmKeycloakTest {
@ClassRule
public static KeycloakRule keycloakRule = new KeycloakRule();
@Rule @Rule
public WebRule webRule = new WebRule(this); public AssertEvents events = new AssertEvents(this);
@Rule @Page
public AssertEvents events = new AssertEvents(keycloakRule);
@WebResource
protected WebDriver driver;
@WebResource
protected AppPage appPage; protected AppPage appPage;
@WebResource @Page
protected LoginPage loginPage; protected LoginPage loginPage;
@WebResource @Page
protected TermsAndConditionsPage termsPage; protected TermsAndConditionsPage termsPage;
@Override
public void configureTestRealm(RealmRepresentation testRealm) {
}
@Before @Before
public void before() { public void addTermsAndConditionRequiredAction() {
keycloakRule.configure(new KeycloakRule.KeycloakSetup() { UserRepresentation user = ActionUtil.findUserWithAdminClient(adminClient, "test-user@localhost");
@Override UserBuilder.edit(user).requiredAction(TermsAndConditions.PROVIDER_ID);
public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) { adminClient.realm("test").users().get(user.getId()).update(user);
UserModel user = manager.getSession().users().getUserByUsername("test-user@localhost", appRealm);
user.addRequiredAction(TermsAndConditions.PROVIDER_ID);
}
});
} }
@Test @Test
@ -87,7 +74,7 @@ public class TermsAndConditionsTest {
loginPage.login("test-user@localhost", "password"); loginPage.login("test-user@localhost", "password");
termsPage.assertCurrent(); Assert.assertTrue(termsPage.isCurrent());
termsPage.acceptTerms(); termsPage.acceptTerms();
@ -98,7 +85,7 @@ public class TermsAndConditionsTest {
events.expectLogin().session(sessionId).assertEvent(); events.expectLogin().session(sessionId).assertEvent();
// assert user attribute is properly set // assert user attribute is properly set
UserRepresentation user = keycloakRule.getUser("test", "test-user@localhost"); UserRepresentation user = ActionUtil.findUserWithAdminClient(adminClient, "test-user@localhost");
Map<String,List<String>> attributes = user.getAttributesAsListValues(); Map<String,List<String>> attributes = user.getAttributesAsListValues();
assertNotNull("timestamp for terms acceptance was not stored in user attributes", attributes); assertNotNull("timestamp for terms acceptance was not stored in user attributes", attributes);
List<String> termsAndConditions = attributes.get(TermsAndConditions.USER_ATTRIBUTE); List<String> termsAndConditions = attributes.get(TermsAndConditions.USER_ATTRIBUTE);
@ -121,7 +108,7 @@ public class TermsAndConditionsTest {
loginPage.login("test-user@localhost", "password"); loginPage.login("test-user@localhost", "password");
termsPage.assertCurrent(); Assert.assertTrue(termsPage.isCurrent());
termsPage.declineTerms(); termsPage.declineTerms();
@ -132,7 +119,7 @@ public class TermsAndConditionsTest {
// assert user attribute is properly removed // assert user attribute is properly removed
UserRepresentation user = keycloakRule.getUser("test", "test-user@localhost"); UserRepresentation user = ActionUtil.findUserWithAdminClient(adminClient, "test-user@localhost");
Map<String,List<String>> attributes = user.getAttributesAsListValues(); Map<String,List<String>> attributes = user.getAttributesAsListValues();
if (attributes != null) { if (attributes != null) {
assertNull("expected null for terms acceptance user attribute " + TermsAndConditions.USER_ATTRIBUTE, assertNull("expected null for terms acceptance user attribute " + TermsAndConditions.USER_ATTRIBUTE,

View file

@ -92,6 +92,36 @@ public class RealmBuilder {
return this; return this;
} }
public RealmBuilder otpLookAheadWindow(int i) {
rep.setOtpPolicyLookAheadWindow(i);
return this;
}
public RealmBuilder otpDigits(int i) {
rep.setOtpPolicyDigits(i);
return this;
}
public RealmBuilder otpPeriod(int i) {
rep.setOtpPolicyPeriod(i);
return this;
}
public RealmBuilder otpType(String type) {
rep.setOtpPolicyType(type);
return this;
}
public RealmBuilder otpAlgorithm(String algorithm) {
rep.setOtpPolicyAlgorithm(algorithm);
return this;
}
public RealmBuilder otpInitialCounter(int i) {
rep.setOtpPolicyInitialCounter(i);
return this;
}
public RealmRepresentation build() { public RealmRepresentation build() {
return rep; return rep;
} }

View file

@ -109,6 +109,14 @@ public class UserBuilder {
return this; return this;
} }
public UserBuilder requiredAction(String requiredAction) {
if (rep.getRequiredActions() == null) {
rep.setRequiredActions(new LinkedList<String>());
}
rep.getRequiredActions().add(requiredAction);
return this;
}
public UserBuilder serviceAccountId(String serviceAccountId) { public UserBuilder serviceAccountId(String serviceAccountId) {
rep.setServiceAccountClientId(serviceAccountId); rep.setServiceAccountClientId(serviceAccountId);
return this; return this;
@ -132,4 +140,4 @@ public class UserBuilder {
return rep; return rep;
} }
} }