From 78b8c717709b9a3a80e52c7bb0a318bd95deda97 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 6 Jul 2017 20:32:26 +0200 Subject: [PATCH] Use AuthenticationSteps factory methods in tests. See gh-107. --- .../AuthenticationStepsExecutor.java | 1 - ...uthenticationOperatorIntegrationTests.java | 11 ++------- ...IdAuthenticationStepsIntegrationTests.java | 9 ++------ ...AppRoleAuthenticationIntegrationTests.java | 10 ++++---- .../AppRoleAuthenticationUnitTests.java | 8 +++---- .../AwsEc2AuthenticationUnitTests.java | 23 ++++++++----------- ...uthenticationOperatorIntegrationTests.java | 16 +------------ ...teAuthenticationStepsIntegrationTests.java | 11 ++++----- ...uthenticationOperatorIntegrationTests.java | 9 +------- ...leAuthenticationStepsIntegrationTests.java | 5 +--- 10 files changed, 29 insertions(+), 74 deletions(-) diff --git a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsExecutor.java b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsExecutor.java index 7681935a..279e94ff 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsExecutor.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsExecutor.java @@ -175,5 +175,4 @@ public class AuthenticationStepsExecutor implements ClientAuthentication { return entity; } - } diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppIdAuthenticationOperatorIntegrationTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppIdAuthenticationOperatorIntegrationTests.java index c66fa4c2..4d64e8d1 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppIdAuthenticationOperatorIntegrationTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppIdAuthenticationOperatorIntegrationTests.java @@ -20,7 +20,6 @@ import reactor.test.StepVerifier; import org.springframework.vault.util.Settings; import org.springframework.vault.util.TestWebClientFactory; -import org.springframework.web.client.RestTemplate; import org.springframework.web.reactive.function.client.WebClient; /** @@ -42,11 +41,8 @@ public class AppIdAuthenticationOperatorIntegrationTests extends .userIdMechanism(new StaticUserId("static-userid-value")) // .build(); - AppIdAuthentication authentication = new AppIdAuthentication(options, - new RestTemplate()); - AuthenticationStepsOperator supplier = new AuthenticationStepsOperator( - authentication.getAuthenticationSteps(), webClient); + AppIdAuthentication.createAuthenticationSteps(options), webClient); StepVerifier.create(supplier.getVaultToken()).expectNextCount(1).verifyComplete(); } @@ -59,11 +55,8 @@ public class AppIdAuthenticationOperatorIntegrationTests extends .userIdMechanism(new StaticUserId("wrong")) // .build(); - AppIdAuthentication authentication = new AppIdAuthentication(options, - new RestTemplate()); - AuthenticationStepsOperator supplier = new AuthenticationStepsOperator( - authentication.getAuthenticationSteps(), webClient); + AppIdAuthentication.createAuthenticationSteps(options), webClient); StepVerifier.create(supplier.getVaultToken()).expectError().verify(); } diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppIdAuthenticationStepsIntegrationTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppIdAuthenticationStepsIntegrationTests.java index 1e02c15a..665bcb6e 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppIdAuthenticationStepsIntegrationTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppIdAuthenticationStepsIntegrationTests.java @@ -45,11 +45,8 @@ public class AppIdAuthenticationStepsIntegrationTests extends RestTemplate restTemplate = TestRestTemplateFactory.create(Settings .createSslConfiguration()); - AppIdAuthentication authentication = new AppIdAuthentication(options, - restTemplate); - AuthenticationStepsExecutor executor = new AuthenticationStepsExecutor( - authentication.getAuthenticationSteps(), restTemplate); + AppIdAuthentication.createAuthenticationSteps(options), restTemplate); VaultToken login = executor.login(); @@ -67,10 +64,8 @@ public class AppIdAuthenticationStepsIntegrationTests extends RestTemplate restTemplate = TestRestTemplateFactory.create(Settings .createSslConfiguration()); - AuthenticationSteps authenticationChain = new AppIdAuthentication(options, - restTemplate).getAuthenticationSteps(); AuthenticationStepsExecutor executor = new AuthenticationStepsExecutor( - authenticationChain, restTemplate); + AppIdAuthentication.createAuthenticationSteps(options), restTemplate); executor.login(); } diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppRoleAuthenticationIntegrationTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppRoleAuthenticationIntegrationTests.java index 8b3d09bd..f77a0469 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppRoleAuthenticationIntegrationTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppRoleAuthenticationIntegrationTests.java @@ -159,11 +159,10 @@ public class AppRoleAuthenticationIntegrationTests extends IntegrationTestSuppor AppRoleAuthenticationOptions options = AppRoleAuthenticationOptions.builder() .roleId(roleId).secretId("this-is-a-wrong-secret-id").build(); - AppRoleAuthentication authentication = new AppRoleAuthentication(options, - prepare().getRestTemplate()); AuthenticationStepsExecutor executor = new AuthenticationStepsExecutor( - authentication.getAuthenticationSteps(), prepare().getRestTemplate()); + AppRoleAuthentication.createAuthenticationSteps(options), prepare() + .getRestTemplate()); assertThat(executor.login()).isNotNull(); } @@ -180,11 +179,10 @@ public class AppRoleAuthenticationIntegrationTests extends IntegrationTestSuppor AppRoleAuthenticationOptions options = AppRoleAuthenticationOptions.builder() .roleId(roleId).secretId(secretId).build(); - AppRoleAuthentication authentication = new AppRoleAuthentication(options, - prepare().getRestTemplate()); AuthenticationStepsExecutor executor = new AuthenticationStepsExecutor( - authentication.getAuthenticationSteps(), prepare().getRestTemplate()); + AppRoleAuthentication.createAuthenticationSteps(options), prepare() + .getRestTemplate()); assertThat(executor.login()).isNotNull(); diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppRoleAuthenticationUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppRoleAuthenticationUnitTests.java index 313c35bd..97d406d2 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppRoleAuthenticationUnitTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppRoleAuthenticationUnitTests.java @@ -47,7 +47,7 @@ public class AppRoleAuthenticationUnitTests { private MockRestServiceServer mockRest; @Before - public void before() throws Exception { + public void before() { RestTemplate restTemplate = VaultClients.createRestTemplate(); restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler()); @@ -57,7 +57,7 @@ public class AppRoleAuthenticationUnitTests { } @Test - public void loginShouldObtainToken() throws Exception { + public void loginShouldObtainToken() { AppRoleAuthenticationOptions options = AppRoleAuthenticationOptions.builder() .roleId("hello") // @@ -81,7 +81,7 @@ public class AppRoleAuthenticationUnitTests { } @Test - public void loginShouldObtainTokenWithoutSecretId() throws Exception { + public void loginShouldObtainTokenWithoutSecretId() { AppRoleAuthenticationOptions options = AppRoleAuthenticationOptions.builder() .roleId("hello") // @@ -110,7 +110,7 @@ public class AppRoleAuthenticationUnitTests { } @Test(expected = VaultException.class) - public void loginShouldFail() throws Exception { + public void loginShouldFail() { AppRoleAuthenticationOptions options = AppRoleAuthenticationOptions.builder() .roleId("hello") // diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AwsEc2AuthenticationUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AwsEc2AuthenticationUnitTests.java index 0681a915..7d6cf1fa 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AwsEc2AuthenticationUnitTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AwsEc2AuthenticationUnitTests.java @@ -50,7 +50,7 @@ public class AwsEc2AuthenticationUnitTests { private MockRestServiceServer mockRest; @Before - public void before() throws Exception { + public void before() { RestTemplate restTemplate = VaultClients.createRestTemplate(); restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler()); @@ -60,7 +60,7 @@ public class AwsEc2AuthenticationUnitTests { } @Test - public void shouldObtainIdentityDocument() throws Exception { + public void shouldObtainIdentityDocument() { mockRest.expect( requestTo("http://169.254.169.254/latest/dynamic/instance-identity/pkcs7")) // @@ -74,7 +74,7 @@ public class AwsEc2AuthenticationUnitTests { } @Test - public void shouldContainRole() throws Exception { + public void shouldContainRole() { AwsEc2AuthenticationOptions options = AwsEc2AuthenticationOptions.builder() .role("ami").build(); @@ -94,7 +94,7 @@ public class AwsEc2AuthenticationUnitTests { } @Test - public void shouldLogin() throws Exception { + public void shouldLogin() { Nonce nonce = Nonce.provided("foo".toCharArray()); @@ -130,12 +130,12 @@ public class AwsEc2AuthenticationUnitTests { } @Test - public void authenticationChainShouldLogin() throws Exception { + public void authenticationChainShouldLogin() { Nonce nonce = Nonce.provided("foo".toCharArray()); - AwsEc2AuthenticationOptions authenticationOptions = AwsEc2AuthenticationOptions - .builder().nonce(nonce).build(); + AwsEc2AuthenticationOptions options = AwsEc2AuthenticationOptions.builder() + .nonce(nonce).build(); mockRest.expect( requestTo("http://169.254.169.254/latest/dynamic/instance-identity/pkcs7")) // @@ -153,11 +153,8 @@ public class AwsEc2AuthenticationUnitTests { + "\"auth\":{\"client_token\":\"my-token\", \"lease_duration\":20}" + "}")); - AwsEc2Authentication authentication = new AwsEc2Authentication( - authenticationOptions, restTemplate, restTemplate); - AuthenticationStepsExecutor executor = new AuthenticationStepsExecutor( - authentication.getAuthenticationSteps(), restTemplate); + AwsEc2Authentication.createAuthenticationSteps(options), restTemplate); VaultToken login = executor.login(); assertThat(login).isInstanceOf(LoginToken.class); @@ -168,7 +165,7 @@ public class AwsEc2AuthenticationUnitTests { } @Test(expected = VaultException.class) - public void loginShouldFailWhileObtainingIdentityDocument() throws Exception { + public void loginShouldFailWhileObtainingIdentityDocument() { mockRest.expect( requestTo("http://169.254.169.254/latest/dynamic/instance-identity/pkcs7")) // @@ -178,7 +175,7 @@ public class AwsEc2AuthenticationUnitTests { } @Test(expected = VaultException.class) - public void loginShouldFail() throws Exception { + public void loginShouldFail() { mockRest.expect(requestTo("/auth/aws-ec2/login")) // .andRespond(withServerError()); diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationOperatorIntegrationTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationOperatorIntegrationTests.java index b584f821..18478de9 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationOperatorIntegrationTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationOperatorIntegrationTests.java @@ -18,13 +18,7 @@ package org.springframework.vault.authentication; import org.junit.Test; import reactor.test.StepVerifier; -import org.springframework.http.client.ClientHttpRequestFactory; -import org.springframework.vault.client.VaultClients; -import org.springframework.vault.config.ClientHttpRequestFactoryFactory; -import org.springframework.vault.support.ClientOptions; -import org.springframework.vault.util.TestRestTemplateFactory; import org.springframework.vault.util.TestWebClientFactory; -import org.springframework.web.client.RestTemplate; import org.springframework.web.reactive.function.client.WebClient; /** @@ -42,16 +36,8 @@ public class ClientCertificateAuthenticationOperatorIntegrationTests extends WebClient webClient = TestWebClientFactory .create(prepareCertAuthenticationMethod()); - ClientHttpRequestFactory clientHttpRequestFactory = ClientHttpRequestFactoryFactory - .create(new ClientOptions(), prepareCertAuthenticationMethod()); - - RestTemplate restTemplate = VaultClients.createRestTemplate( - TestRestTemplateFactory.TEST_VAULT_ENDPOINT, clientHttpRequestFactory); - ClientCertificateAuthentication authentication = new ClientCertificateAuthentication( - restTemplate); - AuthenticationStepsOperator operator = new AuthenticationStepsOperator( - authentication.getAuthenticationSteps(), webClient); + ClientCertificateAuthentication.createAuthenticationSteps(), webClient); StepVerifier.create(operator.getVaultToken()).expectNextCount(1).verifyComplete(); } diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationStepsIntegrationTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationStepsIntegrationTests.java index 1f4af92a..6c24803f 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationStepsIntegrationTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationStepsIntegrationTests.java @@ -46,11 +46,9 @@ public class ClientCertificateAuthenticationStepsIntegrationTests extends RestTemplate restTemplate = VaultClients.createRestTemplate( TestRestTemplateFactory.TEST_VAULT_ENDPOINT, clientHttpRequestFactory); - ClientCertificateAuthentication authentication = new ClientCertificateAuthentication( - restTemplate); AuthenticationStepsExecutor executor = new AuthenticationStepsExecutor( - authentication.getAuthenticationSteps(), restTemplate); + ClientCertificateAuthentication.createAuthenticationSteps(), restTemplate); VaultToken login = executor.login(); @@ -67,9 +65,8 @@ public class ClientCertificateAuthenticationStepsIntegrationTests extends RestTemplate restTemplate = VaultClients.createRestTemplate( TestRestTemplateFactory.TEST_VAULT_ENDPOINT, clientHttpRequestFactory); - AuthenticationSteps steps = new ClientCertificateAuthentication(restTemplate) - .getAuthenticationSteps(); - - new AuthenticationStepsExecutor(steps, restTemplate).login(); + new AuthenticationStepsExecutor( + ClientCertificateAuthentication.createAuthenticationSteps(), restTemplate) + .login(); } } diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/CubbyholeAuthenticationOperatorIntegrationTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/CubbyholeAuthenticationOperatorIntegrationTests.java index b2ff25fb..68e735eb 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/CubbyholeAuthenticationOperatorIntegrationTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/CubbyholeAuthenticationOperatorIntegrationTests.java @@ -22,9 +22,7 @@ import reactor.test.StepVerifier; import org.springframework.vault.support.VaultToken; import org.springframework.vault.util.Settings; -import org.springframework.vault.util.TestRestTemplateFactory; import org.springframework.vault.util.TestWebClientFactory; -import org.springframework.web.client.RestTemplate; import org.springframework.web.reactive.function.client.WebClient; import static org.assertj.core.api.Assertions.assertThat; @@ -49,14 +47,9 @@ public class CubbyholeAuthenticationOperatorIntegrationTests extends CubbyholeAuthenticationOptions options = CubbyholeAuthenticationOptions.builder() .initialToken(VaultToken.of(initialToken)).wrapped().build(); - RestTemplate restTemplate = TestRestTemplateFactory.create(Settings - .createSslConfiguration()); - - CubbyholeAuthentication authentication = new CubbyholeAuthentication(options, - restTemplate); AuthenticationStepsOperator operator = new AuthenticationStepsOperator( - authentication.getAuthenticationSteps(), webClient); + CubbyholeAuthentication.createAuthenticationSteps(options), webClient); StepVerifier.create(operator.getVaultToken()).consumeNextWith(actual -> { diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/CubbyholeAuthenticationStepsIntegrationTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/CubbyholeAuthenticationStepsIntegrationTests.java index c6128349..b1d274fe 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/CubbyholeAuthenticationStepsIntegrationTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/CubbyholeAuthenticationStepsIntegrationTests.java @@ -47,11 +47,8 @@ public class CubbyholeAuthenticationStepsIntegrationTests extends RestTemplate restTemplate = TestRestTemplateFactory.create(Settings .createSslConfiguration()); - CubbyholeAuthentication authentication = new CubbyholeAuthentication(options, - restTemplate); - AuthenticationStepsExecutor executor = new AuthenticationStepsExecutor( - authentication.getAuthenticationSteps(), restTemplate); + CubbyholeAuthentication.createAuthenticationSteps(options), restTemplate); VaultToken login = executor.login(); assertThat(login.getToken()).doesNotContain(Settings.token().getToken());