BAEL-1410 - refactor tests (#3525)
* initial setup with spring boot/ spring data jpa/ flyway * BAEL-1315 - added flyway test extensions for spring * BAEL-1315 - added flyway test extensions for spring * BAEL-1315 - created multiple migration scripts and locations * BAEL-1315 - test insert after schema creation * cleanup * BAEL-1315 - test data changes by a migration * [BAEL-1410] Spring Boot Security Auto-Configuration * [BAEL-1410] Added some tests for incorrect credentials use case * [BAEL-1410] Added readme and some code improvements * [BAEL-1410] removed form based auth config because is redundant added oauth2 server auto-configuration sample with test * [BAEL-1410] added custom Authorization Server Config * [BAEL-1410] update README * [BAEL-1410]refactor tests * [BAEL-1410]oauth2 resource server * [BAEL-1410]oauth2 sso sample with facebook * [BAEL-1410]remove spring-flyway * [BAEL-1410]refactor tests * [BAEL-1410] refactor tests * [BAEL-1410] update
This commit is contained in:
parent
0d85d1ad01
commit
f888a3f78a
|
@ -37,6 +37,7 @@ public class BasicAuthConfigurationIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenLoggedUserRequestsHomePage_ThenSuccess() throws IllegalStateException, IOException {
|
public void whenLoggedUserRequestsHomePage_ThenSuccess() throws IllegalStateException, IOException {
|
||||||
ResponseEntity<String> response = restTemplate.getForEntity(base.toString(), String.class);
|
ResponseEntity<String> response = restTemplate.getForEntity(base.toString(), String.class);
|
||||||
|
|
||||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
assertTrue(response
|
assertTrue(response
|
||||||
.getBody()
|
.getBody()
|
||||||
|
@ -47,6 +48,7 @@ public class BasicAuthConfigurationIntegrationTest {
|
||||||
public void whenUserWithWrongCredentialsRequestsHomePage_ThenUnauthorizedPage() throws IllegalStateException, IOException {
|
public void whenUserWithWrongCredentialsRequestsHomePage_ThenUnauthorizedPage() throws IllegalStateException, IOException {
|
||||||
restTemplate = new TestRestTemplate("user", "wrongpassword");
|
restTemplate = new TestRestTemplate("user", "wrongpassword");
|
||||||
ResponseEntity<String> response = restTemplate.getForEntity(base.toString(), String.class);
|
ResponseEntity<String> response = restTemplate.getForEntity(base.toString(), String.class);
|
||||||
|
|
||||||
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
|
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
|
||||||
assertTrue(response
|
assertTrue(response
|
||||||
.getBody()
|
.getBody()
|
||||||
|
|
|
@ -2,10 +2,7 @@ package com.baeldung.springbootsecurity.oauth2server;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
||||||
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
|
|
||||||
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||||
import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException;
|
import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException;
|
||||||
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
|
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
|
||||||
|
@ -13,7 +10,6 @@ import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
|
||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
@ -21,54 +17,35 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootAuthorizationServerApplication.class)
|
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootAuthorizationServerApplication.class)
|
||||||
@ActiveProfiles("authz")
|
@ActiveProfiles("authz")
|
||||||
public class CustomConfigAuthorizationServerIntegrationTest {
|
public class CustomConfigAuthorizationServerIntegrationTest extends OAuth2IntegrationTestSupport {
|
||||||
|
|
||||||
@Value("${local.server.port}") protected int port;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() {
|
public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() {
|
||||||
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails();
|
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("read"));
|
||||||
resourceDetails.setClientId("baeldung");
|
OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);
|
||||||
resourceDetails.setClientSecret("baeldung");
|
|
||||||
resourceDetails.setScope(singletonList("read"));
|
|
||||||
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
|
|
||||||
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
|
|
||||||
restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter()));
|
|
||||||
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
|
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
|
||||||
|
|
||||||
assertNotNull(accessToken);
|
assertNotNull(accessToken);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = OAuth2AccessDeniedException.class)
|
@Test(expected = OAuth2AccessDeniedException.class)
|
||||||
public void whenAccessTokenIsRequestedWithInvalidException_ThenExceptionIsThrown() {
|
public void givenOAuth2Context_whenAccessTokenIsRequestedWithInvalidException_ThenExceptionIsThrown() {
|
||||||
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails();
|
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("write"));
|
||||||
resourceDetails.setClientId("baeldung");
|
OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);
|
||||||
resourceDetails.setClientSecret("baeldung");
|
|
||||||
resourceDetails.setScope(singletonList("write"));
|
|
||||||
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
|
|
||||||
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
|
|
||||||
restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter()));
|
|
||||||
restTemplate.getAccessToken();
|
restTemplate.getAccessToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenAccessTokenIsRequestedByClientWithWriteScope_ThenAccessTokenIsNotNull() {
|
public void givenOAuth2Context_whenAccessTokenIsRequestedByClientWithWriteScope_ThenAccessTokenIsNotNull() {
|
||||||
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails();
|
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung-admin", singletonList("write"));
|
||||||
resourceDetails.setClientId("baeldung-admin");
|
OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);
|
||||||
resourceDetails.setClientSecret("baeldung");
|
|
||||||
resourceDetails.setScope(singletonList("write"));
|
|
||||||
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
|
|
||||||
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
|
|
||||||
restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter()));
|
|
||||||
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
|
|
||||||
assertNotNull(accessToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ClientCredentialsResourceDetails getClientCredentialsResourceDetails() {
|
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
|
||||||
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
|
|
||||||
resourceDetails.setAccessTokenUri(format("http://localhost:%d/oauth/token", port));
|
assertNotNull(accessToken);
|
||||||
resourceDetails.setGrantType("client_credentials");
|
|
||||||
return resourceDetails;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,40 +2,28 @@ package com.baeldung.springbootsecurity.oauth2server;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
||||||
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
|
|
||||||
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||||
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
|
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
|
||||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
import static java.util.Collections.singletonList;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootAuthorizationServerApplication.class,
|
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootAuthorizationServerApplication.class,
|
||||||
properties = { "security.oauth2.client.client-id=client", "security.oauth2.client.client-secret=secret" })
|
properties = { "security.oauth2.client.client-id=client", "security.oauth2.client.client-secret=baeldung" })
|
||||||
public class DefaultConfigAuthorizationServerIntegrationTest {
|
public class DefaultConfigAuthorizationServerIntegrationTest extends OAuth2IntegrationTestSupport {
|
||||||
|
|
||||||
@Value("${local.server.port}") protected int port;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() {
|
public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() {
|
||||||
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
|
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("client", asList("read", "write"));
|
||||||
resourceDetails.setAccessTokenUri(format("http://localhost:%d/oauth/token", port));
|
OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);
|
||||||
resourceDetails.setClientId("client");
|
|
||||||
resourceDetails.setClientSecret("secret");
|
|
||||||
resourceDetails.setGrantType("client_credentials");
|
|
||||||
resourceDetails.setScope(asList("read", "write"));
|
|
||||||
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
|
|
||||||
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
|
|
||||||
restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter()));
|
|
||||||
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
|
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
|
||||||
|
|
||||||
assertNotNull(accessToken);
|
assertNotNull(accessToken);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.baeldung.springbootsecurity.oauth2server;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
|
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
|
||||||
|
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||||
|
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static java.lang.String.format;
|
||||||
|
import static java.util.Collections.singletonList;
|
||||||
|
|
||||||
|
public class OAuth2IntegrationTestSupport {
|
||||||
|
|
||||||
|
@Value("${local.server.port}") protected int port;
|
||||||
|
|
||||||
|
protected ClientCredentialsResourceDetails getClientCredentialsResourceDetails(final String clientId, final List<String> scopes) {
|
||||||
|
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
|
||||||
|
resourceDetails.setAccessTokenUri(format("http://localhost:%d/oauth/token", port));
|
||||||
|
resourceDetails.setClientId(clientId);
|
||||||
|
resourceDetails.setClientSecret("baeldung");
|
||||||
|
resourceDetails.setScope(scopes);
|
||||||
|
resourceDetails.setGrantType("client_credentials");
|
||||||
|
return resourceDetails;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected OAuth2RestTemplate getOAuth2RestTemplate(final ClientCredentialsResourceDetails resourceDetails) {
|
||||||
|
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
|
||||||
|
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
|
||||||
|
restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter()));
|
||||||
|
return restTemplate;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue