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
@@ -51,11 +47,20 @@ void setup() {
5147
5248 @ Test
5349 void loginShouldSaveSecurityContextAsJson () throws Exception {
54- Cookie sessionCookie = this .mvc .perform (formLogin ().user ("rüdiger" ).password ("password" ))
55- .andExpect (authenticated ())
56- .andReturn ()
57- .getResponse ()
58- .getCookie ("SESSION" );
50+ loginShouldSaveSecurityContextAsJson ("user" );
51+ }
52+
53+ @ Test
54+ void loginShouldSaveSecurityContextAsJsonWhenSpecialCharacters () throws Exception {
55+ loginShouldSaveSecurityContextAsJson ("rüdiger" );
56+ }
57+
58+ private void loginShouldSaveSecurityContextAsJson (String username ) throws Exception {
59+ Cookie sessionCookie = this .mvc .perform (formLogin ().user (username ).password ("password" ))
60+ .andExpect (authenticated ())
61+ .andReturn ()
62+ .getResponse ()
63+ .getCookie ("SESSION" );
5964 String sessionId = new String (Base64 .getDecoder ().decode (sessionCookie .getValue ()));
6065 Object attributeBytes = this .jdbcClient .sql ("""
6166 SELECT attribute_bytes::text FROM spring_session_attributes
@@ -66,20 +71,29 @@ void loginShouldSaveSecurityContextAsJson() throws Exception {
6671 SecurityContext securityContext = this .objectMapperWithModules .readValue ((String ) attributeBytes ,
6772 SecurityContext .class );
6873 assertThat (securityContext ).isNotNull ();
69- assertThat (securityContext .getAuthentication ().getName ()).isEqualTo ("rüdiger" );
74+ assertThat (securityContext .getAuthentication ().getName ()).isEqualTo (username );
7075 }
7176
7277 @ Test
7378 void loginWhenQueryUsingJsonbOperatorThenReturns () throws Exception {
74- this .mvc .perform (formLogin ().user ("rüdiger" ).password ("password" )).andExpect (authenticated ());
79+ loginWhenQueryUsingJsonbOperatorThenReturns ("user" );
80+ }
81+
82+ @ Test
83+ void loginWhenQueryUsingJsonbOperatorThenReturnsWhenSpecialCharacters () throws Exception {
84+ loginWhenQueryUsingJsonbOperatorThenReturns ("rüdiger" );
85+ }
86+
87+ private void loginWhenQueryUsingJsonbOperatorThenReturns (String username ) throws Exception {
88+ this .mvc .perform (formLogin ().user (username ).password ("password" )).andExpect (authenticated ());
7589 Object attributeBytes = this .jdbcClient .sql ("""
7690 SELECT attribute_bytes::text FROM spring_session_attributes
77- WHERE attribute_bytes -> 'authentication' -> 'principal' ->> 'username' = 'rüdiger '
78- """ ).query ().singleValue ();
91+ WHERE attribute_bytes -> 'authentication' -> 'principal' ->> 'username' = '%s '
92+ """ . formatted ( username ) ).query ().singleValue ();
7993 SecurityContext securityContext = this .objectMapperWithModules .readValue ((String ) attributeBytes ,
8094 SecurityContext .class );
8195 assertThat (securityContext ).isNotNull ();
82- assertThat (securityContext .getAuthentication ().getName ()).isEqualTo ("rüdiger" );
96+ assertThat (securityContext .getAuthentication ().getName ()).isEqualTo (username );
8397 }
8498
8599}
0 commit comments