Skip to content

Commit 462aa50

Browse files
committed
Restore Original Tests in JdbcJsonAttributeTests
1 parent 66d3f1d commit 462aa50

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
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

@@ -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

Comments
 (0)