Skip to content

Commit 187c76e

Browse files
committed
Update Tests in oauth2webclient Sample
Issue gh-7886
1 parent c367378 commit 187c76e

7 files changed

+230
-19
lines changed

samples/boot/oauth2webclient/spring-security-samples-boot-oauth2webclient.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ dependencies {
1212

1313
testCompile project(':spring-security-test')
1414
testCompile 'org.springframework.boot:spring-boot-starter-test'
15+
testCompile 'com.squareup.okhttp3:mockwebserver'
1516
}

samples/boot/oauth2webclient/src/main/java/sample/config/WebClientConfig.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,14 @@
1616

1717
package sample.config;
1818

19+
import org.springframework.beans.factory.annotation.Value;
1920
import org.springframework.context.annotation.Bean;
2021
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
2123
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider;
2224
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder;
2325
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
2426
import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager;
25-
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
2627
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
2728
import org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction;
2829
import org.springframework.web.reactive.function.client.WebClient;
@@ -34,12 +35,15 @@
3435
@Configuration
3536
public class WebClientConfig {
3637

38+
@Value("${resource-uri}") String resourceUri;
39+
3740
@Bean
3841
WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
3942
ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2 =
4043
new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
4144
oauth2.setDefaultOAuth2AuthorizedClient(true);
4245
return WebClient.builder()
46+
.baseUrl(this.resourceUri)
4347
.apply(oauth2.oauth2Configuration())
4448
.build();
4549
}

samples/boot/oauth2webclient/src/main/java/sample/web/OAuth2WebClientController.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
*/
1616
package sample.web;
1717

18-
import org.springframework.beans.factory.annotation.Value;
1918
import org.springframework.stereotype.Controller;
2019
import org.springframework.ui.Model;
2120
import org.springframework.web.bind.annotation.GetMapping;
@@ -33,18 +32,14 @@
3332
public class OAuth2WebClientController {
3433
private final WebClient webClient;
3534

36-
private final String uri;
37-
38-
public OAuth2WebClientController(WebClient webClient, @Value("${resource-uri}") String uri) {
35+
public OAuth2WebClientController(WebClient webClient) {
3936
this.webClient = webClient;
40-
this.uri = uri;
4137
}
4238

4339
@GetMapping("/explicit")
4440
String explicit(Model model) {
4541
String body = this.webClient
4642
.get()
47-
.uri(this.uri)
4843
.attributes(clientRegistrationId("client-id"))
4944
.retrieve()
5045
.bodyToMono(String.class)
@@ -57,7 +52,6 @@ String explicit(Model model) {
5752
String implicit(Model model) {
5853
String body = this.webClient
5954
.get()
60-
.uri(this.uri)
6155
.retrieve()
6256
.bodyToMono(String.class)
6357
.block();

samples/boot/oauth2webclient/src/main/java/sample/web/RegisteredOAuth2AuthorizedClientController.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
*/
1616
package sample.web;
1717

18-
import org.springframework.beans.factory.annotation.Value;
1918
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
2019
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
2120
import org.springframework.stereotype.Controller;
@@ -35,18 +34,14 @@
3534
public class RegisteredOAuth2AuthorizedClientController {
3635
private final WebClient webClient;
3736

38-
private final String uri;
39-
40-
public RegisteredOAuth2AuthorizedClientController(WebClient webClient, @Value("${resource-uri}") String uri) {
37+
public RegisteredOAuth2AuthorizedClientController(WebClient webClient) {
4138
this.webClient = webClient;
42-
this.uri = uri;
4339
}
4440

4541
@GetMapping("/explicit")
4642
String explicit(Model model, @RegisteredOAuth2AuthorizedClient("client-id") OAuth2AuthorizedClient authorizedClient) {
4743
String body = this.webClient
4844
.get()
49-
.uri(this.uri)
5045
.attributes(oauth2AuthorizedClient(authorizedClient))
5146
.retrieve()
5247
.bodyToMono(String.class)
@@ -59,7 +54,6 @@ String explicit(Model model, @RegisteredOAuth2AuthorizedClient("client-id") OAut
5954
String implicit(Model model, @RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient) {
6055
String body = this.webClient
6156
.get()
62-
.uri(this.uri)
6357
.attributes(oauth2AuthorizedClient(authorizedClient))
6458
.retrieve()
6559
.bodyToMono(String.class)

samples/boot/oauth2webclient/src/test/java/sample/OAuth2WebClientApplicationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.junit.Test;
2020
import org.junit.runner.RunWith;
21+
2122
import org.springframework.beans.factory.annotation.Autowired;
2223
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
2324
import org.springframework.boot.test.context.SpringBootTest;
@@ -34,7 +35,6 @@
3435
@AutoConfigureMockMvc
3536
@RunWith(SpringRunner.class)
3637
public class OAuth2WebClientApplicationTests {
37-
3838
@Autowired
3939
private MockMvc mockMvc;
4040

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2002-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import okhttp3.mockwebserver.MockResponse;
20+
import okhttp3.mockwebserver.MockWebServer;
21+
import org.junit.AfterClass;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import sample.config.SecurityConfig;
25+
import sample.web.OAuth2WebClientController;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
29+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
30+
import org.springframework.boot.test.mock.mockito.MockBean;
31+
import org.springframework.context.annotation.Bean;
32+
import org.springframework.context.annotation.Configuration;
33+
import org.springframework.context.annotation.Import;
34+
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
35+
import org.springframework.security.oauth2.client.web.HttpSessionOAuth2AuthorizedClientRepository;
36+
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
37+
import org.springframework.test.context.junit4.SpringRunner;
38+
import org.springframework.test.web.servlet.MockMvc;
39+
import org.springframework.web.reactive.function.client.WebClient;
40+
41+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oauth2Client;
42+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oauth2Login;
43+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
44+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
45+
46+
@WebMvcTest
47+
@Import({ SecurityConfig.class, OAuth2WebClientController.class })
48+
@AutoConfigureMockMvc
49+
@RunWith(SpringRunner.class)
50+
public class OAuth2WebClientControllerTests {
51+
private static MockWebServer web = new MockWebServer();
52+
53+
@Autowired
54+
private MockMvc mockMvc;
55+
56+
@MockBean
57+
ClientRegistrationRepository clientRegistrationRepository;
58+
59+
@AfterClass
60+
public static void shutdown() throws Exception {
61+
web.shutdown();
62+
}
63+
64+
@Test
65+
public void explicitWhenAuthenticatedThenUsesClientIdRegistration() throws Exception {
66+
web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
67+
this.mockMvc.perform(get("/webclient/explicit")
68+
.with(oauth2Login())
69+
.with(oauth2Client("client-id")))
70+
.andExpect(status().isOk());
71+
}
72+
73+
@Test
74+
public void implicitWhenAuthenticatedThenUsesDefaultRegistration() throws Exception {
75+
web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
76+
this.mockMvc.perform(get("/webclient/implicit")
77+
.with(oauth2Login()))
78+
.andExpect(status().isOk());
79+
}
80+
81+
@Test
82+
public void publicExplicitWhenAuthenticatedThenUsesClientIdRegistration() throws Exception {
83+
web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
84+
this.mockMvc.perform(get("/public/webclient/explicit")
85+
.with(oauth2Client("client-id")))
86+
.andExpect(status().isOk());
87+
}
88+
89+
@Test
90+
public void publicImplicitWhenAuthenticatedThenUsesDefaultRegistration() throws Exception {
91+
web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
92+
this.mockMvc.perform(get("/public/webclient/implicit")
93+
.with(oauth2Login()))
94+
.andExpect(status().isOk());
95+
}
96+
97+
@Configuration
98+
static class WebClientConfig {
99+
@Bean
100+
WebClient web() {
101+
return WebClient.create(web.url("/").toString());
102+
}
103+
104+
@Bean
105+
OAuth2AuthorizedClientRepository authorizedClientRepository() {
106+
return new HttpSessionOAuth2AuthorizedClientRepository();
107+
}
108+
}
109+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2002-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import okhttp3.mockwebserver.MockResponse;
20+
import okhttp3.mockwebserver.MockWebServer;
21+
import org.junit.AfterClass;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import sample.config.SecurityConfig;
25+
import sample.web.RegisteredOAuth2AuthorizedClientController;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
29+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
30+
import org.springframework.boot.test.mock.mockito.MockBean;
31+
import org.springframework.context.annotation.Bean;
32+
import org.springframework.context.annotation.Configuration;
33+
import org.springframework.context.annotation.Import;
34+
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
35+
import org.springframework.security.oauth2.client.web.HttpSessionOAuth2AuthorizedClientRepository;
36+
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
37+
import org.springframework.test.context.junit4.SpringRunner;
38+
import org.springframework.test.web.servlet.MockMvc;
39+
import org.springframework.web.reactive.function.client.WebClient;
40+
41+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oauth2Client;
42+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oauth2Login;
43+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
44+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
45+
46+
@WebMvcTest
47+
@Import({ SecurityConfig.class, RegisteredOAuth2AuthorizedClientController.class })
48+
@AutoConfigureMockMvc
49+
@RunWith(SpringRunner.class)
50+
public class RegisteredOAuth2AuthorizedClientControllerTests {
51+
private static MockWebServer web = new MockWebServer();
52+
53+
@Autowired
54+
private MockMvc mockMvc;
55+
56+
@MockBean
57+
ClientRegistrationRepository clientRegistrationRepository;
58+
59+
@AfterClass
60+
public static void shutdown() throws Exception {
61+
web.shutdown();
62+
}
63+
64+
@Test
65+
public void annotationExplicitWhenAuthenticatedThenUsesClientIdRegistration() throws Exception {
66+
web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
67+
this.mockMvc.perform(get("/annotation/explicit")
68+
.with(oauth2Login())
69+
.with(oauth2Client("client-id")))
70+
.andExpect(status().isOk());
71+
}
72+
73+
@Test
74+
public void annotationImplicitWhenAuthenticatedThenUsesDefaultRegistration() throws Exception {
75+
web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
76+
this.mockMvc.perform(get("/annotation/implicit")
77+
.with(oauth2Login()))
78+
.andExpect(status().isOk());
79+
}
80+
81+
@Test
82+
public void publicAnnotationExplicitWhenAuthenticatedThenUsesClientIdRegistration() throws Exception {
83+
web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
84+
this.mockMvc.perform(get("/public/annotation/explicit")
85+
.with(oauth2Client("client-id")))
86+
.andExpect(status().isOk());
87+
}
88+
89+
@Test
90+
public void publicAnnotationImplicitWhenAuthenticatedThenUsesDefaultRegistration() throws Exception {
91+
web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
92+
this.mockMvc.perform(get("/public/annotation/implicit")
93+
.with(oauth2Login()))
94+
.andExpect(status().isOk());
95+
}
96+
97+
@Configuration
98+
static class WebClientConfig {
99+
@Bean
100+
WebClient web() {
101+
return WebClient.create(web.url("/").toString());
102+
}
103+
104+
@Bean
105+
OAuth2AuthorizedClientRepository authorizedClientRepository() {
106+
return new HttpSessionOAuth2AuthorizedClientRepository();
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)