From 9538b2f1e30566ed461eeba7044ac75aaa617917 Mon Sep 17 00:00:00 2001 From: mnhock Date: Thu, 28 Sep 2023 17:36:02 +0200 Subject: [PATCH] Clean up --- README.md | 2 +- docs/about.md | 2 +- .../naikan/project/ProjectXlsxView.java | 3 +- .../naikan/project/ProjectControllerIT.java | 33 ++++++++ ...rningAuthenticationFailureHandlerTest.java | 44 +++++++++++ ...rningAuthenticationSuccessHandlerTest.java | 75 +++++++++++++++++++ 6 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 naikan-web/src/test/java/com/enofex/naikan/security/HttpStatusReturningAuthenticationFailureHandlerTest.java create mode 100644 naikan-web/src/test/java/com/enofex/naikan/security/HttpStatusReturningAuthenticationSuccessHandlerTest.java diff --git a/README.md b/README.md index f1e7692e..538b7184 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Tired of scattered spreadsheets and scattered information? Our user-friendly int
-
+
diff --git a/docs/about.md b/docs/about.md index 0bec67ee..bc6e452c 100644 --- a/docs/about.md +++ b/docs/about.md @@ -57,7 +57,7 @@ See all projects at a glance, filter, sort and search them. ### Projects insights -See all projects statisics at a glance, filter, sort and search them. +See all projects statistics at a glance, filter, sort and search them. Projects insights diff --git a/naikan-web/src/main/java/com/enofex/naikan/project/ProjectXlsxView.java b/naikan-web/src/main/java/com/enofex/naikan/project/ProjectXlsxView.java index 0e682962..5b34398d 100644 --- a/naikan-web/src/main/java/com/enofex/naikan/project/ProjectXlsxView.java +++ b/naikan-web/src/main/java/com/enofex/naikan/project/ProjectXlsxView.java @@ -282,8 +282,7 @@ private void commits(Workbook workbook) { put("File changed", commit -> String.valueOf(commit.changes().files().changed())); }}; - writeRows(sheet, columns, - this.bom.repository() != null ? this.bom.repository().commits().all() : List.of()); + writeRows(sheet, columns, this.bom.repository().commits().all()); } } diff --git a/naikan-web/src/test/java/com/enofex/naikan/project/ProjectControllerIT.java b/naikan-web/src/test/java/com/enofex/naikan/project/ProjectControllerIT.java index c43234bf..dfa8ab4b 100644 --- a/naikan-web/src/test/java/com/enofex/naikan/project/ProjectControllerIT.java +++ b/naikan-web/src/test/java/com/enofex/naikan/project/ProjectControllerIT.java @@ -3,14 +3,17 @@ import static com.enofex.naikan.test.model.Boms.validBom0asInputStream; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.handler; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import com.enofex.naikan.administration.user.User; import com.enofex.naikan.model.Bom; import com.enofex.naikan.model.deserializer.DeserializerFactory; import com.enofex.naikan.test.IntegrationTest; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; @@ -24,6 +27,8 @@ class ProjectControllerIT { private MockMvc mvc; @Autowired private MongoTemplate template; + @Autowired + private ObjectMapper mapper; @Test void shouldFindAll() throws Exception { @@ -254,4 +259,32 @@ void shouldFindRepositoryBranchesById() throws Exception { .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); } + + @Test + void shouldNotUpdateUserFavoritesWhenUserNotFound() throws Exception { + this.mvc.perform( + patch("/api/projects/favorites") + .with(csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(this.mapper.writeValueAsString(new String[]{"not_important"}))) + .andExpect(handler().methodName("updateUserFavorites")) + .andExpect(status().isNotFound()); + } + + @Test + void shouldUpdateUserFavorites() throws Exception { + this.template.save(new User("user"), "users"); + + Bom savedBom = this.template.save( + DeserializerFactory.newJsonDeserializer().of(validBom0asInputStream()), + "projects"); + + this.mvc.perform( + patch("/api/projects/favorites") + .with(csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(this.mapper.writeValueAsString(new String[]{savedBom.id()}))) + .andExpect(handler().methodName("updateUserFavorites")) + .andExpect(status().isNoContent()); + } } \ No newline at end of file diff --git a/naikan-web/src/test/java/com/enofex/naikan/security/HttpStatusReturningAuthenticationFailureHandlerTest.java b/naikan-web/src/test/java/com/enofex/naikan/security/HttpStatusReturningAuthenticationFailureHandlerTest.java new file mode 100644 index 00000000..754276e1 --- /dev/null +++ b/naikan-web/src/test/java/com/enofex/naikan/security/HttpStatusReturningAuthenticationFailureHandlerTest.java @@ -0,0 +1,44 @@ +package com.enofex.naikan.security; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.security.core.AuthenticationException; + +class HttpStatusReturningAuthenticationFailureHandlerTest { + + private HttpStatusReturningAuthenticationFailureHandler failureHandler; + private ObjectMapper objectMapper; + + @BeforeEach + void setUp() { + this.failureHandler = new HttpStatusReturningAuthenticationFailureHandler(); + this.objectMapper = new ObjectMapper(); + } + + @Test + void shouldReturnCorrectValuesAuthenticationFailure() throws Exception { + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + AuthenticationException exception = mock(AuthenticationException.class); + PrintWriter writer = mock(PrintWriter.class); + + when(response.getWriter()).thenReturn(writer); + + this.failureHandler.onAuthenticationFailure(request, response, exception); + + verify(response).setCharacterEncoding(StandardCharsets.UTF_8.name()); + verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); + verify(writer).write(objectMapper.writeValueAsString(Mockito.any())); + verify(writer).flush(); + } +} diff --git a/naikan-web/src/test/java/com/enofex/naikan/security/HttpStatusReturningAuthenticationSuccessHandlerTest.java b/naikan-web/src/test/java/com/enofex/naikan/security/HttpStatusReturningAuthenticationSuccessHandlerTest.java new file mode 100644 index 00000000..90a404c9 --- /dev/null +++ b/naikan-web/src/test/java/com/enofex/naikan/security/HttpStatusReturningAuthenticationSuccessHandlerTest.java @@ -0,0 +1,75 @@ +package com.enofex.naikan.security; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.enofex.naikan.administration.user.AdministrationUserService; +import com.enofex.naikan.administration.user.User; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.security.core.Authentication; + +class HttpStatusReturningAuthenticationSuccessHandlerTest { + + private HttpStatusReturningAuthenticationSuccessHandler successHandler; + private AdministrationUserService userService; + + @BeforeEach + void setUp() { + this.userService = mock(AdministrationUserService.class); + this.successHandler = new HttpStatusReturningAuthenticationSuccessHandler(this.userService); + } + + @Test + void shouldAuthenticateFirstUserAndShouldBeAdmin() { + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + Authentication authentication = mock(Authentication.class); + + when(this.userService.count()).thenReturn(0L); + when(authentication.getName()).thenReturn("firstUser"); + + this.successHandler.onAuthenticationSuccess(request, response, authentication); + + verify(this.userService).save(new User("firstUser", List.of("ROLE_ADMIN"))); + verify(response).setStatus(HttpServletResponse.SC_OK); + } + + @Test + void shouldnAuthenticateIfUserNotFound() { + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + Authentication authentication = mock(Authentication.class); + + when(this.userService.count()).thenReturn(1L); + when(authentication.getName()).thenReturn("newUser"); + when(this.userService.findByName("newUser")).thenReturn(null); + + this.successHandler.onAuthenticationSuccess(request, response, authentication); + + verify(this.userService).save(new User("newUser")); + verify(response).setStatus(HttpServletResponse.SC_OK); + } + + @Test + void shouldnAuthenticateIfUserAlreadyExists() { + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + Authentication authentication = mock(Authentication.class); + + when(this.userService.count()).thenReturn(1L); + when(authentication.getName()).thenReturn("existingUser"); + when(this.userService.findByName("existingUser")).thenReturn(new User("existingUser")); + + this.successHandler.onAuthenticationSuccess(request, response, authentication); + + verify(this.userService, never()).save(Mockito.any(User.class)); + verify(response).setStatus(HttpServletResponse.SC_OK); + } +}