Files
hemhub/src/test/java/se/urmo/hemhub/integration/MeControllerBranchesIT.java
Urban Modig acf9ec8a2c
All checks were successful
continuous-integration/drone/push Build is passing
Add TestSecurityConfig for improved test setup and update configurations
- Introduce `TestSecurityConfig` to simplify JWT usage in test environments.
- Update integration tests to import `TestSecurityConfig`.
- Split environment-specific configurations into new `application-dev.yml` and `applications-prod.yml` files.
- Adjust `docker-compose.yml` for development-specific settings.
- Clean up redundant JWT properties in `application.yml`.
2025-10-14 16:08:01 +02:00

53 lines
2.0 KiB
Java

package se.urmo.hemhub.integration;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import se.urmo.hemhub.support.TestSecurityConfig;
import java.util.Map;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
@Import(TestSecurityConfig.class)
class MeControllerBranchesIT {
@Autowired
MockMvc mvc;
@Test
void me_withoutRealmAccess_rolesEmpty() throws Exception {
mvc.perform(get("/me").with(jwt().jwt(j -> {
j.subject("u1");
j.claim("preferred_username", "test1");
j.claim("household_id", "H1");
// ingen realm_access-claim
})))
.andExpect(status().isOk())
.andExpect(jsonPath("$.roles").isArray())
.andExpect(jsonPath("$.roles.length()").value(0));
}
@Test
void me_withMalformedRealmAccess_rolesEmpty() throws Exception {
mvc.perform(get("/me").with(jwt().jwt(j -> {
j.subject("u2");
j.claim("preferred_username", "test2");
j.claim("household_id", "H2");
j.claim("realm_access", Map.of("roles", "NOT_A_LIST"));
})))
.andExpect(status().isOk())
.andExpect(jsonPath("$.roles.length()").value(0));
}
}