Search in sources :

Example 71 with ModelTest

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

the class UserModelTest method testUpdateUserAttribute.

// KEYCLOAK-3494
@Test
@ModelTest
public void testUpdateUserAttribute(KeycloakSession session) throws Exception {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUpdateAtr1) -> {
        KeycloakSession currentSession = sesUpdateAtr1;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        UserModel user = currentSession.users().addUser(realm, "user");
        user.setSingleAttribute("key1", "value1");
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUpdateAtr2) -> {
        KeycloakSession currentSession = sesUpdateAtr2;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        UserModel user = currentSession.users().getUserByUsername(realm, "user");
        // Update attribute
        List<String> attrVals = new ArrayList<>(Arrays.asList("val2"));
        user.setAttribute("key1", attrVals);
        Map<String, List<String>> allAttrVals = user.getAttributes();
        // Ensure same transaction is able to see updated value
        Assert.assertThat(allAttrVals.keySet(), hasSize(5));
        Assert.assertThat(allAttrVals.keySet(), containsInAnyOrder("key1", UserModel.FIRST_NAME, UserModel.LAST_NAME, UserModel.EMAIL, UserModel.USERNAME));
        Assert.assertThat(allAttrVals.get("key1"), contains("val2"));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) KeycloakSession(org.keycloak.models.KeycloakSession) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 72 with ModelTest

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

the class UserModelTest method testSearchByUserAttribute.

@Test
@ModelTest
public void testSearchByUserAttribute(KeycloakSession session) throws Exception {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesSearchAtr1) -> {
        KeycloakSession currentSession = sesSearchAtr1;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        UserModel user1 = currentSession.users().addUser(realm, "user1");
        UserModel user2 = currentSession.users().addUser(realm, "user2");
        UserModel user3 = currentSession.users().addUser(realm, "user3");
        RealmModel otherRealm = currentSession.realms().getRealmByName("other");
        UserModel otherRealmUser = currentSession.users().addUser(otherRealm, "user1");
        user1.setSingleAttribute("key1", "value1");
        user1.setSingleAttribute("key2", "value21");
        user2.setSingleAttribute("key1", "value1");
        user2.setSingleAttribute("key2", "value22");
        user3.setSingleAttribute("key2", "value21");
        otherRealmUser.setSingleAttribute("key2", "value21");
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesSearchAtr2) -> {
        KeycloakSession currentSession = sesSearchAtr2;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        UserModel user1 = currentSession.users().getUserByUsername(realm, "user1");
        UserModel user2 = currentSession.users().getUserByUsername(realm, "user2");
        UserModel user3 = currentSession.users().getUserByUsername(realm, "user3");
        List<UserModel> users = currentSession.users().searchForUserByUserAttributeStream(realm, "key1", "value1").collect(Collectors.toList());
        Assert.assertThat(users, hasSize(2));
        Assert.assertThat(users, containsInAnyOrder(user1, user2));
        users = currentSession.users().searchForUserByUserAttributeStream(realm, "key2", "value21").collect(Collectors.toList());
        Assert.assertThat(users, hasSize(2));
        Assert.assertThat(users, containsInAnyOrder(user1, user3));
        users = currentSession.users().searchForUserByUserAttributeStream(realm, "key2", "value22").collect(Collectors.toList());
        Assert.assertThat(users, hasSize(1));
        Assert.assertThat(users, contains(user2));
        users = currentSession.users().searchForUserByUserAttributeStream(realm, "key3", "value3").collect(Collectors.toList());
        Assert.assertThat(users, empty());
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) 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 73 with ModelTest

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

the class UserModelTest method testUserNotBefore.

@Test
@ModelTest
public void testUserNotBefore(KeycloakSession session) throws Exception {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUserNotBefore1) -> {
        KeycloakSession currentSession = sesUserNotBefore1;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        UserModel user1 = currentSession.users().addUser(realm, "user1");
        currentSession.users().setNotBeforeForUser(realm, user1, 10);
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUserNotBefore2) -> {
        KeycloakSession currentSession = sesUserNotBefore2;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        UserModel user1 = currentSession.users().getUserByUsername(realm, "user1");
        int notBefore = currentSession.users().getNotBeforeOfUser(realm, user1);
        Assert.assertThat(notBefore, equalTo(10));
        // Try to update
        currentSession.users().setNotBeforeForUser(realm, user1, 20);
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUserNotBefore3) -> {
        KeycloakSession currentSession = sesUserNotBefore3;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        UserModel user1 = currentSession.users().getUserByUsername(realm, "user1");
        int notBefore = currentSession.users().getNotBeforeOfUser(realm, user1);
        Assert.assertThat(notBefore, equalTo(20));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) 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 74 with ModelTest

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

the class SessionTimeoutValidationTest method testIsSessionValid.

@Test
@ModelTest
public void testIsSessionValid(KeycloakSession session) {
    // KEYCLOAK-9833 Large SSO Session Idle/SSO Session Max causes login failure
    RealmModel realm = session.realms().getRealmByName("test");
    int ssoSessionIdleTimeoutOrig = realm.getSsoSessionIdleTimeout();
    int ssoSessionMaxLifespanOrig = realm.getSsoSessionMaxLifespan();
    UserSessionModel userSessionModel = session.sessions().createUserSession(realm, session.users().getUserByUsername(realm, "user1"), "user1", "127.0.0.1", "form", true, null, null);
    realm.setSsoSessionIdleTimeout(Integer.MAX_VALUE);
    Assert.assertTrue("Session validataion with large SsoSessionIdleTimeout failed", AuthenticationManager.isSessionValid(realm, userSessionModel));
    realm.setSsoSessionMaxLifespan(Integer.MAX_VALUE);
    Assert.assertTrue("Session validataion with large SsoSessionMaxLifespan failed", AuthenticationManager.isSessionValid(realm, userSessionModel));
    realm.setSsoSessionIdleTimeout(ssoSessionIdleTimeoutOrig);
    realm.setSsoSessionMaxLifespan(ssoSessionMaxLifespanOrig);
}
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 75 with ModelTest

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

the class UserConsentWithUserStorageModelTest method updateWithClientScopeRemovalTest.

@Test
@ModelTest
public void updateWithClientScopeRemovalTest(KeycloakSession session) {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionScopeRemoval1) -> {
        KeycloakSession currentSession = sessionScopeRemoval1;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        ClientModel fooClient = realm.getClientByClientId("foo-client");
        UserModel john = currentSession.users().getUserByUsername(realm, "john");
        UserConsentModel johnConsent = currentSession.users().getConsentByClient(realm, john.getId(), fooClient.getId());
        Assert.assertEquals(1, johnConsent.getGrantedClientScopes().size());
        // Remove foo protocol mapper from johnConsent
        ClientScopeModel fooScope = KeycloakModelUtils.getClientScopeByName(realm, "foo");
        johnConsent.getGrantedClientScopes().remove(fooScope);
        currentSession.users().updateConsent(realm, john.getId(), johnConsent);
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionScopeRemoval2) -> {
        KeycloakSession currentSession = sessionScopeRemoval2;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        ClientModel fooClient = realm.getClientByClientId("foo-client");
        UserModel john = currentSession.users().getUserByUsername(realm, "john");
        UserConsentModel johnConsent = currentSession.users().getConsentByClient(realm, john.getId(), fooClient.getId());
        Assert.assertEquals(johnConsent.getGrantedClientScopes().size(), 0);
        Assert.assertTrue("Created date should be less than last updated date", johnConsent.getCreatedDate() < johnConsent.getLastUpdatedDate());
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) ClientModel(org.keycloak.models.ClientModel) KeycloakSession(org.keycloak.models.KeycloakSession) ClientScopeModel(org.keycloak.models.ClientScopeModel) 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)

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