8000 attachTo for UserSupplier by graziang · Pull Request #48102 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@
String ref() default "";

String realmRef() default "";

/**
* Attach to an existing user instead of creating one; when attaching to an existing user the config will be ignored
* and the user will not be deleted automatically.
*
* @return the username of the existing user to attach to
*/
String attachTo() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,29 @@
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.testframework.annotations.InjectClient;
import org.keycloak.testframework.injection.AnnotationFields;
import org.keycloak.testframework.injection.DependenciesBuilder;
import org.keycloak.testframework.injection.Dependency;
import org.keycloak.testframework.injection.InstanceContext;
import org.keycloak.testframework.injection.RequestedInstance;
import org.keycloak.testframework.injection.StringUtil;
import org.keycloak.testframework.injection.Supplier;
import org.keycloak.testframework.injection.SupplierHelpers;
import org.keycloak.testframework.util.ApiUtil;

public class ClientSupplier implements Supplier<ManagedClient, InjectClient> {

@Override
public String getRef(InjectClient annotation) {
String ref = StringUtil.convertEmptyToNull(SupplierHelpers.getAnnotationField(annotation, AnnotationFields.REF));
if (ref != null) {
return ref;
}
// Fallback to attachTo
String attachTo = annotation.attachTo();
return attachTo.isEmpty() ? null : attachTo;
}

Comment on lines +24 to +34
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove it, after a team discussion, this is not the way we want support current test-framework lifecycle.

Suggested change
@Override
public String getRef(InjectClient annotation) {
String ref = StringUtil.convertEmptyToNull(SupplierHelpers.getAnnotationField(annotation, AnnotationFields.REF));
if (ref != null) {
return ref;
}
// Fallback to attachTo
String attachTo = annotation.attachTo();
return attachTo.isEmpty() ? null : attachTo;
}

@Override
public List<Dependency> getDependencies(RequestedInstance<ManagedClient, InjectClient> instanceContext) {
return DependenciesBuilder.create(ManagedRealm.class, instanceContext.getAnnotation().realmRef()).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,45 @@ public List<Dependency> getDependencies(RequestedInstance<ManagedUser, InjectUse
public ManagedUser getValue(InstanceContext<ManagedUser, InjectUser> instanceContext) {
ManagedRealm realm = instanceContext.getDependency(ManagedRealm.class, instanceContext.getAnnotation().realmRef());

UserConfig config = SupplierHelpers.getInstanceWithInjectedFields(instanceContext.getAnnotation().config(), instanceContext);
UserRepresentation userRepresentation = config.configure(UserBuilder.create()).build();
String attachTo = instanceContext.getAnnotation().attachTo();
boolean managed = attachTo.isEmpty();

if (userRepresentation.getUsername() == null) {
String username = SupplierHelpers.createName(instanceContext);
userRepresentation.setUsername(username);
}
UserRepresentation userRepresentation;

if (userRepresentation.getRealmRoles() != null || userRepresentation.getClientRoles() != null) {
throw new UnsupportedOperationException("Creating user with roles or client roles is not supported!");
}
if (managed) {
UserConfig config = SupplierHelpers.getInstanceWithInjectedFields(instanceContext.getAnnotation().config(), instanceContext);
userRepresentation = config.configure(UserBuilder.create()).build();

try (Response response = realm.admin().users().create(userRepresentation)) {
if (Status.CONFLICT.equals(Status.fromStatusCode(response.getStatus()))) {
throw new IllegalStateException("User already exist with username: " + userRepresentation.getUsername());
if (userRepresentation.getUsername() == null) {
String username = SupplierHelpers.createName(instanceContext);
userRepresentation.setUsername(username);
}
String uuid = ApiUtil.getCreatedId(response);

instanceContext.addNote(USER_UUID_KEY, uuid);
if (userRepresentation.getRealmRoles() != null || userRepresentation.getClientRoles() != null) {
throw new UnsupportedOperationException("Creating user with roles or client roles is not supported!");
}

UserResource userResource = realm.admin().users().get(uuid);
userRepresentation.setId(uuid);
try (Response response = realm.admin().users().create(userRepresentation)) {
if (Status.CONFLICT.equals(Status.fromStatusCode(response.getStatus()))) {
throw new IllegalStateException("User already exist with username: " + userRepresentation.getUsername());
}
String uuid = ApiUtil.getCreatedId(response);
userRepresentation.setId(uuid);

return new ManagedUser(userRepresentation, userResource);
instanceContext.addNote(USER_UUID_KEY, uuid);
}
} else {
List<UserRepresentation> users = realm.admin().users().searchByUsername(attachTo, true);
if (users.isEmpty()) {
throw new IllegalStateException("No user found with username: " + attachTo);
}
userRepresentation = users.get(0);
}

instanceContext.addNote("managed", managed);

UserResource userResource = realm.admin().users().get(userRepresentation.getId());
return new ManagedUser(userRepresentation, userResource);
}

@Override
Expand All @@ -64,9 +78,11 @@ public boolean compatible(InstanceContext<ManagedUser, InjectUser> a, RequestedI

@Override
public v 5FFD oid close(InstanceContext<ManagedUser, InjectUser> instanceContext) {
try {
instanceContext.getValue().admin().remove();
} catch (NotFoundException ex) {}
if (instanceContext.getNote("managed", Boolean.class)) {
try {
instanceContext.getValue().admin().remove();
} catch (NotFoundException ex) {
}
}
}

}
Loading
0