Loading user-microservice/src/main/java/cz/muni/fi/iamdb/config/SecurityConfig.java +2 −0 Original line number Diff line number Diff line Loading @@ -11,6 +11,7 @@ import org.springdoc.core.customizers.OpenApiCustomizer; import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.context.event.EventListener; import org.springframework.http.HttpMethod; import org.springframework.security.config.Customizer; Loading @@ -18,6 +19,7 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration @Profile("prod") public class SecurityConfig { private static final Logger log = LoggerFactory.getLogger(App.class); private static final String SECURITY_SCHEME_OAUTH2 = "MUNI"; Loading user-microservice/src/main/resources/application.yml +2 −0 Original line number Diff line number Diff line Loading @@ -25,6 +25,8 @@ server: include-message: always spring: profiles: active: prod jpa: open-in-view: false hibernate: Loading user-microservice/src/test/java/cz/muni/fi/iamdb/config/SecurityConfig.java 0 → 100644 +28 −0 Original line number Diff line number Diff line package cz.muni.fi.iamdb.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration @Profile("test") public class SecurityConfig { /** * Configure access restrictions to the API. * Introspection of opaque access token is configured, introspection endpoint is defined in application.yml. */ @Bean SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(x -> x .anyRequest().permitAll() ) // .oauth2ResourceServer(oauth2 -> oauth2.opaqueToken(Customizer.withDefaults())) ; return http.build(); } } user-microservice/src/test/java/cz/muni/fi/iamdb/integration/IntegrationTest.java +136 −136 Original line number Diff line number Diff line //package cz.muni.fi.iamdb.integration; // //import com.fasterxml.jackson.databind.JsonNode; //import com.fasterxml.jackson.databind.ObjectMapper; //import jakarta.transaction.Transactional; //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.test.annotation.Rollback; //import org.springframework.test.context.ActiveProfiles; //import org.springframework.test.web.servlet.MockMvc; // //import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; //import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; //import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; //import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; // //@SpringBootTest //@AutoConfigureMockMvc //@ActiveProfiles("test") //@Rollback //public class IntegrationTest { // // @Autowired // private MockMvc mockMvc; // // // @Test // @Transactional // void testCreate() throws Exception { // String userJson = """ // { // "name": "alice", // "email": "valid@mail.com", // "hashed_password": "the_hash", // "salt": "the_salt", // "role": "USER" // } // """; // mockMvc.perform(post("/users") // .contentType("application/json") // .content(userJson)) // .andExpect(status().isCreated()) // .andExpect(jsonPath("$.id").exists()); // } // // @Test // @Transactional // void testFindAll() throws Exception { // mockMvc.perform(get("/users")) // .andExpect(status().isOk()) // .andExpect(jsonPath("$").isArray()); // } // // @Test // @Transactional // void testFindById() throws Exception { // // setup // String userJson = """ // { // "name": "alice", // "email": "valid@mail.com", // "hashed_password": "the_hash", // "salt": "the_salt", // "role": "USER" // } // """; // // // String jsonResponse = mockMvc.perform(post("/users") // .contentType("application/json") // .content(userJson)).andReturn().getResponse().getContentAsString(); // JsonNode jsonNode = new ObjectMapper().readTree(jsonResponse); // String insertedUuidButString = jsonNode.get("id").asText(); // // // get & assert // mockMvc.perform(get("/users/" + insertedUuidButString)) // .andExpect(status().isOk()) // .andExpect(jsonPath("$.id").value(insertedUuidButString)); // } // // @Test // @Transactional // void testDelete() throws Exception { // // setup // String userJson = """ // { // "name": "alice", // "email": "valid@mail.com", // "hashed_password": "the_hash", // "salt": "the_salt", // "role": "USER" // } // """; // // // String jsonResponse = mockMvc.perform(post("/users") // .contentType("application/json") // .content(userJson)).andReturn().getResponse().getContentAsString(); // JsonNode jsonNode = new ObjectMapper().readTree(jsonResponse); // String insertedUuidButString = jsonNode.get("id").asText(); // // // act // mockMvc.perform(delete("/users/" + insertedUuidButString)) // .andExpect(status().isNoContent()); // // mockMvc.perform(delete("/users/" + insertedUuidButString)) // .andExpect(status().isNoContent()); // delete idempotent // } // // @Test // @Transactional // void testDeleteNonexistent() throws Exception { // mockMvc.perform(delete("/users/00000000-0000-0000-0000-000000000000")) // .andExpect(status().isNoContent()); // delete idempotent // } // // @Test // void testCreateInvalid() throws Exception { // String userJson = """ // { // "name": "alice", // "hashed_password": "the_hash", // "salt": "the_salt", // "role": "USER" // } // """; // // mockMvc.perform(post("/users") // .contentType("application/json") // .content(userJson)) // // FUCK WHOLE JPA // .andExpect(status().isInternalServerError()); // } //} package cz.muni.fi.iamdb.integration; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.transaction.Transactional; 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.test.annotation.Rollback; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; @SpringBootTest @AutoConfigureMockMvc(addFilters = false) @ActiveProfiles("test") @Rollback public class IntegrationTest { @Autowired private MockMvc mockMvc; @Test @Transactional void testCreate() throws Exception { String userJson = """ { "name": "alice", "email": "valid@mail.com", "hashed_password": "the_hash", "salt": "the_salt", "role": "USER" } """; mockMvc.perform(post("/users") .contentType("application/json") .content(userJson)) .andExpect(status().isCreated()) .andExpect(jsonPath("$.id").exists()); } @Test @Transactional void testFindAll() throws Exception { mockMvc.perform(get("/users")) .andExpect(status().isOk()) .andExpect(jsonPath("$").isArray()); } @Test @Transactional void testFindById() throws Exception { // setup String userJson = """ { "name": "alice", "email": "valid@mail.com", "hashed_password": "the_hash", "salt": "the_salt", "role": "USER" } """; String jsonResponse = mockMvc.perform(post("/users") .contentType("application/json") .content(userJson)).andReturn().getResponse().getContentAsString(); JsonNode jsonNode = new ObjectMapper().readTree(jsonResponse); String insertedUuidButString = jsonNode.get("id").asText(); // get & assert mockMvc.perform(get("/users/" + insertedUuidButString)) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(insertedUuidButString)); } @Test @Transactional void testDelete() throws Exception { // setup String userJson = """ { "name": "alice", "email": "valid@mail.com", "hashed_password": "the_hash", "salt": "the_salt", "role": "USER" } """; String jsonResponse = mockMvc.perform(post("/users") .contentType("application/json") .content(userJson)).andReturn().getResponse().getContentAsString(); JsonNode jsonNode = new ObjectMapper().readTree(jsonResponse); String insertedUuidButString = jsonNode.get("id").asText(); // act mockMvc.perform(delete("/users/" + insertedUuidButString)) .andExpect(status().isNoContent()); mockMvc.perform(delete("/users/" + insertedUuidButString)) .andExpect(status().isNoContent()); // delete idempotent } @Test @Transactional void testDeleteNonexistent() throws Exception { mockMvc.perform(delete("/users/00000000-0000-0000-0000-000000000000")) .andExpect(status().isNoContent()); // delete idempotent } @Test void testCreateInvalid() throws Exception { String userJson = """ { "name": "alice", "hashed_password": "the_hash", "salt": "the_salt", "role": "USER" } """; mockMvc.perform(post("/users") .contentType("application/json") .content(userJson)) // FUCK WHOLE JPA .andExpect(status().isInternalServerError()); } } user-microservice/src/test/java/cz/muni/fi/iamdb/rest/UserRestControllerWebMvcTest.java +2 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ import cz.muni.fi.iamdb.facade.UserFacade; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; Loading @@ -18,6 +19,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @AutoConfigureMockMvc(addFilters = false) @WebMvcTest(controllers = UserRestController.class) class UserRestControllerTest { Loading Loading
user-microservice/src/main/java/cz/muni/fi/iamdb/config/SecurityConfig.java +2 −0 Original line number Diff line number Diff line Loading @@ -11,6 +11,7 @@ import org.springdoc.core.customizers.OpenApiCustomizer; import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.context.event.EventListener; import org.springframework.http.HttpMethod; import org.springframework.security.config.Customizer; Loading @@ -18,6 +19,7 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration @Profile("prod") public class SecurityConfig { private static final Logger log = LoggerFactory.getLogger(App.class); private static final String SECURITY_SCHEME_OAUTH2 = "MUNI"; Loading
user-microservice/src/main/resources/application.yml +2 −0 Original line number Diff line number Diff line Loading @@ -25,6 +25,8 @@ server: include-message: always spring: profiles: active: prod jpa: open-in-view: false hibernate: Loading
user-microservice/src/test/java/cz/muni/fi/iamdb/config/SecurityConfig.java 0 → 100644 +28 −0 Original line number Diff line number Diff line package cz.muni.fi.iamdb.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration @Profile("test") public class SecurityConfig { /** * Configure access restrictions to the API. * Introspection of opaque access token is configured, introspection endpoint is defined in application.yml. */ @Bean SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(x -> x .anyRequest().permitAll() ) // .oauth2ResourceServer(oauth2 -> oauth2.opaqueToken(Customizer.withDefaults())) ; return http.build(); } }
user-microservice/src/test/java/cz/muni/fi/iamdb/integration/IntegrationTest.java +136 −136 Original line number Diff line number Diff line //package cz.muni.fi.iamdb.integration; // //import com.fasterxml.jackson.databind.JsonNode; //import com.fasterxml.jackson.databind.ObjectMapper; //import jakarta.transaction.Transactional; //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.test.annotation.Rollback; //import org.springframework.test.context.ActiveProfiles; //import org.springframework.test.web.servlet.MockMvc; // //import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; //import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; //import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; //import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; // //@SpringBootTest //@AutoConfigureMockMvc //@ActiveProfiles("test") //@Rollback //public class IntegrationTest { // // @Autowired // private MockMvc mockMvc; // // // @Test // @Transactional // void testCreate() throws Exception { // String userJson = """ // { // "name": "alice", // "email": "valid@mail.com", // "hashed_password": "the_hash", // "salt": "the_salt", // "role": "USER" // } // """; // mockMvc.perform(post("/users") // .contentType("application/json") // .content(userJson)) // .andExpect(status().isCreated()) // .andExpect(jsonPath("$.id").exists()); // } // // @Test // @Transactional // void testFindAll() throws Exception { // mockMvc.perform(get("/users")) // .andExpect(status().isOk()) // .andExpect(jsonPath("$").isArray()); // } // // @Test // @Transactional // void testFindById() throws Exception { // // setup // String userJson = """ // { // "name": "alice", // "email": "valid@mail.com", // "hashed_password": "the_hash", // "salt": "the_salt", // "role": "USER" // } // """; // // // String jsonResponse = mockMvc.perform(post("/users") // .contentType("application/json") // .content(userJson)).andReturn().getResponse().getContentAsString(); // JsonNode jsonNode = new ObjectMapper().readTree(jsonResponse); // String insertedUuidButString = jsonNode.get("id").asText(); // // // get & assert // mockMvc.perform(get("/users/" + insertedUuidButString)) // .andExpect(status().isOk()) // .andExpect(jsonPath("$.id").value(insertedUuidButString)); // } // // @Test // @Transactional // void testDelete() throws Exception { // // setup // String userJson = """ // { // "name": "alice", // "email": "valid@mail.com", // "hashed_password": "the_hash", // "salt": "the_salt", // "role": "USER" // } // """; // // // String jsonResponse = mockMvc.perform(post("/users") // .contentType("application/json") // .content(userJson)).andReturn().getResponse().getContentAsString(); // JsonNode jsonNode = new ObjectMapper().readTree(jsonResponse); // String insertedUuidButString = jsonNode.get("id").asText(); // // // act // mockMvc.perform(delete("/users/" + insertedUuidButString)) // .andExpect(status().isNoContent()); // // mockMvc.perform(delete("/users/" + insertedUuidButString)) // .andExpect(status().isNoContent()); // delete idempotent // } // // @Test // @Transactional // void testDeleteNonexistent() throws Exception { // mockMvc.perform(delete("/users/00000000-0000-0000-0000-000000000000")) // .andExpect(status().isNoContent()); // delete idempotent // } // // @Test // void testCreateInvalid() throws Exception { // String userJson = """ // { // "name": "alice", // "hashed_password": "the_hash", // "salt": "the_salt", // "role": "USER" // } // """; // // mockMvc.perform(post("/users") // .contentType("application/json") // .content(userJson)) // // FUCK WHOLE JPA // .andExpect(status().isInternalServerError()); // } //} package cz.muni.fi.iamdb.integration; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.transaction.Transactional; 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.test.annotation.Rollback; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; @SpringBootTest @AutoConfigureMockMvc(addFilters = false) @ActiveProfiles("test") @Rollback public class IntegrationTest { @Autowired private MockMvc mockMvc; @Test @Transactional void testCreate() throws Exception { String userJson = """ { "name": "alice", "email": "valid@mail.com", "hashed_password": "the_hash", "salt": "the_salt", "role": "USER" } """; mockMvc.perform(post("/users") .contentType("application/json") .content(userJson)) .andExpect(status().isCreated()) .andExpect(jsonPath("$.id").exists()); } @Test @Transactional void testFindAll() throws Exception { mockMvc.perform(get("/users")) .andExpect(status().isOk()) .andExpect(jsonPath("$").isArray()); } @Test @Transactional void testFindById() throws Exception { // setup String userJson = """ { "name": "alice", "email": "valid@mail.com", "hashed_password": "the_hash", "salt": "the_salt", "role": "USER" } """; String jsonResponse = mockMvc.perform(post("/users") .contentType("application/json") .content(userJson)).andReturn().getResponse().getContentAsString(); JsonNode jsonNode = new ObjectMapper().readTree(jsonResponse); String insertedUuidButString = jsonNode.get("id").asText(); // get & assert mockMvc.perform(get("/users/" + insertedUuidButString)) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(insertedUuidButString)); } @Test @Transactional void testDelete() throws Exception { // setup String userJson = """ { "name": "alice", "email": "valid@mail.com", "hashed_password": "the_hash", "salt": "the_salt", "role": "USER" } """; String jsonResponse = mockMvc.perform(post("/users") .contentType("application/json") .content(userJson)).andReturn().getResponse().getContentAsString(); JsonNode jsonNode = new ObjectMapper().readTree(jsonResponse); String insertedUuidButString = jsonNode.get("id").asText(); // act mockMvc.perform(delete("/users/" + insertedUuidButString)) .andExpect(status().isNoContent()); mockMvc.perform(delete("/users/" + insertedUuidButString)) .andExpect(status().isNoContent()); // delete idempotent } @Test @Transactional void testDeleteNonexistent() throws Exception { mockMvc.perform(delete("/users/00000000-0000-0000-0000-000000000000")) .andExpect(status().isNoContent()); // delete idempotent } @Test void testCreateInvalid() throws Exception { String userJson = """ { "name": "alice", "hashed_password": "the_hash", "salt": "the_salt", "role": "USER" } """; mockMvc.perform(post("/users") .contentType("application/json") .content(userJson)) // FUCK WHOLE JPA .andExpect(status().isInternalServerError()); } }
user-microservice/src/test/java/cz/muni/fi/iamdb/rest/UserRestControllerWebMvcTest.java +2 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ import cz.muni.fi.iamdb.facade.UserFacade; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; Loading @@ -18,6 +19,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @AutoConfigureMockMvc(addFilters = false) @WebMvcTest(controllers = UserRestController.class) class UserRestControllerTest { Loading