Search in sources :

Example 1 with TestPasswordRuleConf

use of org.apache.syncope.fit.core.reference.TestPasswordRuleConf in project syncope by apache.

the class UserITCase method customPolicyRules.

@Test
public void customPolicyRules() {
    // Using custom policy rules with application/xml requires to overwrite
    // org.apache.syncope.common.lib.policy.AbstractAccountRuleConf's and / or
    // org.apache.syncope.common.lib.policy.AbstractPasswordRuleConf's
    // @XmlSeeAlso - the power of JAXB :-/
    assumeTrue(MediaType.APPLICATION_JSON_TYPE.equals(clientFactory.getContentType().getMediaType()));
    ImplementationTO implementationTO = new ImplementationTO();
    implementationTO.setKey("TestAccountRuleConf" + UUID.randomUUID().toString());
    implementationTO.setEngine(ImplementationEngine.JAVA);
    implementationTO.setType(ImplementationType.ACCOUNT_RULE);
    implementationTO.setBody(POJOHelper.serialize(new TestAccountRuleConf()));
    Response response = implementationService.create(implementationTO);
    implementationTO.setKey(response.getHeaderString(RESTHeaders.RESOURCE_KEY));
    AccountPolicyTO accountPolicy = new AccountPolicyTO();
    accountPolicy.setDescription("Account Policy with custom rules");
    accountPolicy.getRules().add(implementationTO.getKey());
    accountPolicy = createPolicy(PolicyType.ACCOUNT, accountPolicy);
    assertNotNull(accountPolicy);
    implementationTO = new ImplementationTO();
    implementationTO.setKey("TestPasswordRuleConf" + UUID.randomUUID().toString());
    implementationTO.setEngine(ImplementationEngine.JAVA);
    implementationTO.setType(ImplementationType.PASSWORD_RULE);
    implementationTO.setBody(POJOHelper.serialize(new TestPasswordRuleConf()));
    response = implementationService.create(implementationTO);
    implementationTO.setKey(response.getHeaderString(RESTHeaders.RESOURCE_KEY));
    PasswordPolicyTO passwordPolicy = new PasswordPolicyTO();
    passwordPolicy.setDescription("Password Policy with custom rules");
    passwordPolicy.getRules().add(implementationTO.getKey());
    passwordPolicy = createPolicy(PolicyType.PASSWORD, passwordPolicy);
    assertNotNull(passwordPolicy);
    RealmTO realm = realmService.list("/even/two").get(0);
    String oldAccountPolicy = realm.getAccountPolicy();
    realm.setAccountPolicy(accountPolicy.getKey());
    String oldPasswordPolicy = realm.getPasswordPolicy();
    realm.setPasswordPolicy(passwordPolicy.getKey());
    realmService.update(realm);
    try {
        UserTO user = getUniqueSampleTO("custompolicyrules@syncope.apache.org");
        user.setRealm(realm.getFullPath());
        try {
            createUser(user);
            fail("This should not happen");
        } catch (SyncopeClientException e) {
            assertEquals(ClientExceptionType.InvalidUser, e.getType());
            assertTrue(e.getElements().iterator().next().startsWith("InvalidPassword"));
        }
        user.setPassword(user.getPassword() + "XXX");
        try {
            createUser(user);
            fail("This should not happen");
        } catch (SyncopeClientException e) {
            assertEquals(ClientExceptionType.InvalidUser, e.getType());
            assertTrue(e.getElements().iterator().next().startsWith("InvalidUsername"));
        }
        user.setUsername("YYY" + user.getUsername());
        user = createUser(user).getEntity();
        assertNotNull(user);
    } finally {
        realm.setAccountPolicy(oldAccountPolicy);
        realm.setPasswordPolicy(oldPasswordPolicy);
        realmService.update(realm);
        policyService.delete(PolicyType.PASSWORD, passwordPolicy.getKey());
        policyService.delete(PolicyType.ACCOUNT, accountPolicy.getKey());
    }
}
Also used : ImplementationTO(org.apache.syncope.common.lib.to.ImplementationTO) Response(javax.ws.rs.core.Response) TestPasswordRuleConf(org.apache.syncope.fit.core.reference.TestPasswordRuleConf) UserTO(org.apache.syncope.common.lib.to.UserTO) RealmTO(org.apache.syncope.common.lib.to.RealmTO) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) AccountPolicyTO(org.apache.syncope.common.lib.policy.AccountPolicyTO) TestAccountRuleConf(org.apache.syncope.fit.core.reference.TestAccountRuleConf) PasswordPolicyTO(org.apache.syncope.common.lib.policy.PasswordPolicyTO) Test(org.junit.jupiter.api.Test)

Aggregations

Response (javax.ws.rs.core.Response)1 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)1 AccountPolicyTO (org.apache.syncope.common.lib.policy.AccountPolicyTO)1 PasswordPolicyTO (org.apache.syncope.common.lib.policy.PasswordPolicyTO)1 ImplementationTO (org.apache.syncope.common.lib.to.ImplementationTO)1 RealmTO (org.apache.syncope.common.lib.to.RealmTO)1 UserTO (org.apache.syncope.common.lib.to.UserTO)1 TestAccountRuleConf (org.apache.syncope.fit.core.reference.TestAccountRuleConf)1 TestPasswordRuleConf (org.apache.syncope.fit.core.reference.TestPasswordRuleConf)1 Test (org.junit.jupiter.api.Test)1