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