Skip to content

Commit 6fd52ca

Browse files
committed
Extract JdbcJsonSpecialCharsAttributeTests
Signed-off-by: Rob Winch <[email protected]>
1 parent d295bff commit 6fd52ca

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
spring.security.user.name=rüdiger
21
spring.security.user.password=password
32
spring.session.jdbc.schema=classpath:schema.sql
43
spring.session.jdbc.initialize-schema=always

spring-session-samples/spring-session-sample-boot-jdbc-json-attribute/src/test/java/sample/JdbcJsonAttributeTests.java

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package sample;
22

3-
import java.sql.Types;
43
import java.util.Base64;
5-
import java.util.List;
6-
import java.util.Map;
74

85
import com.fasterxml.jackson.databind.ObjectMapper;
96
import jakarta.servlet.http.Cookie;
10-
import org.assertj.core.api.Assertions;
117
import org.junit.jupiter.api.BeforeEach;
128
import org.junit.jupiter.api.Test;
139

1410
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.autoconfigure.security.SecurityProperties;
1512
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1613
import org.springframework.boot.test.context.SpringBootTest;
1714
import org.springframework.context.annotation.Import;
@@ -40,6 +37,13 @@ class JdbcJsonAttributeTests {
4037
@Autowired
4138
JdbcClient jdbcClient;
4239

40+
String username;
41+
42+
@Autowired
43+
void setSecurityProperties(SecurityProperties securityProperties) {
44+
this.username = securityProperties.getUser().getName();
45+
}
46+
4347
@BeforeEach
4448
void setup() {
4549
ObjectMapper copy = this.objectMapper.copy();
@@ -51,7 +55,7 @@ void setup() {
5155

5256
@Test
5357
void loginShouldSaveSecurityContextAsJson() throws Exception {
54-
Cookie sessionCookie = this.mvc.perform(formLogin().user("rüdiger").password("password"))
58+
Cookie sessionCookie = this.mvc.perform(formLogin().user(this.username).password("password"))
5559
.andExpect(authenticated())
5660
.andReturn()
5761
.getResponse()
@@ -66,20 +70,20 @@ void loginShouldSaveSecurityContextAsJson() throws Exception {
6670
SecurityContext securityContext = this.objectMapperWithModules.readValue((String) attributeBytes,
6771
SecurityContext.class);
6872
assertThat(securityContext).isNotNull();
69-
assertThat(securityContext.getAuthentication().getName()).isEqualTo("rüdiger");
73+
assertThat(securityContext.getAuthentication().getName()).isEqualTo(this.username);
7074
}
7175

7276
@Test
7377
void loginWhenQueryUsingJsonbOperatorThenReturns() throws Exception {
74-
this.mvc.perform(formLogin().user("rüdiger").password("password")).andExpect(authenticated());
78+
this.mvc.perform(formLogin().user(this.username).password("password")).andExpect(authenticated());
7579
Object attributeBytes = this.jdbcClient.sql("""
7680
SELECT attribute_bytes::text FROM spring_session_attributes
77-
WHERE attribute_bytes -> 'authentication' -> 'principal' ->> 'username' = 'rüdiger'
78-
""").query().singleValue();
81+
WHERE attribute_bytes -> 'authentication' -> 'principal' ->> 'username' = '%s'
82+
""".formatted(this.username)).query().singleValue();
7983
SecurityContext securityContext = this.objectMapperWithModules.readValue((String) attributeBytes,
8084
SecurityContext.class);
8185
assertThat(securityContext).isNotNull();
82-
assertThat(securityContext.getAuthentication().getName()).isEqualTo("rüdiger");
86+
assertThat(securityContext.getAuthentication().getName()).isEqualTo(this.username);
8387
}
8488

8589
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2014-2022 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 org.springframework.test.context.TestPropertySource;
20+
21+
/**
22+
* Test that special characters in usernames work.
23+
*
24+
* @author Rob Winch
25+
*/
26+
@TestPropertySource(properties = "spring.security.user.name=rüdiger")
27+
class JdbcJsonSpecialCharsAttributeTests extends JdbcJsonAttributeTests {
28+
29+
}

0 commit comments

Comments
 (0)