From 820a7b378d94035cb90a4a584047383fb5eaa355 Mon Sep 17 00:00:00 2001 From: clean-coder Date: Tue, 24 Sep 2024 07:30:24 +0200 Subject: [PATCH 1/5] #1005: test ClientConfigController --- .../controller/ClientConfigControllerIT.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 backend/src/test/java/ch/puzzle/okr/controller/ClientConfigControllerIT.java diff --git a/backend/src/test/java/ch/puzzle/okr/controller/ClientConfigControllerIT.java b/backend/src/test/java/ch/puzzle/okr/controller/ClientConfigControllerIT.java new file mode 100644 index 0000000000..88320a1ba4 --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/controller/ClientConfigControllerIT.java @@ -0,0 +1,62 @@ +package ch.puzzle.okr.controller; + +import ch.puzzle.okr.dto.ClientConfigDto; +import ch.puzzle.okr.service.clientconfig.ClientConfigService; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.BDDMockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +import java.util.Map; + +import static ch.puzzle.okr.controller.OverviewControllerIT.JSON_PATH_ROOT; +import static org.mockito.ArgumentMatchers.anyString; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; + +@WithMockUser(value = "spring") +@ExtendWith(MockitoExtension.class) +@WebMvcTest(ClientConfigController.class) +public class ClientConfigControllerIT { + + @Autowired + private MockMvc mvc; + @MockBean + private ClientConfigService configService; + + @Test + void shouldGetClientConfig() throws Exception { + BDDMockito.given(configService.getConfigBasedOnActiveEnv(anyString())) + .willReturn(createClientConfigDto()); + + mvc.perform(get("/config").contentType(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(jsonPath(JSON_PATH_ROOT, Matchers.aMapWithSize(9))) + .andExpect(jsonPath("$.activeProfile", Matchers.is("Active_Profile"))) + .andExpect(jsonPath("$.issuer", Matchers.is("Issuer"))) + .andExpect(jsonPath("$.clientId", Matchers.is("Client_Id"))) + .andExpect(jsonPath("$.favicon", Matchers.is("Favicon"))) + .andExpect(jsonPath("$.logo", Matchers.is("Logo"))) + .andExpect(jsonPath("$.triangles", Matchers.is("Triangles"))) + .andExpect(jsonPath("$.backgroundLogo", Matchers.is("Background_Logo"))) + .andExpect(jsonPath("$.title", Matchers.is("Title"))) + .andExpect(jsonPath("$.customStyles.font-family", Matchers.is("verdana"))) + .andExpect(jsonPath("$.customStyles.font-size", Matchers.is("20px"))); + } + + private ClientConfigDto createClientConfigDto() { + Map customStyles = Map.of("font-family", "verdana", "font-size", "20px"); + return new ClientConfigDto("Active_Profile", "Issuer", "Client_Id", "Favicon", "Logo", + "Triangles", "Background_Logo", "Title", customStyles); + } + +} + From 5e2cf2e088932cb5cf9ef6d4b8e42c3dcf9c312a Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Tue, 24 Sep 2024 05:30:59 +0000 Subject: [PATCH 2/5] [FM] Automated formating backend --- .../puzzle/okr/controller/ClientConfigControllerIT.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/backend/src/test/java/ch/puzzle/okr/controller/ClientConfigControllerIT.java b/backend/src/test/java/ch/puzzle/okr/controller/ClientConfigControllerIT.java index 88320a1ba4..e6b5244a6b 100644 --- a/backend/src/test/java/ch/puzzle/okr/controller/ClientConfigControllerIT.java +++ b/backend/src/test/java/ch/puzzle/okr/controller/ClientConfigControllerIT.java @@ -34,8 +34,7 @@ public class ClientConfigControllerIT { @Test void shouldGetClientConfig() throws Exception { - BDDMockito.given(configService.getConfigBasedOnActiveEnv(anyString())) - .willReturn(createClientConfigDto()); + BDDMockito.given(configService.getConfigBasedOnActiveEnv(anyString())).willReturn(createClientConfigDto()); mvc.perform(get("/config").contentType(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) @@ -54,9 +53,8 @@ void shouldGetClientConfig() throws Exception { private ClientConfigDto createClientConfigDto() { Map customStyles = Map.of("font-family", "verdana", "font-size", "20px"); - return new ClientConfigDto("Active_Profile", "Issuer", "Client_Id", "Favicon", "Logo", - "Triangles", "Background_Logo", "Title", customStyles); + return new ClientConfigDto("Active_Profile", "Issuer", "Client_Id", "Favicon", "Logo", "Triangles", + "Background_Logo", "Title", customStyles); } } - From 4e05b6be8a8fcc6b054855eb42ecfa7338491f21 Mon Sep 17 00:00:00 2001 From: clean-coder Date: Tue, 24 Sep 2024 08:48:20 +0200 Subject: [PATCH 3/5] #1005: deleteById test for CheckInController --- .../java/ch/puzzle/okr/controller/CheckInControllerIT.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/backend/src/test/java/ch/puzzle/okr/controller/CheckInControllerIT.java b/backend/src/test/java/ch/puzzle/okr/controller/CheckInControllerIT.java index 7adab25166..cbb63560fb 100644 --- a/backend/src/test/java/ch/puzzle/okr/controller/CheckInControllerIT.java +++ b/backend/src/test/java/ch/puzzle/okr/controller/CheckInControllerIT.java @@ -174,4 +174,10 @@ class NonMetricOrOrdinalKeyResult extends KeyResult { .with(SecurityMockMvcRequestPostProcessors.csrf()).content(JSON)) .andExpect(MockMvcResultMatchers.status().is4xxClientError()); } + + @Test + void shouldDeleteCheckInById() throws Exception { + mvc.perform(delete(CHECK_IN_5_URL).with(SecurityMockMvcRequestPostProcessors.csrf())) + .andExpect(MockMvcResultMatchers.status().isOk()); + } } From 086858ff619d0cb7f19c0abbd37924b3da19e2c6 Mon Sep 17 00:00:00 2001 From: clean-coder Date: Tue, 24 Sep 2024 09:20:11 +0200 Subject: [PATCH 4/5] #1005: additional tests for TeamController --- .../puzzle/okr/controller/TeamControllerIT.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/backend/src/test/java/ch/puzzle/okr/controller/TeamControllerIT.java b/backend/src/test/java/ch/puzzle/okr/controller/TeamControllerIT.java index 65c033f9d9..999c8cacf1 100644 --- a/backend/src/test/java/ch/puzzle/okr/controller/TeamControllerIT.java +++ b/backend/src/test/java/ch/puzzle/okr/controller/TeamControllerIT.java @@ -40,6 +40,7 @@ class TeamControllerIT { private static final String BASE_URL = "/api/v2/teams"; private static final String URL_TEAM_1 = "/api/v2/teams/1"; public static final String PUZZLE = "Puzzle"; + public static final String SUB_URL_USER_5 = "/user/5"; static Team teamPuzzle = Team.Builder.builder().withId(5L).withName(PUZZLE).build(); static Team teamOKR = Team.Builder.builder().withId(7L).withName("OKR").build(); static List teamList = Arrays.asList(teamPuzzle, teamOKR); @@ -182,4 +183,18 @@ void addUsersToTeam_shouldReturnOk() throws Exception { mvc.perform(put(URL_TEAM_1 + "/addusers").contentType(MediaType.APPLICATION_JSON).content(ADD_USERS) .with(SecurityMockMvcRequestPostProcessors.csrf())).andExpect(MockMvcResultMatchers.status().isOk()); } + + @Test + void removeUserFromTeam_shouldReturnOk() throws Exception { + mvc.perform(put(URL_TEAM_1 + SUB_URL_USER_5 + "/removeuser").contentType(MediaType.APPLICATION_JSON) + .content(ADD_USERS).with(SecurityMockMvcRequestPostProcessors.csrf())) + .andExpect(MockMvcResultMatchers.status().isOk()); + } + + @Test + void updateOrAddTeamMembership_shouldReturnOk() throws Exception { + mvc.perform(put(URL_TEAM_1 + SUB_URL_USER_5 + "/updateaddteammembership/true") + .contentType(MediaType.APPLICATION_JSON).content(ADD_USERS) + .with(SecurityMockMvcRequestPostProcessors.csrf())).andExpect(MockMvcResultMatchers.status().isOk()); + } } From 9ffef7cac67de7f92650551276b32ce1002c0854 Mon Sep 17 00:00:00 2001 From: clean-coder Date: Wed, 25 Sep 2024 14:54:13 +0200 Subject: [PATCH 5/5] #1005: additional tests for UserController --- .../okr/controller/UserControllerIT.java | 84 ++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/backend/src/test/java/ch/puzzle/okr/controller/UserControllerIT.java b/backend/src/test/java/ch/puzzle/okr/controller/UserControllerIT.java index 811168e1e8..33d0af9aac 100644 --- a/backend/src/test/java/ch/puzzle/okr/controller/UserControllerIT.java +++ b/backend/src/test/java/ch/puzzle/okr/controller/UserControllerIT.java @@ -3,6 +3,7 @@ import ch.puzzle.okr.dto.UserDto; import ch.puzzle.okr.mapper.UserMapper; import ch.puzzle.okr.models.User; +import ch.puzzle.okr.models.authorization.AuthorizationUser; import ch.puzzle.okr.service.authorization.AuthorizationService; import ch.puzzle.okr.service.authorization.UserAuthorizationService; import org.hamcrest.Matchers; @@ -17,6 +18,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @@ -25,7 +27,9 @@ import java.util.Collections; import java.util.List; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static ch.puzzle.okr.controller.ActionControllerIT.SUCCESSFUL_UPDATE_BODY; +import static org.mockito.ArgumentMatchers.any; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @WithMockUser(value = "spring") @@ -81,4 +85,82 @@ void shouldGetAllUsersIfNoUserExists() throws Exception { mvc.perform(get("/api/v1/users").contentType(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()).andExpect(jsonPath("$", Matchers.hasSize(0))); } + + @Test + void shouldReturnCurrentUser() throws Exception { + BDDMockito.given(authorizationService.updateOrAddAuthorizationUser()) + .willReturn(new AuthorizationUser(userAlice)); + BDDMockito.given(userMapper.toDto(userAlice)).willReturn(userAliceDto); + + mvc.perform(get("/api/v1/users/current").contentType(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isOk()) // + .andExpect(jsonPath("$", Matchers.aMapWithSize(7))) // + .andExpect(jsonPath("$.id", Is.is(2))) // + .andExpect(jsonPath("$.version", Is.is(3))) // + .andExpect(jsonPath("$.firstname", Is.is(FIRSTNAME_1))) // + .andExpect(jsonPath("$.lastname", Is.is(LASTNAME_1))) // + .andExpect(jsonPath("$.email", Is.is(EMAIL_1))) // + .andExpect(jsonPath("$.userTeamList", Matchers.empty())) // + .andExpect(jsonPath("$.isOkrChampion", Is.is(false))); + } + + @Test + void shouldReturnUserById() throws Exception { + BDDMockito.given(userAuthorizationService.getById(2)).willReturn(userAlice); + BDDMockito.given(userMapper.toDto(userAlice)).willReturn(userAliceDto); + + mvc.perform(get("/api/v1/users/2").contentType(MediaType.APPLICATION_JSON)) // + .andExpect(MockMvcResultMatchers.status().isOk()) // + .andExpect(jsonPath("$", Matchers.aMapWithSize(7))) // + .andExpect(jsonPath("$.id", Is.is(2))) // + .andExpect(jsonPath("$.version", Is.is(3))) // + .andExpect(jsonPath("$.firstname", Is.is(FIRSTNAME_1))) // + .andExpect(jsonPath("$.lastname", Is.is(LASTNAME_1))) // + .andExpect(jsonPath("$.email", Is.is(EMAIL_1))) // + .andExpect(jsonPath("$.userTeamList", Matchers.empty())) // + .andExpect(jsonPath("$.isOkrChampion", Is.is(false))); + } + + @Test + void shouldSetOkrChampion() throws Exception { + BDDMockito.given(userAuthorizationService.setIsOkrChampion(2, true)).willReturn(userAlice); + BDDMockito.given(userMapper.toDto(userAlice)).willReturn(userAliceDto); + + mvc.perform(put("/api/v1/users/2/isokrchampion/true") // + .content(SUCCESSFUL_UPDATE_BODY) // + .contentType(MediaType.APPLICATION_JSON) // + .with(SecurityMockMvcRequestPostProcessors.csrf()) // + ) // + .andExpect(MockMvcResultMatchers.status().isOk()) // + .andExpect(jsonPath("$", Matchers.aMapWithSize(7))) // + .andExpect(jsonPath("$.id", Is.is(2))) // + .andExpect(jsonPath("$.version", Is.is(3))) // + .andExpect(jsonPath("$.firstname", Is.is(FIRSTNAME_1))) // + .andExpect(jsonPath("$.lastname", Is.is(LASTNAME_1))) // + .andExpect(jsonPath("$.email", Is.is(EMAIL_1))) // + .andExpect(jsonPath("$.userTeamList", Matchers.empty())) // + .andExpect(jsonPath("$.isOkrChampion", Is.is(false))); + } + + @Test + void shouldCreateUsers() throws Exception { + BDDMockito.given(userAuthorizationService.createUsers(any())).willReturn(List.of(userAlice)); + BDDMockito.given(userMapper.toDtos(List.of(userAlice))).willReturn(List.of(userAliceDto)); + + mvc.perform(post("/api/v1/users/createall") // + .content(SUCCESSFUL_UPDATE_BODY) // + .contentType(MediaType.APPLICATION_JSON) // + .with(SecurityMockMvcRequestPostProcessors.csrf()) // + ) // + .andExpect(MockMvcResultMatchers.status().isOk()) // + .andExpect(jsonPath("$", Matchers.hasSize(1))) // + .andExpect(jsonPath("$[0].id", Is.is(2))) // + .andExpect(jsonPath("$[0].version", Is.is(3))) // + .andExpect(jsonPath("$[0].firstname", Is.is(FIRSTNAME_1))) // + .andExpect(jsonPath("$[0].lastname", Is.is(LASTNAME_1))) // + .andExpect(jsonPath("$[0].email", Is.is(EMAIL_1))) // + .andExpect(jsonPath("$[0].userTeamList", Matchers.empty())) // + .andExpect(jsonPath("$[0].isOkrChampion", Is.is(false))); + } + } \ No newline at end of file