diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 76e32f5..1065820 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,10 +13,10 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 0 - - name: Set up JDK 17 + - name: Set up JDK 21 uses: actions/setup-java@v4 with: - java-version: 17 + java-version: 21 distribution: 'zulu' - name: Cache SonarQube packages uses: actions/cache@v4 diff --git a/Dockerfile b/Dockerfile index 8f4201f..5680209 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM eclipse-temurin:17-jdk-alpine +FROM eclipse-temurin:21-jdk-alpine WORKDIR /app diff --git a/pom.xml b/pom.xml index 6058c82..2423941 100644 --- a/pom.xml +++ b/pom.xml @@ -8,21 +8,21 @@ 1.0.0 war Simple-Invoicing - Final Project — Spring Boot Advanced — May 2024 + Final Project – Spring Boot Advanced – May 2024 - 17 - 17 + 21 + 21 UTF-8 - 17 - 3.5.7 + 21 + 4.0.2 3.2.6 2.3.2 - 9.5.0 - 42.7.8 + 9.6.0 + 42.7.10 3.20.0 0.8.14 - 3.14.1 + 3.15.0 1.18.42 mark79-github @@ -91,6 +91,16 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-webmvc-test + test + + + org.springframework.boot + spring-boot-starter-data-jpa-test + test + org.springframework.security spring-security-test diff --git a/src/main/java/bg/softuni/invoice/config/SecurityConfiguration.java b/src/main/java/bg/softuni/invoice/config/SecurityConfiguration.java index bafc390..50a52d8 100644 --- a/src/main/java/bg/softuni/invoice/config/SecurityConfiguration.java +++ b/src/main/java/bg/softuni/invoice/config/SecurityConfiguration.java @@ -1,7 +1,6 @@ package bg.softuni.invoice.config; import lombok.AllArgsConstructor; -import org.springframework.boot.autoconfigure.security.servlet.PathRequest; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationProvider; @@ -28,15 +27,22 @@ public class SecurityConfiguration { private final BCryptPasswordEncoder bCryptPasswordEncoder; @Bean - public SecurityFilterChain configureSecurity(HttpSecurity httpSecurity) throws Exception { + public SecurityFilterChain configureSecurity(HttpSecurity httpSecurity) { httpSecurity .cors(AbstractHttpConfigurer::disable) .csrf(csrf -> csrf.csrfTokenRepository(csrfTokenRepository())) .authorizeHttpRequests(auth -> auth .requestMatchers("/").permitAll() - .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() + .requestMatchers( + "/css/**", + "/js/**", + "/images/**", + "/webjars/**", + "/favicon.ico" + ).permitAll() .requestMatchers("/actuator/health").permitAll() .requestMatchers("/user/register", "/user/login").anonymous() + .requestMatchers("/log/**").hasRole("ROOT") .anyRequest().authenticated() ) .formLogin(form -> form diff --git a/src/main/java/bg/softuni/invoice/service/ScheduleService.java b/src/main/java/bg/softuni/invoice/service/ScheduleService.java index bb519a1..31dea1e 100644 --- a/src/main/java/bg/softuni/invoice/service/ScheduleService.java +++ b/src/main/java/bg/softuni/invoice/service/ScheduleService.java @@ -1,14 +1,8 @@ package bg.softuni.invoice.service; -import org.springframework.scheduling.annotation.Async; -import org.springframework.scheduling.annotation.EnableAsync; - -@EnableAsync public interface ScheduleService { - - @Async void deleteLogs(); - @Async void changeStatus(); } + diff --git a/src/main/java/bg/softuni/invoice/web/controller/HomeController.java b/src/main/java/bg/softuni/invoice/web/controller/HomeController.java index aca6ecc..7f271e5 100644 --- a/src/main/java/bg/softuni/invoice/web/controller/HomeController.java +++ b/src/main/java/bg/softuni/invoice/web/controller/HomeController.java @@ -2,19 +2,23 @@ import bg.softuni.invoice.web.annotation.PageTitle; import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.Authentication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; -import java.security.Principal; - @Controller public class HomeController { @GetMapping("/") @PageTitle("invoices") - public String index(Principal principal) { + public String index(Authentication authentication) { + + boolean isLoggedIn = authentication != null + && authentication.isAuthenticated() + && !(authentication instanceof AnonymousAuthenticationToken); - if (principal != null) { + if (isLoggedIn) { return "home/home"; } diff --git a/src/main/java/bg/softuni/invoice/web/controller/UserController.java b/src/main/java/bg/softuni/invoice/web/controller/UserController.java index bf7cc84..2354429 100644 --- a/src/main/java/bg/softuni/invoice/web/controller/UserController.java +++ b/src/main/java/bg/softuni/invoice/web/controller/UserController.java @@ -149,7 +149,7 @@ public String profileConfirm(@Valid @GetMapping("/all") @PageTitle("User all") - @PreAuthorize("hasRole('ROLE_ROOT')") + @PreAuthorize("hasRole('ROOT')") public String allUsers(Model model) { List users = this.userService.getAllUsers() @@ -172,7 +172,7 @@ public String allUsers(Model model) { } @PostMapping("/set-admin/{id}") - @PreAuthorize("hasRole('ROLE_ROOT')") + @PreAuthorize("hasRole('ROOT')") public String setAdminRole(@PathVariable String id) { this.userService.setAdmin(id); @@ -180,7 +180,7 @@ public String setAdminRole(@PathVariable String id) { } @PostMapping("/set-user/{id}") - @PreAuthorize("hasRole('ROLE_ROOT')") + @PreAuthorize("hasRole('ROOT')") public String setUserRole(@PathVariable String id) { this.userService.setUser(id); @@ -188,7 +188,7 @@ public String setUserRole(@PathVariable String id) { } @PostMapping("/set-enabled/{id}") - @PreAuthorize("hasRole('ROLE_ROOT')") + @PreAuthorize("hasRole('ROOT')") public String setEnabled(@PathVariable String id) { this.userService.setUserEnabled(id); @@ -196,7 +196,7 @@ public String setEnabled(@PathVariable String id) { } @PostMapping("/set-disabled/{id}") - @PreAuthorize("hasRole('ROLE_ROOT')") + @PreAuthorize("hasRole('ROOT')") public String setDisabled(@PathVariable String id) { this.userService.setUserDisabled(id); diff --git a/src/main/java/bg/softuni/invoice/web/interceptor/LoggingInterceptor.java b/src/main/java/bg/softuni/invoice/web/interceptor/LoggingInterceptor.java index 37edd70..95ad581 100644 --- a/src/main/java/bg/softuni/invoice/web/interceptor/LoggingInterceptor.java +++ b/src/main/java/bg/softuni/invoice/web/interceptor/LoggingInterceptor.java @@ -7,7 +7,7 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; -import org.springframework.boot.autoconfigure.security.servlet.PathRequest; +import org.springframework.boot.security.autoconfigure.web.servlet.PathRequest; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; diff --git a/src/test/java/bg/softuni/invoice/repository/CompanyRepositoryTest.java b/src/test/java/bg/softuni/invoice/repository/CompanyRepositoryTest.java index 452b94a..c1c0629 100644 --- a/src/test/java/bg/softuni/invoice/repository/CompanyRepositoryTest.java +++ b/src/test/java/bg/softuni/invoice/repository/CompanyRepositoryTest.java @@ -7,7 +7,7 @@ import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest; import java.util.List; import java.util.Optional; diff --git a/src/test/java/bg/softuni/invoice/repository/InvoiceRepositoryTest.java b/src/test/java/bg/softuni/invoice/repository/InvoiceRepositoryTest.java index 50cb51d..523ff34 100644 --- a/src/test/java/bg/softuni/invoice/repository/InvoiceRepositoryTest.java +++ b/src/test/java/bg/softuni/invoice/repository/InvoiceRepositoryTest.java @@ -9,7 +9,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest; import java.math.BigDecimal; import java.time.LocalDate; @@ -84,7 +84,7 @@ void testGetAllByUser_withDifferentUser_returnsOnlyRelevantInvoices() { List resultForUser2 = invoiceRepository.getAllByUser(user2); assertThat(resultForUser1).hasSize(1); - assertThat(resultForUser1.get(0).getTotalValue()).isEqualByComparingTo("200.00"); + assertThat(resultForUser1.getFirst().getTotalValue()).isEqualByComparingTo("200.00"); assertThat(resultForUser2).isEmpty(); } diff --git a/src/test/java/bg/softuni/invoice/repository/ItemRepositoryTest.java b/src/test/java/bg/softuni/invoice/repository/ItemRepositoryTest.java index 3fd08c5..da8a911 100644 --- a/src/test/java/bg/softuni/invoice/repository/ItemRepositoryTest.java +++ b/src/test/java/bg/softuni/invoice/repository/ItemRepositoryTest.java @@ -5,7 +5,7 @@ import jakarta.persistence.EntityManager; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest; import java.math.BigDecimal; import java.util.Optional; diff --git a/src/test/java/bg/softuni/invoice/repository/LogRepositoryTest.java b/src/test/java/bg/softuni/invoice/repository/LogRepositoryTest.java index 060f377..7119912 100644 --- a/src/test/java/bg/softuni/invoice/repository/LogRepositoryTest.java +++ b/src/test/java/bg/softuni/invoice/repository/LogRepositoryTest.java @@ -5,7 +5,7 @@ import jakarta.persistence.EntityManager; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest; import java.time.LocalDateTime; import java.util.List; diff --git a/src/test/java/bg/softuni/invoice/repository/RoleRepositoryTest.java b/src/test/java/bg/softuni/invoice/repository/RoleRepositoryTest.java index 7770348..4cf667e 100644 --- a/src/test/java/bg/softuni/invoice/repository/RoleRepositoryTest.java +++ b/src/test/java/bg/softuni/invoice/repository/RoleRepositoryTest.java @@ -4,7 +4,7 @@ import jakarta.persistence.EntityManager; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest; import java.util.Optional; diff --git a/src/test/java/bg/softuni/invoice/repository/UserRepositoryTest.java b/src/test/java/bg/softuni/invoice/repository/UserRepositoryTest.java index 9ba4089..d82d89c 100644 --- a/src/test/java/bg/softuni/invoice/repository/UserRepositoryTest.java +++ b/src/test/java/bg/softuni/invoice/repository/UserRepositoryTest.java @@ -4,7 +4,7 @@ import jakarta.persistence.EntityManager; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest; import java.util.Optional; diff --git a/src/test/java/bg/softuni/invoice/web/controller/CompanyControllerTest.java b/src/test/java/bg/softuni/invoice/web/controller/CompanyControllerTest.java index 37f7147..4b5be1a 100644 --- a/src/test/java/bg/softuni/invoice/web/controller/CompanyControllerTest.java +++ b/src/test/java/bg/softuni/invoice/web/controller/CompanyControllerTest.java @@ -10,18 +10,20 @@ import org.mockito.Mockito; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; import org.springframework.http.MediaType; -import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.flash; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; -@WebMvcTest(CompanyController.class) +@SpringBootTest +@AutoConfigureMockMvc class CompanyControllerTest { @Autowired @@ -43,7 +45,6 @@ class CompanyControllerTest { private ItemService itemService; @Test - @WithMockUser(roles = {"ADMIN"}) void testAddConfirm_withValidInput_shouldRedirectToAll() throws Exception { CompanyAddBindingModel validModel = new CompanyAddBindingModel(); validModel.setName("Valid Name"); @@ -59,7 +60,7 @@ void testAddConfirm_withValidInput_shouldRedirectToAll() throws Exception { Mockito.when(companyService.getCompanyByUniqueIdentifier(validModel.getUniqueIdentifier())).thenReturn(null); Mockito.when(modelMapper.map(validModel, CompanyServiceModel.class)).thenReturn(serviceModel); - mockMvc.perform(post("/company/add") + mockMvc.perform(post("/company/add").with(user("test-admin").roles("ADMIN")) .contentType(MediaType.APPLICATION_FORM_URLENCODED) .with(csrf()) .param("name", validModel.getName()) @@ -69,7 +70,6 @@ void testAddConfirm_withValidInput_shouldRedirectToAll() throws Exception { } @Test - @WithMockUser(roles = {"ADMIN"}) void testAddConfirm_withNameConflict_shouldRedirectToAdd() throws Exception { CompanyAddBindingModel conflictingModel = new CompanyAddBindingModel(); conflictingModel.setName("Existing Company"); @@ -81,7 +81,7 @@ void testAddConfirm_withNameConflict_shouldRedirectToAdd() throws Exception { Mockito.when(companyService.getCompanyByName(conflictingModel.getName())).thenReturn(conflictingServiceModel); - mockMvc.perform(post("/company/add") + mockMvc.perform(post("/company/add").with(user("test-admin").roles("ADMIN")) .contentType(MediaType.APPLICATION_FORM_URLENCODED) .with(csrf()) .param("name", conflictingModel.getName()) @@ -93,7 +93,6 @@ void testAddConfirm_withNameConflict_shouldRedirectToAdd() throws Exception { } @Test - @WithMockUser(roles = {"ADMIN"}) void testAddConfirm_withUniqueIdentifierConflict_shouldRedirectToAdd() throws Exception { CompanyAddBindingModel conflictingModel = new CompanyAddBindingModel(); conflictingModel.setName("New Company"); @@ -106,7 +105,7 @@ void testAddConfirm_withUniqueIdentifierConflict_shouldRedirectToAdd() throws Ex Mockito.when(companyService.getCompanyByName(conflictingModel.getName())).thenReturn(null); Mockito.when(companyService.getCompanyByUniqueIdentifier(conflictingModel.getUniqueIdentifier())).thenReturn(conflictingServiceModel); - mockMvc.perform(post("/company/add") + mockMvc.perform(post("/company/add").with(user("test-admin").roles("ADMIN")) .contentType(MediaType.APPLICATION_FORM_URLENCODED) .with(csrf()) .param("name", conflictingModel.getName()) @@ -118,9 +117,8 @@ void testAddConfirm_withUniqueIdentifierConflict_shouldRedirectToAdd() throws Ex } @Test - @WithMockUser(roles = {"ADMIN"}) void testAddConfirm_withValidationErrors_shouldRedirectToAdd() throws Exception { - mockMvc.perform(post("/company/add") + mockMvc.perform(post("/company/add").with(user("test-admin").roles("ADMIN")) .contentType(MediaType.APPLICATION_FORM_URLENCODED) .with(csrf()) .param("name", "") diff --git a/src/test/java/bg/softuni/invoice/controller/HomeControllerTest.java b/src/test/java/bg/softuni/invoice/web/controller/HomeControllerTest.java similarity index 73% rename from src/test/java/bg/softuni/invoice/controller/HomeControllerTest.java rename to src/test/java/bg/softuni/invoice/web/controller/HomeControllerTest.java index 4ca8c47..23fa6c2 100644 --- a/src/test/java/bg/softuni/invoice/controller/HomeControllerTest.java +++ b/src/test/java/bg/softuni/invoice/web/controller/HomeControllerTest.java @@ -1,18 +1,16 @@ -package bg.softuni.invoice.controller; +package bg.softuni.invoice.web.controller; 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.security.test.context.support.WithMockUser; +import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; import org.springframework.test.web.servlet.MockMvc; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; - @SpringBootTest @AutoConfigureMockMvc class HomeControllerTest { @@ -24,22 +22,22 @@ class HomeControllerTest { void index_shouldReturnCorrectView() throws Exception { this.mockMvc .perform(get("/")) + .andExpect(status().isOk()) .andExpect(view().name("home/index")); } @Test - @WithMockUser void index_withAuthenticatedUserShouldReturnCorrectView() throws Exception { this.mockMvc - .perform(get("/")) + .perform(get("/").with(user("test-user").roles("USER"))) + .andExpect(status().isOk()) .andExpect(view().name("home/home")); } @Test - @WithMockUser() void home_shouldReturnCorrectView() throws Exception { this.mockMvc - .perform(get("/home").with(csrf())) + .perform(get("/home").with(user("test-user").roles("USER"))) .andExpect(status().is3xxRedirection()) .andExpect(view().name("redirect:/invoice/all")); } @@ -48,14 +46,15 @@ void home_shouldReturnCorrectView() throws Exception { void index_withNullPrincipal_shouldReturnIndexView() throws Exception { this.mockMvc .perform(get("/")) + .andExpect(status().isOk()) .andExpect(view().name("home/index")); } @Test - @WithMockUser void index_withAuthenticatedPrincipal_shouldReturnHomeView() throws Exception { this.mockMvc - .perform(get("/")) + .perform(get("/").with(user("test-user").roles("USER"))) + .andExpect(status().isOk()) .andExpect(view().name("home/home")); } } diff --git a/src/test/java/bg/softuni/invoice/controller/LogControllerTest.java b/src/test/java/bg/softuni/invoice/web/controller/LogControllerTest.java similarity index 64% rename from src/test/java/bg/softuni/invoice/controller/LogControllerTest.java rename to src/test/java/bg/softuni/invoice/web/controller/LogControllerTest.java index e7ae2a6..81ac88a 100644 --- a/src/test/java/bg/softuni/invoice/controller/LogControllerTest.java +++ b/src/test/java/bg/softuni/invoice/web/controller/LogControllerTest.java @@ -1,17 +1,16 @@ -package bg.softuni.invoice.controller; +package bg.softuni.invoice.web.controller; 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.security.test.context.support.WithMockUser; +import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; import org.springframework.test.web.servlet.MockMvc; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; - @SpringBootTest @AutoConfigureMockMvc class LogControllerTest { @@ -20,10 +19,10 @@ class LogControllerTest { private MockMvc mockMvc; @Test - @WithMockUser(roles = {"ROOT"}) void all_shouldReturnCorrectView() throws Exception { this.mockMvc - .perform(get("/log/all").with(csrf())) + .perform(get("/log/all").with(user("test-root").roles("ROOT"))) + .andExpect(status().isOk()) .andExpect(view().name("log/all")); } -} \ No newline at end of file +} diff --git a/src/test/java/bg/softuni/invoice/controller/UserControllerTest.java b/src/test/java/bg/softuni/invoice/web/controller/UserControllerTest.java similarity index 83% rename from src/test/java/bg/softuni/invoice/controller/UserControllerTest.java rename to src/test/java/bg/softuni/invoice/web/controller/UserControllerTest.java index 185b69d..b7a5449 100644 --- a/src/test/java/bg/softuni/invoice/controller/UserControllerTest.java +++ b/src/test/java/bg/softuni/invoice/web/controller/UserControllerTest.java @@ -1,4 +1,4 @@ -package bg.softuni.invoice.controller; +package bg.softuni.invoice.web.controller; import bg.softuni.invoice.repository.UserRepository; import bg.softuni.invoice.service.RoleService; @@ -6,12 +6,12 @@ import org.junit.jupiter.api.Assertions; 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.security.test.context.support.WithMockUser; +import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; @@ -45,10 +45,9 @@ void login_shouldReturnCorrectView() throws Exception { } @Test - @WithMockUser void login_withLoggedInUserShouldReturnErrorView() throws Exception { this.mockMvc - .perform(get("/user/login")) + .perform(get("/user/login").with(user("test-user").roles("USER"))) .andExpect(status().is4xxClientError()) .andExpect(forwardedUrl("/error")); } @@ -64,7 +63,8 @@ void register_shouldReturnCorrectView() throws Exception { @Test void register_registerSuccessfullyRedirect() throws Exception { this.mockMvc - .perform(post("/user/register").with(csrf()) + .perform(post("/user/register") + .with(csrf()) .param("username", "admin@admin.com") .param("firstName", "Admin") .param("lastName", "Admin") @@ -80,7 +80,8 @@ void register_registerSuccessfullyRedirect() throws Exception { @Test void register_whenBindingResultHasErrorsRedirect() throws Exception { this.mockMvc - .perform(post("/user/register").with(csrf()) + .perform(post("/user/register") + .with(csrf()) .param("username", "") .param("firstName", "") .param("lastName", "") @@ -95,7 +96,8 @@ void register_whenBindingResultHasErrorsRedirect() throws Exception { void register_whenUsernameAlreadyExistsRedirect() throws Exception { this.mockMvc - .perform(post("/user/register").with(csrf()) + .perform(post("/user/register") + .with(csrf()) .param("username", "admin@admin.com") .param("firstName", "Admin") .param("lastName", "Admin") @@ -104,7 +106,8 @@ void register_whenUsernameAlreadyExistsRedirect() throws Exception { ); this.mockMvc - .perform(post("/user/register").with(csrf()) + .perform(post("/user/register") + .with(csrf()) .param("username", "admin@admin.com") .param("firstName", "Test") .param("lastName", "Test") @@ -116,30 +119,26 @@ void register_whenUsernameAlreadyExistsRedirect() throws Exception { } @Test - @WithMockUser void register_withLoggedInUserShouldReturnErrorView() throws Exception { this.mockMvc - .perform(get("/user/register").with(csrf())) + .perform(get("/user/register").with(user("test-user").roles("USER"))) .andExpect(status().is4xxClientError()) .andExpect(forwardedUrl("/error")); } @Test - @WithMockUser(roles = "ROOT") void all_withLoggedInRootUserShouldReturnCorrectView() throws Exception { this.mockMvc - .perform(get("/user/all").with(csrf())) + .perform(get("/user/all").with(user("test-root").roles("ROOT"))) .andExpect(model().attributeExists("users")) .andExpect(model().attributeExists("comparator")) .andExpect(view().name("user/all")); } @Test - @WithMockUser(roles = "ADMIN") void all_withLoggedInAdminOrUserShouldReturnCorrectViewAdmin() throws Exception { this.mockMvc - .perform(get("/user/all").with(csrf())) + .perform(get("/user/all").with(user("test-admin").roles("ADMIN"))) .andExpect(view().name("error")); } - } diff --git a/src/test/java/bg/softuni/invoice/web/error/GlobalExceptionHandlerTest.java b/src/test/java/bg/softuni/invoice/web/error/GlobalExceptionHandlerTest.java index 5278194..b5dfcc5 100644 --- a/src/test/java/bg/softuni/invoice/web/error/GlobalExceptionHandlerTest.java +++ b/src/test/java/bg/softuni/invoice/web/error/GlobalExceptionHandlerTest.java @@ -2,8 +2,8 @@ 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.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; import org.springframework.test.web.servlet.MockMvc; import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.Model; @@ -11,7 +11,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @@ -28,7 +28,7 @@ class GlobalExceptionHandlerTest { void handleAllErrors_whenFailureUrl_redirectsToLoginPage() throws Exception { mockMvc.perform(get("/a-nonexistent-url")) .andExpect(status().is3xxRedirection()) - .andExpect(redirectedUrlPattern("**/user/login")); + .andExpect(redirectedUrl("/user/login")); } @Test diff --git a/system.properties b/system.properties index 0dc726c..ec9df9a 100644 --- a/system.properties +++ b/system.properties @@ -1 +1 @@ -java.runtime.version=17 \ No newline at end of file +java.runtime.version=21 \ No newline at end of file