package at.asitplus.eidas.specific.connector.test.task; import java.io.UnsupportedEncodingException; import org.apache.commons.lang3.RandomStringUtils; 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.context.i18n.LocaleContextHolder; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import at.asitplus.eidas.specific.connector.MsEidasNodeConstants; import at.asitplus.eidas.specific.connector.processes.tasks.EvaluateCountrySelectionTask; import at.gv.egiz.eaaf.core.api.data.EaafConstants; import at.gv.egiz.eaaf.core.exceptions.TaskExecutionException; import at.gv.egiz.eaaf.core.impl.idp.controller.tasks.AbstractLocaleAuthServletTask; import at.gv.egiz.eaaf.core.impl.idp.module.test.TestRequestImpl; import at.gv.egiz.eaaf.core.impl.idp.process.ExecutionContextImpl; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "/applicationContext.xml", "/specific_eIDAS_connector.beans.xml", "/eaaf_core.beans.xml", "/eaaf_pvp.beans.xml", "/eaaf_pvp_idp.beans.xml", "/spring/SpringTest-context_simple_storage.xml" }) @WebAppConfiguration public class EvaluateCountrySelectionTaskTest { @Autowired private EvaluateCountrySelectionTask task; private ExecutionContextImpl executionContext = new ExecutionContextImpl(); private TestRequestImpl pendingReq; private MockHttpServletRequest httpReq; private MockHttpServletResponse httpResp; /** * jUnit class initializer. * */ @BeforeClass public static void classInitializer() { final String current = new java.io.File(".").toURI().toString(); System.setProperty("eidas.ms.configuration", current + "../basicConfig/default_config.properties"); } /** * jUnit test set-up. * */ @Before public void initialize() { httpReq = new MockHttpServletRequest("POST", "https://localhost/ms_connector"); httpResp = new MockHttpServletResponse(); RequestContextHolder.resetRequestAttributes(); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(httpReq, httpResp)); pendingReq = new TestRequestImpl(); pendingReq.setAuthUrl("https://localhost/ms_connector"); pendingReq.setPendingReqId(RandomStringUtils.randomAlphanumeric(10)); LocaleContextHolder.resetLocaleContext(); } @Test public void withoutCountrySelection() throws TaskExecutionException, UnsupportedEncodingException { task.execute(pendingReq, executionContext); //result validation Assert.assertFalse("wrong pendingReq auth flag", pendingReq.isAuthenticated()); Assert.assertFalse("wrong process-cancelled flag", executionContext.isProcessCancelled()); Assert.assertEquals("wrong change-language flag", false, executionContext.get(EaafConstants.PROCESSCONTEXT_SWITCH_LANGUAGE)); Assert.assertNull("Country-selection found", executionContext.get(MsEidasNodeConstants.REQ_PARAM_SELECTED_COUNTRY)); Assert.assertNull("Environment-selection found", executionContext.get(MsEidasNodeConstants.REQ_PARAM_SELECTED_ENVIRONMENT)); } @Test public void withCountrySelection() throws TaskExecutionException, UnsupportedEncodingException { String ccc = RandomStringUtils.randomAlphabetic(2); httpReq.setParameter(MsEidasNodeConstants.REQ_PARAM_SELECTED_COUNTRY, ccc); task.execute(pendingReq, executionContext); //result validation Assert.assertFalse("wrong pendingReq auth flag", pendingReq.isAuthenticated()); Assert.assertFalse("wrong process-cancelled flag", executionContext.isProcessCancelled()); Assert.assertEquals("wrong change-language flag", false, executionContext.get(EaafConstants.PROCESSCONTEXT_SWITCH_LANGUAGE)); Assert.assertNotNull("no Country-selection found", executionContext.get(MsEidasNodeConstants.REQ_PARAM_SELECTED_COUNTRY)); Assert.assertEquals("Wrong country found", ccc, executionContext.get(MsEidasNodeConstants.REQ_PARAM_SELECTED_COUNTRY)); Assert.assertNull("Environment-selection found", executionContext.get(MsEidasNodeConstants.REQ_PARAM_SELECTED_ENVIRONMENT)); } @Test public void withCountrySelectionAndEnvironment() throws TaskExecutionException, UnsupportedEncodingException { String ccc = RandomStringUtils.randomAlphabetic(2); String environment = RandomStringUtils.randomAlphabetic(5); httpReq.setParameter(MsEidasNodeConstants.REQ_PARAM_SELECTED_COUNTRY, ccc); httpReq.setParameter(MsEidasNodeConstants.REQ_PARAM_SELECTED_ENVIRONMENT, environment); task.execute(pendingReq, executionContext); //result validation Assert.assertFalse("wrong pendingReq auth flag", pendingReq.isAuthenticated()); Assert.assertFalse("wrong process-cancelled flag", executionContext.isProcessCancelled()); Assert.assertEquals("wrong change-language flag", false, executionContext.get(EaafConstants.PROCESSCONTEXT_SWITCH_LANGUAGE)); Assert.assertNotNull("no Country-selection found", executionContext.get(MsEidasNodeConstants.REQ_PARAM_SELECTED_COUNTRY)); Assert.assertEquals("Wrong country found", ccc, executionContext.get(MsEidasNodeConstants.REQ_PARAM_SELECTED_COUNTRY)); Assert.assertNotNull("No environment-selection found", executionContext.get(MsEidasNodeConstants.REQ_PARAM_SELECTED_ENVIRONMENT)); Assert.assertEquals("Wrong environment-selection", environment, executionContext.get(MsEidasNodeConstants.REQ_PARAM_SELECTED_ENVIRONMENT)); } @Test public void withCountrySelectionAndEnvironmentAndLangSelection() throws TaskExecutionException, UnsupportedEncodingException { String ccc = RandomStringUtils.randomAlphabetic(2); String environment = RandomStringUtils.randomAlphabetic(5); String lang = RandomStringUtils.randomAlphabetic(2); httpReq.setParameter(MsEidasNodeConstants.REQ_PARAM_SELECTED_COUNTRY, ccc); httpReq.setParameter(MsEidasNodeConstants.REQ_PARAM_SELECTED_ENVIRONMENT, environment); httpReq.setParameter(AbstractLocaleAuthServletTask.PROP_REQ_PARAM_LOCALE, lang); task.execute(pendingReq, executionContext); //result validation Assert.assertFalse("wrong pendingReq auth flag", pendingReq.isAuthenticated()); Assert.assertFalse("wrong process-cancelled flag", executionContext.isProcessCancelled()); Assert.assertEquals("wrong change-language flag", true, executionContext.get(EaafConstants.PROCESSCONTEXT_SWITCH_LANGUAGE)); Assert.assertNull("Country-selection found", executionContext.get(MsEidasNodeConstants.REQ_PARAM_SELECTED_COUNTRY)); Assert.assertNull("Environment-selection found", executionContext.get(MsEidasNodeConstants.REQ_PARAM_SELECTED_ENVIRONMENT)); } @Test public void withCountrySelectionAndEnvironmentAndStopSelection() throws TaskExecutionException, UnsupportedEncodingException { String ccc = RandomStringUtils.randomAlphabetic(2); String environment = RandomStringUtils.randomAlphabetic(5); httpReq.setParameter(MsEidasNodeConstants.REQ_PARAM_SELECTED_COUNTRY, ccc); httpReq.setParameter(MsEidasNodeConstants.REQ_PARAM_SELECTED_ENVIRONMENT, environment); httpReq.setParameter(EaafConstants.PARAM_HTTP_STOP_PROCESS, "true"); task.execute(pendingReq, executionContext); //result validation Assert.assertTrue("wrong process-cancelled flag", executionContext.isProcessCancelled()); Assert.assertTrue("wrong pendingReq stop flag", pendingReq.isAbortedByUser()); Assert.assertFalse("wrong pendingReq auth flag", pendingReq.isAuthenticated()); Assert.assertEquals("wrong change-language flag", false, executionContext.get(EaafConstants.PROCESSCONTEXT_SWITCH_LANGUAGE)); Assert.assertNull("Country-selection found", executionContext.get(MsEidasNodeConstants.REQ_PARAM_SELECTED_COUNTRY)); Assert.assertNull("Environment-selection found", executionContext.get(MsEidasNodeConstants.REQ_PARAM_SELECTED_ENVIRONMENT)); } }