Search in sources :

Example 11 with ModelTest

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

the class UserConsentModelTest method basicConsentTest.

@Test
@ModelTest
public void basicConsentTest(KeycloakSession session) {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCT) -> {
        KeycloakSession currentSession = sessionCT;
        RealmModel realm = currentSession.realms().getRealm("original");
        ClientModel fooClient = realm.getClientByClientId("foo-client");
        ClientModel barClient = realm.getClientByClientId("bar-client");
        UserModel john = currentSession.users().getUserByUsername(realm, "john");
        UserModel mary = currentSession.users().getUserByUsername(realm, "mary");
        UserConsentModel johnFooConsent = currentSession.users().getConsentByClient(realm, john.getId(), fooClient.getId());
        Assert.assertEquals(johnFooConsent.getGrantedClientScopes().size(), 1);
        Assert.assertTrue(isClientScopeGranted(realm, "foo", johnFooConsent));
        Assert.assertNotNull("Created Date should be set", johnFooConsent.getCreatedDate());
        Assert.assertNotNull("Last Updated Date should be set", johnFooConsent.getLastUpdatedDate());
        UserConsentModel johnBarConsent = currentSession.users().getConsentByClient(realm, john.getId(), barClient.getId());
        Assert.assertEquals(johnBarConsent.getGrantedClientScopes().size(), 1);
        Assert.assertTrue(isClientScopeGranted(realm, "bar", johnBarConsent));
        Assert.assertNotNull("Created Date should be set", johnBarConsent.getCreatedDate());
        Assert.assertNotNull("Last Updated Date should be set", johnBarConsent.getLastUpdatedDate());
        UserConsentModel maryConsent = currentSession.users().getConsentByClient(realm, mary.getId(), fooClient.getId());
        Assert.assertEquals(maryConsent.getGrantedClientScopes().size(), 1);
        Assert.assertTrue(isClientScopeGranted(realm, "foo", maryConsent));
        Assert.assertNotNull("Created Date should be set", maryConsent.getCreatedDate());
        Assert.assertNotNull("Last Updated Date should be set", maryConsent.getLastUpdatedDate());
        ClientModel hardcodedClient = currentSession.clients().getClientByClientId(realm, "hardcoded-client");
        UserConsentModel maryHardcodedConsent = currentSession.users().getConsentByClient(realm, mary.getId(), hardcodedClient.getId());
        Assert.assertEquals(maryHardcodedConsent.getGrantedClientScopes().size(), 0);
        Assert.assertNotNull("Created Date should be set", maryHardcodedConsent.getCreatedDate());
        Assert.assertNotNull("Last Updated Date should be set", maryHardcodedConsent.getLastUpdatedDate());
        Assert.assertNull(currentSession.users().getConsentByClient(realm, mary.getId(), barClient.getId()));
        Assert.assertNull(currentSession.users().getConsentByClient(realm, john.getId(), hardcodedClient.getId()));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) ClientModel(org.keycloak.models.ClientModel) KeycloakSession(org.keycloak.models.KeycloakSession) 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)

Example 12 with ModelTest

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

the class UserConsentModelTest method deleteUserTest.

@Test
@ModelTest
public void deleteUserTest(KeycloakSession session) {
    // Validate user deleted without any referential constraint errors
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionUT) -> {
        KeycloakSession currentSession = sessionUT;
        RealmModel realm = currentSession.realms().getRealm("original");
        UserModel john = currentSession.users().getUserByUsername(realm, "john");
        currentSession.users().removeUser(realm, john);
        UserModel mary = currentSession.users().getUserByUsername(realm, "mary");
        currentSession.users().removeUser(realm, mary);
    });
}
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 13 with ModelTest

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

the class UserModelTest method testUserRequiredActions.

@Test
@ModelTest
public void testUserRequiredActions(KeycloakSession session) throws Exception {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUserReqActions) -> {
        KeycloakSession currentSession = sesUserReqActions;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        UserModel user = currentSession.users().addUser(realm, "user");
        List<String> requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
        Assert.assertThat(requiredActions, empty());
        user.addRequiredAction(RequiredAction.CONFIGURE_TOTP);
        String id = realm.getId();
        realm = currentSession.realms().getRealm(id);
        user = currentSession.users().getUserByUsername(realm, "user");
        requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
        Assert.assertThat(requiredActions, hasSize(1));
        Assert.assertThat(requiredActions, contains(RequiredAction.CONFIGURE_TOTP.name()));
        user.addRequiredAction(RequiredAction.CONFIGURE_TOTP);
        user = currentSession.users().getUserByUsername(realm, "user");
        requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
        Assert.assertThat(requiredActions, hasSize(1));
        Assert.assertThat(requiredActions, contains(RequiredAction.CONFIGURE_TOTP.name()));
        user.addRequiredAction(RequiredAction.VERIFY_EMAIL.name());
        user = currentSession.users().getUserByUsername(realm, "user");
        requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
        Assert.assertThat(requiredActions, hasSize(2));
        Assert.assertThat(requiredActions, containsInAnyOrder(RequiredAction.CONFIGURE_TOTP.name(), RequiredAction.VERIFY_EMAIL.name()));
        user.removeRequiredAction(RequiredAction.CONFIGURE_TOTP.name());
        user = currentSession.users().getUserByUsername(realm, "user");
        requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
        Assert.assertThat(requiredActions, hasSize(1));
        Assert.assertThat(requiredActions, contains(RequiredAction.VERIFY_EMAIL.name()));
        user.removeRequiredAction(RequiredAction.VERIFY_EMAIL.name());
        user = currentSession.users().getUserByUsername(realm, "user");
        requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
        Assert.assertThat(requiredActions, 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 14 with ModelTest

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

the class UserModelTest method webOriginSetTest.

@Test
@ModelTest
public void webOriginSetTest(KeycloakSession session) {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesWebOrigin) -> {
        KeycloakSession currentSession = sesWebOrigin;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        ClientModel client = realm.addClient("user");
        Assert.assertThat(client.getWebOrigins(), empty());
        client.addWebOrigin("origin-1");
        Assert.assertThat(client.getWebOrigins(), hasSize(1));
        client.addWebOrigin("origin-2");
        Assert.assertThat(client.getWebOrigins(), hasSize(2));
        client.removeWebOrigin("origin-2");
        Assert.assertThat(client.getWebOrigins(), hasSize(1));
        client.removeWebOrigin("origin-1");
        Assert.assertThat(client.getWebOrigins(), empty());
        client = realm.addClient("oauthclient2");
        Assert.assertThat(client.getWebOrigins(), empty());
        client.addWebOrigin("origin-1");
        Assert.assertThat(client.getWebOrigins(), hasSize(1));
        client.addWebOrigin("origin-2");
        Assert.assertThat(client.getWebOrigins(), hasSize(2));
        client.removeWebOrigin("origin-2");
        Assert.assertThat(client.getWebOrigins(), hasSize(1));
        client.removeWebOrigin("origin-1");
        Assert.assertThat(client.getWebOrigins(), empty());
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) 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 15 with ModelTest

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

the class UserModelTest method testUpdateUserSingleAttribute.

// KEYCLOAK-3608
@Test
@ModelTest
public void testUpdateUserSingleAttribute(KeycloakSession session) {
    AtomicReference<Map<String, List<String>>> expectedAtomic = new AtomicReference<>();
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUpdateUserSingleAtr) -> {
        KeycloakSession currentSession = sesUpdateUserSingleAtr;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        Map<String, List<String>> expected = new HashMap<>();
        expected.put("key1", Collections.singletonList("value3"));
        expected.put("key2", Collections.singletonList("value2"));
        expected.put(UserModel.FIRST_NAME, Collections.singletonList(null));
        expected.put(UserModel.LAST_NAME, Collections.singletonList(null));
        expected.put(UserModel.EMAIL, Collections.singletonList(null));
        expected.put(UserModel.USERNAME, Collections.singletonList("user"));
        UserModel user = currentSession.users().addUser(realm, "user");
        user.setSingleAttribute("key1", "value1");
        user.setSingleAttribute("key2", "value2");
        // KEYCLOAK-7014
        user.setSingleAttribute("key3", null);
        // Overwrite the first attribute
        user.setSingleAttribute("key1", "value3");
        Assert.assertThat(user.getAttributes(), equalTo(expected));
        expectedAtomic.set(expected);
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUpdateUserSingleAtr2) -> {
        KeycloakSession currentSession = sesUpdateUserSingleAtr2;
        RealmModel realm = currentSession.realms().getRealmByName("original");
        Map<String, List<String>> expected = expectedAtomic.get();
        Assert.assertThat(currentSession.users().getUserByUsername(realm, "user").getAttributes(), equalTo(expected));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) HashMap(java.util.HashMap) KeycloakSession(org.keycloak.models.KeycloakSession) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) 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