Allow for the backend to return granted scopes in any order.

Closes #12395
This commit is contained in:
Alexander Schwartz 2022-06-08 09:25:32 +02:00 committed by Pedro Igor
parent 5d2bf6ea33
commit 9272c7a5ec
2 changed files with 20 additions and 4 deletions

View file

@ -36,6 +36,7 @@ import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.representations.idm.UserSessionRepresentation;
import org.keycloak.util.TokenUtil;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -310,8 +311,24 @@ public class AssertEvents implements TestRule {
}
public ExpectedEvent detail(String key, String value) {
if (key.equals(Details.SCOPE)) {
// the scopes can be given in any order,
// therefore, use a matcher that takes a string and ignores the order of the scopes
return detail(key, new TypeSafeMatcher<String>() {
@Override
protected boolean matchesSafely(String actualValue) {
return Matchers.containsInAnyOrder(value.split(" ")).matches(Arrays.asList(actualValue.split(" ")));
}
@Override
public void describeTo(Description description) {
description.appendText("contains scope in any order");
}
});
} else {
return detail(key, CoreMatchers.equalTo(value));
}
}
public ExpectedEvent detail(String key, Matcher<? super String> matcher) {
if (details == null) {

View file

@ -1029,9 +1029,8 @@ public class AccountRestServiceTest extends AbstractRestServiceTest {
.asJson(ConsentRepresentation.class);
assertTrue(consentRepresentation.getCreatedDate() > 0);
assertTrue(consentRepresentation.getLastUpdatedDate() > 0);
assertEquals(2, consentRepresentation.getGrantedScopes().size());
assertEquals(requestedScopes.get(0).getId(), consentRepresentation.getGrantedScopes().get(0).getId());
assertEquals(requestedScopes.get(1).getId(), consentRepresentation.getGrantedScopes().get(1).getId());
assertThat(consentRepresentation.getGrantedScopes().stream().map(ConsentScopeRepresentation::getId).collect(Collectors.toList()),
containsInAnyOrder(requestedScopes.stream().map(ClientScopeRepresentation::getId).toArray()));
events.poll();
String expectedScopeDetails = requestedScopes.stream().map(cs->cs.getName()).collect(Collectors.joining(" "));