Extract JdbcJsonSpecialCharsAttributeTests

Signed-off-by: Rob Winch <362503+rwinch@users.noreply.github.com>
This commit is contained in:
Rob Winch
2025-04-22 11:08:42 -05:00
parent d295bff7cb
commit 6fd52ca662
3 changed files with 43 additions and 11 deletions

View File

@@ -1,4 +1,3 @@
spring.security.user.name=r<EFBFBD>diger
spring.security.user.password=password
spring.session.jdbc.schema=classpath:schema.sql
spring.session.jdbc.initialize-schema=always

View File

@@ -1,17 +1,14 @@
package sample;
import java.sql.Types;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.Cookie;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
@@ -40,6 +37,13 @@ class JdbcJsonAttributeTests {
@Autowired
JdbcClient jdbcClient;
String username;
@Autowired
void setSecurityProperties(SecurityProperties securityProperties) {
this.username = securityProperties.getUser().getName();
}
@BeforeEach
void setup() {
ObjectMapper copy = this.objectMapper.copy();
@@ -51,7 +55,7 @@ class JdbcJsonAttributeTests {
@Test
void loginShouldSaveSecurityContextAsJson() throws Exception {
Cookie sessionCookie = this.mvc.perform(formLogin().user("rüdiger").password("password"))
Cookie sessionCookie = this.mvc.perform(formLogin().user(this.username).password("password"))
.andExpect(authenticated())
.andReturn()
.getResponse()
@@ -66,20 +70,20 @@ class JdbcJsonAttributeTests {
SecurityContext securityContext = this.objectMapperWithModules.readValue((String) attributeBytes,
SecurityContext.class);
assertThat(securityContext).isNotNull();
assertThat(securityContext.getAuthentication().getName()).isEqualTo("rüdiger");
assertThat(securityContext.getAuthentication().getName()).isEqualTo(this.username);
}
@Test
void loginWhenQueryUsingJsonbOperatorThenReturns() throws Exception {
this.mvc.perform(formLogin().user("rüdiger").password("password")).andExpect(authenticated());
this.mvc.perform(formLogin().user(this.username).password("password")).andExpect(authenticated());
Object attributeBytes = this.jdbcClient.sql("""
SELECT attribute_bytes::text FROM spring_session_attributes
WHERE attribute_bytes -> 'authentication' -> 'principal' ->> 'username' = 'rüdiger'
""").query().singleValue();
WHERE attribute_bytes -> 'authentication' -> 'principal' ->> 'username' = '%s'
""".formatted(this.username)).query().singleValue();
SecurityContext securityContext = this.objectMapperWithModules.readValue((String) attributeBytes,
SecurityContext.class);
assertThat(securityContext).isNotNull();
assertThat(securityContext.getAuthentication().getName()).isEqualTo("rüdiger");
assertThat(securityContext.getAuthentication().getName()).isEqualTo(this.username);
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample;
import org.springframework.test.context.TestPropertySource;
/**
* Test that special characters in usernames work.
*
* @author Rob Winch
*/
@TestPropertySource(properties = "spring.security.user.name=rüdiger")
class JdbcJsonSpecialCharsAttributeTests extends JdbcJsonAttributeTests {
}