11package sample ;
22
3- import java .sql .Types ;
43import java .util .Base64 ;
5- import java .util .List ;
6- import java .util .Map ;
74
85import com .fasterxml .jackson .databind .ObjectMapper ;
96import jakarta .servlet .http .Cookie ;
10- import org .assertj .core .api .Assertions ;
117import org .junit .jupiter .api .BeforeEach ;
128import org .junit .jupiter .api .Test ;
139
1410import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .boot .autoconfigure .security .SecurityProperties ;
1512import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
1613import org .springframework .boot .test .context .SpringBootTest ;
1714import 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}
0 commit comments