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.assertTrue; import java.io.UnsupportedEncodingException; import java.text.MessageFormat; import java.util.Collections; import java.util.Locale; 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 com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.json.JsonMapper; 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.GenerateOtherLoginMethodGuiTask; import at.asitplus.eidas.specific.modules.auth.eidas.v2.tasks.ReceiveOtherLoginMethodGuiResponseTask; import at.asitplus.eidas.specific.modules.auth.eidas.v2.test.dummy.DummyOA; import at.asitplus.eidas.specific.modules.auth.eidas.v2.utils.MatchingTaskUtils; import at.gv.egiz.eaaf.core.api.IRequestStorage; 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.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", "/common_gui.beans.xml" }) @WebAppConfiguration public class GenerateOtherLoginMethodGuiTaskTest { private static final String TEST_PATTER_REQ_PARAM = ""; @Autowired MsConnectorDummyConfigMap config; @Autowired GenerateOtherLoginMethodGuiTask task; @Autowired IRequestStorage storage; private ExecutionContextImpl executionContext; private TestRequestImpl pendingReq; private MockHttpServletRequest httpReq; private MockHttpServletResponse httpResp; @BeforeClass public static void classInitializer() { Locale.setDefault(Locale.ENGLISH); } /** * 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)); pendingReq = new TestRequestImpl(); pendingReq.setAuthUrl("https://localhost/ms_connector"); pendingReq.setPendingReqId(RandomStringUtils.randomAlphanumeric(10)); pendingReq.setSpConfig(new DummyOA()); executionContext = new ExecutionContextImpl(); 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 unsetExecutionContextParameters() throws TaskExecutionException, UnsupportedEncodingException { httpReq.addHeader("Accept", "application/json"); ReceiveOtherLoginMethodGuiResponseTask.ALL_EXECUTIONCONTEXT_PARAMETERS.forEach( el -> executionContext.put(el, RandomStringUtils.randomAlphabetic(5))); // execute test task.execute(pendingReq, executionContext); //result validation Assert.assertEquals("httpStausCode", 200, httpResp.getStatus()); Assert.assertEquals("http ContentType", "application/json;charset=UTF-8", httpResp.getContentType()); ReceiveOtherLoginMethodGuiResponseTask.ALL_EXECUTIONCONTEXT_PARAMETERS.forEach( el -> assertNull("executionContext parameter: " + el, executionContext.get(el))); // remove pendingRequestId and changeLanguage because it's added by default executionContext.remove(EaafConstants.PROCESS_ENGINE_PENDINGREQUESTID); executionContext.remove("changeLanguage"); // in case of 'ReceiveOtherLoginMethodGuiResponseTask.ALL_EXECUTIONCONTEXT_PARAMETERS' does not include all parameters assertTrue("ExecutionContext is not empty", executionContext.keySet().isEmpty()); } @Test @SneakyThrows public void jsonResponseInsertErnpScreen() throws TaskExecutionException, UnsupportedEncodingException { executionContext.put(Constants.TRANSITION_TO_REQUESTING_NEW_ERNP_ENTRY_TASK, true); httpReq.addHeader("Accept", "application/json"); task.execute(pendingReq, executionContext); //result validation Assert.assertEquals("httpStausCode", 200, httpResp.getStatus()); Assert.assertEquals("http ContentType", "application/json;charset=UTF-8", httpResp.getContentType()); final String content = httpResp.getContentAsString(); assertNotNull("response body is null", content); Assert.assertFalse("response body is empty", content.isEmpty()); final JsonNode json = new JsonMapper().readTree(content); assertNotNull("response body is null", json); assertNull("advancedMatchFailed", json.get(Constants.HTML_FORM_ADVANCED_MATCHING_FAILED)); assertNotNull("createNewErnpEntryScreen", json.get(Constants.HTML_FORM_WITH_CREATE_NEW_ERNP_ENTRY_OPTION)); assertTrue("createNewErnpEntryScreen", json.get(Constants.HTML_FORM_WITH_CREATE_NEW_ERNP_ENTRY_OPTION).asBoolean()); assertNotNull("disallowNewErnpEntry", json.get(Constants.HTML_FORM_DISALLOW_CREATENEW_ERNP_ENTRY)); assertFalse("disallowNewErnpEntry", json.get(Constants.HTML_FORM_DISALLOW_CREATENEW_ERNP_ENTRY).asBoolean()); assertNull("advancedMatchingFailedReason", json.get(Constants.HTML_FORM_ADVANCED_MATCHING_FAILED_REASON)); assertNotNull("pendingRequest not stored", storage.getPendingRequest(pendingReq.getPendingRequestId())); } @Test @SneakyThrows public void jsonResponseInsertErnpScreenButNotAllowed() throws TaskExecutionException, UnsupportedEncodingException { executionContext.put(Constants.TRANSITION_TO_REQUESTING_NEW_ERNP_ENTRY_TASK, true); httpReq.addHeader("Accept", "application/json"); MatchingTaskUtils.storeIntermediateMatchingResult(pendingReq, new RegisterStatusResults(new RegisterOperationStatus(null, false), Collections.emptyList(), Collections.emptyList())); task.execute(pendingReq, executionContext); //result validation Assert.assertEquals("httpStausCode", 200, httpResp.getStatus()); Assert.assertEquals("http ContentType", "application/json;charset=UTF-8", httpResp.getContentType()); final String content = httpResp.getContentAsString(); assertNotNull("response body is null", content); Assert.assertFalse("response body is empty", content.isEmpty()); final JsonNode json = new JsonMapper().readTree(content); assertNotNull("response body is null", json); assertNull("advancedMatchFailed", json.get(Constants.HTML_FORM_ADVANCED_MATCHING_FAILED)); assertNotNull("createNewErnpEntryScreen", json.get(Constants.HTML_FORM_WITH_CREATE_NEW_ERNP_ENTRY_OPTION)); assertTrue("createNewErnpEntryScreen", json.get(Constants.HTML_FORM_WITH_CREATE_NEW_ERNP_ENTRY_OPTION).asBoolean()); assertNotNull("disallowNewErnpEntry", json.get(Constants.HTML_FORM_DISALLOW_CREATENEW_ERNP_ENTRY)); assertTrue("disallowNewErnpEntry", json.get(Constants.HTML_FORM_DISALLOW_CREATENEW_ERNP_ENTRY).asBoolean()); assertNull("advancedMatchingFailedReason", json.get(Constants.HTML_FORM_ADVANCED_MATCHING_FAILED_REASON)); assertNotNull("pendingRequest not stored", storage.getPendingRequest(pendingReq.getPendingRequestId())); } @Test @SneakyThrows public void jsonResponseMathingFailed() throws TaskExecutionException, UnsupportedEncodingException { String reason = RandomStringUtils.randomAlphabetic(5); executionContext.put(Constants.CONTEXT_FLAG_ADVANCED_MATCHING_FAILED, true); executionContext.put(Constants.CONTEXT_FLAG_ADVANCED_MATCHING_FAILED_REASON, reason); httpReq.addHeader("Accept", "application/json"); task.execute(pendingReq, executionContext); //result validation Assert.assertEquals("httpStausCode", 200, httpResp.getStatus()); Assert.assertEquals("http ContentType", "application/json;charset=UTF-8", httpResp.getContentType()); final String content = httpResp.getContentAsString(); assertNotNull("response body is null", content); Assert.assertFalse("response body is empty", content.isEmpty()); final JsonNode json = new JsonMapper().readTree(content); assertNotNull("response body is null", json); assertNotNull("advancedMatchFailed", json.get(Constants.HTML_FORM_ADVANCED_MATCHING_FAILED)); assertTrue("advancedMatchFailed", json.get(Constants.HTML_FORM_ADVANCED_MATCHING_FAILED).asBoolean()); assertNotNull("advancedMatchingFailedReason", json.get(Constants.HTML_FORM_ADVANCED_MATCHING_FAILED_REASON)); assertEquals("advancedMatchingFailedReason", reason, json.get(Constants.HTML_FORM_ADVANCED_MATCHING_FAILED_REASON).asText()); assertNotNull("createNewErnpEntryScreen", json.get(Constants.HTML_FORM_WITH_CREATE_NEW_ERNP_ENTRY_OPTION)); assertFalse("createNewErnpEntryScreen", json.get(Constants.HTML_FORM_WITH_CREATE_NEW_ERNP_ENTRY_OPTION).asBoolean()); assertNotNull("enableMatchingByAddressSearch", json.get(Constants.HTML_FORM_WITH_CREATE_NEW_ERNP_ENTRY_OPTION)); assertFalse("enableMatchingByAddressSearch", json.get(Constants.HTML_FORM_WITH_CREATE_NEW_ERNP_ENTRY_OPTION).asBoolean()); assertNotNull("pendingRequest not stored", storage.getPendingRequest(pendingReq.getPendingRequestId())); } @Test public void insertErnpRequested() throws TaskExecutionException, UnsupportedEncodingException { executionContext.put(Constants.TRANSITION_TO_REQUESTING_NEW_ERNP_ENTRY_TASK, true); task.execute(pendingReq, executionContext); Assert.assertEquals("Wrong http StatusCode", 200, httpResp.getStatus()); Assert.assertEquals("Wrong http ContentType", "text/html;charset=UTF-8", httpResp.getContentType()); String html = httpResp.getContentAsString(); Assert.assertNotNull("html result is null", html); Assert.assertFalse("html result is empty", html.isEmpty()); Assert.assertTrue("No language selector with pendingRequestId", html.contains("/otherLoginMethod?lang=en&pendingid=" + pendingReq.getPendingRequestId())); Assert.assertTrue("Missing eIDAS infos", html.contains(MessageFormat.format(TEST_PATTER_REQ_PARAM, SelectedLoginMethod.ADD_ME_AS_NEW))); } @Test public void advancedMatchingFailedMsg() throws TaskExecutionException, UnsupportedEncodingException { executionContext.put(Constants.CONTEXT_FLAG_ADVANCED_MATCHING_FAILED, true); config.putConfigValue("auth.eIDAS.matching.byaddress.enable", "true"); task.execute(pendingReq, executionContext); String html = doBasicValidation(true); Assert.assertFalse("Missing eIDAS infos", html.contains(MessageFormat.format(TEST_PATTER_REQ_PARAM, SelectedLoginMethod.ADD_ME_AS_NEW))); Assert.assertTrue("missing errorfield", html.contains("
")); return html; } }