add view-realm role to test user for test realm to avoid org.keycloak.services.ForbiddenException in test log
This commit is contained in:
parent
15a5c87cc5
commit
4cba764ac3
4 changed files with 54 additions and 12 deletions
|
@ -25,8 +25,8 @@ import java.net.URI;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import org.jboss.logging.Logger;
|
||||||
import org.keycloak.admin.client.resource.ClientResource;
|
import org.keycloak.admin.client.resource.ClientResource;
|
||||||
import org.keycloak.admin.client.resource.RoleScopeResource;
|
|
||||||
import org.keycloak.admin.client.resource.UserResource;
|
import org.keycloak.admin.client.resource.UserResource;
|
||||||
import org.keycloak.representations.idm.CredentialRepresentation;
|
import org.keycloak.representations.idm.CredentialRepresentation;
|
||||||
import static org.keycloak.representations.idm.CredentialRepresentation.PASSWORD;
|
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.
|
* Created by st on 28.05.15.
|
||||||
*/
|
*/
|
||||||
public class ApiUtil {
|
public class ApiUtil {
|
||||||
|
|
||||||
|
private static final Logger log = Logger.getLogger(ApiUtil.class);
|
||||||
|
|
||||||
public static String getCreatedId(Response response) {
|
public static String getCreatedId(Response response) {
|
||||||
URI location = response.getLocation();
|
URI location = response.getLocation();
|
||||||
|
@ -96,16 +98,32 @@ public class ApiUtil {
|
||||||
userResource.resetPassword(newCredential);
|
userResource.resetPassword(newCredential);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void assignClientRoles(UserResource userResource, String clientId, String... roles) {
|
public static void assignClientRoles(RealmResource realm, String userId, String clientName, String... roles) {
|
||||||
RoleScopeResource rsr = userResource.roles().clientLevel(clientId);
|
String realmName = realm.toRepresentation().getRealm();
|
||||||
List<String> rolesList = Arrays.asList(roles);
|
String clientId = "";
|
||||||
List<RoleRepresentation> realmMgmtRoles = new ArrayList<>();
|
for (ClientRepresentation clientRepresentation : realm.clients().findAll()) {
|
||||||
for (RoleRepresentation rr : rsr.listAvailable()) {
|
if (clientRepresentation.getClientId().equals(clientName)) {
|
||||||
if (rolesList.contains(rr.getName())) {
|
clientId = clientRepresentation.getId();
|
||||||
realmMgmtRoles.add(rr);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rsr.add(realmMgmtRoles);
|
|
||||||
|
if (!clientId.isEmpty()) {
|
||||||
|
ClientResource clientResource = realm.clients().get(clientId);
|
||||||
|
|
||||||
|
List<RoleRepresentation> 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package org.keycloak.testsuite.console.page.events;
|
||||||
|
|
||||||
import org.keycloak.testsuite.console.page.fragment.OnOffSwitch;
|
import org.keycloak.testsuite.console.page.fragment.OnOffSwitch;
|
||||||
import org.keycloak.testsuite.page.Form;
|
import org.keycloak.testsuite.page.Form;
|
||||||
|
import static org.keycloak.testsuite.util.WaitUtils.*;
|
||||||
import org.openqa.selenium.By;
|
import org.openqa.selenium.By;
|
||||||
import org.openqa.selenium.WebElement;
|
import org.openqa.selenium.WebElement;
|
||||||
import org.openqa.selenium.support.FindBy;
|
import org.openqa.selenium.support.FindBy;
|
||||||
|
@ -101,5 +102,9 @@ public class Config extends Events {
|
||||||
public void clearAdminEvents() {
|
public void clearAdminEvents() {
|
||||||
clearAdminEventsButton.click();
|
clearAdminEventsButton.click();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void waitForClearEventsButtonPresent() {
|
||||||
|
waitAjaxForElement(clearLoginEventsButton);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
package org.keycloak.testsuite;
|
||||||
|
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
@ -10,7 +27,7 @@ import org.keycloak.admin.client.resource.RealmResource;
|
||||||
import static org.keycloak.representations.idm.CredentialRepresentation.PASSWORD;
|
import static org.keycloak.representations.idm.CredentialRepresentation.PASSWORD;
|
||||||
import org.keycloak.representations.idm.RealmRepresentation;
|
import org.keycloak.representations.idm.RealmRepresentation;
|
||||||
import org.keycloak.representations.idm.UserRepresentation;
|
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 static org.keycloak.testsuite.admin.Users.setPasswordFor;
|
||||||
import org.keycloak.testsuite.auth.page.AuthRealm;
|
import org.keycloak.testsuite.auth.page.AuthRealm;
|
||||||
import static org.keycloak.testsuite.auth.page.AuthRealm.TEST;
|
import static org.keycloak.testsuite.auth.page.AuthRealm.TEST;
|
||||||
|
@ -56,6 +73,8 @@ public abstract class AbstractAuthTest extends AbstractKeycloakTest {
|
||||||
log.debug("creating test user");
|
log.debug("creating test user");
|
||||||
String id = createUserAndResetPasswordWithAdminClient(testRealmResource(), testUser, PASSWORD);
|
String id = createUserAndResetPasswordWithAdminClient(testRealmResource(), testUser, PASSWORD);
|
||||||
testUser.setId(id);
|
testUser.setId(id);
|
||||||
|
|
||||||
|
assignClientRoles(testRealmResource(), id, "realm-management", "view-realm");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UserRepresentation createUserRepresentation(String username, String email, String firstName, String lastName, boolean enabled) {
|
public static UserRepresentation createUserRepresentation(String username, String email, String firstName, String lastName, boolean enabled) {
|
||||||
|
|
|
@ -21,14 +21,14 @@ import static org.keycloak.representations.idm.CredentialRepresentation.PASSWORD
|
||||||
public class LoginEventsTest extends AbstractConsoleTest {
|
public class LoginEventsTest extends AbstractConsoleTest {
|
||||||
@Page
|
@Page
|
||||||
private LoginEvents loginEventsPage;
|
private LoginEvents loginEventsPage;
|
||||||
|
|
||||||
@Page
|
@Page
|
||||||
private Config configPage;
|
private Config configPage;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void beforeLoginEventsTest() {
|
public void beforeLoginEventsTest() {
|
||||||
configPage.navigateTo();
|
configPage.navigateTo();
|
||||||
configPage.form().setSaveEvents(true);
|
configPage.form().setSaveEvents(true);
|
||||||
|
configPage.form().waitForClearEventsButtonPresent();
|
||||||
configPage.form().addSaveType("LOGIN");
|
configPage.form().addSaveType("LOGIN");
|
||||||
configPage.form().addSaveType("LOGIN_ERROR");
|
configPage.form().addSaveType("LOGIN_ERROR");
|
||||||
configPage.form().addSaveType("LOGOUT");
|
configPage.form().addSaveType("LOGOUT");
|
||||||
|
|
Loading…
Reference in a new issue