Skip to content

Commit b2e0539

Browse files
CaptainAyesjohnr
authored andcommitted
Add localization to DefaultLoginPageConfigurerTests test in order to avoid failure when system language is different
1 parent 9a9926e commit b2e0539

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

config/src/test/java/org/springframework/security/config/annotation/web/configurers/DefaultLoginPageConfigurerTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import org.springframework.beans.factory.annotation.Autowired;
2323
import org.springframework.context.annotation.Bean;
2424
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.context.support.MessageSourceAccessor;
2526
import org.springframework.mock.web.MockHttpSession;
2627
import org.springframework.security.config.ObjectPostProcessor;
2728
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2829
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
2930
import org.springframework.security.config.test.SpringTestContext;
3031
import org.springframework.security.config.test.SpringTestContextExtension;
32+
import org.springframework.security.core.SpringSecurityMessageSource;
3133
import org.springframework.security.core.userdetails.PasswordEncodedUser;
3234
import org.springframework.security.core.userdetails.UserDetailsService;
3335
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@@ -75,6 +77,8 @@ public class DefaultLoginPageConfigurerTests {
7577
@Autowired
7678
MockMvc mvc;
7779

80+
MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
81+
7882
@Test
7983
public void getWhenFormLoginEnabledThenRedirectsToLoginPage() throws Exception {
8084
this.spring.register(DefaultLoginPageConfig.class).autowire();
@@ -144,6 +148,8 @@ public void loginPageWhenErrorThenDefaultLoginPageWithError() throws Exception {
144148
this.mvc.perform(get("/login?error").session((MockHttpSession) mvcResult.getRequest().getSession())
145149
.sessionAttr(csrfAttributeName, csrfToken))
146150
.andExpect((result) -> {
151+
String badCredentialsLocalizedMessage = this.messages
152+
.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials");
147153
CsrfToken token = (CsrfToken) result.getRequest().getAttribute(CsrfToken.class.getName());
148154
assertThat(result.getResponse().getContentAsString()).isEqualTo("""
149155
<!DOCTYPE html>
@@ -160,7 +166,7 @@ public void loginPageWhenErrorThenDefaultLoginPageWithError() throws Exception {
160166
<div class="content">
161167
<form class="login-form" method="post" action="/login">
162168
<h2>Please sign in</h2>
163-
<div class="alert alert-danger" role="alert">Bad credentials</div>
169+
<div class="alert alert-danger" role="alert">%s</div>
164170
<p>
165171
<label for="username" class="screenreader">Username</label>
166172
<input type="text" id="username" name="username" placeholder="Username" required autofocus>
@@ -178,7 +184,7 @@ public void loginPageWhenErrorThenDefaultLoginPageWithError() throws Exception {
178184
179185
</div>
180186
</body>
181-
</html>""".formatted(token.getToken()));
187+
</html>""".formatted(badCredentialsLocalizedMessage, token.getToken()));
182188
});
183189
// @formatter:on
184190
}

0 commit comments

Comments
 (0)