Skip to content

Commit

Permalink
Ensure that our identity-provider endpoint returns all IDPs, inactive…
Browse files Browse the repository at this point in the history
… too so they can be managed.
  • Loading branch information
fhanik committed Mar 27, 2015
1 parent 1ff5e92 commit 54353d6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 21 deletions.
@@ -1,5 +1,5 @@
/*******************************************************************************
* Cloud Foundry
* Cloud Foundry
* Copyright (c) [2009-2014] Pivotal Software, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
Expand All @@ -17,16 +17,17 @@

/**
* @author Dave Syer
*
*
*/
public class SearchResults<T> {

private final Collection<T> resources;
private final int startIndex;
private final int itemsPerPage;
private final int totalResults;
private final Collection<String> schemas;
private Collection<T> resources;
private int startIndex;
private int itemsPerPage;
private int totalResults;
private Collection<String> schemas;

public SearchResults() {}
public SearchResults(Collection<String> schemas, Collection<T> resources, int startIndex, int itemsPerPage,
int totalResults) {
this.schemas = new ArrayList<String>(schemas);
Expand Down
Expand Up @@ -52,7 +52,7 @@ public ResponseEntity<IdentityProvider> updateIdentityProvider(@PathVariable Str

@RequestMapping(method = GET)
public ResponseEntity<List<IdentityProvider>> retrieveIdentityProviders() {
List<IdentityProvider> identityProviderList = identityProviderProvisioning.retrieveAll(IdentityZoneHolder.get().getId());
List<IdentityProvider> identityProviderList = identityProviderProvisioning.retrieveAll(false,IdentityZoneHolder.get().getId());
return new ResponseEntity<>(identityProviderList, HttpStatus.OK);
}
}
Expand Up @@ -25,6 +25,8 @@
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.cloudfoundry.identity.uaa.rest.SearchResults;
import org.codehaus.jackson.type.TypeReference;
import org.junit.Assert;
import org.apache.commons.codec.binary.Base64;
import org.cloudfoundry.identity.uaa.authentication.Origin;
Expand Down Expand Up @@ -72,7 +74,7 @@ public class MockMvcUtils {
public static MockMvcUtils utils() {
return new MockMvcUtils();
}

public IdentityZone createZoneUsingWebRequest(MockMvc mockMvc, String accessToken) throws Exception {
final String zoneId = UUID.randomUUID().toString();
IdentityZone identityZone = MultitenancyFixture.identityZone(zoneId, zoneId);
Expand All @@ -84,19 +86,19 @@ public IdentityZone createZoneUsingWebRequest(MockMvc mockMvc, String accessToke
.andExpect(status().isCreated()).andReturn();
return new ObjectMapper().readValue(result.getResponse().getContentAsByteArray(), IdentityZone.class);
}

public static class IdentityZoneCreationResult {
private final IdentityZone identityZone;
private final UaaPrincipal zoneAdmin;
private final String zoneAdminToken;

public IdentityZoneCreationResult(IdentityZone identityZone, UaaPrincipal zoneAdmin, String zoneAdminToken) {
super();
this.identityZone = identityZone;
this.zoneAdmin = zoneAdmin;
this.zoneAdminToken = zoneAdminToken;
}

public IdentityZone getIdentityZone() {
return identityZone;
}
Expand All @@ -109,7 +111,7 @@ public String getZoneAdminToken() {
return zoneAdminToken;
}
}

public IdentityZoneCreationResult createOtherIdentityZoneAndReturnResult(String subdomain, MockMvc mockMvc,
ApplicationContext webApplicationContext, ClientDetails bootstrapClient) throws Exception {
String identityToken = getClientCredentialsOAuthAccessToken(mockMvc, "identity", "identitysecret",
Expand Down Expand Up @@ -148,14 +150,14 @@ public IdentityZoneCreationResult createOtherIdentityZoneAndReturnResult(String
.accept(APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(bootstrapClient)))
.andExpect(status().isCreated());

return new IdentityZoneCreationResult(identityZone, marissa, zoneAdminAuthcodeToken);
}

public IdentityZone createOtherIdentityZone(String subdomain, MockMvc mockMvc,
ApplicationContext webApplicationContext, ClientDetails bootstrapClient) throws Exception {
return createOtherIdentityZoneAndReturnResult(subdomain, mockMvc, webApplicationContext, bootstrapClient).getIdentityZone();

}

public IdentityZone createOtherIdentityZone(String subdomain, MockMvc mockMvc,
Expand Down Expand Up @@ -210,6 +212,22 @@ public ScimUser createUser(MockMvc mockMvc, String accessToken, ScimUser user) t
return new ObjectMapper().readValue(userResult.getResponse().getContentAsString(), ScimUser.class);
}

public ScimGroup getGroup(MockMvc mockMvc, String accessToken, String displayName) throws Exception {
String filter = "displayName eq \""+displayName+"\"";
SearchResults<ScimGroup> results = JsonUtils.readValue(
mockMvc.perform(get("/Groups")
.header("Authorization", "Bearer " + accessToken)
.contentType(APPLICATION_JSON)
.param("filter", filter))
.andReturn().getResponse().getContentAsString(),
new TypeReference<SearchResults<ScimGroup>>() {});
if (results==null || results.getResources()==null || results.getResources().isEmpty()) {
return null;
} else {
return results.getResources().iterator().next();
}
}

public ScimGroup createGroup(MockMvc mockMvc, String accessToken, ScimGroup group) throws Exception {
return new ObjectMapper().readValue(
mockMvc.perform(post("/Groups")
Expand All @@ -221,6 +239,18 @@ public ScimGroup createGroup(MockMvc mockMvc, String accessToken, ScimGroup grou
ScimGroup.class);
}

public ScimGroup updateGroup(MockMvc mockMvc, String accessToken, ScimGroup group) throws Exception {
return new ObjectMapper().readValue(
mockMvc.perform(put("/Groups/" + group.getId())
.header("If-Match", group.getVersion())
.header("Authorization", "Bearer " + accessToken)
.contentType(APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsBytes(group)))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsByteArray(),
ScimGroup.class);
}

public BaseClientDetails createClient(MockMvc mockMvc, String accessToken, BaseClientDetails clientDetails)
throws Exception {
MockHttpServletRequestBuilder createClientPost = post("/oauth/clients")
Expand Down Expand Up @@ -345,7 +375,7 @@ public String getClientCredentialsOAuthAccessToken(MockMvc mockMvc, String usern
OAuthToken oauthToken = objectMapper.readValue(result.getResponse().getContentAsByteArray(), OAuthToken.class);
return oauthToken.accessToken;
}

public <T extends ApplicationEvent> TestApplicationEventListener<T> addEventListener(ConfigurableApplicationContext applicationContext, Class<T> clazz) {
TestApplicationEventListener<T> listener = TestApplicationEventListener.forEventClass(clazz);
applicationContext.addApplicationListener(listener);
Expand Down
Expand Up @@ -44,9 +44,11 @@
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import static java.util.Arrays.asList;
Expand Down Expand Up @@ -117,7 +119,41 @@ public void testCreateAndUpdateIdentityProvider() throws Exception {
ScimUser user = createAdminForZone("idps.write");
String accessToken = mockMvcUtils.getUserOAuthAccessToken(mockMvc, client.getClientId(), client.getClientSecret(), user.getUserName(), "password", "idps.write");

createAndUpdateIdentityProvider(accessToken, null);
createAndUpdateIdentityProvider(accessToken, null);
}

@Test
public void testEnsureWeRetrieveInactiveIDPsToo() throws Exception {
String clientId = RandomStringUtils.randomAlphabetic(6);
BaseClientDetails client = new BaseClientDetails(clientId,null,"idps.write,idps.read","password",null);
client.setClientSecret("test-client-secret");
mockMvcUtils.createClient(mockMvc, adminToken, client);

ScimUser user = createAdminForZone("idps.read,idps.write");
String accessToken = mockMvcUtils.getUserOAuthAccessToken(mockMvc, client.getClientId(), client.getClientSecret(), user.getUserName(), "password", "idps.read,idps.write");
String randomOriginKey = new RandomValueStringGenerator().generate();
IdentityProvider identityProvider = MultitenancyFixture.identityProvider(randomOriginKey, IdentityZone.getUaa().getId());
IdentityProvider createdIDP = createIdentityProvider(null, identityProvider, accessToken, status().isCreated());

MockHttpServletRequestBuilder requestBuilder = get("/identity-providers")
.header("Authorization", "Bearer" + accessToken)
.contentType(APPLICATION_JSON);

int numberOfIdps = identityProviderProvisioning.retrieveAll(false, IdentityZone.getUaa().getId()).size();

MvcResult result = mockMvc.perform(requestBuilder).andExpect(status().isOk()).andReturn();
List<IdentityProvider> identityProviderList = JsonUtils.readValue(result.getResponse().getContentAsString(), new TypeReference<List<IdentityProvider>>() {});
assertEquals(numberOfIdps, identityProviderList.size());
assertTrue(identityProviderList.contains(createdIDP));

createdIDP.setActive(false);
createdIDP = JsonUtils.readValue(updateIdentityProvider(null, createdIDP, accessToken, status().isOk()).getResponse().getContentAsString(), IdentityProvider.class);

result = mockMvc.perform(requestBuilder).andExpect(status().isOk()).andReturn();
identityProviderList = JsonUtils.readValue(result.getResponse().getContentAsString(), new TypeReference<List<IdentityProvider>>() {
});
assertEquals(numberOfIdps, identityProviderList.size());
assertTrue(identityProviderList.contains(createdIDP));
}

private void createAndUpdateIdentityProvider(String accessToken, String zoneId) throws Exception {
Expand Down Expand Up @@ -265,7 +301,7 @@ private MvcResult updateIdentityProvider(String zoneId, IdentityProvider identit
return result;
}

private ScimUser createAdminForZone(String scope) throws Exception {
private ScimUser createAdminForZone(String scopes) throws Exception {
String random = RandomStringUtils.randomAlphabetic(6);
ScimUser user = new ScimUser();
user.setUserName(random + "@example.com");
Expand All @@ -277,9 +313,19 @@ private ScimUser createAdminForZone(String scope) throws Exception {

// Create the zones.<zone_id>.admin Group
// Add User to the zones.<zone_id>.admin Group
ScimGroup group = new ScimGroup(scope);
group.setMembers(Arrays.asList(new ScimGroupMember(createdUser.getId())));
mockMvcUtils.createGroup(mockMvc,adminToken,group);
for (String scope : StringUtils.commaDelimitedListToSet(scopes)) {
ScimGroup group = mockMvcUtils.getGroup(mockMvc, adminToken, scope);
if (group==null) {
group = new ScimGroup(scope);
group.setMembers(Arrays.asList(new ScimGroupMember(createdUser.getId())));
mockMvcUtils.createGroup(mockMvc, adminToken, group);
} else {
List<ScimGroupMember> members = new LinkedList(group.getMembers());
members.add(new ScimGroupMember(createdUser.getId()));
group.setMembers(members);
mockMvcUtils.updateGroup(mockMvc, adminToken, group);
}
}
return createdUser;
}
}

0 comments on commit 54353d6

Please sign in to comment.