Search in sources :

Example 16 with ModelTest

use of org.keycloak.testsuite.arquillian.annotation.ModelTest 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 17 with ModelTest

use of org.keycloak.testsuite.arquillian.annotation.ModelTest in project keycloak by keycloak.

the class CompositeRolesModelTest method testComposites.

@Test
@ModelTest
public void testComposites(KeycloakSession session) {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession session5) -> {
        RealmModel realm = session5.realms().getRealm("TestComposites");
        Set<RoleModel> requestedRoles = getRequestedRoles(realm.getClientByClientId("APP_COMPOSITE_APPLICATION"), session.users().getUserByUsername(realm, "APP_COMPOSITE_USER"));
        Assert.assertEquals(5, requestedRoles.size());
        assertContains(realm, "APP_COMPOSITE_APPLICATION", "APP_COMPOSITE_ROLE", requestedRoles);
        assertContains(realm, "APP_COMPOSITE_APPLICATION", "APP_COMPOSITE_CHILD", requestedRoles);
        assertContains(realm, "APP_COMPOSITE_APPLICATION", "APP_ROLE_2", requestedRoles);
        assertContains(realm, "APP_ROLE_APPLICATION", "APP_ROLE_1", requestedRoles);
        assertContains(realm, "realm", "REALM_ROLE_1", requestedRoles);
        Set<RoleModel> requestedRoles2 = getRequestedRoles(realm.getClientByClientId("APP_COMPOSITE_APPLICATION"), session5.users().getUserByUsername(realm, "REALM_APP_COMPOSITE_USER"));
        Assert.assertEquals(4, requestedRoles2.size());
        assertContains(realm, "APP_ROLE_APPLICATION", "APP_ROLE_1", requestedRoles2);
        requestedRoles = getRequestedRoles(realm.getClientByClientId("REALM_COMPOSITE_1_APPLICATION"), session5.users().getUserByUsername(realm, "REALM_COMPOSITE_1_USER"));
        Assert.assertEquals(1, requestedRoles.size());
        assertContains(realm, "realm", "REALM_COMPOSITE_1", requestedRoles);
        requestedRoles = getRequestedRoles(realm.getClientByClientId("REALM_COMPOSITE_2_APPLICATION"), session5.users().getUserByUsername(realm, "REALM_COMPOSITE_1_USER"));
        Assert.assertEquals(3, requestedRoles.size());
        assertContains(realm, "realm", "REALM_COMPOSITE_1", requestedRoles);
        assertContains(realm, "realm", "REALM_COMPOSITE_CHILD", requestedRoles);
        assertContains(realm, "realm", "REALM_ROLE_4", requestedRoles);
        requestedRoles = getRequestedRoles(realm.getClientByClientId("REALM_ROLE_1_APPLICATION"), session5.users().getUserByUsername(realm, "REALM_COMPOSITE_1_USER"));
        Assert.assertEquals(1, requestedRoles.size());
        assertContains(realm, "realm", "REALM_ROLE_1", requestedRoles);
        requestedRoles = getRequestedRoles(realm.getClientByClientId("REALM_COMPOSITE_1_APPLICATION"), session5.users().getUserByUsername(realm, "REALM_ROLE_1_USER"));
        Assert.assertEquals(1, requestedRoles.size());
        assertContains(realm, "realm", "REALM_ROLE_1", requestedRoles);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) 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 18 with ModelTest

use of org.keycloak.testsuite.arquillian.annotation.ModelTest 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)

Example 19 with ModelTest

use of org.keycloak.testsuite.arquillian.annotation.ModelTest in project keycloak by keycloak.

the class SimpleModelTest method simpleModelTest.

@Test
@ModelTest
public void simpleModelTest(KeycloakSession session) {
    log.infof("simpleModelTest");
    RealmModel realm = session.realms().getRealmByName("master");
    Assert.assertNotNull("Master realm was not found!", realm);
}
Also used : RealmModel(org.keycloak.models.RealmModel) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 20 with ModelTest

use of org.keycloak.testsuite.arquillian.annotation.ModelTest in project keycloak by keycloak.

the class SimpleModelTest method simpleModelTestWithNestedTransactions.

@Test
@ModelTest
public void simpleModelTestWithNestedTransactions(KeycloakSession session) {
    log.infof("simpleModelTestWithNestedTransactions");
    // Transaction 1
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession session1) -> {
        RealmModel realm = session1.realms().createRealm("foo");
        realm.setDefaultRole(session1.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
    });
    // Transaction 2 - should be able to see the created realm. Update it
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession session2) -> {
        RealmModel realm = session2.realms().getRealmByName("foo");
        Assert.assertNotNull(realm);
        realm.setAttribute("bar", "baz");
    });
    // Transaction 3 - Doublecheck update is visible. Then rollback transaction!
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession session3) -> {
        RealmModel realm = session3.realms().getRealmByName("foo");
        Assert.assertNotNull(realm);
        String attrValue = realm.getAttribute("bar");
        Assert.assertEquals("baz", attrValue);
        realm.setAttribute("bar", "baz2");
        session3.getTransactionManager().setRollbackOnly();
    });
    // Transaction 4 - should still see the old value of attribute. Delete realm
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession session4) -> {
        RealmModel realm = session4.realms().getRealmByName("foo");
        Assert.assertNotNull(realm);
        String attrValue = realm.getAttribute("bar");
        Assert.assertEquals("baz", attrValue);
        new RealmManager(session4).removeRealm(realm);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) KeycloakSession(org.keycloak.models.KeycloakSession) RealmManager(org.keycloak.services.managers.RealmManager) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Aggregations

ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)82 Test (org.junit.Test)81 RealmModel (org.keycloak.models.RealmModel)76 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)66 KeycloakSession (org.keycloak.models.KeycloakSession)60 UserModel (org.keycloak.models.UserModel)37 ClientModel (org.keycloak.models.ClientModel)36 UserSessionModel (org.keycloak.models.UserSessionModel)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)19 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)14 AuthenticatedClientSessionModel (org.keycloak.models.AuthenticatedClientSessionModel)12 UserConsentModel (org.keycloak.models.UserConsentModel)10 RealmManager (org.keycloak.services.managers.RealmManager)10 RoleModel (org.keycloak.models.RoleModel)9 ClientScopeModel (org.keycloak.models.ClientScopeModel)6 UserManager (org.keycloak.models.UserManager)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 ResetTimeOffsetEvent (org.keycloak.models.utils.ResetTimeOffsetEvent)5 List (java.util.List)4