Skip to content

Commit

Permalink
Fix ExpiredAccountsHandler checktime and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
enricovianello committed Sep 26, 2024
1 parent aa87cb4 commit 32e8578
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -190,32 +186,23 @@ 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"));

while (true) {
Page<IamAccount> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();

Expand All @@ -135,18 +136,20 @@ 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()));

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 32e8578

Please sign in to comment.