Add household and membership domain with role-based APIs
All checks were successful
continuous-integration/drone/push Build is passing

Introduced `Household` and `HouseholdMember` entities, services, and repositories, enabling role-based access (`OWNER` and `MEMBER`). Added REST APIs for household creation, member management, and listing. Configured Flyway migrations, updated tests, and included IntelliJ HTTP client script for API testing. Updated `README.md` with feature details, usage instructions, and architecture overview.
This commit is contained in:
Urban Modig
2025-10-06 21:46:30 +02:00
parent 89315e01dd
commit 84d7647481
17 changed files with 604 additions and 9 deletions

View File

@ -0,0 +1,63 @@
package se.urmo.hemhub.integration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import java.util.UUID;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
class HouseholdControllerIT {
@Autowired MockMvc mvc;
@Test
void create_household_and_list_for_user() throws Exception {
var jwtUser = jwt().jwt(j -> {
j.subject("sub-user-1");
j.claim("email","u1@example.com");
j.claim("preferred_username","u1");
j.claim("realm_access", java.util.Map.of("roles", java.util.List.of("OWNER","MEMBER")));
});
// create
mvc.perform(post("/api/v1/households")
.with(jwtUser)
.contentType("application/json")
.content("{\"name\":\"Familjen U1\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").exists());
// list
mvc.perform(get("/api/v1/households").with(jwtUser))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].name").value("Familjen U1"));
}
@Test
void add_member_requires_owner() throws Exception {
var owner = jwt().jwt(j -> { j.subject("owner-sub"); j.claim("email","o@ex.com"); j.claim("preferred_username","owner"); });
var other = jwt().jwt(j -> { j.subject("other-sub"); j.claim("email","x@ex.com"); j.claim("preferred_username","x"); });
// create household as owner
var res = mvc.perform(post("/api/v1/households").with(owner)
.contentType("application/json").content("{\"name\":\"H1\"}"))
.andExpect(status().isOk()).andReturn();
var id = com.jayway.jsonpath.JsonPath.read(res.getResponse().getContentAsString(), "$.id");
// cannot add as non-owner
mvc.perform(post("/api/v1/households/"+id+"/members").with(other)
.contentType("application/json")
.content("{\"userSub\":\"u2\",\"role\":\"MEMBER\"}"))
.andExpect(status().isForbidden());
}
}

View File

@ -1,4 +1,4 @@
package se.urmo.hemhub.web;
package se.urmo.hemhub.integration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

View File

@ -1,4 +1,4 @@
package se.urmo.hemhub.web;
package se.urmo.hemhub.integration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;