Skip to content

Commit

Permalink
tests: address flakyness in ScimUserEndpointsAliasMockMvcTests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kehrlann committed Jan 2, 2025
1 parent 5e2e6d6 commit 4d4fb1a
Showing 1 changed file with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.cloudfoundry.identity.uaa.scim.endpoints;

import com.fasterxml.jackson.core.type.TypeReference;

import org.cloudfoundry.identity.uaa.DefaultTestContext;
import org.cloudfoundry.identity.uaa.alias.AliasMockMvcTestBase;
import org.cloudfoundry.identity.uaa.mock.util.MockMvcUtils;
Expand All @@ -14,6 +13,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;
Expand All @@ -26,8 +26,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;
Expand All @@ -52,14 +54,23 @@

@DefaultTestContext
public class ScimUserEndpointsAliasMockMvcTests extends AliasMockMvcTestBase {

// There is a race conditions where multiple users with the same name are created between tests
// We ensure that the tests are independent by having a custom username per test, that we clean
// up after every test.
private String TEST_USERNAME;

private IdentityProviderAliasHandler idpEntityAliasHandler;
private IdentityProviderEndpoints identityProviderEndpoints;
private ScimUserAliasHandler scimUserAliasHandler;
private ScimUserEndpoints scimUserEndpoints;
private ScimUserService scimUserService;
@Autowired
JdbcTemplate jdbcTemplate;

@BeforeEach
void setUp() throws Exception {
TEST_USERNAME = new AlphanumericRandomValueStringGenerator(12).generate().toLowerCase();
setUpTokensAndCustomZone();

idpEntityAliasHandler = requireNonNull(webApplicationContext.getBean(IdentityProviderAliasHandler.class));
Expand All @@ -69,6 +80,11 @@ void setUp() throws Exception {
scimUserService = requireNonNull(webApplicationContext.getBean(ScimUserService.class));
}

@AfterEach
void tearDown() {
jdbcTemplate.execute("DELETE FROM users WHERE username = '" + TEST_USERNAME + "'");
}

@Nested
class Read {
@Nested
Expand Down Expand Up @@ -242,7 +258,6 @@ private void shouldAccept_ShouldCreateAliasUser(
final ScimUser createdScimUser = createScimUser(zone1, scimUser);

// find alias user
//TODO this is flaky
final List<ScimUser> usersZone2 = readRecentlyCreatedUsersInZone(zone2);
final Optional<ScimUser> aliasUserOpt = usersZone2.stream()
.filter(user -> user.getId().equals(createdScimUser.getAliasId()))
Expand Down Expand Up @@ -1588,7 +1603,7 @@ private static void assertIsCorrectAliasPair(
assertThat(originalUser.getSchemas()).isEqualTo(aliasUser.getSchemas());
}

private static ScimUser buildScimUser(
private ScimUser buildScimUser(
final String origin,
final String zoneId,
final String aliasId,
Expand All @@ -1600,7 +1615,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");
Expand Down

0 comments on commit 4d4fb1a

Please sign in to comment.