Skip to content

Commit 43a071a

Browse files
Ihor Ilkevychjgrandja
Ihor Ilkevych
authored andcommitted
Add WebFlux oauth2Login with formLogin test
Closes gh-9326
1 parent ad4497f commit 43a071a

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

config/src/test/java/org/springframework/security/config/web/server/FormLoginTests.java

+6-1
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-2021 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.
@@ -434,6 +434,11 @@ public DefaultLoginPage assertLoginFormNotPresent() {
434434
return this;
435435
}
436436

437+
public DefaultLoginPage assertLoginFormPresent() {
438+
loginForm().username("");
439+
return this;
440+
}
441+
437442
public LoginForm loginForm() {
438443
if (this.loginForm == null) {
439444
this.loginForm = PageFactory.initElements(this.driver, LoginForm.class);

config/src/test/java/org/springframework/security/config/web/server/OAuth2LoginTests.java

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -33,14 +33,17 @@
3333
import org.springframework.http.HttpHeaders;
3434
import org.springframework.security.authentication.ReactiveAuthenticationManager;
3535
import org.springframework.security.authentication.TestingAuthenticationToken;
36+
import org.springframework.security.authentication.UserDetailsRepositoryReactiveAuthenticationManager;
3637
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
3738
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
3839
import org.springframework.security.config.test.SpringTestRule;
40+
import org.springframework.security.config.users.ReactiveAuthenticationTestConfiguration;
3941
import org.springframework.security.core.Authentication;
4042
import org.springframework.security.core.AuthenticationException;
4143
import org.springframework.security.core.authority.AuthorityUtils;
4244
import org.springframework.security.core.context.SecurityContext;
4345
import org.springframework.security.core.context.SecurityContextImpl;
46+
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
4447
import org.springframework.security.htmlunit.server.WebTestClientHtmlUnitDriverBuilder;
4548
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
4649
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
@@ -176,6 +179,24 @@ public void defaultLoginPageWithSingleClientRegistrationThenRedirect() {
176179
assertThat(driver.getCurrentUrl()).startsWith("https://github.com/login/oauth/authorize");
177180
}
178181

182+
@Test
183+
public void defaultLoginPageWithSingleClientRegistrationAndFormLoginThenLinks() {
184+
this.spring.register(OAuth2LoginWithSingleClientRegistrations.class, OAuth2LoginWithFormLogin.class).autowire();
185+
// @formatter:off
186+
WebTestClient webTestClient = WebTestClientBuilder
187+
.bindToWebFilters(new GitHubWebFilter(), this.springSecurity)
188+
.build();
189+
WebDriver driver = WebTestClientHtmlUnitDriverBuilder
190+
.webTestClientSetup(webTestClient)
191+
.build();
192+
FormLoginTests.HomePage.to(driver, FormLoginTests.DefaultLoginPage.class)
193+
.assertAt()
194+
.assertLoginFormPresent()
195+
.oauth2Login()
196+
.assertClientRegistrationByName(OAuth2LoginTests.github.getClientName());
197+
// @formatter:on
198+
}
199+
179200
// gh-8118
180201
@Test
181202
public void defaultLoginPageWithSingleClientRegistrationAndXhrRequestThenDoesNotRedirectForAuthorization() {
@@ -584,6 +605,30 @@ String home(@RegisteredOAuth2AuthorizedClient("github") OAuth2AuthorizedClient a
584605

585606
}
586607

608+
@Configuration
609+
static class OAuth2LoginWithFormLogin {
610+
611+
@Bean
612+
SecurityWebFilterChain springSecurityFilter(ServerHttpSecurity http) {
613+
ReactiveUserDetailsService reactiveUserDetailsService = ReactiveAuthenticationTestConfiguration
614+
.userDetailsService();
615+
ReactiveAuthenticationManager authenticationManager = new UserDetailsRepositoryReactiveAuthenticationManager(
616+
reactiveUserDetailsService);
617+
http.authenticationManager(authenticationManager);
618+
// @formatter:off
619+
http
620+
.authorizeExchange()
621+
.anyExchange().authenticated()
622+
.and()
623+
.oauth2Login()
624+
.and()
625+
.formLogin();
626+
// @formatter:on
627+
return http.build();
628+
}
629+
630+
}
631+
587632
@Configuration
588633
static class OAuth2LoginMockAuthenticationManagerConfig {
589634

0 commit comments

Comments
 (0)