Skip to content

Commit c33af51

Browse files
committed
SAML2 autoconfiguration is not imported by @WebMvcTest
Signed-off-by: Dmytro Nosan <[email protected]>
1 parent d98ca77 commit c33af51

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

spring-boot-project/spring-boot-test-autoconfigure/build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,19 @@ dependencies {
114114
testImplementation("org.junit.platform:junit-platform-launcher")
115115
testImplementation("org.mockito:mockito-core")
116116
testImplementation("org.mockito:mockito-junit-jupiter")
117+
testImplementation("org.opensaml:opensaml-core:4.0.1")
118+
testImplementation("org.opensaml:opensaml-saml-api:4.0.1")
119+
testImplementation("org.opensaml:opensaml-saml-impl:4.0.1")
117120
testImplementation("org.skyscreamer:jsonassert")
118121
testImplementation("org.springframework:spring-core-test")
122+
testImplementation("org.springframework.boot:spring-boot-starter-oauth2-client")
119123
testImplementation("org.springframework.hateoas:spring-hateoas")
120124
testImplementation("org.springframework.plugin:spring-plugin-core")
125+
testImplementation("org.springframework.security:spring-security-saml2-service-provider") {
126+
exclude group: "org.opensaml", module: "opensaml-core"
127+
exclude group: "org.opensaml", module: "opensaml-saml-api"
128+
exclude group: "org.opensaml", module: "opensaml-saml-impl"
129+
}
121130
testImplementation("org.thymeleaf:thymeleaf")
122131
}
123132

spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc.imports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebClientAutoConf
44
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebDriverAutoConfiguration
55
org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration
66
org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration
7+
org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration
78
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
89
org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration
910
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2012-2025 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 org.springframework.boot.test.autoconfigure.web.servlet.mockmvc;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
23+
import org.springframework.security.test.context.support.WithAnonymousUser;
24+
import org.springframework.test.web.servlet.MockMvc;
25+
26+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29+
30+
/**
31+
* Tests for {@link WebMvcTest @WebMvcTest} with OAuth2.
32+
*
33+
* @author Dmytro Nosan
34+
*/
35+
@WebMvcTest(controllers = ExampleController1.class,
36+
properties = { "spring.security.oauth2.client.registration.test.client-id=test",
37+
"spring.security.oauth2.client.registration.test.authorization-grant-type=authorization-code",
38+
"spring.security.oauth2.client.provider.test.authorization-uri=https://auth.example.org" })
39+
class WebMvcTestOAuth2Tests {
40+
41+
@Autowired
42+
private MockMvc mockMvc;
43+
44+
@Test
45+
@WithAnonymousUser
46+
void loginShouldBeAuthorized() throws Exception {
47+
this.mockMvc.perform(get("/one")).andExpect(status().isFound()).andExpect(redirectedUrlPattern("**/login"));
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2025 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 org.springframework.boot.test.autoconfigure.web.servlet.mockmvc;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
23+
import org.springframework.security.test.context.support.WithAnonymousUser;
24+
import org.springframework.test.web.servlet.MockMvc;
25+
26+
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29+
30+
/**
31+
* Tests for {@link WebMvcTest @WebMvcTest} with SAML2.
32+
*
33+
* @author Dmytro Nosan
34+
*/
35+
@WebMvcTest(controllers = ExampleController1.class, properties = {
36+
"spring.security.saml2.relyingparty.registration.test.entity-id=relyingparty",
37+
"spring.security.saml2.relyingparty.registration.test.assertingparty.entity-id=assertingparty",
38+
"spring.security.saml2.relyingparty.registration.test.assertingparty.singlesignon.url=https://example.com",
39+
"spring.security.saml2.relyingparty.registration.test.assertingparty.singlesignon.sign-request=false" })
40+
class WebMvcTestSaml2Tests {
41+
42+
@Autowired
43+
private MockMvc mockMvc;
44+
45+
@Test
46+
@WithAnonymousUser
47+
void loginShouldBeAuthorized() throws Exception {
48+
this.mockMvc.perform(get("/one"))
49+
.andExpect(status().isFound())
50+
.andExpect(redirectedUrlPattern("**/saml2/authenticate?registrationId=test"));
51+
}
52+
53+
}

0 commit comments

Comments
 (0)