Search in sources :

Example 46 with RealmModel

use of org.keycloak.models.RealmModel in project keycloak by keycloak.

the class LDAPAccountRestApiTest method afterImportTestRealm.

@Override
protected void afterImportTestRealm() {
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        // Delete all LDAP users and add some new for testing
        LDAPTestUtils.removeAllLDAPUsers(ctx.getLdapProvider(), appRealm);
        LDAPObject john = LDAPTestUtils.addLDAPUser(ctx.getLdapProvider(), appRealm, "johnkeycloak", "John", "Doe", "john@email.org", null, "1234");
        LDAPTestUtils.updateLDAPPassword(ctx.getLdapProvider(), john, "Password1");
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) LDAPObject(org.keycloak.storage.ldap.idm.model.LDAPObject)

Example 47 with RealmModel

use of org.keycloak.models.RealmModel in project keycloak by keycloak.

the class LDAPAccountRestApiTest method testUpdateProfileSimple.

@Test
public void testUpdateProfileSimple() throws IOException {
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        appRealm.setEditUsernameAllowed(false);
    });
    UserRepresentation user = SimpleHttp.doGet(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).asJson(UserRepresentation.class);
    user.setEmail("john-alias@email.org");
    SimpleHttp.doPost(getAccountUrl(null), httpClient).json(user).auth(tokenUtil.getToken()).asStatus();
    UserRepresentation usernew = SimpleHttp.doGet(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).asJson(UserRepresentation.class);
    assertEquals("johnkeycloak", usernew.getUsername());
    assertEquals("John", usernew.getFirstName());
    assertEquals("Doe", usernew.getLastName());
    assertEquals("john-alias@email.org", usernew.getEmail());
    assertFalse(usernew.isEmailVerified());
    // clean up
    usernew.setEmail("john@email.org");
    SimpleHttp.doPost(getAccountUrl(null), httpClient).json(usernew).auth(tokenUtil.getToken()).asStatus();
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test)

Example 48 with RealmModel

use of org.keycloak.models.RealmModel in project keycloak by keycloak.

the class LDAPAccountRestApiTest method testIgnoreReadOnlyAttributes.

@Test
public void testIgnoreReadOnlyAttributes() throws IOException {
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        appRealm.setEditUsernameAllowed(false);
    });
    UserRepresentation user = SimpleHttp.doGet(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).asJson(UserRepresentation.class);
    user.setEmail("john-alias@email.org");
    SimpleHttp.doPost(getAccountUrl(null), httpClient).json(user).auth(tokenUtil.getToken()).asStatus();
    UserRepresentation usernew = SimpleHttp.doGet(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).asJson(UserRepresentation.class);
    assertEquals("johnkeycloak", usernew.getUsername());
    assertEquals("John", usernew.getFirstName());
    assertEquals("Doe", usernew.getLastName());
    assertEquals("john-alias@email.org", usernew.getEmail());
    assertFalse(usernew.isEmailVerified());
    usernew.getAttributes().clear();
    // clean up
    usernew.setEmail("john@email.org");
    final int i = SimpleHttp.doPost(getAccountUrl(null), httpClient).json(usernew).auth(tokenUtil.getToken()).asStatus();
    org.keycloak.representations.idm.UserRepresentation userRep = testRealm().users().search(usernew.getUsername()).get(0);
    userRep.setAttributes(null);
    testRealm().users().get(userRep.getId()).update(userRep);
    usernew = SimpleHttp.doGet(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).asJson(UserRepresentation.class);
    assertTrue(usernew.getAttributes().containsKey(LDAPConstants.LDAP_ID));
    assertTrue(usernew.getAttributes().containsKey(LDAPConstants.LDAP_ENTRY_DN));
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test)

Example 49 with RealmModel

use of org.keycloak.models.RealmModel in project keycloak by keycloak.

the class PasswordPolicyTest method testUpperCase.

@Test
public void testUpperCase() {
    testingClient.server("passwordPolicy").run(session -> {
        RealmModel realmModel = session.getContext().getRealm();
        PasswordPolicyManagerProvider policyManager = session.getProvider(PasswordPolicyManagerProvider.class);
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "upperCase"));
        Assert.assertEquals("invalidPasswordMinUpperCaseCharsMessage", policyManager.validate("jdoe", "abcd1234").getMessage());
        Assert.assertArrayEquals(new Object[] { 1 }, policyManager.validate("jdoe", "abcd1234").getParameters());
        assertNull(policyManager.validate("jdoe", "abCd1234"));
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "upperCase(2)"));
        Assert.assertEquals("invalidPasswordMinUpperCaseCharsMessage", policyManager.validate("jdoe", "abCd1234").getMessage());
        Assert.assertArrayEquals(new Object[] { 2 }, policyManager.validate("jdoe", "abCd1234").getParameters());
        assertNull(policyManager.validate("jdoe", "AbCd1234"));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) PasswordPolicyManagerProvider(org.keycloak.policy.PasswordPolicyManagerProvider) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 50 with RealmModel

use of org.keycloak.models.RealmModel in project keycloak by keycloak.

the class PasswordPolicyTest method testSpecialChars.

@Test
public void testSpecialChars() {
    testingClient.server("passwordPolicy").run(session -> {
        RealmModel realmModel = session.getContext().getRealm();
        PasswordPolicyManagerProvider policyManager = session.getProvider(PasswordPolicyManagerProvider.class);
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "specialChars"));
        Assert.assertEquals("invalidPasswordMinSpecialCharsMessage", policyManager.validate("jdoe", "abcd1234").getMessage());
        Assert.assertArrayEquals(new Object[] { 1 }, policyManager.validate("jdoe", "abcd1234").getParameters());
        assertNull(policyManager.validate("jdoe", "ab&d1234"));
        realmModel.setPasswordPolicy(PasswordPolicy.parse(session, "specialChars(2)"));
        Assert.assertEquals("invalidPasswordMinSpecialCharsMessage", policyManager.validate("jdoe", "ab&d1234").getMessage());
        Assert.assertArrayEquals(new Object[] { 2 }, policyManager.validate("jdoe", "ab&d1234").getParameters());
        assertNull(policyManager.validate("jdoe", "ab&d-234"));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) PasswordPolicyManagerProvider(org.keycloak.policy.PasswordPolicyManagerProvider) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Aggregations

RealmModel (org.keycloak.models.RealmModel)591 Test (org.junit.Test)249 UserModel (org.keycloak.models.UserModel)225 KeycloakSession (org.keycloak.models.KeycloakSession)152 ClientModel (org.keycloak.models.ClientModel)149 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)90 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)84 ComponentModel (org.keycloak.component.ComponentModel)83 RoleModel (org.keycloak.models.RoleModel)73 UserSessionModel (org.keycloak.models.UserSessionModel)64 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)62 List (java.util.List)55 LDAPStorageProvider (org.keycloak.storage.ldap.LDAPStorageProvider)51 GroupModel (org.keycloak.models.GroupModel)47 HashMap (java.util.HashMap)38 Collectors (java.util.stream.Collectors)34 CachedUserModel (org.keycloak.models.cache.CachedUserModel)34 Path (javax.ws.rs.Path)30 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)30 Map (java.util.Map)29