From 2c49670334049a065d86defc8524f2e5eae6ca78 Mon Sep 17 00:00:00 2001 From: Christian Kollmann Date: Thu, 25 Feb 2021 11:32:57 +0100 Subject: Refactor and test ReceiveOtherLoginMethodGuiResponse --- .../v2/tasks/GenerateOtherLoginMethodGuiTask.java | 5 +- .../ReceiveOtherLoginMethodGuiResponseTask.java | 71 ++++++++--------- ...ReceiveOtherLoginMethodGuiResponseTaskTest.java | 89 ++++++++++++---------- 3 files changed, 87 insertions(+), 78 deletions(-) (limited to 'eidas_modules') diff --git a/eidas_modules/authmodule-eIDAS-v2/src/main/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/tasks/GenerateOtherLoginMethodGuiTask.java b/eidas_modules/authmodule-eIDAS-v2/src/main/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/tasks/GenerateOtherLoginMethodGuiTask.java index 0236b9c2..56aaa2db 100644 --- a/eidas_modules/authmodule-eIDAS-v2/src/main/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/tasks/GenerateOtherLoginMethodGuiTask.java +++ b/eidas_modules/authmodule-eIDAS-v2/src/main/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/tasks/GenerateOtherLoginMethodGuiTask.java @@ -40,8 +40,11 @@ import javax.servlet.http.HttpServletResponse; /** * Task that provides GUI for user to select an alternative login method. - * This page is shown when the matching of the eIDAS data to ZMR/ERnP data is ambiguous + * This page is shown when the matching of the eIDAS data to ZMR/ERnP data is ambiguous. + * This corresponds to Steps 10, 14, 16 in the eIDAS Matching Concept. + * The response is handled in {@link ReceiveOtherLoginMethodGuiResponseTask} * + * @author amarsalek * @author ckollmann */ @Slf4j diff --git a/eidas_modules/authmodule-eIDAS-v2/src/main/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/tasks/ReceiveOtherLoginMethodGuiResponseTask.java b/eidas_modules/authmodule-eIDAS-v2/src/main/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/tasks/ReceiveOtherLoginMethodGuiResponseTask.java index d8b80689..12eb7a83 100644 --- a/eidas_modules/authmodule-eIDAS-v2/src/main/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/tasks/ReceiveOtherLoginMethodGuiResponseTask.java +++ b/eidas_modules/authmodule-eIDAS-v2/src/main/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/tasks/ReceiveOtherLoginMethodGuiResponseTask.java @@ -26,13 +26,11 @@ package at.asitplus.eidas.specific.modules.auth.eidas.v2.tasks; 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.exception.InvalidUserInputException; -import at.gv.egiz.eaaf.core.api.data.EaafConstants; import at.gv.egiz.eaaf.core.api.idp.process.ExecutionContext; import at.gv.egiz.eaaf.core.exceptions.TaskExecutionException; import at.gv.egiz.eaaf.core.impl.idp.auth.modules.AbstractAuthServletTask; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringEscapeUtils; -import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @@ -41,7 +39,9 @@ import java.util.Enumeration; /** * Handles user's selection from {@link GenerateOtherLoginMethodGuiTask}. + * This corresponds to Steps 10, 14, 16 in the eIDAS Matching Concept. * + * @author amarsalek * @author ckollmann */ @Slf4j @@ -51,44 +51,45 @@ public class ReceiveOtherLoginMethodGuiResponseTask extends AbstractAuthServletT @Override public void execute(ExecutionContext executionContext, HttpServletRequest request, HttpServletResponse response) throws TaskExecutionException { - int found = 0; try { - // set parameter execution context - final Enumeration reqParamNames = request.getParameterNames(); - while (reqParamNames.hasMoreElements()) { - final String paramName = reqParamNames.nextElement(); - if (StringUtils.isNotEmpty(paramName) - && !EaafConstants.PROCESS_ENGINE_PENDINGREQUESTID.equalsIgnoreCase(paramName) - && Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER.equalsIgnoreCase(paramName)) { - String value = StringEscapeUtils.escapeHtml(request.getParameter(paramName)); - SelectedLoginMethod selection = SelectedLoginMethod.valueOf(value); - executionContext.put(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, selection); - switch (selection) { - case EIDAS_LOGIN: - executionContext.put(Constants.TRANSITION_TO_GENERATE_EIDAS_LOGIN, true); - found++; - break; - case MOBILE_PHONE_SIGNATURE_LOGIN: - executionContext.put(Constants.TRANSITION_TO_GENERATE_MOBILE_PHONE_SIGNATURE_REQUEST_TASK, true); - found++; - break; - case NO_OTHER_LOGIN: - executionContext.put(Constants.TRANSITION_TO_GENERATE_GUI_QUERY_AUSTRIAN_RESIDENCE_TASK, true); - found++; - break; - default: - throw new InvalidUserInputException(); - } - } - } + SelectedLoginMethod selection = SelectedLoginMethod.valueOf(extractUserSelection(request)); + executionContext.put(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, selection); + transitionToNextTask(executionContext, selection); + } catch (final IllegalArgumentException e) { + log.error("Parsing selected login method FAILED.", e); + throw new TaskExecutionException(pendingReq, "Parsing selected login method FAILED.", + new InvalidUserInputException()); } catch (final Exception e) { log.error("Parsing selected login method FAILED.", e); throw new TaskExecutionException(pendingReq, "Parsing selected login method FAILED.", e); } - if (found != 1) { - log.error("Parsing selected login method FAILED."); - throw new TaskExecutionException(pendingReq, "Parsing selected login method FAILED.", - new InvalidUserInputException()); + } + + private String extractUserSelection(HttpServletRequest request) throws InvalidUserInputException { + Enumeration paramNames = request.getParameterNames(); + while (paramNames.hasMoreElements()) { + String paramName = paramNames.nextElement(); + if (Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER.equalsIgnoreCase(paramName)) { + return StringEscapeUtils.escapeHtml(request.getParameter(paramName)); + } + } + throw new InvalidUserInputException(); + } + + private void transitionToNextTask(ExecutionContext executionContext, SelectedLoginMethod selection) + throws InvalidUserInputException { + switch (selection) { + case EIDAS_LOGIN: + executionContext.put(Constants.TRANSITION_TO_GENERATE_EIDAS_LOGIN, true); + return; + case MOBILE_PHONE_SIGNATURE_LOGIN: + executionContext.put(Constants.TRANSITION_TO_GENERATE_MOBILE_PHONE_SIGNATURE_REQUEST_TASK, true); + return; + case NO_OTHER_LOGIN: + executionContext.put(Constants.TRANSITION_TO_GENERATE_GUI_QUERY_AUSTRIAN_RESIDENCE_TASK, true); + return; + default: + throw new InvalidUserInputException(); } } diff --git a/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/tasks/ReceiveOtherLoginMethodGuiResponseTaskTest.java b/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/tasks/ReceiveOtherLoginMethodGuiResponseTaskTest.java index ae4b5d8c..c6b2e1fe 100644 --- a/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/tasks/ReceiveOtherLoginMethodGuiResponseTaskTest.java +++ b/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/tasks/ReceiveOtherLoginMethodGuiResponseTaskTest.java @@ -2,12 +2,12 @@ package at.asitplus.eidas.specific.modules.auth.eidas.v2.test.tasks; 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.exception.InvalidUserInputException; import at.asitplus.eidas.specific.modules.auth.eidas.v2.tasks.ReceiveOtherLoginMethodGuiResponseTask; import at.gv.egiz.eaaf.core.exceptions.TaskExecutionException; import at.gv.egiz.eaaf.core.impl.idp.module.test.TestRequestImpl; import at.gv.egiz.eaaf.core.impl.idp.process.ExecutionContextImpl; import org.apache.commons.lang3.RandomStringUtils; -import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -23,16 +23,10 @@ import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; -import java.io.UnsupportedEncodingException; +import static org.junit.Assert.*; +import static org.springframework.util.Assert.isInstanceOf; @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" }) @ContextConfiguration(locations = { "/SpringTest-context_tasks_test.xml", "/SpringTest-context_basic_mapConfig.xml" @@ -41,27 +35,25 @@ import java.io.UnsupportedEncodingException; @WebAppConfiguration public class ReceiveOtherLoginMethodGuiResponseTaskTest { - @Autowired private ReceiveOtherLoginMethodGuiResponseTask task; + @Autowired + private ReceiveOtherLoginMethodGuiResponseTask task; - private ExecutionContextImpl executionContext = new ExecutionContextImpl(); + 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 public void initialize() { @@ -79,57 +71,70 @@ public class ReceiveOtherLoginMethodGuiResponseTaskTest { @Test public void withMobileSignatureSelection() throws TaskExecutionException { - test(SelectedLoginMethod.MOBILE_PHONE_SIGNATURE_LOGIN); + testTransition(SelectedLoginMethod.MOBILE_PHONE_SIGNATURE_LOGIN, Constants.TRANSITION_TO_GENERATE_MOBILE_PHONE_SIGNATURE_REQUEST_TASK); } @Test public void withEidasSelection() throws TaskExecutionException { - test(SelectedLoginMethod.MOBILE_PHONE_SIGNATURE_LOGIN); + testTransition(SelectedLoginMethod.EIDAS_LOGIN, Constants.TRANSITION_TO_GENERATE_EIDAS_LOGIN); } @Test public void withNoOtherLoginSelection() throws TaskExecutionException { - test(SelectedLoginMethod.NO_OTHER_LOGIN); + testTransition(SelectedLoginMethod.NO_OTHER_LOGIN, Constants.TRANSITION_TO_GENERATE_GUI_QUERY_AUSTRIAN_RESIDENCE_TASK); } - public void test(SelectedLoginMethod loginMethod) throws TaskExecutionException { - String parameterValue = loginMethod.name(); - httpReq.setParameter(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, parameterValue); + public void testTransition(SelectedLoginMethod loginMethod, String expectedTransition) throws TaskExecutionException { + httpReq.setParameter(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, loginMethod.name()); task.execute(pendingReq, executionContext); - //result validation - Assert.assertFalse("wrong pendingReq auth flag", pendingReq.isAuthenticated()); - Assert.assertFalse("wrong process-cancelled flag", executionContext.isProcessCancelled()); - - Assert.assertNotNull("no login-selection found", - executionContext.get(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER)); - Assert.assertEquals("Wrong login-selection found", loginMethod, - executionContext.get(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER)); + 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, executionContext.get(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER)); + assertEquals("Next task", true, executionContext.get(expectedTransition)); } + public void withInvalidSelection() { + httpReq.setParameter(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, RandomStringUtils.randomAlphabetic(2)); - @Test(expected = TaskExecutionException.class) - public void withInvalidSelection() throws TaskExecutionException { - String parameterValue = RandomStringUtils.randomAlphabetic(2); - httpReq.setParameter(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, parameterValue); - task.execute(pendingReq, executionContext); + TaskExecutionException e = assertThrows(TaskExecutionException.class, + () -> task.execute(pendingReq, executionContext)); + + assertEquals(pendingReq.getPendingRequestId(), e.getPendingRequestID()); + isInstanceOf(InvalidUserInputException.class, e.getOriginalException()); } - @Test(expected = TaskExecutionException.class) - public void withNullSelection() throws TaskExecutionException { + @Test + public void withNullSelection() { httpReq.setParameter(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, "null"); - task.execute(pendingReq, executionContext); + + TaskExecutionException e = assertThrows(TaskExecutionException.class, + () -> task.execute(pendingReq, executionContext)); + + assertEquals(pendingReq.getPendingRequestId(), e.getPendingRequestID()); + isInstanceOf(InvalidUserInputException.class, e.getOriginalException()); } - @Test(expected = TaskExecutionException.class) - public void withEmptySelection() throws TaskExecutionException { + @Test + public void withEmptySelection() { httpReq.setParameter(Constants.REQ_SELECTED_LOGIN_METHOD_PARAMETER, ""); - task.execute(pendingReq, executionContext); + + TaskExecutionException e = assertThrows(TaskExecutionException.class, + () -> task.execute(pendingReq, executionContext)); + + assertEquals(pendingReq.getPendingRequestId(), e.getPendingRequestID()); + isInstanceOf(InvalidUserInputException.class, e.getOriginalException()); } - @Test(expected = TaskExecutionException.class) - public void withoutLoginMethodSelection() throws TaskExecutionException, UnsupportedEncodingException { - task.execute(pendingReq, executionContext); + @Test + public void withoutLoginMethodSelection() { + + TaskExecutionException e = assertThrows(TaskExecutionException.class, + () -> task.execute(pendingReq, executionContext)); + + assertEquals(pendingReq.getPendingRequestId(), e.getPendingRequestID()); + isInstanceOf(InvalidUserInputException.class, e.getOriginalException()); } } -- cgit v1.2.3