@Test public void removeRequiredAction() { 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); user = realm.users().get(id); userRep = user.toRepresentation(); userRep.getRequiredActions().clear(); user.update(userRep); assertTrue(user.toRepresentation().getRequiredActions().isEmpty()); }
@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()); } }
@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()); } }