RHSSO-130: AccessTokenTest migration

This commit is contained in:
Bruno Oliveira 2016-04-08 14:08:33 -03:00
parent 2047363e86
commit 1cc4ca2e71
9 changed files with 522 additions and 380 deletions

View file

@ -88,8 +88,8 @@ public class RoleNameMapper extends AbstractOIDCProtocolMapper implements OIDCAc
}
@Override
public AccessToken transformAccessToken(AccessToken token, ProtocolMapperModel mappingModel, KeycloakSession session,
UserSessionModel userSession, ClientSessionModel clientSession) {
public AccessToken transformAccessToken(AccessToken token, ProtocolMapperModel mappingModel, KeycloakSession session2,
UserSessionModel userSession2, ClientSessionModel clientSessio2n) {
String role = mappingModel.getConfig().get(ROLE_CONFIG);
String newName = mappingModel.getConfig().get(NEW_ROLE_NAME);

View file

@ -655,4 +655,4 @@ public class OAuthClient {
return publicKeys.get(realm);
}
}
}

View file

@ -99,6 +99,10 @@ public class ApiUtil {
return user;
}
public static UserResource findUserByUsernameId(RealmResource realm, String username) {
return realm.users().get(findUserByUsername(realm, username).getId());
}
public static String createUserWithAdminClient(RealmResource realm, UserRepresentation user) {
Response response = realm.users().create(user);
String createdId = getCreatedId(response);
@ -158,5 +162,4 @@ public class ApiUtil {
}
return contains;
}
}

View file

@ -17,17 +17,10 @@
package org.keycloak.testsuite.util;
import org.codehaus.plexus.util.xml.pull.XmlSerializer;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import javax.ws.rs.client.Client;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
@ -83,7 +76,6 @@ public class ClientBuilder {
Map<String, String> attributes = new HashMap<String, String>();
attributes.put(name, value);
rep.setAttributes(attributes);
return this;
}

View file

@ -0,0 +1,93 @@
package org.keycloak.testsuite.util;
import org.keycloak.models.utils.ModelToRepresentation;
import org.keycloak.protocol.oidc.mappers.AddressMapper;
import org.keycloak.protocol.oidc.mappers.HardcodedClaim;
import org.keycloak.protocol.oidc.mappers.HardcodedRole;
import org.keycloak.protocol.oidc.mappers.RoleNameMapper;
import org.keycloak.protocol.oidc.mappers.UserAttributeMapper;
import org.keycloak.representations.idm.ProtocolMapperRepresentation;
/**
* @author <a href="mailto:bruno@abstractj.org">Bruno Oliveira</a>.
*/
public class ProtocolMapperUtil {
/**
* This is the migration of the method present at RoleNameMapper
*
* @param name
* @param role
* @param newName
* @return
*/
public static ProtocolMapperRepresentation createRoleNameMapper(String name,
String role,
String newName) {
return ModelToRepresentation.toRepresentation(RoleNameMapper.create(name, role, newName));
}
public static ProtocolMapperRepresentation createHardcodedRole(String name,
String role) {
return ModelToRepresentation.toRepresentation(HardcodedRole.create(name, role));
}
/**
* This is the migration of the method present at AddressMapper
*
* @param idToken
* @param accessToken
* @return
*/
public static ProtocolMapperRepresentation createAddressMapper(boolean idToken, boolean accessToken) {
return ModelToRepresentation.toRepresentation(AddressMapper.createAddressMapper(idToken, accessToken));
}
/**
* This is the migration of the method present at HardcodedClaim
*
* @param name
* @param hardcodedName
* @param hardcodedValue
* @param claimType
* @param consentRequired
* @param consentText
* @param accessToken
* @param idToken
* @return
*/
public static ProtocolMapperRepresentation createHardcodedClaim(String name,
String hardcodedName,
String hardcodedValue, String claimType,
boolean consentRequired, String consentText,
boolean accessToken, boolean idToken) {
return ModelToRepresentation.toRepresentation(HardcodedClaim.create(name, hardcodedName, hardcodedValue,
claimType, consentRequired, consentText, accessToken, idToken));
}
/**
* Migrated from UserAttributeMapper
*
* @param name
* @param userAttribute
* @param tokenClaimName
* @param claimType
* @param consentRequired
* @param consentText
* @param accessToken
* @param idToken
* @param multivalued
* @return
*/
public static ProtocolMapperRepresentation createClaimMapper(String name,
String userAttribute,
String tokenClaimName, String claimType,
boolean consentRequired, String consentText,
boolean accessToken, boolean idToken, boolean multivalued) {
return ModelToRepresentation.toRepresentation(UserAttributeMapper.createClaimMapper(name, userAttribute, tokenClaimName,
claimType, consentRequired, consentText, accessToken, idToken, multivalued));
}
}

View file

@ -0,0 +1,26 @@
package org.keycloak.testsuite.util;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.RealmRepresentation;
/**
* @author <a href="mailto:bruno@abstractj.org">Bruno Oliveira</a>.
*/
public class RealmManager {
private static RealmResource realm;
private RealmManager() {
}
public static RealmManager realm(RealmResource realm) {
RealmManager.realm = realm;
return new RealmManager();
}
public void accessCodeLifeSpan(Integer accessCodeLifespan) {
RealmRepresentation realmRepresentation = realm.toRepresentation();
realmRepresentation.setAccessCodeLifespan(accessCodeLifespan);
realm.update(realmRepresentation);
}
}

View file

@ -20,6 +20,7 @@ package org.keycloak.testsuite.util;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@ -64,8 +65,9 @@ public class UserBuilder {
public UserBuilder role(String role) {
if (rep.getRealmRoles() == null) {
rep.setRealmRoles(new LinkedList<String>());
rep.setRealmRoles(new ArrayList<String>());
}
rep.getRealmRoles().add(role);
return this;
}

View file

@ -0,0 +1,74 @@
package org.keycloak.testsuite.util;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.UserRepresentation;
import java.util.ArrayList;
import java.util.Arrays;
import static org.keycloak.testsuite.admin.ApiUtil.findUserByUsernameId;
/**
* @author <a href="mailto:bruno@abstractj.org">Bruno Oliveira</a>.
*/
public class UserManager {
private static RealmResource realm;
private UserManager() {
}
public static UserManager realm(RealmResource realm) {
UserManager.realm = realm;
return new UserManager();
}
public UserManagerBuilder username(String username) {
return new UserManagerBuilder(findUserByUsernameId(realm, username));
}
public UserManagerBuilder user(UserResource user) {
return new UserManagerBuilder(user);
}
public class UserManagerBuilder {
private final UserResource userResource;
public UserManagerBuilder(UserResource userResource) {
this.userResource = userResource;
}
public void removeRequiredAction(String action) {
UserRepresentation user = initializeRequiredActions();
user.getRequiredActions().remove(action);
userResource.update(user);
}
public void addRequiredAction(String... actions) {
UserRepresentation user = initializeRequiredActions();
user.setRequiredActions(Arrays.asList(actions));
userResource.update(user);
}
public void assignRoles(String... roles) {
UserRepresentation user = userResource.toRepresentation();
if (user != null && user.getRealmRoles() == null) {
user.setRealmRoles(new ArrayList<String>());
}
user.setRealmRoles(Arrays.asList(roles));
userResource.update(user);
}
private UserRepresentation initializeRequiredActions() {
UserRepresentation user = userResource.toRepresentation();
if (user != null && user.getRequiredActions() == null) {
user.setRequiredActions(new ArrayList<String>());
}
return user;
}
}
}