From 1da7b554c5933349ddc4a936a83a42a9589d5227 Mon Sep 17 00:00:00 2001 From: Daniel Garnier-Moiroux Date: Fri, 20 Dec 2024 18:21:27 +0100 Subject: [PATCH] tests: address flakyness in ScimUserEndpointsAliasMockMvcTests --- .../ScimUserEndpointsAliasMockMvcTests.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/uaa/src/test/java/org/cloudfoundry/identity/uaa/scim/endpoints/ScimUserEndpointsAliasMockMvcTests.java b/uaa/src/test/java/org/cloudfoundry/identity/uaa/scim/endpoints/ScimUserEndpointsAliasMockMvcTests.java index d9585f5b2c4..7b4890a0f29 100644 --- a/uaa/src/test/java/org/cloudfoundry/identity/uaa/scim/endpoints/ScimUserEndpointsAliasMockMvcTests.java +++ b/uaa/src/test/java/org/cloudfoundry/identity/uaa/scim/endpoints/ScimUserEndpointsAliasMockMvcTests.java @@ -14,6 +14,7 @@ import org.cloudfoundry.identity.uaa.scim.ScimUserAliasHandler; import org.cloudfoundry.identity.uaa.scim.jdbc.JdbcScimUserProvisioning; import org.cloudfoundry.identity.uaa.scim.services.ScimUserService; +import org.cloudfoundry.identity.uaa.util.AlphanumericRandomValueStringGenerator; import org.cloudfoundry.identity.uaa.util.JsonUtils; import org.cloudfoundry.identity.uaa.util.UaaStringUtils; import org.cloudfoundry.identity.uaa.zone.IdentityZone; @@ -26,8 +27,10 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.web.servlet.MvcResult; @@ -52,15 +55,22 @@ @DefaultTestContext public class ScimUserEndpointsAliasMockMvcTests extends AliasMockMvcTestBase { + private static final String TEST_USERNAME = new AlphanumericRandomValueStringGenerator(12).generate().toLowerCase(); + private IdentityProviderAliasHandler idpEntityAliasHandler; private IdentityProviderEndpoints identityProviderEndpoints; private ScimUserAliasHandler scimUserAliasHandler; private ScimUserEndpoints scimUserEndpoints; private ScimUserService scimUserService; + @Autowired + JdbcTemplate jdbcTemplate; @BeforeEach void setUp() throws Exception { setUpTokensAndCustomZone(); + // There is a race conditions where multiple users with the same name are created between tests + // We ensure that the tests are independent by deleting the test user before every test. + jdbcTemplate.execute("DELETE FROM users WHERE username = '" + TEST_USERNAME + "'"); idpEntityAliasHandler = requireNonNull(webApplicationContext.getBean(IdentityProviderAliasHandler.class)); identityProviderEndpoints = requireNonNull(webApplicationContext.getBean(IdentityProviderEndpoints.class)); @@ -242,7 +252,6 @@ private void shouldAccept_ShouldCreateAliasUser( final ScimUser createdScimUser = createScimUser(zone1, scimUser); // find alias user - //TODO this is flaky final List usersZone2 = readRecentlyCreatedUsersInZone(zone2); final Optional aliasUserOpt = usersZone2.stream() .filter(user -> user.getId().equals(createdScimUser.getAliasId())) @@ -1600,7 +1609,7 @@ private static ScimUser buildScimUser( scimUser.setAliasZid(aliasZid); scimUser.setZoneId(zoneId); - scimUser.setUserName("john.doe"); + scimUser.setUserName(TEST_USERNAME); scimUser.setName(new ScimUser.Name("John", "Doe")); scimUser.setPrimaryEmail("john.doe@example.com"); scimUser.setPassword("some-password");