Search in sources :

Example 1 with SAML2UserAuthenticator

use of org.apache.cloudstack.saml.SAML2UserAuthenticator in project cloudstack by apache.

the class SAML2UserAuthenticatorTest method authenticate.

@Test
public void authenticate() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    SAML2UserAuthenticator authenticator = new SAML2UserAuthenticator();
    Field daoField = SAML2UserAuthenticator.class.getDeclaredField("_userAccountDao");
    daoField.setAccessible(true);
    daoField.set(authenticator, userAccountDao);
    Field userDaoField = SAML2UserAuthenticator.class.getDeclaredField("_userDao");
    userDaoField.setAccessible(true);
    userDaoField.set(authenticator, userDao);
    UserAccountVO account = new UserAccountVO();
    account.setPassword("5f4dcc3b5aa765d61d8327deb882cf99");
    account.setId(1L);
    UserVO user = new UserVO();
    Mockito.when(userAccountDao.getUserAccount(Mockito.anyString(), Mockito.anyLong())).thenReturn(account);
    Mockito.when(userDao.getUser(Mockito.anyLong())).thenReturn(user);
    Pair<Boolean, ActionOnFailedAuthentication> pair;
    Map<String, Object[]> params = new HashMap<String, Object[]>();
    // When there is no SAMLRequest in params
    pair = authenticator.authenticate("someUID", "random", 1l, params);
    Assert.assertFalse(pair.first());
    // When there is SAMLRequest in params and user is same as the mocked one
    params.put(SAMLPluginConstants.SAML_RESPONSE, new String[] { "RandomString" });
    pair = authenticator.authenticate("someUID", "random", 1l, params);
    Assert.assertFalse(pair.first());
    // When there is SAMLRequest in params but username is null
    pair = authenticator.authenticate(null, "random", 1l, params);
    Assert.assertFalse(pair.first());
    // When there is SAMLRequest in params but username is empty
    pair = authenticator.authenticate("", "random", 1l, params);
    Assert.assertFalse(pair.first());
    // When there is SAMLRequest in params but username is not valid
    pair = authenticator.authenticate("someOtherUID", "random", 1l, params);
    Assert.assertFalse(pair.first());
}
Also used : SAML2UserAuthenticator(org.apache.cloudstack.saml.SAML2UserAuthenticator) Field(java.lang.reflect.Field) UserAccountVO(com.cloud.user.UserAccountVO) UserVO(com.cloud.user.UserVO) HashMap(java.util.HashMap) ActionOnFailedAuthentication(com.cloud.server.auth.UserAuthenticator.ActionOnFailedAuthentication) Test(org.junit.Test)

Aggregations

ActionOnFailedAuthentication (com.cloud.server.auth.UserAuthenticator.ActionOnFailedAuthentication)1 UserAccountVO (com.cloud.user.UserAccountVO)1 UserVO (com.cloud.user.UserVO)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 SAML2UserAuthenticator (org.apache.cloudstack.saml.SAML2UserAuthenticator)1 Test (org.junit.Test)1