From 4cba764ac34565cabed26213c01abc3fb89214ac Mon Sep 17 00:00:00 2001 From: vramik Date: Wed, 30 Sep 2015 14:23:24 +0200 Subject: [PATCH] add view-realm role to test user for test realm to avoid org.keycloak.services.ForbiddenException in test log --- .../org/keycloak/testsuite/admin/ApiUtil.java | 36 ++++++++++++++----- .../testsuite/console/page/events/Config.java | 5 +++ .../keycloak/testsuite/AbstractAuthTest.java | 21 ++++++++++- .../console/events/LoginEventsTest.java | 4 +-- 4 files changed, 54 insertions(+), 12 deletions(-) diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/admin/ApiUtil.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/admin/ApiUtil.java index eb202e5cd3..a196fc7601 100644 --- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/admin/ApiUtil.java +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/admin/ApiUtil.java @@ -25,8 +25,8 @@ import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.jboss.logging.Logger; import org.keycloak.admin.client.resource.ClientResource; -import org.keycloak.admin.client.resource.RoleScopeResource; import org.keycloak.admin.client.resource.UserResource; import org.keycloak.representations.idm.CredentialRepresentation; import static org.keycloak.representations.idm.CredentialRepresentation.PASSWORD; @@ -37,6 +37,8 @@ import org.keycloak.representations.idm.UserRepresentation; * Created by st on 28.05.15. */ public class ApiUtil { + + private static final Logger log = Logger.getLogger(ApiUtil.class); public static String getCreatedId(Response response) { URI location = response.getLocation(); @@ -96,16 +98,32 @@ public class ApiUtil { userResource.resetPassword(newCredential); } - public static void assignClientRoles(UserResource userResource, String clientId, String... roles) { - RoleScopeResource rsr = userResource.roles().clientLevel(clientId); - List rolesList = Arrays.asList(roles); - List realmMgmtRoles = new ArrayList<>(); - for (RoleRepresentation rr : rsr.listAvailable()) { - if (rolesList.contains(rr.getName())) { - realmMgmtRoles.add(rr); + public static void assignClientRoles(RealmResource realm, String userId, String clientName, String... roles) { + String realmName = realm.toRepresentation().getRealm(); + String clientId = ""; + for (ClientRepresentation clientRepresentation : realm.clients().findAll()) { + if (clientRepresentation.getClientId().equals(clientName)) { + clientId = clientRepresentation.getId(); } } - rsr.add(realmMgmtRoles); + + if (!clientId.isEmpty()) { + ClientResource clientResource = realm.clients().get(clientId); + + List roleRepresentations = new ArrayList<>(); + for (String roleName : roles) { + RoleRepresentation role = clientResource.roles().get(roleName).toRepresentation(); + roleRepresentations.add(role); + } + + UserResource userResource = realm.users().get(userId); + log.debug("assigning roles: " + Arrays.toString(roles) + " to user: \"" + + userResource.toRepresentation().getUsername() + "\" of client: \"" + + clientName + "\" in realm: \"" + realmName + "\""); + userResource.roles().clientLevel(clientId).add(roleRepresentations); + } else { + log.warn("client with name " + clientName + "doesn't exist in realm " + realmName); + } } } diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/console/page/events/Config.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/console/page/events/Config.java index be84772e0f..fc22df1200 100644 --- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/console/page/events/Config.java +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/console/page/events/Config.java @@ -2,6 +2,7 @@ package org.keycloak.testsuite.console.page.events; import org.keycloak.testsuite.console.page.fragment.OnOffSwitch; import org.keycloak.testsuite.page.Form; +import static org.keycloak.testsuite.util.WaitUtils.*; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; @@ -101,5 +102,9 @@ public class Config extends Events { public void clearAdminEvents() { clearAdminEventsButton.click(); } + + public void waitForClearEventsButtonPresent() { + waitAjaxForElement(clearLoginEventsButton); + } } } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AbstractAuthTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AbstractAuthTest.java index 98aa62ef56..31c9b77e57 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AbstractAuthTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AbstractAuthTest.java @@ -1,3 +1,20 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2013 Red Hat, Inc. and/or its affiliates. + * + * 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; import java.text.MessageFormat; @@ -10,7 +27,7 @@ import org.keycloak.admin.client.resource.RealmResource; import static org.keycloak.representations.idm.CredentialRepresentation.PASSWORD; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.UserRepresentation; -import static org.keycloak.testsuite.admin.ApiUtil.createUserAndResetPasswordWithAdminClient; +import static org.keycloak.testsuite.admin.ApiUtil.*; import static org.keycloak.testsuite.admin.Users.setPasswordFor; import org.keycloak.testsuite.auth.page.AuthRealm; import static org.keycloak.testsuite.auth.page.AuthRealm.TEST; @@ -56,6 +73,8 @@ public abstract class AbstractAuthTest extends AbstractKeycloakTest { log.debug("creating test user"); String id = createUserAndResetPasswordWithAdminClient(testRealmResource(), testUser, PASSWORD); testUser.setId(id); + + assignClientRoles(testRealmResource(), id, "realm-management", "view-realm"); } public static UserRepresentation createUserRepresentation(String username, String email, String firstName, String lastName, boolean enabled) { diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/console/events/LoginEventsTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/console/events/LoginEventsTest.java index 078c56df0c..d8d112463b 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/console/events/LoginEventsTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/console/events/LoginEventsTest.java @@ -21,14 +21,14 @@ import static org.keycloak.representations.idm.CredentialRepresentation.PASSWORD public class LoginEventsTest extends AbstractConsoleTest { @Page private LoginEvents loginEventsPage; - @Page private Config configPage; - + @Before public void beforeLoginEventsTest() { configPage.navigateTo(); configPage.form().setSaveEvents(true); + configPage.form().waitForClearEventsButtonPresent(); configPage.form().addSaveType("LOGIN"); configPage.form().addSaveType("LOGIN_ERROR"); configPage.form().addSaveType("LOGOUT");