8000 Refactor builders in testsuite part 2 by stianst · Pull Request #48454 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.keycloak.testframework.realm.ManagedRealm;
import org.keycloak.testframework.realm.RealmBuilder;
import org.keycloak.testframework.realm.RealmConfig;
import org.keycloak.testframework.realm.UserBuilder;
import org.keycloak.testframework.server.KeycloakUrls;
import org.keycloak.testsuite.util.oauth.AccessTokenResponse;

Expand Down Expand Up @@ -749,20 +750,20 @@ private static void createScopePermission(AuthorizationResource authz, String na
public static class TestRealmConfig implements RealmConfig {
@Override
public RealmBuilder configure(RealmBuilder realm) {
realm.addRole("admin");
realm.realmRoles("admin");

realm.addUser("admin-user")
realm.users(UserBuilder.create("admin-user")
.id(ADMIN_USER_ID)
.name("Admin", "User")
.email("admin@localhost")
.password("password")
.roles("admin");
.realmRoles("admin"));

realm.addUser("regular-user")
realm.users(UserBuilder.create("regular-user")
.id(REGULAR_USER_ID)
.name("Regular", "User")
.email("regular@localhost")
.password("password");
.password("password"));

return realm;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ the shorter suffix `Builder` to keep things consistently.

Additionally, some methods where updated for consistency across the builders:

* `RoleBuilder` method `setSingleAttribute` was renamed to `attribute`
* `RoleBuilder` method `setSingleAttribute` renamed to `attribute`
* `UserBuilder` method `roles` renamed to `realmRoles`
* `RealmBuilder` method `client` and `addClient` replaced with `clients`
* `RealmBuilder` method `user` and `addUser` replaced with `users`
* `RealmBuilder` method `addRole` replaced with `realmRoles`
* `RealmBuilder` method `addClientRole` replaced with `clientRoles`
* `RealmBuilder` method `addAuthenticationFlow` replaced with `authenticationFlows`
* `RealmBuilder` method `addClientScope` replaced with `clientScopes`
* `RealmBuilder` method `identityProvider` renamed to `identityProviders`
* `RealmBuilder` method `identityProviderMapper` renamed to `identityProviderMappers`
* `RealmBuilder` method `requiredAction` renamed to `requiredActions`

=== Identity Provider alias is now immutable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
import org.keycloak.testframework.annotations.InjectRealm;
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.annotations.TestSetup;
import org.keycloak.testframework.realm.ClientBuilder;
import org.keycloak.testframework.realm.ManagedClient;
import org.keycloak.testframework.realm.ManagedRealm;
import org.keycloak.testframework.realm.RealmBuilder;
import org.keycloak.testframework.realm.RealmConfig;
import org.keycloak.testframework.realm.UserBuilder;
import org.keycloak.testframework.server.KeycloakServerConfig;
import org.keycloak.testframework.server.KeycloakServerConfigBuilder;

Expand Down Expand Up @@ -698,76 +700,73 @@ public static class AuthorizationRealmConfig implements RealmConfig {
public RealmBuilder configure(RealmBuilder realm) {
realm.name("authztest");
realm.adminPermissionsEnabled(true);
realm.addClient("test-client")
.secret("test-secret")
.directAccessGrantsEnabled(true);
realm.clients(ClientBuilder.create("test-client").secret("test-secret").directAccessGrantsEnabled(true));

// Role: realm-admin
realm.addUser("realm-admin")
realm.users(UserBuilder.create("realm-admin")
.name("Realm", "Admin")
.email("realmadmin@localhost")
.emailVerified(true)
.password("password")
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.REALM_ADMIN);
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.REALM_ADMIN));
CURRENT_USERS.add("realm-admin");

// Role: view-clients
realm.addUser("view-clients")
realm.users(UserBuilder.create("view-clients")
.name("View", "Clients")
.email("viewclients@localhost")
.emailVerified(true)
.password("password")
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.VIEW_CLIENTS);
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.VIEW_CLIENTS));
CURRENT_USERS.add("view-clients");

// Role: manage-clients
realm.addUser("manage-clients")
realm.users(UserBuilder.create("manage-clients")
.name("Manage", "Clients")
.email("manageclients@localhost")
.emailVerified(true)
.password("password")
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.MANAGE_CLIENTS);
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.MANAGE_CLIENTS));
CURRENT_USERS.add("manage-clients");

// Role: query-clients
realm.addUser("query-clients")
realm.users(UserBuilder.create("query-clients")
.name("Query", "Clients")
.email("queryclients@localhost")
.emailVerified(true)
.password("password")
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.QUERY_CLIENTS);
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.QUERY_CLIENTS));
CURRENT_USERS.add("query-clients");

// NO role
realm.addUser("no-access")
realm.users(UserBuilder.create("no-access")
.name("No", "Access")
.email("noaccess@localhost")
.emailVerified(true)
.password("password");
.password("password"));
CURRENT_USERS.add("no-access");

// Role: manage-realm
realm.addUser("manage-realm")
realm.users(UserBuilder.create("manage-realm")
.name("Manage", "Realm")
.email("managerealm@localhost")
.emailVerified(true)
.password("password")
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.MANAGE_REALM);
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.MANAGE_REALM));
CURRENT_USERS.add("manage-realm");

// FGAP v2
// fgap-user has QUERY_CLIENTS role but will be granted fine-grained permissions for specific clients
realm.addUser("fgap-user")
realm.users(UserBuilder.create("fgap-user")
.id(FGAP_USER_ID)
.name("FGAP", "User")
.email("fgapuser@localhost")
.emailVerified(true)
.password("password")
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.QUERY_CLIENTS);
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.QUERY_CLIENTS));
CURRENT_USERS.add("fgap-user");

realm.addClient("fgap-denied-client")
.enabled(true);
realm.clients(ClientBuilder.create("fgap-denied-client").enabled(true));

return realm;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.keycloak.testframework.annotations.InjectHttpClient;
import org.keycloak.testframework.annotations.InjectRealm;
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.realm.ClientBuilder;
import org.keycloak.testframework.realm.ManagedClient;
import org.keycloak.testframework.realm.ManagedRealm;
import org.keycloak.testframework.realm.RealmBuilder;
Expand Down Expand Up @@ -1017,9 +1018,9 @@ public static class NoAccessRealmConfig implements RealmConfig {

@Override
public RealmBuilder configure(RealmBuilder realm) {
realm.addClient("myclient")
realm.clients(ClientBuilder.create("myclient")
.secret("mysecret")
.serviceAccountsEnabled(true);
.serviceAccountsEnabled(true));
return realm;
}
}
Expand Down
27 changes: 7 additions & 20 deletions test-framework/builders/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,25 @@
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<exclusions>
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.

removing this exclusion -> wouldn't it bring multiple unused dependencies inside the test framework?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Doesn't matter as the framework as a dep on this regardless due to the embedded server

<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi</artifactId>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
<artifactId>keycloak-services</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi-private</artifactId>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
<artifactId>keycloak-model-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.logmanager</groupId>
<artifactId>jboss-logmanager</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,52 @@
* limitations under the License.
*/

package org.keycloak.testsuite.util;
package org.keycloak.testframework.realm;


import org.keycloak.representations.idm.AuthenticationExecutionRepresentation;

/**
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
public class ExecutionBuilder {
public class AuthenticationExecutionBuilder extends Builder<AuthenticationExecutionRepresentation> {

private AuthenticationExecutionRepresentation rep = new AuthenticationExecutionRepresentation();

public static ExecutionBuilder create() {
return new ExecutionBuilder();
private AuthenticationExecutionBuilder(AuthenticationExecutionRepresentation rep) {
super(rep);
}

private ExecutionBuilder() {
public static AuthenticationExecutionBuilder create() {
return new AuthenticationExecutionBuilder(new AuthenticationExecutionRepresentation());
}

public ExecutionBuilder id(String id) {
public AuthenticationExecutionBuilder id(String id) {
rep.setId(id);
return this;
}

public ExecutionBuilder parentFlow(String parentFlow) {
public AuthenticationExecutionBuilder parentFlow(String parentFlow) {
rep.setParentFlow(parentFlow);
return this;
}

public ExecutionBuilder requirement(String requirement) {
public AuthenticationExecutionBuilder requirement(String requirement) {
rep.setRequirement(requirement);
return this;
}

public ExecutionBuilder authenticator(String authenticator) {
public AuthenticationExecutionBuilder authenticator(String authenticator) {
rep.setAuthenticator(authenticator);
return this;
}

public ExecutionBuilder priority(int priority) {
public AuthenticationExecutionBuilder priority(int priority) {
rep.setPriority(priority);
return this;
}

public ExecutionBuilder authenticatorFlow(boolean authenticatorFlow) {
public AuthenticationExecutionBuilder authenticatorFlow(boolean authenticatorFlow) {
rep.setAuthenticatorFlow(authenticatorFlow);
return this;
}

public AuthenticationExecutionRepresentation build() {
return rep;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

import org.keycloak.representations.idm.AuthenticationExecutionExportRepresentation;

public class AuthenticationExecutionExportBuilder {

private final AuthenticationExecutionExportRepresentation rep;
public class AuthenticationExecutionExportBuilder extends Builder<AuthenticationExecutionExportRepresentation> {

private AuthenticationExecutionExportBuilder(AuthenticationExecutionExportRepresentation rep) {
this.rep = rep;
super(rep);
}

public static AuthenticationExecutionExportBuilder create() {
AuthenticationExecutionExportRepresentation rep = new AuthenticationExecutionExportRepresentation();
return new AuthenticationExecutionExportBuilder(rep);
return new AuthenticationExecutionExportBuilder(new AuthenticationExecutionExportRepresentation());
}

public static AuthenticationExecutionExportBuilder authenticator(String authenticator, String requirement, Integer priority, boolean userSetupAllowed) {
return create().authenticatorFlow(false).authenticator(authenticator).requirement(requirement).priority(priority);
}

public static AuthenticationExecutionExportBuilder alias(String flowAlias, String requirement, Integer priority, boolean userSetupAllowed) {
return create().authenticatorFlow(true).flowAlias(flowAlias).requirement(requirement).priority(priority);
}

public static AuthenticationExecutionExportBuilder update(AuthenticationExecutionExportRepresentation rep) {
Expand All @@ -39,17 +44,14 @@ public AuthenticationExecutionExportBuilder priority(Integer priority) {
return this;
}

public AuthenticationExecutionExportBuilder userSetupAllowed(boolean allowed) {
rep.setUserSetupAllowed(allowed);
public AuthenticationExecutionExportBuilder authenticatorFlow(boolean authenticatorFlow) {
rep.setAuthenticatorFlow(authenticatorFlow);
return this;
}

public AuthenticationExecutionExportBuilder authenticatorFlow(boolean enabled) {
rep.setAuthenticatorFlow(enabled);
public AuthenticationExecutionExportBuilder userSetupAllowed(boolean userSetupAllowed) {
rep.setUserSetupAllowed(userSetupAllowed);
return this;
}

public AuthenticationExecutionExportRepresentation build() {
return rep;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.keycloak.testframework.realm;

import org.keycloak.representations.idm.AuthenticationFlowRepresentation;

public class AuthenticationFlowBuilder extends Builder<AuthenticationFlowRepresentation> {

private AuthenticationFlowBuilder(AuthenticationFlowRepresentation rep) {
super(rep);
}

public static AuthenticationFlowBuilder create() {
return new AuthenticationFlowBuilder(new AuthenticationFlowRepresentation());
}

public static AuthenticationFlowBuilder create(String alias, String description, String providerId, boolean topLevel, boolean builtIn) {
return create().alias(alias).description(description).providerId(providerId).topLevel(topLevel).builtIn(builtIn);
}

public static AuthenticationFlowBuilder update(AuthenticationFlowRepresentation rep) {
return new AuthenticationFlowBuilder(rep);
}

public AuthenticationFlowBuilder alias(String alias) {
rep.setAlias(alias);
return this;
}

public AuthenticationFlowBuilder description(String description) {
rep.setDescription(description);
return this;
}

public AuthenticationFlowBuilder providerId(String providerId) {
rep.setProviderId(providerId);
return this;
}

public AuthenticationFlowBuilder topLevel(boolean enabled) {
rep.setTopLevel(enabled);
return this;
}

public AuthenticationFlowBuilder builtIn(boolean enabled) {
rep.setBuiltIn(enabled);
return this;
}

public AuthenticationFlowBuilder authenticationExecutions(AuthenticationExecutionExportBuilder... authenticationExecutions) {
rep.setAuthenticationExecutions(combine(rep.getAuthenticationExecutions(), authenticationExecutions));
return this;
}

}
Loading
Loading
0