package at.gv.egiz.eaaf.modules.pvp2.idp.test; import java.util.Date; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import at.gv.egiz.eaaf.core.api.data.EaafConstants; import at.gv.egiz.eaaf.core.api.idp.IAuthData; import at.gv.egiz.eaaf.core.api.idp.auth.data.IIdentityLink; import at.gv.egiz.eaaf.core.impl.idp.module.test.DummyAuthConfig; import at.gv.egiz.eaaf.modules.pvp2.idp.exception.ResponderErrorException; import at.gv.egiz.eaaf.modules.pvp2.idp.impl.AuthenticationAction; import at.gv.egiz.eaaf.modules.pvp2.idp.impl.PvpSProfilePendingRequest; import at.gv.egiz.eaaf.modules.pvp2.impl.metadata.PvpMetadataResolverFactory; import at.gv.egiz.eaaf.modules.pvp2.impl.opensaml.initialize.EaafOpenSaml3xInitializer; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"/spring/test_eaaf_pvp.beans.xml"}) @TestPropertySource( locations = {"/config/config_1.props"}) public class AuthenticationActionTest { @Autowired private DummyAuthConfig authConfig; @Autowired private PvpMetadataResolverFactory metadataResolverFactory; @Autowired private AuthenticationAction action; protected MockHttpServletRequest httpReq; protected MockHttpServletResponse httpResp; private PvpSProfilePendingRequest pendingReq; /** * JUnit class initializer. * * @throws Exception In case of an OpenSAML3 initialization error */ @BeforeClass public static void classInitializer() throws Exception { EaafOpenSaml3xInitializer.eaafInitialize(); } /** * Test initializer. * */ @Before public void initialize() { httpReq = new MockHttpServletRequest(); httpResp = new MockHttpServletResponse(); pendingReq = new PvpSProfilePendingRequest(); } @Test public void checkNeedAuthFlag() { Assert.assertTrue("Wrong 'needAuth' flag", action.needAuthentication(pendingReq, httpReq, httpResp)); } @Test public void noAuthnRequestInPendingRequest() { IAuthData authData = generateAuthData(); try { action.processRequest(pendingReq, httpReq, httpResp, authData); Assert.fail("No SAML requst not detected"); } catch (ResponderErrorException e) { Assert.assertEquals("Wrong errorCode", "pvp2.01", e.getErrorId()); } } private IAuthData generateAuthData() { return new IAuthData() { @Override public boolean isSsoSession() { return false; } @Override public boolean isForeigner() { return false; } @Override public boolean isBaseIdTransferRestrication() { return true; } @Override public Date getSsoSessionValidTo() { return null; } @Override public String getSessionIndex() { return null; } @Override public String getNameIdFormat() { return null; } @Override public String getNameID() { return null; } @Override public IIdentityLink getIdentityLink() { return null; } @Override public String getIdentificationValue() { return null; } @Override public String getIdentificationType() { return null; } @Override public String getGivenName() { return RandomStringUtils.randomAlphabetic(10); } @Override public T getGenericData(String key, Class clazz) { // TODO Auto-generated method stub return null; } @Override public String getFormatedDateOfBirth() { return DateFormatUtils.format(getDateOfBirth(), "yyyy-MM-dd"); } @Override public String getFamilyName() { return RandomStringUtils.randomAlphabetic(10); } @Override public String getEncryptedSourceIdType() { // TODO Auto-generated method stub return null; } @Override public String getEncryptedSourceId() { // TODO Auto-generated method stub return null; } @Override public String getEidasQaaLevel() { return EaafConstants.EIDAS_LOA_LOW; } @Override public Date getDateOfBirth() { return new Date(); } @Override public String getCiticenCountryCode() { // TODO Auto-generated method stub return null; } @Override public String getBpkType() { return EaafConstants.URN_PREFIX_CDID + RandomStringUtils.randomAlphabetic(2); } @Override public String getBpk() { return RandomStringUtils.randomAlphabetic(10); } @Override public String getAuthenticationIssuer() { return RandomStringUtils.randomAlphabetic(10); } @Override public String getAuthenticationIssueInstantString() { return DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.format(getAuthenticationIssueInstant()); } @Override public Date getAuthenticationIssueInstant() { return new Date(); } }; } }