@Test
  public void updateUserWithNewUsernameNotPossible() {
    String id = createUser();

    UserResource user = realm.users().get(id);
    UserRepresentation userRep = user.toRepresentation();
    userRep.setUsername("user11");
    user.update(userRep);

    userRep = realm.users().get(id).toRepresentation();
    assertEquals("user1", userRep.getUsername());
  }
  public void setRequiredActionEnabled(
      String realm, String userId, String requiredAction, boolean enabled) {
    UsersResource usersResource = adminClient.realm(realm).users();

    UserResource userResource = usersResource.get(userId);
    UserRepresentation userRepresentation = userResource.toRepresentation();

    List<String> requiredActions = userRepresentation.getRequiredActions();
    if (enabled && !requiredActions.contains(requiredAction)) {
      requiredActions.add(requiredAction);
    } else if (!enabled && requiredActions.contains(requiredAction)) {
      requiredActions.remove(requiredAction);
    }

    userResource.update(userRepresentation);
  }
  @Test
  public void updateUserWithNewUsernameAccessingViaOldUsername() {
    switchEditUsernameAllowedOn();
    createUser();

    try {
      UserResource user = realm.users().get("user1");
      UserRepresentation userRep = user.toRepresentation();
      userRep.setUsername("user1");
      user.update(userRep);

      realm.users().get("user11").toRepresentation();
      fail("Expected failure");
    } catch (ClientErrorException e) {
      assertEquals(404, e.getResponse().getStatus());
    }
  }
  @Test
  public void updateUserWithExistingUsername() {
    switchEditUsernameAllowedOn();
    createUser();

    UserRepresentation userRep = new UserRepresentation();
    userRep.setUsername("user2");
    Response response = realm.users().create(userRep);
    String createdId = ApiUtil.getCreatedId(response);
    response.close();

    try {
      UserResource user = realm.users().get(createdId);
      userRep = user.toRepresentation();
      userRep.setUsername("user1");
      user.update(userRep);
      fail("Expected failure");
    } catch (ClientErrorException e) {
      assertEquals(409, e.getResponse().getStatus());
    }
  }
  @Test
  public void getFederatedIdentities() {
    // Add sample identity provider
    addSampleIdentityProvider();

    // Add sample user
    String id = createUser();
    UserResource user = realm.users().get(id);
    assertEquals(0, user.getFederatedIdentity().size());

    // Add social link to the user
    FederatedIdentityRepresentation link = new FederatedIdentityRepresentation();
    link.setUserId("social-user-id");
    link.setUserName("social-username");
    Response response = user.addFederatedIdentity("social-provider-id", link);
    assertEquals(204, response.getStatus());

    // Verify social link is here
    user = realm.users().get(id);
    List<FederatedIdentityRepresentation> federatedIdentities = user.getFederatedIdentity();
    assertEquals(1, federatedIdentities.size());
    link = federatedIdentities.get(0);
    assertEquals("social-provider-id", link.getIdentityProvider());
    assertEquals("social-user-id", link.getUserId());
    assertEquals("social-username", link.getUserName());

    // Remove social link now
    user.removeFederatedIdentity("social-provider-id");
    assertEquals(0, user.getFederatedIdentity().size());

    removeSampleIdentityProvider();
  }
  @Test
  public void addRequiredAction() {
    String id = createUser();

    UserResource user = realm.users().get(id);
    assertTrue(user.toRepresentation().getRequiredActions().isEmpty());

    UserRepresentation userRep = user.toRepresentation();
    userRep.getRequiredActions().add("UPDATE_PASSWORD");
    user.update(userRep);

    assertEquals(1, user.toRepresentation().getRequiredActions().size());
    assertEquals("UPDATE_PASSWORD", user.toRepresentation().getRequiredActions().get(0));
  }
  @Test
  public void sendVerifyEmail() {
    UserRepresentation userRep = new UserRepresentation();
    userRep.setUsername("user1");
    Response response = realm.users().create(userRep);
    String id = ApiUtil.getCreatedId(response);
    response.close();

    UserResource user = realm.users().get(id);

    try {
      user.sendVerifyEmail();
      fail("Expected failure");
    } catch (ClientErrorException e) {
      assertEquals(400, e.getResponse().getStatus());

      ErrorRepresentation error = e.getResponse().readEntity(ErrorRepresentation.class);
      Assert.assertEquals("User email missing", error.getErrorMessage());
    }
    try {
      userRep = user.toRepresentation();
      userRep.setEmail("user1@localhost");
      userRep.setEnabled(false);
      user.update(userRep);
      user.sendVerifyEmail();
      fail("Expected failure");
    } catch (ClientErrorException e) {
      assertEquals(400, e.getResponse().getStatus());

      ErrorRepresentation error = e.getResponse().readEntity(ErrorRepresentation.class);
      Assert.assertEquals("User is disabled", error.getErrorMessage());
    }
    try {
      userRep.setEnabled(true);
      user.update(userRep);
      user.sendVerifyEmail("invalidClientId");
      fail("Expected failure");
    } catch (ClientErrorException e) {
      assertEquals(400, e.getResponse().getStatus());

      ErrorRepresentation error = e.getResponse().readEntity(ErrorRepresentation.class);
      Assert.assertEquals("invalidClientId not enabled", error.getErrorMessage());
    }
  }
Beispiel #8
0
  @Test
  public void addUserTest() throws Throwable {
    AddUser.main(new String[] {"-u", "addusertest-admin", "-p", "password"});
    assertEquals(1, dir.listFiles().length);

    List<RealmRepresentation> realms =
        JsonSerialization.readValue(
            new FileInputStream(new File(dir, "keycloak-add-user.json")),
            new TypeReference<List<RealmRepresentation>>() {});
    assertEquals(1, realms.size());
    assertEquals(1, realms.get(0).getUsers().size());

    UserRepresentation user = realms.get(0).getUsers().get(0);
    assertEquals(new Integer(100000), user.getCredentials().get(0).getHashIterations());
    assertNull(user.getCredentials().get(0).getValue());

    CredentialRepresentation credentials = user.getCredentials().get(0);

    assertEquals(Pbkdf2PasswordHashProvider.ID, credentials.getAlgorithm());
    assertEquals(new Integer(100000), credentials.getHashIterations());

    KeycloakServer server = new KeycloakServer();
    try {
      server.start();

      Keycloak keycloak =
          Keycloak.getInstance(
              "http://localhost:8081/auth",
              "master",
              "addusertest-admin",
              "password",
              Constants.ADMIN_CLI_CLIENT_ID);
      keycloak.realms().findAll();

      RealmRepresentation testRealm = new RealmRepresentation();
      testRealm.setEnabled(true);
      testRealm.setId("test");
      testRealm.setRealm("test");

      keycloak.realms().create(testRealm);

      RealmResource realm = keycloak.realm("master");

      List<UserRepresentation> users =
          realm.users().search("addusertest-admin", null, null, null, null, null);
      assertEquals(1, users.size());

      UserRepresentation created = users.get(0);
      assertNotNull(created.getCreatedTimestamp());

      UserResource userResource = realm.users().get(created.getId());

      List<RoleRepresentation> realmRoles = userResource.roles().realmLevel().listAll();

      assertRoles(realmRoles, "admin", "offline_access");

      List<ClientRepresentation> clients = realm.clients().findAll();
      String accountId = null;
      for (ClientRepresentation c : clients) {
        if (c.getClientId().equals("account")) {
          accountId = c.getId();
        }
      }

      List<RoleRepresentation> accountRoles = userResource.roles().clientLevel(accountId).listAll();

      assertRoles(accountRoles, "view-profile", "manage-account");

      keycloak.close();

      assertEquals(0, dir.listFiles().length);
    } finally {
      server.stop();
    }
  }