Search in sources :

Example 11 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class UserConsentModelTest method after.

@After
public void after() {
    testingClient.server().run(session -> {
        RealmManager realmManager = new RealmManager(session);
        RealmModel realm = realmManager.getRealmByName("original");
        if (realm != null) {
            session.sessions().removeUserSessions(realm);
            UserModel user = session.users().getUserByUsername(realm, "user");
            UserModel user1 = session.users().getUserByUsername(realm, "user1");
            UserModel user2 = session.users().getUserByUsername(realm, "user2");
            UserModel user3 = session.users().getUserByUsername(realm, "user3");
            UserManager um = new UserManager(session);
            if (user != null) {
                um.removeUser(realm, user);
            }
            if (user1 != null) {
                um.removeUser(realm, user1);
            }
            if (user2 != null) {
                um.removeUser(realm, user2);
            }
            if (user3 != null) {
                um.removeUser(realm, user3);
            }
            realmManager.removeRealm(realm);
        }
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) UserManager(org.keycloak.models.UserManager) RealmManager(org.keycloak.services.managers.RealmManager) After(org.junit.After)

Example 12 with RealmManager

use of org.keycloak.services.managers.RealmManager 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)

Example 13 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class UserConsentWithUserStorageModelTest method deleteClientTest.

@Test
@ModelTest
public void deleteClientTest(KeycloakSession session) {
    AtomicReference<String> barClientID = new AtomicReference<>();
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesDelClient1) -> {
        KeycloakSession currentSession = sesDelClient1;
        RealmManager realmManager = new RealmManager(currentSession);
        RealmModel realm = realmManager.getRealmByName("original");
        ClientModel barClient = realm.getClientByClientId("bar-client");
        barClientID.set(barClient.getId());
        realm.removeClient(barClient.getId());
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesDelClient2) -> {
        KeycloakSession currentSession = sesDelClient2;
        RealmManager realmManager = new RealmManager(currentSession);
        RealmModel realm = realmManager.getRealm("original");
        ClientModel fooClient = realm.getClientByClientId("foo-client");
        Assert.assertNull(realm.getClientByClientId("bar-client"));
        UserModel john = realmManager.getSession().users().getUserByUsername(realm, "john");
        UserConsentModel johnFooConsent = realmManager.getSession().users().getConsentByClient(realm, john.getId(), fooClient.getId());
        Assert.assertEquals(johnFooConsent.getGrantedClientScopes().size(), 1);
        Assert.assertTrue(isClientScopeGranted(realm, "foo", johnFooConsent));
        Assert.assertNull(realmManager.getSession().users().getConsentByClient(realm, john.getId(), barClientID.get()));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) ClientModel(org.keycloak.models.ClientModel) KeycloakSession(org.keycloak.models.KeycloakSession) AtomicReference(java.util.concurrent.atomic.AtomicReference) RealmManager(org.keycloak.services.managers.RealmManager) UserConsentModel(org.keycloak.models.UserConsentModel) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 14 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class UserConsentWithUserStorageModelTest method setupEnv.

public static void setupEnv(KeycloakSession session) {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionSetUpEnv) -> {
        KeycloakSession currentSession = sessionSetUpEnv;
        RealmManager realmManager = new RealmManager(currentSession);
        RealmModel realm = realmManager.createRealm("original");
        UserStorageProviderModel model = new UserStorageProviderModel();
        model.setName("memory");
        model.setPriority(0);
        model.setProviderId(UserMapStorageFactory.PROVIDER_ID);
        model.setParentId(realm.getId());
        model.getConfig().putSingle(IMPORT_ENABLED, Boolean.toString(false));
        realm.addComponentModel(model);
        ClientModel fooClient = realm.addClient("foo-client");
        ClientModel barClient = realm.addClient("bar-client");
        ClientScopeModel fooScope = realm.addClientScope("foo");
        fooScope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
        ClientScopeModel barScope = realm.addClientScope("bar");
        fooScope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
        UserModel john = currentSession.users().addUser(realm, "john");
        UserModel mary = currentSession.users().addUser(realm, "mary");
        UserConsentModel johnFooGrant = new UserConsentModel(fooClient);
        johnFooGrant.addGrantedClientScope(fooScope);
        realmManager.getSession().users().addConsent(realm, john.getId(), johnFooGrant);
        UserConsentModel johnBarGrant = new UserConsentModel(barClient);
        johnBarGrant.addGrantedClientScope(barScope);
        // Update should fail as grant doesn't yet exists
        try {
            currentSession.users().updateConsent(realm, john.getId(), johnBarGrant);
            Assert.fail("Not expected to end here");
        } catch (ModelException expected) {
        }
        realmManager.getSession().users().addConsent(realm, john.getId(), johnBarGrant);
        UserConsentModel maryFooGrant = new UserConsentModel(fooClient);
        maryFooGrant.addGrantedClientScope(fooScope);
        realmManager.getSession().users().addConsent(realm, mary.getId(), maryFooGrant);
        ClientStorageProviderModel clientStorage = new ClientStorageProviderModel();
        clientStorage.setProviderId(HardcodedClientStorageProviderFactory.PROVIDER_ID);
        clientStorage.getConfig().putSingle(HardcodedClientStorageProviderFactory.CLIENT_ID, "hardcoded-client");
        clientStorage.getConfig().putSingle(HardcodedClientStorageProviderFactory.REDIRECT_URI, "http://localhost:8081/*");
        clientStorage.getConfig().putSingle(HardcodedClientStorageProviderFactory.CONSENT, "true");
        clientStorage.setParentId(realm.getId());
        clientStorageComponent = realm.addComponentModel(clientStorage);
        ClientModel hardcodedClient = currentSession.clients().getClientByClientId(realm, "hardcoded-client");
        Assert.assertNotNull(hardcodedClient);
        UserConsentModel maryHardcodedGrant = new UserConsentModel(hardcodedClient);
        realmManager.getSession().users().addConsent(realm, mary.getId(), maryHardcodedGrant);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) ClientModel(org.keycloak.models.ClientModel) ModelException(org.keycloak.models.ModelException) KeycloakSession(org.keycloak.models.KeycloakSession) ClientScopeModel(org.keycloak.models.ClientScopeModel) RealmManager(org.keycloak.services.managers.RealmManager) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) ClientStorageProviderModel(org.keycloak.storage.client.ClientStorageProviderModel) UserConsentModel(org.keycloak.models.UserConsentModel)

Example 15 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class BadRealmTest method testBadRealmId.

@Test
@ModelTest
public void testBadRealmId(KeycloakSession session) {
    RealmManager manager = new RealmManager(session);
    try {
        manager.createRealm(id + script, name);
        fail();
    } catch (ReservedCharValidator.ReservedCharException ex) {
    }
}
Also used : ReservedCharValidator(org.keycloak.utils.ReservedCharValidator) RealmManager(org.keycloak.services.managers.RealmManager) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) Test(org.junit.Test)

Aggregations

RealmManager (org.keycloak.services.managers.RealmManager)47 RealmModel (org.keycloak.models.RealmModel)34 Test (org.junit.Test)19 UserModel (org.keycloak.models.UserModel)17 KeycloakSession (org.keycloak.models.KeycloakSession)16 ClientModel (org.keycloak.models.ClientModel)13 ClientManager (org.keycloak.services.managers.ClientManager)11 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)8 Path (javax.ws.rs.Path)6 UserSessionModel (org.keycloak.models.UserSessionModel)6 UserManager (org.keycloak.models.UserManager)5 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)4 LinkedList (java.util.LinkedList)3 GET (javax.ws.rs.GET)3 NotFoundException (javax.ws.rs.NotFoundException)3 Produces (javax.ws.rs.Produces)3 UserConsentModel (org.keycloak.models.UserConsentModel)3 ClientPolicyException (org.keycloak.services.clientpolicy.ClientPolicyException)3