diff --git a/src/main/java/uk/gov/pay/adminusers/expungeandarchive/resource/ExpungeAndArchiveHistoricalDataResource.java b/src/main/java/uk/gov/pay/adminusers/expungeandarchive/resource/ExpungeAndArchiveHistoricalDataResource.java index bea604a40..7e63663b7 100644 --- a/src/main/java/uk/gov/pay/adminusers/expungeandarchive/resource/ExpungeAndArchiveHistoricalDataResource.java +++ b/src/main/java/uk/gov/pay/adminusers/expungeandarchive/resource/ExpungeAndArchiveHistoricalDataResource.java @@ -27,7 +27,6 @@ public class ExpungeAndArchiveHistoricalDataResource { public ExpungeAndArchiveHistoricalDataResource(ExpungeAndArchiveHistoricalDataService expungeAndArchiveHistoricalDataService) { this.expungeAndArchiveHistoricalDataService = expungeAndArchiveHistoricalDataService; } - @POST @Produces(APPLICATION_JSON) @Operation( diff --git a/src/main/java/uk/gov/pay/adminusers/expungeandarchive/service/ExpungeAndArchiveHistoricalDataService.java b/src/main/java/uk/gov/pay/adminusers/expungeandarchive/service/ExpungeAndArchiveHistoricalDataService.java index 58e0893dc..d37fefed5 100644 --- a/src/main/java/uk/gov/pay/adminusers/expungeandarchive/service/ExpungeAndArchiveHistoricalDataService.java +++ b/src/main/java/uk/gov/pay/adminusers/expungeandarchive/service/ExpungeAndArchiveHistoricalDataService.java @@ -71,9 +71,9 @@ public void expungeAndArchiveHistoricalData() { if (expungeAndArchiveDataConfig.isExpungeAndArchiveHistoricalDataEnabled()) { ZonedDateTime deleteUsersAndRelatedDataBeforeDate = getDeleteUsersAndRelatedDataBeforeDate(); - int noOfUsersDeleted = userDao.deleteUsersNotAssociatedWithAnyService(deleteUsersAndRelatedDataBeforeDate.toInstant()); int noOfInvitesDeleted = inviteDao.deleteInvites(deleteUsersAndRelatedDataBeforeDate); int noOfForgottenPasswordsDeleted = forgottenPasswordDao.deleteForgottenPasswords(deleteUsersAndRelatedDataBeforeDate); + int noOfUsersDeleted = userDao.deleteUsersNotAssociatedWithAnyService(deleteUsersAndRelatedDataBeforeDate.toInstant()); int noOfServicesArchived = archiveServices(); diff --git a/src/test/java/uk/gov/pay/adminusers/expungeandarchive/resource/ExpungeAndArchiveHistoricalDataResourceIT.java b/src/test/java/uk/gov/pay/adminusers/expungeandarchive/resource/ExpungeAndArchiveHistoricalDataResourceIT.java index 38451595b..c8eb60b2b 100644 --- a/src/test/java/uk/gov/pay/adminusers/expungeandarchive/resource/ExpungeAndArchiveHistoricalDataResourceIT.java +++ b/src/test/java/uk/gov/pay/adminusers/expungeandarchive/resource/ExpungeAndArchiveHistoricalDataResourceIT.java @@ -81,12 +81,17 @@ void shouldDeleteHistoricalUsers() { @Test void shouldDeleteHistoricalInvitesAndForgottenPasswords() { - User user = userDbFixture(databaseHelper).insertUser(); + User userToBeDeleted = userDbFixture(databaseHelper) + .withCreatedAt(now.minusYears(5)) + .insertUser(); + User userThatWontBeDeleted = userDbFixture(databaseHelper) + .withCreatedAt(now) + .insertUser(); - ForgottenPasswordDbFixture forgottenPasswordDbFixture = insertForgottenPassword(user.getId(), now.minusYears(5)); - ForgottenPasswordDbFixture forgottenPasswordThatShouldNotBeDeleted = insertForgottenPassword(user.getId(), now); - String code = insertInvite(now.minusYears(5)); - String codeThatShouldNotBeDeleted = insertInvite(now); + ForgottenPasswordDbFixture forgottenPasswordDbFixture = insertForgottenPassword(userToBeDeleted.getId(), now.minusYears(5)); + ForgottenPasswordDbFixture forgottenPasswordThatShouldNotBeDeleted = insertForgottenPassword(userThatWontBeDeleted.getId(), now); + String code = insertInvite(userToBeDeleted.getId(), now.minusYears(5)); + String codeThatShouldNotBeDeleted = insertInvite(userThatWontBeDeleted.getId(), now); assertForgottenPasswordsExist(forgottenPasswordDbFixture.getId(), forgottenPasswordThatShouldNotBeDeleted.getId()); assertInvitesExist(code, codeThatShouldNotBeDeleted); @@ -106,6 +111,9 @@ void shouldDeleteHistoricalInvitesAndForgottenPasswords() { Optional> inviteByCode = databaseHelper.findInviteByCode(code); assertTrue(inviteByCode.isEmpty()); + + assertUserNotExists(userToBeDeleted.getId()); + assertUserExists(userThatWontBeDeleted.getId()); } @Test @@ -221,9 +229,10 @@ private void assertInvitesExist(String... codes) { }); } - private String insertInvite(ZonedDateTime date) { + private String insertInvite(Integer userId, ZonedDateTime date) { return inviteDbFixture(databaseHelper) .withDate(date) + .withSenderId(userId) .insertInviteToAddUserToService(); } diff --git a/src/test/java/uk/gov/pay/adminusers/fixtures/InviteDbFixture.java b/src/test/java/uk/gov/pay/adminusers/fixtures/InviteDbFixture.java index 81f55e852..005af6f45 100644 --- a/src/test/java/uk/gov/pay/adminusers/fixtures/InviteDbFixture.java +++ b/src/test/java/uk/gov/pay/adminusers/fixtures/InviteDbFixture.java @@ -26,6 +26,7 @@ public class InviteDbFixture { private Integer loginCounter = 0; private String externalServiceId = randomUuid(); private Integer serviceId = randomInt(); + private Integer senderId; private InviteDbFixture(DatabaseTestHelper databaseTestHelper) { this.databaseTestHelper = databaseTestHelper; @@ -40,13 +41,13 @@ public String insertInviteToAddUserToService() { int roleId = RoleDbFixture.roleDbFixture(databaseTestHelper).insertRole().getId(); String userUsername = randomUuid(); String userEmail = userUsername + "@example.com"; - int invitingUserId = + senderId = UserDbFixture.userDbFixture(databaseTestHelper) .withEmail(userEmail) .insertUser() .getId(); databaseTestHelper.addInvite( - nextInt(), invitingUserId, serviceId, roleId, + nextInt(), senderId, serviceId, roleId, email, code, otpKey, date, expiryDate, telephoneNumber, password, disabled, loginCounter ); @@ -124,4 +125,9 @@ public InviteDbFixture withDate(ZonedDateTime date) { this.date = date; return this; } + + public InviteDbFixture withSenderId(Integer senderId) { + this.senderId = senderId; + return this; + } }