Search in sources :

Example 56 with ModelTest

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

the class UserSessionProviderTest method testUpdateSessionInSameTransaction.

@Test
@ModelTest
public void testUpdateSessionInSameTransaction(KeycloakSession session) {
    RealmModel realm = session.realms().getRealmByName("test");
    UserSessionModel[] sessions = createSessions(session);
    int lastRefresh = Time.currentTime();
    session.sessions().getUserSession(realm, sessions[0].getId()).setLastSessionRefresh(lastRefresh);
    assertEquals(lastRefresh, session.sessions().getUserSession(realm, sessions[0].getId()).getLastSessionRefresh());
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserSessionModel(org.keycloak.models.UserSessionModel) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 57 with ModelTest

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

the class UserSessionProviderTest method testGetByClientPaginated.

@Test
@ModelTest
public void testGetByClientPaginated(KeycloakSession session) {
    RealmModel realm = session.realms().getRealmByName("test");
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession kcSession) -> {
        try {
            for (int i = 0; i < 25; i++) {
                Time.setOffset(i);
                UserSessionModel userSession = kcSession.sessions().createUserSession(realm, kcSession.users().getUserByUsername(realm, "user1"), "user1", "127.0.0." + i, "form", false, null, null);
                AuthenticatedClientSessionModel clientSession = kcSession.sessions().createClientSession(realm, realm.getClientByClientId("test-app"), userSession);
                assertNotNull(clientSession);
                clientSession.setRedirectUri("http://redirect");
                clientSession.setNote(OIDCLoginProtocol.STATE_PARAM, "state");
                clientSession.setTimestamp(userSession.getStarted());
                userSession.setLastSessionRefresh(userSession.getStarted());
            }
        } finally {
            Time.setOffset(0);
        }
    });
    assertPaginatedSession(session, realm, realm.getClientByClientId("test-app"), 0, 1, 1);
    assertPaginatedSession(session, realm, realm.getClientByClientId("test-app"), 0, 10, 10);
    assertPaginatedSession(session, realm, realm.getClientByClientId("test-app"), 10, 10, 10);
    assertPaginatedSession(session, realm, realm.getClientByClientId("test-app"), 20, 10, 5);
    assertPaginatedSession(session, realm, realm.getClientByClientId("test-app"), 30, 10, 0);
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserSessionModel(org.keycloak.models.UserSessionModel) KeycloakSession(org.keycloak.models.KeycloakSession) AuthenticatedClientSessionModel(org.keycloak.models.AuthenticatedClientSessionModel) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 58 with ModelTest

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

the class ClientModelTest method testClientRoleRemovalAndClientScopeSameTx.

@Test
@ModelTest
public void testClientRoleRemovalAndClientScopeSameTx(KeycloakSession session) {
    // Client "from" has a role.  Assign this role to a scope to client "scoped".  Delete the role and make sure
    // cache gets cleared
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientRoleRemoveTx1) -> {
        currentSession = sessionClientRoleRemoveTx1;
        RealmModel realm = currentSession.realms().getRealmByName(realmName);
        ClientModel from = realm.addClient("from");
        RoleModel role = from.addRole("clientRole");
        roleId = role.getId();
        ClientModel scoped = realm.addClient("scoped");
        scoped.setFullScopeAllowed(false);
        scoped.addScopeMapping(role);
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientRoleRemoveTx2) -> {
        currentSession = sessionClientRoleRemoveTx2;
        RealmModel realm = currentSession.realms().getRealmByName(realmName);
        ClientModel scoped = realm.getClientByClientId("scoped");
        ClientModel from = realm.getClientByClientId("from");
        RoleModel role = currentSession.roles().getRoleById(realm, roleId);
        from.removeRole(role);
        // used to throw an NPE
        assertThat("Scope Mappings is not 0", scoped.getScopeMappingsStream().count(), is(0L));
        currentSession.clients().removeClient(realm, scoped.getId());
        currentSession.clients().removeClient(realm, from.getId());
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) 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) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 59 with ModelTest

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

the class ClientModelTest method testClientRoleRemovalAndClientScope.

@Test
@ModelTest
public void testClientRoleRemovalAndClientScope(KeycloakSession session) {
    // Client "from" has a role.  Assign this role to a scope to client "scoped".  Delete the role and make sure
    // cache gets cleared
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientRoleRemove1) -> {
        currentSession = sessionClientRoleRemove1;
        RealmModel realm = currentSession.realms().getRealmByName(realmName);
        assertThat("Realm Model 'original' is NULL !!", realm, notNullValue());
        ClientModel from = realm.addClient("from");
        RoleModel role = from.addRole("clientRole");
        roleId = role.getId();
        ClientModel scoped = realm.addClient("scoped");
        scoped.setFullScopeAllowed(false);
        scoped.addScopeMapping(role);
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientRoleRemove2) -> {
        currentSession = sessionClientRoleRemove2;
        RealmModel realm = currentSession.realms().getRealmByName(realmName);
        assertThat("Realm Model 'original' is NULL !!", realm, notNullValue());
        ClientModel from = realm.getClientByClientId("from");
        RoleModel role = currentSession.roles().getRoleById(realm, roleId);
        from.removeRole(role);
        currentSession.clients().removeClient(realm, from.getId());
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientRoleRemove3) -> {
        currentSession = sessionClientRoleRemove3;
        RealmModel realm = currentSession.realms().getRealmByName(realmName);
        assertThat("Realm Model 'original' is NULL !!", realm, notNullValue());
        ClientModel scoped = realm.getClientByClientId("scoped");
        // used to throw an NPE
        assertThat("Scope Mappings must be 0", scoped.getScopeMappingsStream().count(), is(0L));
        currentSession.clients().removeClient(realm, scoped.getId());
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) 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) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 60 with ModelTest

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

the class ClientModelTest method json.

@Test
@ModelTest
public void json(KeycloakSession session) {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionJson) -> {
        currentSession = sessionJson;
        RealmModel realm = currentSession.realms().getRealmByName(realmName);
        client = setUpClient(realm);
        ClientRepresentation representation = ModelToRepresentation.toRepresentation(client, currentSession);
        representation.setId(null);
        for (ProtocolMapperRepresentation protocolMapper : representation.getProtocolMappers()) {
            protocolMapper.setId(null);
        }
        realm = currentSession.realms().createRealm("copy");
        ClientModel copyClient = RepresentationToModel.createClient(currentSession, realm, representation);
        assertEquals(client, copyClient);
        client.unregisterNode("node1");
        client.unregisterNode("10.20.30.40");
        currentSession.clients().removeClient(realm, client.getId());
        currentSession.clients().removeClient(realm, copyClient.getId());
        currentSession.realms().removeRealm(realm.getId());
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) KeycloakSession(org.keycloak.models.KeycloakSession) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) 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