Skip to content

Commit

Permalink
Improve status code check of IntegrationTestUtils.createGroupAndIgnor…
Browse files Browse the repository at this point in the history
…eStatusCode (now "createGroupAndIgnoreConflict"): now only allows 200 OK or 409 CONFLICT
  • Loading branch information
adrianhoelzl-sap committed Sep 4, 2024
1 parent ec2a5e6 commit bdec670
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void setup() {
String token = IntegrationTestUtils.getClientCredentialsToken(serverRunning.getBaseUrl(), "admin", "adminsecret");

ScimGroup group = new ScimGroup(null, "zones.testzone1.admin", null);
IntegrationTestUtils.createGroupAndIgnoreStatusCode(token, "", serverRunning.getBaseUrl(), group);
IntegrationTestUtils.createGroupAndIgnoreConflict(token, "", serverRunning.getBaseUrl(), group);
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,19 @@ public void setup() {
String token = IntegrationTestUtils.getClientCredentialsToken(baseUrl, "admin", "adminsecret");

ScimGroup group = new ScimGroup(null, "zones.uaa.admin", null);
IntegrationTestUtils.createGroupAndIgnoreStatusCode(token, "", baseUrl, group);
IntegrationTestUtils.createGroupAndIgnoreConflict(token, "", baseUrl, group);

group = new ScimGroup(null, "zones.testzone1.admin", null);
IntegrationTestUtils.createGroupAndIgnoreStatusCode(token, "", baseUrl, group);
IntegrationTestUtils.createGroupAndIgnoreConflict(token, "", baseUrl, group);

group = new ScimGroup(null, "zones.testzone2.admin", null);
IntegrationTestUtils.createGroupAndIgnoreStatusCode(token, "", baseUrl, group);
IntegrationTestUtils.createGroupAndIgnoreConflict(token, "", baseUrl, group);

group = new ScimGroup(null, "zones.testzone3.admin", null);
IntegrationTestUtils.createGroupAndIgnoreStatusCode(token, "", baseUrl, group);
IntegrationTestUtils.createGroupAndIgnoreConflict(token, "", baseUrl, group);

group = new ScimGroup(null, "zones.testzone4.admin", null);
IntegrationTestUtils.createGroupAndIgnoreStatusCode(token, "", baseUrl, group);
IntegrationTestUtils.createGroupAndIgnoreConflict(token, "", baseUrl, group);
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.joining;
import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_AUTHORIZATION_CODE;
import static org.cloudfoundry.identity.uaa.provider.ExternalIdentityProviderDefinition.USER_NAME_ATTRIBUTE_NAME;
import static org.cloudfoundry.identity.uaa.security.web.CookieBasedCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME;
Expand Down Expand Up @@ -557,12 +559,13 @@ public static ScimGroup getGroup(String token,
}
}

public static ScimGroup createGroupAndIgnoreStatusCode(String token,
String zoneId,
String url,
ScimGroup group) {
final ResponseEntity<ScimGroup> response = createGroupAndReturnResponse(token, zoneId, url, group);
return response.getBody();
public static ScimGroup createGroupAndIgnoreConflict(
final String token,
final String zoneId,
final String url,
final ScimGroup group
) {
return createGroup(token, zoneId, url, group, HttpStatus.CREATED, HttpStatus.CONFLICT);
}

public static ScimGroup createGroup(
Expand All @@ -571,18 +574,15 @@ public static ScimGroup createGroup(
final String url,
final ScimGroup group
) {
final ResponseEntity<ScimGroup> response = createGroupAndReturnResponse(token, zoneId, url, group);
assertStatusCode(response, HttpStatus.CREATED);
final ScimGroup responseBody = response.getBody();
assertNotNull(responseBody);
return responseBody;
return createGroup(token, zoneId, url, group, HttpStatus.CREATED);
}

private static ResponseEntity<ScimGroup> createGroupAndReturnResponse(
private static ScimGroup createGroup(
final String token,
final String zoneId,
final String url,
final ScimGroup group
final ScimGroup group,
final HttpStatus ...expectedStatusCodes
) {
final RestTemplate template = new RestTemplate();
template.setErrorHandler(fiveHundredErrorHandler);
Expand All @@ -593,12 +593,16 @@ private static ResponseEntity<ScimGroup> createGroupAndReturnResponse(
if (hasText(zoneId)) {
headers.add(IdentityZoneSwitchingFilter.HEADER, zoneId);
}
return template.exchange(
final ResponseEntity<ScimGroup> response = template.exchange(
url + "/Groups",
HttpMethod.POST,
new HttpEntity<>(JsonUtils.writeValueAsBytes(group), headers),
ScimGroup.class
);
assertStatusCode(response, expectedStatusCodes);
final ScimGroup responseBody = response.getBody();
assertNotNull(responseBody);
return responseBody;
}

private static ScimGroup updateGroup(String token,
Expand Down Expand Up @@ -768,10 +772,17 @@ public static UaaClientDetails getClient(String token,
return response.getBody();
}

private static void assertStatusCode(final ResponseEntity<?> response, final HttpStatus expectedStatusCode) {
if (response.getStatusCode() != expectedStatusCode) {
private static void assertStatusCode(final ResponseEntity<?> response, final HttpStatus... expectedStatusCodes) {
final boolean matchesAnyExpectedStatusCode = Stream.of(expectedStatusCodes)
.anyMatch(it -> it.equals(response.getStatusCode()));
if (!matchesAnyExpectedStatusCode) {
final String expectedStatusCodesString = Arrays.stream(expectedStatusCodes)
.map(HttpStatus::value)
.map(Object::toString)
.collect(joining(" or "));
throw new RuntimeException(
"Invalid return code: expected %d, got %d".formatted(expectedStatusCode.value(), response.getStatusCode().value())
"Invalid return code: expected %s, got %d".formatted(expectedStatusCodesString,
response.getStatusCode().value())
);
}
}
Expand Down Expand Up @@ -1002,7 +1013,7 @@ public static String getZoneAdminToken(String baseUrl, ServerRunning serverRunni

String groupName = "zones." + zoneId + ".admin";
ScimGroup group = new ScimGroup(null, groupName, null);
createGroupAndIgnoreStatusCode(getClientCredentialsToken(baseUrl, "admin", "adminsecret"), "", baseUrl, group);
createGroupAndIgnoreConflict(getClientCredentialsToken(baseUrl, "admin", "adminsecret"), "", baseUrl, group);
String groupId = IntegrationTestUtils.findGroupId(adminClient, baseUrl, groupName);
assertThat("Couldn't find group : " + groupId, groupId, is(CoreMatchers.notNullValue()));
IntegrationTestUtils.addMemberToGroup(adminClient, baseUrl, user.getId(), groupId);
Expand Down

0 comments on commit bdec670

Please sign in to comment.