Skip to content

Commit

Permalink
Refactor injection of 'deactivateOnDelete'
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhoelzl-sap committed Sep 4, 2024
1 parent f4221a3 commit fa614f8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Map;
import java.util.UUID;

import com.google.common.annotations.VisibleForTesting;
import org.cloudfoundry.identity.uaa.audit.event.SystemDeletable;
import org.cloudfoundry.identity.uaa.constants.OriginKeys;
import org.cloudfoundry.identity.uaa.resources.ResourceMonitor;
Expand Down Expand Up @@ -129,7 +128,7 @@ public Logger getLogger() {

private final PasswordEncoder passwordEncoder;

private boolean deactivateOnDelete;
private final boolean deactivateOnDelete;

private static final RowMapper<ScimUser> mapper = new ScimUserRowMapper();

Expand Down Expand Up @@ -504,11 +503,6 @@ protected int deleteUser(String userId, int version, String zoneId) {
return updated;
}

@VisibleForTesting
public void setDeactivateOnDelete(boolean deactivateOnDelete) {
this.deactivateOnDelete = deactivateOnDelete;
}

@Override
public int deleteByIdentityZone(String zoneId) {
jdbcTemplate.update(HARD_DELETE_OF_GROUP_MEMBERS_BY_ZONE, zoneId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ class JdbcScimUserProvisioningTests {
private String joeEmail;
private final String JOE_NAME = "joe";

private SimpleSearchQueryConverter joinConverter;
private SimpleSearchQueryConverter filterConverter;

@BeforeEach
void setUp(@Autowired LimitSqlAdapter limitSqlAdapter) {
generator = new RandomValueStringGenerator();
Expand All @@ -124,17 +127,27 @@ void setUp(@Autowired LimitSqlAdapter limitSqlAdapter) {
idzManager = new IdentityZoneManagerImpl();
idzManager.setCurrentIdentityZone(idz);

SimpleSearchQueryConverter joinConverter = new SimpleSearchQueryConverter();
joinConverter = new SimpleSearchQueryConverter();
joinConverter.setAttributeNameMapper(new JoinAttributeNameMapper("u"));
jdbcScimUserProvisioning = new JdbcScimUserProvisioning(namedJdbcTemplate, pagingListFactory, passwordEncoder, idzManager, jdbcIdentityZoneProvisioning, new SimpleSearchQueryConverter(), joinConverter, new TimeServiceImpl(), true);

SimpleSearchQueryConverter filterConverter = new SimpleSearchQueryConverter();
filterConverter = new SimpleSearchQueryConverter();
Map<String, String> replaceWith = new HashMap<>();
replaceWith.put("emails\\.value", "email");
replaceWith.put("groups\\.display", "authorities");
replaceWith.put("phoneNumbers\\.value", "phoneNumber");
filterConverter.setAttributeNameMapper(new SimpleAttributeNameMapper(replaceWith));
jdbcScimUserProvisioning.setQueryConverter(filterConverter);

jdbcScimUserProvisioning = new JdbcScimUserProvisioning(
namedJdbcTemplate,
pagingListFactory,
passwordEncoder,
idzManager,
jdbcIdentityZoneProvisioning,
filterConverter,
joinConverter,
new TimeServiceImpl(),
true
);

addUser(jdbcTemplate, joeId,
JOE_NAME, passwordEncoder.encode("joespassword"), joeEmail, "Joe", "User", "+1-222-1234567", currentIdentityZoneId);
Expand Down Expand Up @@ -990,38 +1003,68 @@ void deactivateWithWrongVersionIsError() {
() -> jdbcScimUserProvisioning.delete(joeId, 1, currentIdentityZoneId));
}

@Test
void canDeleteExistingUserThroughEvent() {
String tmpUserId = createUserForDelete(jdbcTemplate, currentIdentityZoneId);
ScimUser user = jdbcScimUserProvisioning.retrieve(tmpUserId, currentIdentityZoneId);
jdbcScimUserProvisioning.setDeactivateOnDelete(false);
jdbcScimUserProvisioning.onApplicationEvent(new EntityDeletedEvent<Object>(user, mock(Authentication.class), currentIdentityZoneId));
assertEquals(0, jdbcTemplate.queryForList("select * from users where id=?", tmpUserId).size());
assertEquals(0, jdbcScimUserProvisioning.query("username eq \"" + tmpUserId + "\"", currentIdentityZoneId).size());
}
@Nested
class DeactivateOnDeleteDisabled {
@BeforeEach
void setUp() {
jdbcScimUserProvisioning = new JdbcScimUserProvisioning(
namedJdbcTemplate,
pagingListFactory,
passwordEncoder,
idzManager,
jdbcIdentityZoneProvisioning,
filterConverter,
joinConverter,
new TimeServiceImpl(),
false
);
}

@Test
void canDeleteExistingUser() {
String tmpUserId = createUserForDelete(jdbcTemplate, currentIdentityZoneId);
jdbcScimUserProvisioning.setDeactivateOnDelete(false);
jdbcScimUserProvisioning.delete(tmpUserId, 0, currentIdentityZoneId);
assertEquals(0, jdbcTemplate.queryForList("select * from users where id=?", tmpUserId).size());
assertEquals(0, jdbcScimUserProvisioning.query("username eq \"" + tmpUserId + "\"", currentIdentityZoneId).size());
}
@Test
void canDeleteExistingUserThroughEvent() {
String tmpUserId = createUserForDelete(jdbcTemplate, currentIdentityZoneId);
ScimUser user = jdbcScimUserProvisioning.retrieve(tmpUserId, currentIdentityZoneId);
jdbcScimUserProvisioning.onApplicationEvent(
new EntityDeletedEvent<Object>(user, mock(Authentication.class), currentIdentityZoneId));
assertEquals(0, jdbcTemplate.queryForList("select * from users where id=?", tmpUserId).size());
assertEquals(0,
jdbcScimUserProvisioning.query("username eq \"" + tmpUserId + "\"", currentIdentityZoneId).size());
}

@Test
void canDeleteExistingUserAndThenCreateHimAgain() {
String tmpUserId = createUserForDelete(jdbcTemplate, currentIdentityZoneId);
jdbcScimUserProvisioning.setDeactivateOnDelete(false);
ScimUser deletedUser = jdbcScimUserProvisioning.delete(tmpUserId, 0, currentIdentityZoneId);
assertEquals(0, jdbcTemplate.queryForList("select * from users where id=?", tmpUserId).size());
@Test
void canDeleteExistingUser() {
String tmpUserId = createUserForDelete(jdbcTemplate, currentIdentityZoneId);
jdbcScimUserProvisioning.delete(tmpUserId, 0, currentIdentityZoneId);
assertEquals(0, jdbcTemplate.queryForList("select * from users where id=?", tmpUserId).size());
assertEquals(0,
jdbcScimUserProvisioning.query("username eq \"" + tmpUserId + "\"", currentIdentityZoneId).size());
}

deletedUser.setActive(true);
ScimUser user = jdbcScimUserProvisioning.createUser(deletedUser, "foobarspam1234", currentIdentityZoneId);
assertNotNull(user);
assertNotNull(user.getId());
assertNotSame(tmpUserId, user.getId());
assertEquals(1, jdbcScimUserProvisioning.query("username eq \"" + tmpUserId + "\"", currentIdentityZoneId).size());
@Test
void canDeleteExistingUserAndThenCreateHimAgain() {
String tmpUserId = createUserForDelete(jdbcTemplate, currentIdentityZoneId);
ScimUser deletedUser = jdbcScimUserProvisioning.delete(tmpUserId, 0, currentIdentityZoneId);
assertEquals(0, jdbcTemplate.queryForList("select * from users where id=?", tmpUserId).size());

deletedUser.setActive(true);
ScimUser user = jdbcScimUserProvisioning.createUser(deletedUser, "foobarspam1234", currentIdentityZoneId);
assertNotNull(user);
assertNotNull(user.getId());
assertNotSame(tmpUserId, user.getId());
assertEquals(1,
jdbcScimUserProvisioning.query("username eq \"" + tmpUserId + "\"", currentIdentityZoneId).size());
}

@Test
void cannotDeleteNonexistentUser() {
assertThrows(ScimResourceNotFoundException.class,
() -> jdbcScimUserProvisioning.delete("9999", 0, currentIdentityZoneId));
}

@Test
void deleteWithWrongVersionIsError() {
assertThrows(OptimisticLockingFailureException.class, () -> jdbcScimUserProvisioning.delete(joeId, 1, currentIdentityZoneId));
}
}

@Test
Expand Down Expand Up @@ -1180,19 +1223,6 @@ void updatedIncorrectVersionUserVerified() {
assertThrows(OptimisticLockingFailureException.class, () -> jdbcScimUserProvisioning.verifyUser(tmpUserIdString, user.getVersion() + 50, currentIdentityZoneId));
}

@Test
void cannotDeleteNonexistentUser() {
jdbcScimUserProvisioning.setDeactivateOnDelete(false);
assertThrows(ScimResourceNotFoundException.class,
() -> jdbcScimUserProvisioning.delete("9999", 0, currentIdentityZoneId));
}

@Test
void deleteWithWrongVersionIsError() {
jdbcScimUserProvisioning.setDeactivateOnDelete(false);
assertThrows(OptimisticLockingFailureException.class, () -> jdbcScimUserProvisioning.delete(joeId, 1, currentIdentityZoneId));
}

@Test
void canRetrieveUsers() {
assertTrue(2 <= jdbcScimUserProvisioning.retrieveAll(currentIdentityZoneId).size());
Expand Down

0 comments on commit fa614f8

Please sign in to comment.