From 32e8578b90808c952bcbc9140177db43461e082c Mon Sep 17 00:00:00 2001 From: Enrico Vianello Date: Thu, 26 Sep 2024 23:04:23 +0200 Subject: [PATCH] Fix ExpiredAccountsHandler checktime and tests --- .../lifecycle/ExpiredAccountsHandler.java | 17 ++------------- .../test/lifecycle/AccountLifecycleTests.java | 21 +++++++++++-------- .../lifecycle/cern/LifecycleTestSupport.java | 5 +++-- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/iam-login-service/src/main/java/it/infn/mw/iam/core/lifecycle/ExpiredAccountsHandler.java b/iam-login-service/src/main/java/it/infn/mw/iam/core/lifecycle/ExpiredAccountsHandler.java index 35deed2a2..152216835 100644 --- a/iam-login-service/src/main/java/it/infn/mw/iam/core/lifecycle/ExpiredAccountsHandler.java +++ b/iam-login-service/src/main/java/it/infn/mw/iam/core/lifecycle/ExpiredAccountsHandler.java @@ -22,12 +22,8 @@ import java.time.Clock; import java.time.Instant; -import java.time.ZoneId; -import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; -import java.util.Calendar; import java.util.Date; -import java.util.GregorianCalendar; import java.util.Optional; import java.util.Set; @@ -190,22 +186,13 @@ private void handleExpiredAccount(IamAccount expiredAccount) { } } - private Instant getLastMidnight() { - ZonedDateTime zdt = ZonedDateTime.ofInstant(clock.instant(), ZoneId.systemDefault()); - Calendar c = GregorianCalendar.from(zdt); - c.set(Calendar.HOUR_OF_DAY, 0); - c.set(Calendar.MINUTE, 0); - c.set(Calendar.SECOND, 0); - return c.toInstant(); - } - public void handleExpiredAccounts() { LOG.info("Expired accounts handler ... [START]"); accountsScheduledForRemoval.clear(); - checkTime = getLastMidnight(); + checkTime = clock.instant().truncatedTo(ChronoUnit.DAYS); LOG.debug("Comparing end-time with {}", checkTime); Pageable pageRequest = PageRequest.of(0, PAGE_SIZE, Sort.by(Direction.ASC, "endTime")); @@ -213,9 +200,9 @@ public void handleExpiredAccounts() { while (true) { Page expiredAccountsPage = accountRepo.findExpiredAccountsAtTimestamp(Date.from(checkTime), pageRequest); - LOG.debug("expiredAccountsPage: {}", expiredAccountsPage); if (expiredAccountsPage.hasContent()) { + LOG.debug("expiredAccountsPage [{}/{}]", expiredAccountsPage.getNumber()+1, expiredAccountsPage.getNumberOfElements()); for (IamAccount expiredAccount : expiredAccountsPage.getContent()) { handleExpiredAccount(expiredAccount); diff --git a/iam-login-service/src/test/java/it/infn/mw/iam/test/lifecycle/AccountLifecycleTests.java b/iam-login-service/src/test/java/it/infn/mw/iam/test/lifecycle/AccountLifecycleTests.java index 5397c05e9..bd00353a1 100644 --- a/iam-login-service/src/test/java/it/infn/mw/iam/test/lifecycle/AccountLifecycleTests.java +++ b/iam-login-service/src/test/java/it/infn/mw/iam/test/lifecycle/AccountLifecycleTests.java @@ -109,9 +109,9 @@ public void deleteAccount() { } @Test - public void testUserIsNotPendingSuspensionWhenEndTimeIsToday() { + public void testUserSuspensionAtLastMidnight() { - accountService.setAccountEndTime(testAccount, Date.from(ONE_MINUTE_AGO)); + accountService.setAccountEndTime(testAccount, Date.from(LAST_MIDNIGHT)); handler.handleExpiredAccounts(); testAccount = accountService.findByUuid(USER_UUID) @@ -125,8 +125,9 @@ public void testUserIsNotPendingSuspensionWhenEndTimeIsToday() { @Test public void testSuspensionGracePeriodWorks() { - accountService.setAccountEndTime(testAccount, Date.from(FOUR_DAYS_AGO)); - Date lastUpdateTime = testAccount.getLastUpdateTime(); + accountService.setAccountEndTime(testAccount, Date.from(DAY_BEFORE)); + testAccount = accountService.findByUuid(USER_UUID) + .orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND)); handler.handleExpiredAccounts(); @@ -135,8 +136,6 @@ public void testSuspensionGracePeriodWorks() { statusLabel = testAccount.getLabelByName(LIFECYCLE_STATUS_LABEL); assertThat(testAccount.isActive(), is(true)); - assertThat(testAccount.getLastUpdateTime().compareTo(lastUpdateTime) > 0, is(true)); - lastUpdateTime = testAccount.getLastUpdateTime(); assertThat(statusLabel.isPresent(), is(true)); assertThat(statusLabel.get().getValue(), is(ExpiredAccountsHandler.AccountLifecycleStatus.PENDING_SUSPENSION.name())); @@ -144,9 +143,13 @@ public void testSuspensionGracePeriodWorks() { handler.handleExpiredAccounts(); testAccount = accountService.findByUuid(USER_UUID) - .orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND)); - assertThat(testAccount.isActive(), is(true)); - assertThat(testAccount.getLastUpdateTime().compareTo(lastUpdateTime) == 0, is(true)); + .orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND)); + statusLabel = testAccount.getLabelByName(LIFECYCLE_STATUS_LABEL); + + assertThat(testAccount.isActive(), is(true)); + assertThat(statusLabel.isPresent(), is(true)); + assertThat(statusLabel.get().getValue(), + is(ExpiredAccountsHandler.AccountLifecycleStatus.PENDING_SUSPENSION.name())); } @Test diff --git a/iam-login-service/src/test/java/it/infn/mw/iam/test/lifecycle/cern/LifecycleTestSupport.java b/iam-login-service/src/test/java/it/infn/mw/iam/test/lifecycle/cern/LifecycleTestSupport.java index 30e5ed926..3acb61bd4 100644 --- a/iam-login-service/src/test/java/it/infn/mw/iam/test/lifecycle/cern/LifecycleTestSupport.java +++ b/iam-login-service/src/test/java/it/infn/mw/iam/test/lifecycle/cern/LifecycleTestSupport.java @@ -41,9 +41,10 @@ public interface LifecycleTestSupport { String CERN_SSO_ISSUER = "https://auth.cern.ch/auth/realms/cern"; String CERN_PERSON_ID = "12345678"; - Instant NOW = Instant.parse("2020-01-01T12:00:00.00Z"); + Instant LAST_MIDNIGHT = Instant.now().truncatedTo(ChronoUnit.DAYS); + Instant NOW = LAST_MIDNIGHT.plus(12, ChronoUnit.HOURS); + Instant DAY_BEFORE = LAST_MIDNIGHT.minus(1, ChronoUnit.SECONDS); - Instant DAY_BEFORE = NOW.minus(1, ChronoUnit.DAYS); Instant ONE_MINUTE_AGO = NOW.minus(1, ChronoUnit.MINUTES); Instant FOUR_DAYS_AGO = NOW.minus(4, ChronoUnit.DAYS); Instant EIGHT_DAYS_AGO = NOW.minus(8, ChronoUnit.DAYS);