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"));
});
}
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());
});
}
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));
});
}
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);
}
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());
});
}
Aggregations