Search in sources :

Example 66 with KeycloakSession

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

the class AuthenticationSessionProviderTest method setAccessCodeLifespan.

// If parameter is -1, then the parameter won't change.
private void setAccessCodeLifespan(KeycloakSession session, int lifespan, int lifespanUserAction, int lifespanLogin) {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionLifespan) -> {
        KeycloakSession currentSession = sessionLifespan;
        RealmModel realm = currentSession.realms().getRealm("test");
        if (lifespan != -1)
            realm.setAccessCodeLifespan(lifespan);
        if (lifespanUserAction != -1)
            realm.setAccessCodeLifespanUserAction(lifespanUserAction);
        if (lifespanLogin != -1)
            realm.setAccessCodeLifespanLogin(lifespanLogin);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) KeycloakSession(org.keycloak.models.KeycloakSession)

Example 67 with KeycloakSession

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

the class AuthenticationSessionProviderTest method testExpiredOffset.

private void testExpiredOffset(KeycloakSession session, int offset, boolean isSessionNull, String authSessionID) {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionExp) -> {
        KeycloakSession currentSession = sessionExp;
        RealmModel realm = currentSession.realms().getRealm("test");
        Time.setOffset(offset);
        currentSession.authenticationSessions().removeExpired(realm);
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionExpVerify) -> {
        KeycloakSession currentSession = sessionExpVerify;
        RealmModel realm = currentSession.realms().getRealm("test");
        if (isSessionNull)
            assertThat(currentSession.authenticationSessions().getRootAuthenticationSession(realm, authSessionID), nullValue());
        else
            assertThat(currentSession.authenticationSessions().getRootAuthenticationSession(realm, authSessionID), notNullValue());
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) KeycloakSession(org.keycloak.models.KeycloakSession)

Example 68 with KeycloakSession

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

the class CompositeRolesModelTest method testNoClientID.

@Test
@ModelTest
public void testNoClientID(KeycloakSession session) {
    expectedException.expect(RuntimeException.class);
    expectedException.expectMessage("Unknown client specification in scope mappings: some-client");
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession session1) -> {
        try {
            // RealmManager manager = new RealmManager(session1);
            RealmRepresentation rep = loadJson(getClass().getResourceAsStream("/model/testrealm-noclient-id.json"), RealmRepresentation.class);
            rep.setId("TestNoClientID");
            // manager.importRealm(rep);
            adminClient.realms().create(rep);
        } catch (RuntimeException e) {
        }
    });
}
Also used : KeycloakSession(org.keycloak.models.KeycloakSession) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 69 with KeycloakSession

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

the class UserModelTest method persistUser.

@Test
@ModelTest
public void persistUser(KeycloakSession session) {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesPersistUser) -> {
        KeycloakSession currentSession = sesPersistUser;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        UserModel user = currentSession.users().addUser(realm, "user");
        user.setFirstName("first-name");
        user.setLastName("last-name");
        user.setEmail("email");
        assertNotNull(user.getCreatedTimestamp());
        // test that timestamp is current with 10s tollerance
        Assert.assertTrue((System.currentTimeMillis() - user.getCreatedTimestamp()) < 10000);
        user.addRequiredAction(RequiredAction.CONFIGURE_TOTP);
        user.addRequiredAction(RequiredAction.UPDATE_PASSWORD);
        RealmModel searchRealm = currentSession.realms().getRealm(realm.getId());
        UserModel persisted = currentSession.users().getUserByUsername(searchRealm, "user");
        assertUserModel(user, persisted);
        searchRealm = currentSession.realms().getRealm(realm.getId());
        UserModel persisted2 = currentSession.users().getUserById(searchRealm, user.getId());
        assertUserModel(user, persisted2);
        Map<String, String> attributes = new HashMap<>();
        attributes.put(UserModel.LAST_NAME, "last-name");
        List<UserModel> search = currentSession.users().searchForUserStream(realm, attributes).collect(Collectors.toList());
        Assert.assertThat(search, hasSize(1));
        Assert.assertThat(search.get(0).getUsername(), equalTo("user"));
        attributes.clear();
        attributes.put(UserModel.EMAIL, "email");
        search = currentSession.users().searchForUserStream(realm, attributes).collect(Collectors.toList());
        Assert.assertThat(search, hasSize(1));
        Assert.assertThat(search.get(0).getUsername(), equalTo("user"));
        attributes.clear();
        attributes.put(UserModel.LAST_NAME, "last-name");
        attributes.put(UserModel.EMAIL, "email");
        search = currentSession.users().searchForUserStream(realm, attributes).collect(Collectors.toList());
        Assert.assertThat(search, hasSize(1));
        Assert.assertThat(search.get(0).getUsername(), equalTo("user"));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) HashMap(java.util.HashMap) 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 70 with KeycloakSession

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

the class UserSessionProviderTest method testRemoveUserSessionsByRealm.

@Test
@ModelTest
public void testRemoveUserSessionsByRealm(KeycloakSession session) {
    RealmModel realm = session.realms().getRealmByName("test");
    createSessions(session);
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession kcSession) -> {
        kcSession.sessions().removeUserSessions(realm);
    });
    assertEquals(0, session.sessions().getUserSessionsStream(realm, session.users().getUserByUsername(realm, "user1")).count());
    assertEquals(0, session.sessions().getUserSessionsStream(realm, session.users().getUserByUsername(realm, "user2")).count());
}
Also used : RealmModel(org.keycloak.models.RealmModel) 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)

Aggregations

KeycloakSession (org.keycloak.models.KeycloakSession)189 RealmModel (org.keycloak.models.RealmModel)136 UserModel (org.keycloak.models.UserModel)78 Test (org.junit.Test)76 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)61 ClientModel (org.keycloak.models.ClientModel)58 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)53 List (java.util.List)34 AtomicReference (java.util.concurrent.atomic.AtomicReference)22 Collectors (java.util.stream.Collectors)21 IOException (java.io.IOException)20 Map (java.util.Map)19 UserSessionModel (org.keycloak.models.UserSessionModel)19 ArrayList (java.util.ArrayList)18 ClientScopeModel (org.keycloak.models.ClientScopeModel)18 RoleModel (org.keycloak.models.RoleModel)18 Set (java.util.Set)16 RealmManager (org.keycloak.services.managers.RealmManager)16 HashMap (java.util.HashMap)14 RealmRepresentation (org.keycloak.representations.idm.RealmRepresentation)14