From bbfeddb6e5da0a6f208f06687c54329d3fcdbf22 Mon Sep 17 00:00:00 2001 From: rmiccoli Date: Thu, 5 Oct 2023 14:10:54 +0200 Subject: [PATCH] Add junit test for CERN lifecycle handler --- .../cern/CernAccountLifecycleTests.java | 145 ++++++++++++++---- 1 file changed, 113 insertions(+), 32 deletions(-) diff --git a/iam-login-service/src/test/java/it/infn/mw/iam/test/lifecycle/cern/CernAccountLifecycleTests.java b/iam-login-service/src/test/java/it/infn/mw/iam/test/lifecycle/cern/CernAccountLifecycleTests.java index 33af02cc7..24062b7ce 100644 --- a/iam-login-service/src/test/java/it/infn/mw/iam/test/lifecycle/cern/CernAccountLifecycleTests.java +++ b/iam-login-service/src/test/java/it/infn/mw/iam/test/lifecycle/cern/CernAccountLifecycleTests.java @@ -37,6 +37,7 @@ import java.util.UUID; import org.junit.After; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -71,7 +72,7 @@ @SpringBootTest(classes = {IamLoginService.class, CoreControllerTestSupport.class, CernAccountLifecycleTests.TestConfig.class}) @TestPropertySource(properties = { - // @formatter:off +// @formatter:off "lifecycle.account.expiredAccountPolicy.suspensionGracePeriodDays=0", "lifecycle.account.expiredAccountPolicy.removalGracePeriodDays=30", "cern.task.pageSize=5" @@ -109,7 +110,7 @@ CernHrDBApiService hrDb() { @Autowired Clock clock; - + @After public void teardown() { reset(hrDb); @@ -155,7 +156,7 @@ public void testSuspendLifecycleWorks() { @Test public void testNoActionLifecycleWorksForValidAccounts() { VOPersonDTO voPerson = voPerson("988211"); - + when(hrDb.hasValidExperimentParticipation(anyString())).thenReturn(true); when(hrDb.getHrDbPersonRecord(anyString())).thenReturn(voPerson); @@ -175,7 +176,7 @@ public void testNoActionLifecycleWorksForValidAccounts() { assertThat(testAccount.getUserInfo().getGivenName(), is(voPerson.getFirstName())); assertThat(testAccount.getUserInfo().getFamilyName(), is(voPerson.getName())); assertThat(testAccount.getUserInfo().getEmail(), is(voPerson.getEmail())); - + assertThat(testAccount.isActive(), is(true)); Optional statusLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS); @@ -193,7 +194,7 @@ public void testNoActionLifecycleWorksForValidAccounts() { assertThat(timestampLabel.isPresent(), is(true)); assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli()))); } - + @Test public void testNoActionLifecycleWorksForInValidAccounts() { @@ -206,7 +207,7 @@ public void testNoActionLifecycleWorksForInValidAccounts() { testAccount.setActive(false); service.setLabel(testAccount, cernPersonIdLabel()); - + handler.run(); testAccount = @@ -230,6 +231,87 @@ public void testNoActionLifecycleWorksForInValidAccounts() { assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli()))); } + @Ignore + @Test + public void testLifecycleStates() { + + when(hrDb.hasValidExperimentParticipation(anyString())).thenReturn(false); + + IamAccount testAccount = + repo.findByUuid(TEST_USER_UUID).orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND)); + + testAccount.setActive(true); + service.setLabel(testAccount, cernPersonIdLabel()); + + handler.run(); + + testAccount = + repo.findByUuid(TEST_USER_UUID).orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND)); + + assertThat(testAccount.isActive(), is(false)); + Optional statusLabel = + testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS); + Optional actionLabel = + testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_ACTION); + Optional timestampLabel = + testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_TIMESTAMP); + + assertThat(statusLabel.isPresent(), is(true)); + assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.OK.name())); + + assertThat(actionLabel.isPresent(), is(true)); + assertThat(actionLabel.get().getValue(), + is(CernHrLifecycleHandler.Action.DISABLE_ACCOUNT.name())); + + assertThat(timestampLabel.isPresent(), is(true)); + assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli()))); + + handler.run(); + + statusLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS); + actionLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_ACTION); + + assertThat(statusLabel.isPresent(), is(true)); + assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.OK.name())); + + assertThat(actionLabel.isPresent(), is(true)); + assertThat(actionLabel.get().getValue(), + is(CernHrLifecycleHandler.Action.DISABLE_ACCOUNT.name())); + + when(hrDb.hasValidExperimentParticipation(anyString())).thenReturn(true); + when(hrDb.getHrDbPersonRecord(anyString())).thenReturn(voPerson("988211")); + + assertThat(testAccount.isActive(), is(false)); + + handler.run(); + + statusLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS); + actionLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_ACTION); + + assertThat(testAccount.isActive(), is(true)); + + assertThat(statusLabel.isPresent(), is(true)); + assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.OK.name())); + + assertThat(actionLabel.isPresent(), is(true)); + assertThat(actionLabel.get().getValue(), + is(CernHrLifecycleHandler.Action.RESTORE_ACCOUNT.name())); + + handler.run(); + + assertThat(testAccount.isActive(), is(true)); + + statusLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS); + actionLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_ACTION); + + assertThat(statusLabel.isPresent(), is(true)); + assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.OK.name())); + + assertThat(actionLabel.isPresent(), is(true)); + assertThat(actionLabel.get().getValue(), is(CernHrLifecycleHandler.Action.NO_ACTION.name())); + + } + @Test public void testRestoreLifecycleWorks() { @@ -268,20 +350,21 @@ public void testRestoreLifecycleWorks() { assertThat(timestampLabel.isPresent(), is(true)); assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli()))); } - + @Test public void testApiErrorIsHandled() { - when(hrDb.hasValidExperimentParticipation(anyString())).thenThrow(new CernHrDbApiError("API is unreachable")); - + when(hrDb.hasValidExperimentParticipation(anyString())) + .thenThrow(new CernHrDbApiError("API is unreachable")); + IamAccount testAccount = repo.findByUuid(TEST_USER_UUID).orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND)); service.setLabel(testAccount, cernPersonIdLabel()); - + handler.run(); - + testAccount = repo.findByUuid(TEST_USER_UUID).orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND)); - + assertThat(testAccount.isActive(), is(true)); Optional statusLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS); @@ -293,18 +376,18 @@ public void testApiErrorIsHandled() { testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_MESSAGE); assertThat(actionLabel.isPresent(), is(false)); - + assertThat(statusLabel.isPresent(), is(true)); assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.ERROR.name())); - + assertThat(timestampLabel.isPresent(), is(true)); assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli()))); - + assertThat(messageLabel.isPresent(), is(true)); assertThat(messageLabel.get().getValue(), is(HR_DB_API_ERROR)); - + } - + @Test public void testRestoreLifecycleDoesNotTouchSuspendedAccount() { @@ -336,27 +419,26 @@ public void testRestoreLifecycleDoesNotTouchSuspendedAccount() { assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.OK.name())); assertThat(actionLabel.isPresent(), is(true)); - assertThat(actionLabel.get().getValue(), - is(CernHrLifecycleHandler.Action.NO_ACTION.name())); + assertThat(actionLabel.get().getValue(), is(CernHrLifecycleHandler.Action.NO_ACTION.name())); assertThat(timestampLabel.isPresent(), is(true)); assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli()))); } - + @Test public void testIgnoreAccount() { - + when(hrDb.hasValidExperimentParticipation(anyString())).thenReturn(false); IamAccount testAccount = repo.findByUuid(TEST_USER_UUID).orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND)); - + assertThat(testAccount.isActive(), is(true)); - + service.setLabel(testAccount, cernPersonIdLabel()); service.setLabel(testAccount, cernIgnoreLabel()); - + handler.run(); - + testAccount = repo.findByUuid(TEST_USER_UUID).orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND)); @@ -369,17 +451,16 @@ public void testIgnoreAccount() { testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_TIMESTAMP); Optional messageLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_MESSAGE); - + assertThat(statusLabel.isPresent(), is(true)); assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.OK.name())); assertThat(actionLabel.isPresent(), is(true)); - assertThat(actionLabel.get().getValue(), - is(CernHrLifecycleHandler.Action.NO_ACTION.name())); + assertThat(actionLabel.get().getValue(), is(CernHrLifecycleHandler.Action.NO_ACTION.name())); assertThat(timestampLabel.isPresent(), is(true)); assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli()))); - + assertThat(messageLabel.isPresent(), is(true)); assertThat(messageLabel.get().getValue(), is(IGNORE_MESSAGE)); } @@ -400,7 +481,7 @@ public void testPaginationWorks() { accountPage = repo.findAll(pageRequest); for (IamAccount account : accountPage.getContent()) { - + assertThat(account.isActive(), is(false)); Optional statusLabel = account.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS); @@ -418,8 +499,8 @@ public void testPaginationWorks() { assertThat(timestampLabel.isPresent(), is(true)); assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli()))); - - + + } }