package at.asitplus.eidas.specific.modules.auth.eidas.v2.test.tasks; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import java.util.Collections; import org.apache.commons.lang3.RandomStringUtils; 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.ActiveProfiles; 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.core.MsEidasNodeConstants; import at.asitplus.eidas.specific.core.test.config.dummy.MsConnectorDummyConfigMap; import at.asitplus.eidas.specific.modules.auth.eidas.v2.Constants; import at.asitplus.eidas.specific.modules.auth.eidas.v2.dao.SelectedLoginMethod; import at.asitplus.eidas.specific.modules.auth.eidas.v2.service.RegisterSearchService.RegisterOperationStatus; import at.asitplus.eidas.specific.modules.auth.eidas.v2.service.RegisterSearchService.RegisterStatusResults; import at.asitplus.eidas.specific.modules.auth.eidas.v2.tasks.ReceiveOtherLoginMethodGuiResponseTask; import at.asitplus.eidas.specific.modules.auth.eidas.v2.utils.MatchingTaskUtils; import at.gv.egiz.eaaf.core.api.IRequest; import at.gv.egiz.eaaf.core.api.IRequestStorage; import at.gv.egiz.eaaf.core.api.idp.ISpConfiguration; import at.gv.egiz.eaaf.core.exceptions.EaafException; import at.gv.egiz.eaaf.core.exceptions.TaskExecutionException; import at.gv.egiz.eaaf.core.impl.idp.module.test.DummySpConfiguration; import at.gv.egiz.eaaf.core.impl.idp.module.test.TestRequestImpl; import at.gv.egiz.eaaf.core.impl.idp.process.ExecutionContextImpl; import lombok.SneakyThrows; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/SpringTest-context_tasks_test.xml", "/SpringTest-context_basic_mapConfig.xml" }) @ActiveProfiles(profiles = {"deprecatedConfig"}) @WebAppConfiguration public class ReceiveOtherLoginMethodGuiResponseTaskTest { @Autowired MsConnectorDummyConfigMap config; @Autowired private IRequestStorage storage; @Autowired private ReceiveOtherLoginMethodGuiResponseTask task; private final 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 + "src/test/resources/config/junit_config_1.properties"); } /** * jUnit test set-up. */ @Before @SneakyThrows public void initialize() { httpReq = new MockHttpServletRequest("POST", "https://localhost/ms_connector"); httpResp = new MockHttpServletResponse(); RequestContextHolder.resetRequestAttributes(); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(httpReq, httpResp)); ISpConfiguration spConfig = new DummySpConfiguration(Collections.emptyMap(), config); pendingReq = new TestRequestImpl(); pendingReq.setAuthUrl("https://localhost/ms_connector"); pendingReq.setPendingReqId(RandomStringUtils.randomAlphanumeric(10)); pendingReq.setSpConfig(spConfig); config.putConfigValue("auth.eIDAS.matching.byaddress.enable", "false"); MatchingTaskUtils.storeIntermediateMatchingResult(pendingReq, new RegisterStatusResults(new RegisterOperationStatus(null, true), Collections.emptyList(), Collections.emptyList())); LocaleContextHolder.resetLocaleContext(); } @Test @SneakyThrows public void withStopMatchingSelection() throws TaskExecutionException { httpReq.setParameter(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, SelectedLoginMethod.STOP_MATCHING_PROCESS.name()); task.execute(pendingReq, executionContext); assertTrue("stoppedByUser", pendingReq.isAbortedByUser()); assertNotNull("matchingstate", pendingReq.getRawData(MsEidasNodeConstants.DATA_MATCHING_STATE)); assertEquals("matchingState", MsEidasNodeConstants.MatchingStates.CANCELED_BY_USER, pendingReq.getRawData(MsEidasNodeConstants.DATA_MATCHING_STATE)); IRequest storedPendingReq = storage.getPendingRequest(pendingReq.getPendingRequestId()); assertNotNull("pendingReq not stored", storedPendingReq); assertNotNull("matchingstate storedReq", storedPendingReq.getRawData(MsEidasNodeConstants.DATA_MATCHING_STATE)); assertEquals("matchingState storedReq", MsEidasNodeConstants.MatchingStates.CANCELED_BY_USER, storedPendingReq.getRawData(MsEidasNodeConstants.DATA_MATCHING_STATE)); } @Test public void withMobileSignatureSelection() throws TaskExecutionException { testTransition(SelectedLoginMethod.MOBILE_PHONE_SIGNATURE_LOGIN, Constants.TRANSITION_TO_GENERATE_MOBILE_PHONE_SIGNATURE_REQUEST_TASK); assertEquals("return to selection", false, executionContext.get(Constants.TRANSITION_TO_GENERATE_OTHER_LOGIN_METHOD_GUI_TASK)); } @Test public void withEidasSelection() throws TaskExecutionException { testTransition(SelectedLoginMethod.EIDAS_LOGIN, Constants.TRANSITION_TO_GENERATE_EIDAS_LOGIN); assertEquals("return to selection", false, executionContext.get(Constants.TRANSITION_TO_GENERATE_OTHER_LOGIN_METHOD_GUI_TASK)); } @Test public void withNoOtherLoginSelection() throws TaskExecutionException { config.putConfigValue("auth.eIDAS.matching.byaddress.enable", "true"); testTransition(SelectedLoginMethod.NO_OTHER_LOGIN, Constants.TRANSITION_TO_GENERATE_GUI_QUERY_AUSTRIAN_RESIDENCE_TASK); assertEquals("return to selection", false, executionContext.get(Constants.TRANSITION_TO_GENERATE_OTHER_LOGIN_METHOD_GUI_TASK)); } @Test public void withNoOtherLoginSelectionDisabled() throws TaskExecutionException { httpReq.setParameter(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, SelectedLoginMethod.NO_OTHER_LOGIN.name()); TaskExecutionException error = assertThrows("wrong exception", TaskExecutionException.class, () -> task.execute(pendingReq, executionContext)); assertEquals("wrong errorCode", "module.eidasauth.matching.98", ((EaafException) error.getOriginalException()).getErrorId()); } @Test public void withAddMeAsNewSelection() throws TaskExecutionException { testTransition(SelectedLoginMethod.ADD_ME_AS_NEW, Constants.TRANSITION_TO_CREATE_NEW_ERNP_ENTRY_BY_USER_TASK); assertEquals("return to selection", false, executionContext.get(Constants.TRANSITION_TO_GENERATE_OTHER_LOGIN_METHOD_GUI_TASK)); assertEquals("return to selection", false, executionContext.get(Constants.TRANSITION_TO_REQUESTING_NEW_ERNP_ENTRY_TASK)); assertNull("return to selection", executionContext.get(Constants.TRANSITION_TO_GENERATE_EIDAS_LOGIN)); assertNull("return to selection", executionContext.get(Constants.TRANSITION_TO_GENERATE_GUI_QUERY_AUSTRIAN_RESIDENCE_TASK)); assertNull("return to selection", executionContext.get(Constants.TRANSITION_TO_GENERATE_MOBILE_PHONE_SIGNATURE_REQUEST_TASK)); assertNull("return to selection", executionContext.get(Constants.TRANSITION_TO_CREATE_NEW_ERNP_ENTRY_TASK)); } @Test @SneakyThrows public void withAddMeAsNewSelectionButNotAllowed() throws TaskExecutionException { MatchingTaskUtils.storeIntermediateMatchingResult(pendingReq, new RegisterStatusResults(new RegisterOperationStatus(null, false), Collections.emptyList(), Collections.emptyList())); httpReq.setParameter(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, SelectedLoginMethod.ADD_ME_AS_NEW.name()); TaskExecutionException error = assertThrows("wrong exception", TaskExecutionException.class, () -> task.execute(pendingReq, executionContext)); assertEquals("wrong errorCode", "module.eidasauth.matching.98", ((EaafException) error.getOriginalException()).getErrorId()); } @Test public void withRequestingNewEntrySelection() throws TaskExecutionException { testTransition(SelectedLoginMethod.REQUESTING_NEW_ENTRY, Constants.TRANSITION_TO_REQUESTING_NEW_ERNP_ENTRY_TASK); assertEquals("return to selection", true, executionContext.get(Constants.TRANSITION_TO_REQUESTING_NEW_ERNP_ENTRY_TASK)); assertEquals("return to selection", false, executionContext.get(Constants.TRANSITION_TO_GENERATE_OTHER_LOGIN_METHOD_GUI_TASK)); assertNull("return to selection", executionContext.get(Constants.TRANSITION_TO_CREATE_NEW_ERNP_ENTRY_TASK)); assertNull("return to selection", executionContext.get(Constants.TRANSITION_TO_GENERATE_EIDAS_LOGIN)); assertNull("return to selection", executionContext.get(Constants.TRANSITION_TO_GENERATE_GUI_QUERY_AUSTRIAN_RESIDENCE_TASK)); assertNull("return to selection", executionContext.get(Constants.TRANSITION_TO_GENERATE_MOBILE_PHONE_SIGNATURE_REQUEST_TASK)); } public void testTransition(SelectedLoginMethod loginMethod, String expectedTransition) throws TaskExecutionException { httpReq.setParameter(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, loginMethod.name()); executionContext.put(Constants.CONTEXT_FLAG_ADVANCED_MATCHING_FAILED, true); task.execute(pendingReq, executionContext); assertFalse("wrong pendingReq auth flag", pendingReq.isAuthenticated()); assertFalse("wrong process-cancelled flag", executionContext.isProcessCancelled()); assertNotNull("no login-selection found", executionContext.get(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER)); assertEquals("Wrong login-selection found", loginMethod.name(), executionContext.get(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER)); assertEquals("Next task", true, executionContext.get(expectedTransition)); assertNull("find advancedMatchingError flag", executionContext.get(Constants.CONTEXT_FLAG_ADVANCED_MATCHING_FAILED)); } public void withInvalidSelection() throws TaskExecutionException { httpReq.setParameter(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, RandomStringUtils.randomAlphabetic(2)); task.execute(pendingReq, executionContext); assertEquals("Next task", true, executionContext.get(Constants.TRANSITION_TO_GENERATE_OTHER_LOGIN_METHOD_GUI_TASK)); assertEquals("advancedMatchingError flag", true, executionContext.get(Constants.CONTEXT_FLAG_ADVANCED_MATCHING_FAILED)); } @Test public void withNullSelection() throws TaskExecutionException { httpReq.setParameter(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, "null"); task.execute(pendingReq, executionContext); assertEquals("Next task", true, executionContext.get(Constants.TRANSITION_TO_GENERATE_OTHER_LOGIN_METHOD_GUI_TASK)); assertEquals("advancedMatchingError flag", true, executionContext.get(Constants.CONTEXT_FLAG_ADVANCED_MATCHING_FAILED)); } @Test public void withEmptySelection() throws TaskExecutionException { httpReq.setParameter(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, ""); task.execute(pendingReq, executionContext); assertEquals("Next task", true, executionContext.get(Constants.TRANSITION_TO_GENERATE_OTHER_LOGIN_METHOD_GUI_TASK)); assertEquals("advancedMatchingError flag", true, executionContext.get(Constants.CONTEXT_FLAG_ADVANCED_MATCHING_FAILED)); } @Test public void withoutLoginMethodSelection() throws TaskExecutionException { task.execute(pendingReq, executionContext); assertEquals("Next task", true, executionContext.get(Constants.TRANSITION_TO_GENERATE_OTHER_LOGIN_METHOD_GUI_TASK)); assertEquals("advancedMatchingError flag", true, executionContext.get(Constants.CONTEXT_FLAG_ADVANCED_MATCHING_FAILED)); } }