[keycloak-user] Create Test Users - IT

Dana Danet Dana.Danet at Evisions.com
Thu Jan 5 14:18:44 EST 2017


There must be something I am missing as I can’t get the Credentials to set when programmatically creating a test user for integration tests to a running Keycloak 2.5.0 instance with a known realm.  The user is created.

When setting a breakpoint in my code I see that my user is created but no credentials are created.  This returns 0 tuples.

select * from credential c where c.user_id = (select u.id from user_entity u where u.username = 'test-user’)


Below is my code:


@Before
public void setup() {
    log.debug("Setting up test harness user.");

    keycloak = KeycloakBuilder.builder()
                              .serverUrl(authServerUrl)
                              .realm(realm)
                              .username(adminUser)
                              .password(adminPassword)
                              .clientId("admin-cli")
                              .resteasyClient(new ResteasyClientBuilder()
                                      .connectionPoolSize(10)
                                      .build()
                              ).build();

    setupTestUser();
}

private void setupTestUser() {
    log.debug("\nSetting up test harness user.");

    /*
      Create the credentials via test config values
     */
    CredentialRepresentation credential = new CredentialRepresentation();
    credential.setType(CredentialRepresentation.PASSWORD);
    credential.setValue(password);
    credential.setTemporary(false);

    /*
      Create the user via test config values
     */
    user = new UserRepresentation();
    user.setUsername(username);
    user.setFirstName("Test");
    user.setLastName("User");
    user.setCredentials(Arrays.asList(credential));
    user.setEnabled(true) ;

    Response result = keycloak.realm(realm).users().create(user);

    final String locationHeader = result.getHeaderString("Location");
    final String userId = locationHeader.replaceAll(".*/(.*)$", "$1");
    user.setId(userId);

    log.debug("\n\nTest Harness UserId  ************** {}\n", userId);
}

@After
public void tearDown() {
    keycloak.realm(realm).users().get(user.getId()).remove();
}



More information about the keycloak-user mailing list