1
1
package sample ;
2
2
3
- import java .sql .Types ;
4
3
import java .util .Base64 ;
5
- import java .util .List ;
6
- import java .util .Map ;
7
4
8
5
import com .fasterxml .jackson .databind .ObjectMapper ;
9
6
import jakarta .servlet .http .Cookie ;
10
- import org .assertj .core .api .Assertions ;
11
7
import org .junit .jupiter .api .BeforeEach ;
12
8
import org .junit .jupiter .api .Test ;
13
9
14
10
import org .springframework .beans .factory .annotation .Autowired ;
11
+ import org .springframework .boot .autoconfigure .security .SecurityProperties ;
15
12
import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
16
13
import org .springframework .boot .test .context .SpringBootTest ;
17
14
import org .springframework .context .annotation .Import ;
@@ -40,6 +37,13 @@ class JdbcJsonAttributeTests {
40
37
@ Autowired
41
38
JdbcClient jdbcClient ;
42
39
40
+ String username ;
41
+
42
+ @ Autowired
43
+ void setSecurityProperties (SecurityProperties securityProperties ) {
44
+ this .username = securityProperties .getUser ().getName ();
45
+ }
46
+
43
47
@ BeforeEach
44
48
void setup () {
45
49
ObjectMapper copy = this .objectMapper .copy ();
@@ -51,7 +55,7 @@ void setup() {
51
55
52
56
@ Test
53
57
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" ))
55
59
.andExpect (authenticated ())
56
60
.andReturn ()
57
61
.getResponse ()
@@ -66,20 +70,20 @@ void loginShouldSaveSecurityContextAsJson() throws Exception {
66
70
SecurityContext securityContext = this .objectMapperWithModules .readValue ((String ) attributeBytes ,
67
71
SecurityContext .class );
68
72
assertThat (securityContext ).isNotNull ();
69
- assertThat (securityContext .getAuthentication ().getName ()).isEqualTo ("rüdiger" );
73
+ assertThat (securityContext .getAuthentication ().getName ()).isEqualTo (this . username );
70
74
}
71
75
72
76
@ Test
73
77
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 ());
75
79
Object attributeBytes = this .jdbcClient .sql ("""
76
80
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 ();
79
83
SecurityContext securityContext = this .objectMapperWithModules .readValue ((String ) attributeBytes ,
80
84
SecurityContext .class );
81
85
assertThat (securityContext ).isNotNull ();
82
- assertThat (securityContext .getAuthentication ().getName ()).isEqualTo ("rüdiger" );
86
+ assertThat (securityContext .getAuthentication ().getName ()).isEqualTo (this . username );
83
87
}
84
88
85
89
}
0 commit comments