Skip to content

Commit 759e47b

Browse files
committed
Migrate OpenIDLoginConfigurerTests groovy->java
Issue: gh-4939
1 parent 9323d8e commit 759e47b

File tree

2 files changed

+131
-86
lines changed

2 files changed

+131
-86
lines changed

config/src/test/groovy/org/springframework/security/config/annotation/web/configurers/openid/OpenIDLoginConfigurerTests.groovy

Lines changed: 0 additions & 86 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2002-2019 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.security.config.annotation.web.configurers.openid;
18+
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.security.config.annotation.ObjectPostProcessor;
24+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
25+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
26+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
27+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
28+
import org.springframework.security.config.test.SpringTestRule;
29+
import org.springframework.security.openid.OpenIDAuthenticationFilter;
30+
import org.springframework.security.openid.OpenIDAuthenticationProvider;
31+
import org.springframework.test.web.servlet.MockMvc;
32+
33+
import static org.mockito.ArgumentMatchers.any;
34+
import static org.mockito.Mockito.spy;
35+
import static org.mockito.Mockito.verify;
36+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
37+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
38+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
39+
40+
/**
41+
* Tests for {@link OpenIDLoginConfigurer}
42+
*
43+
* @author Rob Winch
44+
* @author Eleftheria Stein
45+
*/
46+
public class OpenIDLoginConfigurerTests {
47+
48+
@Rule
49+
public final SpringTestRule spring = new SpringTestRule();
50+
51+
@Autowired
52+
MockMvc mvc;
53+
54+
@Test
55+
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnOpenIDAuthenticationFilter() {
56+
ObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class);
57+
this.spring.register(ObjectPostProcessorConfig.class).autowire();
58+
59+
verify(ObjectPostProcessorConfig.objectPostProcessor)
60+
.postProcess(any(OpenIDAuthenticationFilter.class));
61+
}
62+
63+
@Test
64+
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnOpenIDAuthenticationProvider() {
65+
ObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class);
66+
this.spring.register(ObjectPostProcessorConfig.class).autowire();
67+
68+
verify(ObjectPostProcessorConfig.objectPostProcessor)
69+
.postProcess(any(OpenIDAuthenticationProvider.class));
70+
}
71+
72+
@EnableWebSecurity
73+
static class ObjectPostProcessorConfig extends WebSecurityConfigurerAdapter {
74+
static ObjectPostProcessor<Object> objectPostProcessor;
75+
76+
@Override
77+
protected void configure(HttpSecurity http) throws Exception {
78+
// @formatter:off
79+
http
80+
.openidLogin();
81+
// @formatter:on
82+
}
83+
84+
@Bean
85+
static ObjectPostProcessor<Object> objectPostProcessor() {
86+
return objectPostProcessor;
87+
}
88+
}
89+
90+
static class ReflectingObjectPostProcessor implements ObjectPostProcessor<Object> {
91+
@Override
92+
public <O> O postProcess(O object) {
93+
return object;
94+
}
95+
}
96+
97+
@Test
98+
public void openidLoginWhenInvokedTwiceThenUsesOriginalLoginPage() throws Exception {
99+
this.spring.register(InvokeTwiceDoesNotOverrideConfig.class).autowire();
100+
101+
this.mvc.perform(get("/"))
102+
.andExpect(status().isFound())
103+
.andExpect(redirectedUrl("http://localhost/login/custom"));
104+
}
105+
106+
@EnableWebSecurity
107+
static class InvokeTwiceDoesNotOverrideConfig extends WebSecurityConfigurerAdapter {
108+
109+
@Override
110+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
111+
// @formatter:off
112+
auth
113+
.inMemoryAuthentication();
114+
// @formatter:on
115+
}
116+
117+
@Override
118+
protected void configure(HttpSecurity http) throws Exception {
119+
// @formatter:off
120+
http
121+
.authorizeRequests()
122+
.anyRequest().authenticated()
123+
.and()
124+
.openidLogin()
125+
.loginPage("/login/custom")
126+
.and()
127+
.openidLogin();
128+
// @formatter:on
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)