Search in sources :

Example 86 with UserModel

use of org.keycloak.models.UserModel in project keycloak by keycloak.

the class UserConsentModelTest method deleteUserTest.

@Test
@ModelTest
public void deleteUserTest(KeycloakSession session) {
    // Validate user deleted without any referential constraint errors
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionUT) -> {
        KeycloakSession currentSession = sessionUT;
        RealmModel realm = currentSession.realms().getRealm("original");
        UserModel john = currentSession.users().getUserByUsername(realm, "john");
        currentSession.users().removeUser(realm, john);
        UserModel mary = currentSession.users().getUserByUsername(realm, "mary");
        currentSession.users().removeUser(realm, mary);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) KeycloakSession(org.keycloak.models.KeycloakSession) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 87 with UserModel

use of org.keycloak.models.UserModel in project keycloak by keycloak.

the class UserModelTest method testUserRequiredActions.

@Test
@ModelTest
public void testUserRequiredActions(KeycloakSession session) throws Exception {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUserReqActions) -> {
        KeycloakSession currentSession = sesUserReqActions;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        UserModel user = currentSession.users().addUser(realm, "user");
        List<String> requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
        Assert.assertThat(requiredActions, empty());
        user.addRequiredAction(RequiredAction.CONFIGURE_TOTP);
        String id = realm.getId();
        realm = currentSession.realms().getRealm(id);
        user = currentSession.users().getUserByUsername(realm, "user");
        requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
        Assert.assertThat(requiredActions, hasSize(1));
        Assert.assertThat(requiredActions, contains(RequiredAction.CONFIGURE_TOTP.name()));
        user.addRequiredAction(RequiredAction.CONFIGURE_TOTP);
        user = currentSession.users().getUserByUsername(realm, "user");
        requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
        Assert.assertThat(requiredActions, hasSize(1));
        Assert.assertThat(requiredActions, contains(RequiredAction.CONFIGURE_TOTP.name()));
        user.addRequiredAction(RequiredAction.VERIFY_EMAIL.name());
        user = currentSession.users().getUserByUsername(realm, "user");
        requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
        Assert.assertThat(requiredActions, hasSize(2));
        Assert.assertThat(requiredActions, containsInAnyOrder(RequiredAction.CONFIGURE_TOTP.name(), RequiredAction.VERIFY_EMAIL.name()));
        user.removeRequiredAction(RequiredAction.CONFIGURE_TOTP.name());
        user = currentSession.users().getUserByUsername(realm, "user");
        requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
        Assert.assertThat(requiredActions, hasSize(1));
        Assert.assertThat(requiredActions, contains(RequiredAction.VERIFY_EMAIL.name()));
        user.removeRequiredAction(RequiredAction.VERIFY_EMAIL.name());
        user = currentSession.users().getUserByUsername(realm, "user");
        requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
        Assert.assertThat(requiredActions, empty());
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) KeycloakSession(org.keycloak.models.KeycloakSession) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 88 with UserModel

use of org.keycloak.models.UserModel in project keycloak by keycloak.

the class UserModelTest method testUpdateUserSingleAttribute.

// KEYCLOAK-3608
@Test
@ModelTest
public void testUpdateUserSingleAttribute(KeycloakSession session) {
    AtomicReference<Map<String, List<String>>> expectedAtomic = new AtomicReference<>();
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUpdateUserSingleAtr) -> {
        KeycloakSession currentSession = sesUpdateUserSingleAtr;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        Map<String, List<String>> expected = new HashMap<>();
        expected.put("key1", Collections.singletonList("value3"));
        expected.put("key2", Collections.singletonList("value2"));
        expected.put(UserModel.FIRST_NAME, Collections.singletonList(null));
        expected.put(UserModel.LAST_NAME, Collections.singletonList(null));
        expected.put(UserModel.EMAIL, Collections.singletonList(null));
        expected.put(UserModel.USERNAME, Collections.singletonList("user"));
        UserModel user = currentSession.users().addUser(realm, "user");
        user.setSingleAttribute("key1", "value1");
        user.setSingleAttribute("key2", "value2");
        // KEYCLOAK-7014
        user.setSingleAttribute("key3", null);
        // Overwrite the first attribute
        user.setSingleAttribute("key1", "value3");
        Assert.assertThat(user.getAttributes(), equalTo(expected));
        expectedAtomic.set(expected);
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUpdateUserSingleAtr2) -> {
        KeycloakSession currentSession = sesUpdateUserSingleAtr2;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        Map<String, List<String>> expected = expectedAtomic.get();
        Assert.assertThat(currentSession.users().getUserByUsername(realm, "user").getAttributes(), equalTo(expected));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) HashMap(java.util.HashMap) KeycloakSession(org.keycloak.models.KeycloakSession) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 89 with UserModel

use of org.keycloak.models.UserModel in project keycloak by keycloak.

the class UserModelTest method testGrantToAll.

@Test
@ModelTest
public void testGrantToAll(KeycloakSession session) throws Exception {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesGrantToAll1) -> {
        KeycloakSession currentSession = sesGrantToAll1;
        RealmModel realm1 = currentSession.realms().getRealmByName("realm1");
        realm1.addRole("role1");
        currentSession.users().addUser(realm1, "user1");
        currentSession.users().addUser(realm1, "user2");
        RealmModel realm2 = currentSession.realms().getRealmByName("realm2");
        currentSession.users().addUser(realm2, "user1");
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesGrantToAll2) -> {
        KeycloakSession currentSession = sesGrantToAll2;
        RealmModel realm1 = currentSession.realms().getRealmByName("realm1");
        RoleModel role1 = realm1.getRole("role1");
        currentSession.users().grantToAllUsers(realm1, role1);
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesGrantToAll2) -> {
        KeycloakSession currentSession = sesGrantToAll2;
        RealmModel realm1 = currentSession.realms().getRealmByName("realm1");
        RoleModel role1 = realm1.getRole("role1");
        UserModel user1 = currentSession.users().getUserByUsername(realm1, "user1");
        UserModel user2 = currentSession.users().getUserByUsername(realm1, "user2");
        Assert.assertTrue(user1.hasRole(role1));
        Assert.assertTrue(user2.hasRole(role1));
        RealmModel realm2 = currentSession.realms().getRealmByName("realm2");
        UserModel realm2User1 = currentSession.users().getUserByUsername(realm2, "user1");
        Assert.assertFalse(realm2User1.hasRole(role1));
        currentSession.realms().removeRealm(realm1.getId());
        currentSession.realms().removeRealm(realm2.getId());
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) KeycloakSession(org.keycloak.models.KeycloakSession) RoleModel(org.keycloak.models.RoleModel) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 90 with UserModel

use of org.keycloak.models.UserModel in project keycloak by keycloak.

the class CredentialModelTest method testCredentialCRUD.

@Test
@ModelTest
public void testCredentialCRUD(KeycloakSession session) throws Exception {
    AtomicReference<String> passwordId = new AtomicReference<>();
    AtomicReference<String> otp1Id = new AtomicReference<>();
    AtomicReference<String> otp2Id = new AtomicReference<>();
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession currentSession) -> {
        RealmModel realm = currentSession.realms().getRealmByName("test");
        UserModel user = currentSession.users().getUserByUsername(realm, "test-user@localhost");
        List<CredentialModel> list = currentSession.userCredentialManager().getStoredCredentialsStream(realm, user).collect(Collectors.toList());
        Assert.assertEquals(1, list.size());
        passwordId.set(list.get(0).getId());
        // Create 2 OTP credentials (password was already created)
        CredentialModel otp1 = OTPCredentialModel.createFromPolicy(realm, "secret1");
        CredentialModel otp2 = OTPCredentialModel.createFromPolicy(realm, "secret2");
        otp1 = currentSession.userCredentialManager().createCredential(realm, user, otp1);
        otp2 = currentSession.userCredentialManager().createCredential(realm, user, otp2);
        otp1Id.set(otp1.getId());
        otp2Id.set(otp2.getId());
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession currentSession) -> {
        RealmModel realm = currentSession.realms().getRealmByName("test");
        UserModel user = currentSession.users().getUserByUsername(realm, "test-user@localhost");
        // Assert priorities: password, otp1, otp2
        List<CredentialModel> list = currentSession.userCredentialManager().getStoredCredentialsStream(realm, user).collect(Collectors.toList());
        assertOrder(list, passwordId.get(), otp1Id.get(), otp2Id.get());
        // Assert can't move password when newPreviousCredential not found
        Assert.assertFalse(currentSession.userCredentialManager().moveCredentialTo(realm, user, passwordId.get(), "not-known"));
        // Assert can't move credential when not found
        Assert.assertFalse(currentSession.userCredentialManager().moveCredentialTo(realm, user, "not-known", otp2Id.get()));
        // Move otp2 up 1 position
        Assert.assertTrue(currentSession.userCredentialManager().moveCredentialTo(realm, user, otp2Id.get(), passwordId.get()));
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession currentSession) -> {
        RealmModel realm = currentSession.realms().getRealmByName("test");
        UserModel user = currentSession.users().getUserByUsername(realm, "test-user@localhost");
        // Assert priorities: password, otp2, otp1
        List<CredentialModel> list = currentSession.userCredentialManager().getStoredCredentialsStream(realm, user).collect(Collectors.toList());
        assertOrder(list, passwordId.get(), otp2Id.get(), otp1Id.get());
        // Move otp2 to the top
        Assert.assertTrue(currentSession.userCredentialManager().moveCredentialTo(realm, user, otp2Id.get(), null));
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession currentSession) -> {
        RealmModel realm = currentSession.realms().getRealmByName("test");
        UserModel user = currentSession.users().getUserByUsername(realm, "test-user@localhost");
        // Assert priorities: otp2, password, otp1
        List<CredentialModel> list = currentSession.userCredentialManager().getStoredCredentialsStream(realm, user).collect(Collectors.toList());
        assertOrder(list, otp2Id.get(), passwordId.get(), otp1Id.get());
        // Move password down
        Assert.assertTrue(currentSession.userCredentialManager().moveCredentialTo(realm, user, passwordId.get(), otp1Id.get()));
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession currentSession) -> {
        RealmModel realm = currentSession.realms().getRealmByName("test");
        UserModel user = currentSession.users().getUserByUsername(realm, "test-user@localhost");
        // Assert priorities: otp2, otp1, password
        List<CredentialModel> list = currentSession.userCredentialManager().getStoredCredentialsStream(realm, user).collect(Collectors.toList());
        assertOrder(list, otp2Id.get(), otp1Id.get(), passwordId.get());
        // Remove otp2 down two positions
        Assert.assertTrue(currentSession.userCredentialManager().moveCredentialTo(realm, user, otp2Id.get(), passwordId.get()));
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession currentSession) -> {
        RealmModel realm = currentSession.realms().getRealmByName("test");
        UserModel user = currentSession.users().getUserByUsername(realm, "test-user@localhost");
        // Assert priorities: otp2, otp1, password
        List<CredentialModel> list = currentSession.userCredentialManager().getStoredCredentialsStream(realm, user).collect(Collectors.toList());
        assertOrder(list, otp1Id.get(), passwordId.get(), otp2Id.get());
        // Remove password
        Assert.assertTrue(currentSession.userCredentialManager().removeStoredCredential(realm, user, passwordId.get()));
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession currentSession) -> {
        RealmModel realm = currentSession.realms().getRealmByName("test");
        UserModel user = currentSession.users().getUserByUsername(realm, "test-user@localhost");
        // Assert priorities: otp2, password
        List<CredentialModel> list = currentSession.userCredentialManager().getStoredCredentialsStream(realm, user).collect(Collectors.toList());
        assertOrder(list, otp1Id.get(), otp2Id.get());
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) CredentialModel(org.keycloak.credential.CredentialModel) OTPCredentialModel(org.keycloak.models.credential.OTPCredentialModel) KeycloakSession(org.keycloak.models.KeycloakSession) AtomicReference(java.util.concurrent.atomic.AtomicReference) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Aggregations

UserModel (org.keycloak.models.UserModel)383 RealmModel (org.keycloak.models.RealmModel)220 Test (org.junit.Test)126 ClientModel (org.keycloak.models.ClientModel)86 KeycloakSession (org.keycloak.models.KeycloakSession)81 CachedUserModel (org.keycloak.models.cache.CachedUserModel)52 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)43 List (java.util.List)41 UserSessionModel (org.keycloak.models.UserSessionModel)40 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)40 RoleModel (org.keycloak.models.RoleModel)39 ComponentModel (org.keycloak.component.ComponentModel)31 HashMap (java.util.HashMap)30 Response (javax.ws.rs.core.Response)29 Path (javax.ws.rs.Path)28 UserManager (org.keycloak.models.UserManager)28 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)27 Map (java.util.Map)25 GroupModel (org.keycloak.models.GroupModel)24 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)24