Search in sources :

Example 31 with ModelTest

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

the class AuthenticationSessionProviderTest method testAuthenticationSessionRestart.

@Test
@ModelTest
public void testAuthenticationSessionRestart(KeycloakSession session) {
    AtomicReference<String> parentAuthSessionID = new AtomicReference<>();
    AtomicReference<String> tabID = new AtomicReference<>();
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionRestart1) -> {
        KeycloakSession currentSession = sessionRestart1;
        RealmModel realm = currentSession.realms().getRealm("test");
        ClientModel client1 = realm.getClientByClientId("test-app");
        UserModel user1 = currentSession.users().getUserByUsername(realm, "user1");
        AuthenticationSessionModel authSession = currentSession.authenticationSessions().createRootAuthenticationSession(realm).createAuthenticationSession(client1);
        parentAuthSessionID.set(authSession.getParentSession().getId());
        tabID.set(authSession.getTabId());
        authSession.setAction("foo");
        authSession.getParentSession().setTimestamp(100);
        authSession.setAuthenticatedUser(user1);
        authSession.setAuthNote("foo", "bar");
        authSession.setClientNote("foo2", "bar2");
        authSession.setExecutionStatus("123", CommonClientSessionModel.ExecutionStatus.SUCCESS);
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionRestart2) -> {
        KeycloakSession currentSession = sessionRestart2;
        RealmModel realm = currentSession.realms().getRealm("test");
        // Test restart root authentication session
        ClientModel client1 = realm.getClientByClientId("test-app");
        AuthenticationSessionModel authSession = currentSession.authenticationSessions().getRootAuthenticationSession(realm, parentAuthSessionID.get()).getAuthenticationSession(client1, tabID.get());
        authSession.getParentSession().restartSession(realm);
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionRestart3) -> {
        KeycloakSession currentSession = sessionRestart3;
        RealmModel realm = currentSession.realms().getRealm("test");
        ClientModel client1 = realm.getClientByClientId("test-app");
        RootAuthenticationSessionModel rootAuthSession = currentSession.authenticationSessions().getRootAuthenticationSession(realm, parentAuthSessionID.get());
        assertThat(rootAuthSession.getAuthenticationSession(client1, tabID.get()), nullValue());
        assertThat(rootAuthSession.getTimestamp() > 0, is(true));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) ClientModel(org.keycloak.models.ClientModel) AuthenticationSessionModel(org.keycloak.sessions.AuthenticationSessionModel) RootAuthenticationSessionModel(org.keycloak.sessions.RootAuthenticationSessionModel) KeycloakSession(org.keycloak.models.KeycloakSession) RootAuthenticationSessionModel(org.keycloak.sessions.RootAuthenticationSessionModel) 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 32 with ModelTest

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

the class AuthenticationSessionProviderTest method testLoginSessionsCRUD.

@Test
@ModelTest
public void testLoginSessionsCRUD(KeycloakSession session) {
    AtomicReference<String> rootAuthSessionID = new AtomicReference<>();
    AtomicReference<String> tabID = new AtomicReference<>();
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCRUD1) -> {
        KeycloakSession currentSession = sessionCRUD1;
        RealmModel realm = currentSession.realms().getRealm("test");
        ClientModel client1 = realm.getClientByClientId("test-app");
        RootAuthenticationSessionModel rootAuthSession = currentSession.authenticationSessions().createRootAuthenticationSession(realm);
        rootAuthSessionID.set(rootAuthSession.getId());
        AuthenticationSessionModel authSession = rootAuthSession.createAuthenticationSession(client1);
        tabID.set(authSession.getTabId());
        authSession.setAction("foo");
        rootAuthSession.setTimestamp(100);
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCRUD2) -> {
        KeycloakSession currentSession = sessionCRUD2;
        RealmModel realm = currentSession.realms().getRealm("test");
        ClientModel client1 = realm.getClientByClientId("test-app");
        // Ensure currentSession is here
        RootAuthenticationSessionModel rootAuthSession = currentSession.authenticationSessions().getRootAuthenticationSession(realm, rootAuthSessionID.get());
        AuthenticationSessionModel authSession = rootAuthSession.getAuthenticationSession(client1, tabID.get());
        testAuthenticationSession(authSession, client1.getId(), null, "foo");
        assertThat(rootAuthSession.getTimestamp(), is(100));
        // Update and commit
        authSession.setAction("foo-updated");
        rootAuthSession.setTimestamp(200);
        authSession.setAuthenticatedUser(currentSession.users().getUserByUsername(realm, "user1"));
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCRUD3) -> {
        KeycloakSession currentSession = sessionCRUD3;
        RealmModel realm = currentSession.realms().getRealm("test");
        UserModel user1 = currentSession.users().getUserByUsername(realm, "user1");
        // Ensure currentSession was updated
        RootAuthenticationSessionModel rootAuthSession = currentSession.authenticationSessions().getRootAuthenticationSession(realm, rootAuthSessionID.get());
        ClientModel client1 = realm.getClientByClientId("test-app");
        AuthenticationSessionModel authSession = rootAuthSession.getAuthenticationSession(client1, tabID.get());
        testAuthenticationSession(authSession, client1.getId(), user1.getId(), "foo-updated");
        assertThat(rootAuthSession.getTimestamp(), is(200));
        // Remove and commit
        currentSession.authenticationSessions().removeRootAuthenticationSession(realm, rootAuthSession);
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCRUD4) -> {
        KeycloakSession currentSession = sessionCRUD4;
        RealmModel realm = currentSession.realms().getRealm("test");
        // Ensure currentSession was removed
        assertThat(currentSession.authenticationSessions().getRootAuthenticationSession(realm, rootAuthSessionID.get()), nullValue());
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) ClientModel(org.keycloak.models.ClientModel) AuthenticationSessionModel(org.keycloak.sessions.AuthenticationSessionModel) RootAuthenticationSessionModel(org.keycloak.sessions.RootAuthenticationSessionModel) KeycloakSession(org.keycloak.models.KeycloakSession) RootAuthenticationSessionModel(org.keycloak.sessions.RootAuthenticationSessionModel) 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 33 with ModelTest

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

the class AuthenticationSessionProviderTest method testExpiredAuthSessions.

@Test
@ModelTest
public void testExpiredAuthSessions(KeycloakSession session) {
    AtomicReference<String> authSessionID = new AtomicReference<>();
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionExpired) -> {
        KeycloakSession mainSession = sessionExpired;
        try {
            // AccessCodeLifespan = 10 ; AccessCodeLifespanUserAction = 10 ; AccessCodeLifespanLogin = 30
            setAccessCodeLifespan(mainSession, 10, 10, 30);
            createAuthSession(mainSession, authSessionID);
            testExpiredOffset(mainSession, 25, false, authSessionID.get());
            testExpiredOffset(mainSession, 35, true, authSessionID.get());
            // AccessCodeLifespan = Not set ; AccessCodeLifespanUserAction = 10 ; AccessCodeLifespanLogin = Not set
            setAccessCodeLifespan(mainSession, -1, 40, -1);
            createAuthSession(mainSession, authSessionID);
            testExpiredOffset(mainSession, 35, false, authSessionID.get());
            testExpiredOffset(mainSession, 45, true, authSessionID.get());
            // AccessCodeLifespan = 50 ; AccessCodeLifespanUserAction = Not set ; AccessCodeLifespanLogin = Not set
            setAccessCodeLifespan(mainSession, 50, -1, -1);
            createAuthSession(mainSession, authSessionID);
            testExpiredOffset(mainSession, 45, false, authSessionID.get());
            testExpiredOffset(mainSession, 55, true, authSessionID.get());
        } finally {
            Time.setOffset(0);
            session.getKeycloakSessionFactory().publish(new ResetTimeOffsetEvent());
            setAccessCodeLifespan(mainSession, 60, 300, 1800);
        }
    });
}
Also used : ResetTimeOffsetEvent(org.keycloak.models.utils.ResetTimeOffsetEvent) 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 34 with ModelTest

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

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

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