diff options
author | Christian Kollmann <christian.kollmann@a-sit.at> | 2021-02-25 11:18:44 +0100 |
---|---|---|
committer | Christian Kollmann <christian.kollmann@a-sit.at> | 2021-02-25 13:27:15 +0100 |
commit | 8c13c21799beed76eea848504677eb935026397c (patch) | |
tree | 33903c15c4c55521e0d2d22f36890958d52fc074 /eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus | |
parent | 33b975f1bda7a2838d280d3f080b095d069d6d02 (diff) | |
download | National_eIDAS_Gateway-8c13c21799beed76eea848504677eb935026397c.tar.gz National_eIDAS_Gateway-8c13c21799beed76eea848504677eb935026397c.tar.bz2 National_eIDAS_Gateway-8c13c21799beed76eea848504677eb935026397c.zip |
Implement and test ReceiveAustrianResidenceGuiResponseTask
Diffstat (limited to 'eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus')
-rw-r--r-- | eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/tasks/ReceiveAustrianResidenceGuiResponseTaskTest.java | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/tasks/ReceiveAustrianResidenceGuiResponseTaskTest.java b/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/tasks/ReceiveAustrianResidenceGuiResponseTaskTest.java new file mode 100644 index 00000000..38693f49 --- /dev/null +++ b/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/tasks/ReceiveAustrianResidenceGuiResponseTaskTest.java @@ -0,0 +1,196 @@ +package at.asitplus.eidas.specific.modules.auth.eidas.v2.test.tasks; + +import at.asitplus.eidas.specific.connector.test.config.dummy.MsConnectorDummyConfigMap; +import at.asitplus.eidas.specific.modules.auth.eidas.v2.Constants; +import at.asitplus.eidas.specific.modules.auth.eidas.v2.dao.MergedRegisterSearchResult; +import at.asitplus.eidas.specific.modules.auth.eidas.v2.dao.RegisterResult; +import at.asitplus.eidas.specific.modules.auth.eidas.v2.dao.SimpleEidasData; +import at.asitplus.eidas.specific.modules.auth.eidas.v2.exception.ManualFixNecessaryException; +import at.asitplus.eidas.specific.modules.auth.eidas.v2.service.RegisterSearchService; +import at.asitplus.eidas.specific.modules.auth.eidas.v2.tasks.ReceiveAustrianResidenceGuiResponseTask; +import at.gv.egiz.eaaf.core.api.idp.process.ExecutionContext; +import at.gv.egiz.eaaf.core.exceptions.EaafStorageException; +import at.gv.egiz.eaaf.core.exceptions.TaskExecutionException; +import at.gv.egiz.eaaf.core.impl.idp.auth.data.AuthProcessDataWrapper; +import at.gv.egiz.eaaf.core.impl.idp.module.test.TestRequestImpl; +import at.gv.egiz.eaaf.core.impl.idp.process.ExecutionContextImpl; +import com.google.common.collect.Lists; +import org.apache.commons.lang3.RandomStringUtils; +import org.jetbrains.annotations.NotNull; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +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.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.util.Collections; +import java.util.List; + +import static at.asitplus.eidas.specific.modules.auth.eidas.v2.tasks.ReceiveAustrianResidenceGuiResponseTask.*; +import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.springframework.util.Assert.isInstanceOf; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { + "/SpringTest-context_tasks_test.xml", + "/SpringTest-context_basic_mapConfig.xml" +}) +public class ReceiveAustrianResidenceGuiResponseTaskTest { + + @Autowired + protected MsConnectorDummyConfigMap authConfig; + @Autowired + private ReceiveAustrianResidenceGuiResponseTask task; + @MockBean + private RegisterSearchService registerSearchService; + + private final ExecutionContext executionContext = new ExecutionContextImpl(); + private MockHttpServletRequest httpReq; + private MockHttpServletResponse httpResp; + private TestRequestImpl pendingReq; + + /** + * jUnit test set-up. + * + * @throws Exception In case of an set-up error + */ + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + 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 noRegisterResult() throws Exception { + UserInput userInput = setupUserInput(); + SimpleEidasData eidasData = setupEidasData(); + MergedRegisterSearchResult registerSearchResult = buildEmptyResult(); + mockRegisterSearch(userInput, registerSearchResult); + + task.execute(pendingReq, executionContext); + + assertEquals("Transition To S9", true, executionContext.get(Constants.TRANSITION_TO_CREATE_NEW_ERNP_ENTRY_TASK)); + } + + @Test + public void exactlyOneRegisterResult_Matching() throws Exception { + UserInput userInput = setupUserInput(); + SimpleEidasData eidasData = setupEidasData(); + MergedRegisterSearchResult registerSearchResult = buildResultWithOneMatch(buildMatchingRegisterResult(eidasData)); + mockRegisterSearch(userInput, registerSearchResult); + + task.execute(pendingReq, executionContext); + + assertNull("Transition To S9", executionContext.get(Constants.TRANSITION_TO_CREATE_NEW_ERNP_ENTRY_TASK)); + Mockito.verify(registerSearchService).step7aKittProcess(any(), eq(registerSearchResult), eq(eidasData), eq(pendingReq)); + } + + @Test + public void exactlyOneRegisterResult_NotMatching() throws Exception { + UserInput userInput = setupUserInput(); + SimpleEidasData eidasData = setupEidasData(); + MergedRegisterSearchResult registerSearchResult = buildResultWithOneMatch(buildNotMatchingRegisterResult(eidasData)); + mockRegisterSearch(userInput, registerSearchResult); + + task.execute(pendingReq, executionContext); + + assertEquals("Transition To S9", true, executionContext.get(Constants.TRANSITION_TO_CREATE_NEW_ERNP_ENTRY_TASK)); + } + + @Test + public void moreThanOneRegisterResult() throws Exception { + UserInput userInput = setupUserInput(); + SimpleEidasData eidasData = setupEidasData(); + MergedRegisterSearchResult registerSearchResult = buildResultWithTwoMatches(); + mockRegisterSearch(userInput, registerSearchResult); + + TaskExecutionException e = assertThrows(TaskExecutionException.class, + () -> task.execute(pendingReq, executionContext)); + + assertEquals(pendingReq.getPendingRequestId(), e.getPendingRequestID()); + isInstanceOf(ManualFixNecessaryException.class, e.getOriginalException()); + assertNull("Transition To S16", executionContext.get(Constants.TRANSITION_TO_CREATE_NEW_ERNP_ENTRY_TASK)); + } + + private void mockRegisterSearch(UserInput userInput, MergedRegisterSearchResult registerSearchResult) { + Mockito.when(registerSearchService.searchWithResidence(eq(userInput.getZipcode()), eq(userInput.getCity()), eq(userInput.getStreet()))).thenReturn(registerSearchResult); + } + + @NotNull + private MergedRegisterSearchResult buildEmptyResult() { + return new MergedRegisterSearchResult(Collections.emptyList(), Collections.emptyList()); + } + + @NotNull + private MergedRegisterSearchResult buildResultWithOneMatch(RegisterResult registerResult) { + return new MergedRegisterSearchResult(Collections.singletonList(registerResult), Collections.emptyList()); + } + + @NotNull + private MergedRegisterSearchResult buildResultWithTwoMatches() { + List<RegisterResult> results = Lists.newArrayList(buildRandomRegisterResult(), buildRandomRegisterResult()); + return new MergedRegisterSearchResult(results, Collections.emptyList()); + } + + @NotNull + private RegisterResult buildRandomRegisterResult() { + return new RegisterResult(RandomStringUtils.randomAlphabetic(8), RandomStringUtils.randomAlphabetic(8), RandomStringUtils.randomAlphabetic(8), RandomStringUtils.randomAlphabetic(8), RandomStringUtils.randomAlphabetic(8)); + } + + private RegisterResult buildMatchingRegisterResult(SimpleEidasData eidData) { + return new RegisterResult(RandomStringUtils.randomAlphabetic(8), eidData.getPseudonym(), eidData.getGivenName(), eidData.getFamilyName(), eidData.getDateOfBirth()); + } + + private RegisterResult buildNotMatchingRegisterResult(SimpleEidasData eidData) { + return new RegisterResult(RandomStringUtils.randomAlphabetic(8), eidData.getPseudonym() + RandomStringUtils.randomAlphabetic(8), eidData.getGivenName(), eidData.getFamilyName(), eidData.getDateOfBirth()); + } + + private void setHttpParameters(UserInput input) { + httpReq.setParameter(PARAM_FORMER_RESIDENCE_AVAILABLE, String.valueOf(input.isFormerResidenceAvailable())); + httpReq.setParameter(PARAM_STREET, input.getStreet()); + httpReq.setParameter(PARAM_CITY, input.getCity()); + httpReq.setParameter(PARAM_ZIPCODE, input.getZipcode()); + } + + @NotNull + private SimpleEidasData setupEidasData() throws EaafStorageException { + SimpleEidasData result = new SimpleEidasData(); + result.setPseudonym(RandomStringUtils.randomAlphabetic(8)); + result.setFamilyName(RandomStringUtils.randomAlphabetic(8)); + result.setGivenName(RandomStringUtils.randomAlphabetic(8)); + result.setDateOfBirth("1970-01-01"); + AuthProcessDataWrapper authProcessDataWrapper = pendingReq.getSessionData(AuthProcessDataWrapper.class); + authProcessDataWrapper.setGenericDataToSession(Constants.DATA_SIMPLE_EIDAS, result); + return result; + } + + @NotNull + private UserInput setupUserInput() { + UserInput result = new UserInput(true, RandomStringUtils.randomAlphabetic(8), RandomStringUtils.randomAlphabetic(8), RandomStringUtils.randomAlphabetic(8)); + setHttpParameters(result); + return result; + } + + +} |