Skip to content

Commit f888a3f

Browse files
BogdanStoeanpivovarit
authored andcommitted
BAEL-1410 - refactor tests (eugenp#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
1 parent 0d85d1a commit f888a3f

4 files changed

Lines changed: 58 additions & 57 deletions

File tree

‎spring-boot-security/src/test/java/com/baeldung/springbootsecurity/basic_auth/BasicAuthConfigurationIntegrationTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public void setUp() throws MalformedURLException {
3737
@Test
3838
public void whenLoggedUserRequestsHomePage_ThenSuccess() throws IllegalStateException, IOException {
3939
ResponseEntity<String> response = restTemplate.getForEntity(base.toString(), String.class);
40+
4041
assertEquals(HttpStatus.OK, response.getStatusCode());
4142
assertTrue(response
4243
.getBody()
@@ -47,6 +48,7 @@ public void whenLoggedUserRequestsHomePage_ThenSuccess() throws IllegalStateExce
4748
public void whenUserWithWrongCredentialsRequestsHomePage_ThenUnauthorizedPage() throws IllegalStateException, IOException {
4849
restTemplate = new TestRestTemplate("user", "wrongpassword");
4950
ResponseEntity<String> response = restTemplate.getForEntity(base.toString(), String.class);
51+
5052
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
5153
assertTrue(response
5254
.getBody()

‎spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/CustomConfigAuthorizationServerIntegrationTest.java‎

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,50 @@
22

33
import org.junit.Test;
44
import org.junit.runner.RunWith;
5-
import org.springframework.beans.factory.annotation.Value;
65
import org.springframework.boot.test.context.SpringBootTest;
7-
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
8-
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
96
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
107
import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException;
118
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
129
import org.springframework.security.oauth2.common.OAuth2AccessToken;
1310
import org.springframework.test.context.ActiveProfiles;
1411
import org.springframework.test.context.junit4.SpringRunner;
1512

16-
import static java.lang.String.format;
1713
import static java.util.Collections.singletonList;
1814
import static org.junit.Assert.assertNotNull;
1915
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
2016

2117
@RunWith(SpringRunner.class)
2218
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootAuthorizationServerApplication.class)
2319
@ActiveProfiles("authz")
24-
public class CustomConfigAuthorizationServerIntegrationTest {
25-
26-
@Value("${local.server.port}") protected int port;
20+
public class CustomConfigAuthorizationServerIntegrationTest extends OAuth2IntegrationTestSupport {
2721

2822
@Test
29-
public void whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() {
30-
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails();
31-
resourceDetails.setClientId("baeldung");
32-
resourceDetails.setClientSecret("baeldung");
33-
resourceDetails.setScope(singletonList("read"));
34-
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
35-
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
36-
restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter()));
23+
public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() {
24+
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("read"));
25+
OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);
26+
3727
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
28+
3829
assertNotNull(accessToken);
3930

4031
}
4132

4233
@Test(expected = OAuth2AccessDeniedException.class)
43-
public void whenAccessTokenIsRequestedWithInvalidException_ThenExceptionIsThrown() {
44-
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails();
45-
resourceDetails.setClientId("baeldung");
46-
resourceDetails.setClientSecret("baeldung");
47-
resourceDetails.setScope(singletonList("write"));
48-
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
49-
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
50-
restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter()));
34+
public void givenOAuth2Context_whenAccessTokenIsRequestedWithInvalidException_ThenExceptionIsThrown() {
35+
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("write"));
36+
OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);
37+
5138
restTemplate.getAccessToken();
5239
}
5340

5441
@Test
55-
public void whenAccessTokenIsRequestedByClientWithWriteScope_ThenAccessTokenIsNotNull() {
56-
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails();
57-
resourceDetails.setClientId("baeldung-admin");
58-
resourceDetails.setClientSecret("baeldung");
59-
resourceDetails.setScope(singletonList("write"));
60-
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
61-
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
62-
restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter()));
42+
public void givenOAuth2Context_whenAccessTokenIsRequestedByClientWithWriteScope_ThenAccessTokenIsNotNull() {
43+
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung-admin", singletonList("write"));
44+
OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);
45+
6346
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
64-
assertNotNull(accessToken);
65-
}
6647

67-
private ClientCredentialsResourceDetails getClientCredentialsResourceDetails() {
68-
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
69-
resourceDetails.setAccessTokenUri(format("http://localhost:%d/oauth/token", port));
70-
resourceDetails.setGrantType("client_credentials");
71-
return resourceDetails;
48+
assertNotNull(accessToken);
7249
}
7350

7451
}

‎spring-boot-security/src/test/java/com/baeldung/springbootsecurity/oauth2server/DefaultConfigAuthorizationServerIntegrationTest.java‎

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,28 @@
22

33
import org.junit.Test;
44
import org.junit.runner.RunWith;
5-
import org.springframework.beans.factory.annotation.Value;
65
import org.springframework.boot.test.context.SpringBootTest;
7-
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
8-
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
96
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
107
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
118
import org.springframework.security.oauth2.common.OAuth2AccessToken;
129
import org.springframework.test.context.junit4.SpringRunner;
1310

14-
import static java.lang.String.format;
1511
import static java.util.Arrays.asList;
16-
import static java.util.Collections.singletonList;
1712
import static org.junit.Assert.assertNotNull;
1813
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
1914

2015
@RunWith(SpringRunner.class)
2116
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootAuthorizationServerApplication.class,
22-
properties = { "security.oauth2.client.client-id=client", "security.oauth2.client.client-secret=secret" })
23-
public class DefaultConfigAuthorizationServerIntegrationTest {
24-
25-
@Value("${local.server.port}") protected int port;
17+
properties = { "security.oauth2.client.client-id=client", "security.oauth2.client.client-secret=baeldung" })
18+
public class DefaultConfigAuthorizationServerIntegrationTest extends OAuth2IntegrationTestSupport {
2619

2720
@Test
28-
public void whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() {
29-
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
30-
resourceDetails.setAccessTokenUri(format("http://localhost:%d/oauth/token", port));
31-
resourceDetails.setClientId("client");
32-
resourceDetails.setClientSecret("secret");
33-
resourceDetails.setGrantType("client_credentials");
34-
resourceDetails.setScope(asList("read", "write"));
35-
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
36-
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
37-
restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter()));
21+
public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() {
22+
ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("client", asList("read", "write"));
23+
OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);
24+
3825
OAuth2AccessToken accessToken = restTemplate.getAccessToken();
26+
3927
assertNotNull(accessToken);
4028

4129
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.springbootsecurity.oauth2server;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
5+
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
6+
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
7+
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
8+
9+
import java.util.List;
10+
11+
import static java.lang.String.format;
12+
import static java.util.Collections.singletonList;
13+
14+
public class OAuth2IntegrationTestSupport {
15+
16+
@Value("${local.server.port}") protected int port;
17+
18+
protected ClientCredentialsResourceDetails getClientCredentialsResourceDetails(final String clientId, final List<String> scopes) {
19+
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
20+
resourceDetails.setAccessTokenUri(format("http://localhost:%d/oauth/token", port));
21+
resourceDetails.setClientId(clientId);
22+
resourceDetails.setClientSecret("baeldung");
23+
resourceDetails.setScope(scopes);
24+
resourceDetails.setGrantType("client_credentials");
25+
return resourceDetails;
26+
}
27+
28+
protected OAuth2RestTemplate getOAuth2RestTemplate(final ClientCredentialsResourceDetails resourceDetails) {
29+
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
30+
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
31+
restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter()));
32+
return restTemplate;
33+
}
34+
}

0 commit comments

Comments
 (0)