Example #1
0
  @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));
  }
Example #2
0
  @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);
  }
Example #4
0
  @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());
    }
  }
Example #5
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());
    }
  }
Example #6
0
  @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());
    }
  }