Skip to content

Commit

Permalink
Test Refactor (ClientAdminBootstrapTests)
Browse files Browse the repository at this point in the history
- Apply IntelliJ sanitizations and reformat

[#164842699]
  • Loading branch information
joshuatcasey committed Mar 27, 2019
1 parent 5d5bc67 commit 25512b4
Showing 1 changed file with 39 additions and 80 deletions.
@@ -1,16 +1,3 @@
/*******************************************************************************
* Cloud Foundry
* Copyright (c) [2009-2016] Pivotal Software, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product includes a number of subcomponents with
* separate copyright notices and license terms. Your use of these
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
*******************************************************************************/

package org.cloudfoundry.identity.uaa.client; package org.cloudfoundry.identity.uaa.client;


import org.cloudfoundry.identity.uaa.audit.event.EntityDeletedEvent; import org.cloudfoundry.identity.uaa.audit.event.EntityDeletedEvent;
Expand Down Expand Up @@ -38,39 +25,16 @@
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;


import java.util.Arrays; import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;


import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_AUTHORIZATION_CODE; import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.*;
import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_IMPLICIT; import static org.junit.Assert.*;
import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_JWT_BEARER;
import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_REFRESH_TOKEN;
import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_SAML2_BEARER;
import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_USER_TOKEN;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.*;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.same; import static org.mockito.Mockito.same;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


public class ClientAdminBootstrapTests extends JdbcTestBase { public class ClientAdminBootstrapTests extends JdbcTestBase {


Expand Down Expand Up @@ -100,14 +64,13 @@ public void testSimpleAddClient() throws Exception {
testSimpleAddClient("foo"); testSimpleAddClient("foo");
} }


public ClientDetails testSimpleAddClient(String clientId) throws Exception { private void testSimpleAddClient(String clientId) throws Exception {
Map<String, Object> map = createClientMap(clientId); Map<String, Object> map = createClientMap(clientId);
ClientDetails created = doSimpleTest(map); ClientDetails created = doSimpleTest(map);
assertSet((String) map.get("redirect-uri"), null, created.getRegisteredRedirectUri(), String.class); assertSet((String) map.get("redirect-uri"), null, created.getRegisteredRedirectUri(), String.class);
return created;
} }


public Map<String, Object> createClientMap(String clientId) { private Map<String, Object> createClientMap(String clientId) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("id", clientId); map.put("id", clientId);
map.put("secret", "bar"); map.put("secret", "bar");
Expand All @@ -120,11 +83,11 @@ public Map<String, Object> createClientMap(String clientId) {


@Test @Test
public void client_slated_for_deletion_does_not_get_inserted() throws Exception { public void client_slated_for_deletion_does_not_get_inserted() throws Exception {
String autoApproveId = "autoapprove-"+new RandomValueStringGenerator().generate().toLowerCase(); String autoApproveId = "autoapprove-" + new RandomValueStringGenerator().generate().toLowerCase();
testSimpleAddClient(autoApproveId); testSimpleAddClient(autoApproveId);
reset(clientRegistrationService); reset(clientRegistrationService);
bootstrap = spy(bootstrap); bootstrap = spy(bootstrap);
String clientId = "client-"+new RandomValueStringGenerator().generate().toLowerCase(); String clientId = "client-" + new RandomValueStringGenerator().generate().toLowerCase();
Map<String, Map<String, Object>> clients = Collections.singletonMap(clientId, createClientMap(clientId)); Map<String, Map<String, Object>> clients = Collections.singletonMap(clientId, createClientMap(clientId));
bootstrap.setClients(clients); bootstrap.setClients(clients);
bootstrap.setAutoApproveClients(singletonList(autoApproveId)); bootstrap.setAutoApproveClients(singletonList(autoApproveId));
Expand All @@ -138,38 +101,34 @@ public void client_slated_for_deletion_does_not_get_inserted() throws Exception
@Test @Test
public void test_delete_from_yaml_existing_client() throws Exception { public void test_delete_from_yaml_existing_client() throws Exception {
bootstrap = spy(bootstrap); bootstrap = spy(bootstrap);
String clientId = "client-"+new RandomValueStringGenerator().generate().toLowerCase(); String clientId = "client-" + new RandomValueStringGenerator().generate().toLowerCase();
testSimpleAddClient(clientId); testSimpleAddClient(clientId);
verify(bootstrap, never()).publish(any()); verify(bootstrap, never()).publish(any());
bootstrap.setClientsToDelete(Arrays.asList(clientId)); bootstrap.setClientsToDelete(Collections.singletonList(clientId));
bootstrap.onApplicationEvent(new ContextRefreshedEvent(mock(ApplicationContext.class))); bootstrap.onApplicationEvent(new ContextRefreshedEvent(mock(ApplicationContext.class)));
ArgumentCaptor<EntityDeletedEvent> captor = ArgumentCaptor.forClass(EntityDeletedEvent.class); ArgumentCaptor<EntityDeletedEvent> captor = ArgumentCaptor.forClass(EntityDeletedEvent.class);
verify(bootstrap, times(1)).publish(captor.capture()); verify(bootstrap, times(1)).publish(captor.capture());
assertNotNull(captor.getValue()); assertNotNull(captor.getValue());
verify(publisher, times(1)).publishEvent(same(captor.getValue())); verify(publisher, times(1)).publishEvent(same(captor.getValue()));
assertEquals(clientId, captor.getValue().getObjectId()); assertEquals(clientId, captor.getValue().getObjectId());
assertEquals(clientId, ((ClientDetails)captor.getValue().getDeleted()).getClientId()); assertEquals(clientId, ((ClientDetails) captor.getValue().getDeleted()).getClientId());
assertSame(SystemAuthentication.SYSTEM_AUTHENTICATION, captor.getValue().getAuthentication()); assertSame(SystemAuthentication.SYSTEM_AUTHENTICATION, captor.getValue().getAuthentication());
assertNotNull(captor.getValue().getAuditEvent()); assertNotNull(captor.getValue().getAuditEvent());
} }


@Test @Test
public void test_delete_from_yaml_non_existing_client() throws Exception { public void test_delete_from_yaml_non_existing_client() {
ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class); ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
bootstrap = spy(bootstrap); bootstrap = spy(bootstrap);
bootstrap.setApplicationEventPublisher(publisher); bootstrap.setApplicationEventPublisher(publisher);
String clientId = "client-"+new RandomValueStringGenerator().generate().toLowerCase(); String clientId = "client-" + new RandomValueStringGenerator().generate().toLowerCase();
verify(bootstrap, never()).publish(any()); verify(bootstrap, never()).publish(any());
bootstrap.setClientsToDelete(Arrays.asList(clientId)); bootstrap.setClientsToDelete(Collections.singletonList(clientId));
bootstrap.onApplicationEvent(new ContextRefreshedEvent(mock(ApplicationContext.class))); bootstrap.onApplicationEvent(new ContextRefreshedEvent(mock(ApplicationContext.class)));
verify(bootstrap, never()).publish(any()); verify(bootstrap, never()).publish(any());
verify(publisher, never()).publishEvent(any()); verify(publisher, never()).publishEvent(any());
} }


public Integer countClients(String clientId) {
return jdbcTemplate.queryForObject("SELECT count(*) FROM oauth_client_details WHERE client_id = ? AND identity_zone_id = ?", Integer.class, clientId, IdentityZoneHolder.get().getId());
}

@Test(expected = InvalidClientDetailsException.class) @Test(expected = InvalidClientDetailsException.class)
public void no_registered_redirect_url_for_auth_code() throws Exception { public void no_registered_redirect_url_for_auth_code() throws Exception {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
Expand Down Expand Up @@ -228,7 +187,7 @@ public void clientMetadata_getsBootstrapped() throws Exception {
map.put("app-launch-url", "http://takemetothispage.com"); map.put("app-launch-url", "http://takemetothispage.com");
map.put("app-icon", "bAsE64encODEd/iMAgE="); map.put("app-icon", "bAsE64encODEd/iMAgE=");
map.put("redirect-uri", "http://localhost/callback"); map.put("redirect-uri", "http://localhost/callback");
map.put("authorized-grant-types","client_credentials"); map.put("authorized-grant-types", "client_credentials");
bootstrap.setClients(Collections.singletonMap((String) map.get("id"), map)); bootstrap.setClients(Collections.singletonMap((String) map.get("id"), map));
bootstrap.afterPropertiesSet(); bootstrap.afterPropertiesSet();


Expand Down Expand Up @@ -309,13 +268,13 @@ public void testOverrideClient() throws Exception {
when(clientMetadataProvisioning.update(any(ClientMetadata.class), anyString())).thenReturn(new ClientMetadata()); when(clientMetadataProvisioning.update(any(ClientMetadata.class), anyString())).thenReturn(new ClientMetadata());


doThrow(new ClientAlreadyExistsException("Planned")) doThrow(new ClientAlreadyExistsException("Planned"))
.when(clientRegistrationService).addClientDetails(any(ClientDetails.class), anyString()); .when(clientRegistrationService).addClientDetails(any(ClientDetails.class), anyString());
bootstrap.afterPropertiesSet(); bootstrap.afterPropertiesSet();
verify(clientRegistrationService, times(1)).addClientDetails(any(ClientDetails.class), anyString()); verify(clientRegistrationService, times(1)).addClientDetails(any(ClientDetails.class), anyString());
ArgumentCaptor<ClientDetails> captor = ArgumentCaptor.forClass(ClientDetails.class); ArgumentCaptor<ClientDetails> captor = ArgumentCaptor.forClass(ClientDetails.class);
verify(clientRegistrationService, times(1)).updateClientDetails(captor.capture(), anyString()); verify(clientRegistrationService, times(1)).updateClientDetails(captor.capture(), anyString());
verify(clientRegistrationService, times(1)).updateClientSecret("foo", "bar", IdentityZoneHolder.get().getId()); verify(clientRegistrationService, times(1)).updateClientSecret("foo", "bar", IdentityZoneHolder.get().getId());
assertEquals(new HashSet(Arrays.asList("client_credentials")), captor.getValue().getAuthorizedGrantTypes()); assertEquals(new HashSet(Collections.singletonList("client_credentials")), captor.getValue().getAuthorizedGrantTypes());
} }


@Test @Test
Expand All @@ -337,13 +296,13 @@ public void testOverrideClientWithEmptySecret() throws Exception {
when(clientMetadataProvisioning.update(any(ClientMetadata.class), anyString())).thenReturn(new ClientMetadata()); when(clientMetadataProvisioning.update(any(ClientMetadata.class), anyString())).thenReturn(new ClientMetadata());


doThrow(new ClientAlreadyExistsException("Planned")) doThrow(new ClientAlreadyExistsException("Planned"))
.when(clientRegistrationService).addClientDetails(any(ClientDetails.class), anyString()); .when(clientRegistrationService).addClientDetails(any(ClientDetails.class), anyString());
bootstrap.afterPropertiesSet(); bootstrap.afterPropertiesSet();
verify(clientRegistrationService, times(1)).addClientDetails(any(ClientDetails.class), anyString()); verify(clientRegistrationService, times(1)).addClientDetails(any(ClientDetails.class), anyString());
ArgumentCaptor<ClientDetails> captor = ArgumentCaptor.forClass(ClientDetails.class); ArgumentCaptor<ClientDetails> captor = ArgumentCaptor.forClass(ClientDetails.class);
verify(clientRegistrationService, times(1)).updateClientDetails(captor.capture(), anyString()); verify(clientRegistrationService, times(1)).updateClientDetails(captor.capture(), anyString());
verify(clientRegistrationService, times(1)).updateClientSecret("foo", "", IdentityZoneHolder.get().getId()); verify(clientRegistrationService, times(1)).updateClientSecret("foo", "", IdentityZoneHolder.get().getId());
assertEquals(new HashSet(Arrays.asList("client_credentials")), captor.getValue().getAuthorizedGrantTypes()); assertEquals(new HashSet(Collections.singletonList("client_credentials")), captor.getValue().getAuthorizedGrantTypes());
} }


@Test @Test
Expand All @@ -358,14 +317,14 @@ public void testOverrideClientByDefault() throws Exception {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("secret", "bar"); map.put("secret", "bar");
map.put("redirect-uri", "http://localhost/callback"); map.put("redirect-uri", "http://localhost/callback");
map.put("authorized-grant-types","client_credentials"); map.put("authorized-grant-types", "client_credentials");
bootstrap.setClients(Collections.singletonMap("foo", map)); bootstrap.setClients(Collections.singletonMap("foo", map));
when(clientMetadataProvisioning.update(any(ClientMetadata.class), anyString())).thenReturn(new ClientMetadata()); when(clientMetadataProvisioning.update(any(ClientMetadata.class), anyString())).thenReturn(new ClientMetadata());
doThrow(new ClientAlreadyExistsException("Planned")).when(clientRegistrationService) doThrow(new ClientAlreadyExistsException("Planned")).when(clientRegistrationService)
.addClientDetails( .addClientDetails(
any(ClientDetails.class), any(ClientDetails.class),
anyString() anyString()
); );
bootstrap.afterPropertiesSet(); bootstrap.afterPropertiesSet();
verify(clientRegistrationService, times(1)).addClientDetails(any(ClientDetails.class), anyString()); verify(clientRegistrationService, times(1)).addClientDetails(any(ClientDetails.class), anyString());
verify(clientRegistrationService, times(1)).updateClientDetails(any(ClientDetails.class), anyString()); verify(clientRegistrationService, times(1)).updateClientDetails(any(ClientDetails.class), anyString());
Expand All @@ -380,12 +339,12 @@ public void testOverrideClientWithYaml() throws Exception {


@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
Map fooBeforeClient = new Yaml().loadAs("id: foo\noverride: true\nsecret: somevalue\n" Map fooBeforeClient = new Yaml().loadAs("id: foo\noverride: true\nsecret: somevalue\n"
+ "access-token-validity: 100\nredirect-uri: http://localhost/callback\n" + "access-token-validity: 100\nredirect-uri: http://localhost/callback\n"
+ "authorized-grant-types: client_credentials", Map.class); + "authorized-grant-types: client_credentials", Map.class);
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
Map barBeforeClient = new Yaml().loadAs("id: bar\noverride: true\nsecret: somevalue\n" Map barBeforeClient = new Yaml().loadAs("id: bar\noverride: true\nsecret: somevalue\n"
+ "access-token-validity: 100\nredirect-uri: http://localhost/callback\n" + "access-token-validity: 100\nredirect-uri: http://localhost/callback\n"
+ "authorized-grant-types: client_credentials", Map.class); + "authorized-grant-types: client_credentials", Map.class);
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
Map clients = new HashMap(); Map clients = new HashMap();
clients.put("foo", fooBeforeClient); clients.put("foo", fooBeforeClient);
Expand All @@ -394,9 +353,9 @@ public void testOverrideClientWithYaml() throws Exception {
bootstrap.afterPropertiesSet(); bootstrap.afterPropertiesSet();


Map fooUpdateClient = new HashMap(fooBeforeClient); Map fooUpdateClient = new HashMap(fooBeforeClient);
fooUpdateClient.put("secret","bar"); fooUpdateClient.put("secret", "bar");
Map barUpdateClient = new HashMap(fooBeforeClient); Map barUpdateClient = new HashMap(fooBeforeClient);
barUpdateClient.put("secret","bar"); barUpdateClient.put("secret", "bar");
clients = new HashMap(); clients = new HashMap();
clients.put("foo", fooUpdateClient); clients.put("foo", fooUpdateClient);
clients.put("bar", barUpdateClient); clients.put("bar", barUpdateClient);
Expand All @@ -405,7 +364,7 @@ public void testOverrideClientWithYaml() throws Exception {
reset(clientRegistrationService); reset(clientRegistrationService);
when(clientMetadataProvisioning.update(any(ClientMetadata.class), anyString())).thenReturn(new ClientMetadata()); when(clientMetadataProvisioning.update(any(ClientMetadata.class), anyString())).thenReturn(new ClientMetadata());
doThrow(new ClientAlreadyExistsException("Planned")).when(clientRegistrationService).addClientDetails( doThrow(new ClientAlreadyExistsException("Planned")).when(clientRegistrationService).addClientDetails(
any(ClientDetails.class), anyString()); any(ClientDetails.class), anyString());
bootstrap.afterPropertiesSet(); bootstrap.afterPropertiesSet();
verify(clientRegistrationService, times(2)).addClientDetails(any(ClientDetails.class), anyString()); verify(clientRegistrationService, times(2)).addClientDetails(any(ClientDetails.class), anyString());
verify(clientRegistrationService, times(2)).updateClientDetails(any(ClientDetails.class), anyString()); verify(clientRegistrationService, times(2)).updateClientDetails(any(ClientDetails.class), anyString());
Expand Down Expand Up @@ -463,11 +422,11 @@ private ClientDetails doSimpleTest(Map<String, Object> map) throws Exception {
ClientDetails created = clientRegistrationService.loadClientByClientId((String) map.get("id")); ClientDetails created = clientRegistrationService.loadClientByClientId((String) map.get("id"));
assertNotNull(created); assertNotNull(created);
assertSet((String) map.get("scope"), Collections.singleton("uaa.none"), created.getScope(), String.class); assertSet((String) map.get("scope"), Collections.singleton("uaa.none"), created.getScope(), String.class);
assertSet((String) map.get("resource-ids"), new HashSet(Arrays.asList("none")), created.getResourceIds(), String.class); assertSet((String) map.get("resource-ids"), new HashSet(Collections.singletonList("none")), created.getResourceIds(), String.class);


String authTypes = (String) map.get("authorized-grant-types"); String authTypes = (String) map.get("authorized-grant-types");
if (authTypes!=null && authTypes.contains(GRANT_TYPE_AUTHORIZATION_CODE)) { if (authTypes != null && authTypes.contains(GRANT_TYPE_AUTHORIZATION_CODE)) {
authTypes+=",refresh_token"; authTypes += ",refresh_token";
} }
assertSet(authTypes, Collections.emptySet(), created.getAuthorizedGrantTypes(), String.class); assertSet(authTypes, Collections.emptySet(), created.getAuthorizedGrantTypes(), String.class);


Expand All @@ -485,9 +444,9 @@ private ClientDetails doSimpleTest(Map<String, Object> map) throws Exception {
"refresh-token-validity")) { "refresh-token-validity")) {
info.remove(key); info.remove(key);
} }
for (Map.Entry<String,Object> entry : info.entrySet()) { for (Map.Entry<String, Object> entry : info.entrySet()) {
assertTrue("Client should contain additional information key:"+ entry.getKey(), created.getAdditionalInformation().containsKey(entry.getKey())); assertTrue("Client should contain additional information key:" + entry.getKey(), created.getAdditionalInformation().containsKey(entry.getKey()));
if (entry.getValue()!=null) { if (entry.getValue() != null) {
assertEquals(entry.getValue(), created.getAdditionalInformation().get(entry.getKey())); assertEquals(entry.getValue(), created.getAdditionalInformation().get(entry.getKey()));
} }
} }
Expand All @@ -497,8 +456,8 @@ private ClientDetails doSimpleTest(Map<String, Object> map) throws Exception {
} }


private void assertSet(String expectedValue, Collection defaultValueIfNull, Collection actualValue, Class<?> type) { private void assertSet(String expectedValue, Collection defaultValueIfNull, Collection actualValue, Class<?> type) {
Collection assertScopes = defaultValueIfNull; Collection assertScopes = defaultValueIfNull;
if (expectedValue!=null) { if (expectedValue != null) {
if (String.class.equals(type)) { if (String.class.equals(type)) {
assertScopes = StringUtils.commaDelimitedListToSet(expectedValue); assertScopes = StringUtils.commaDelimitedListToSet(expectedValue);
} else { } else {
Expand Down

0 comments on commit 25512b4

Please sign in to comment.