From 41fe91dcce36ec7f9c857ceeacd182c42401c7d8 Mon Sep 17 00:00:00 2001 From: nastiausenko Date: Wed, 17 Apr 2024 17:32:52 +0300 Subject: [PATCH 1/2] refactor change password tests --- .../urlshortener/user/UserControllerTest.java | 134 ++++++++++-------- 1 file changed, 72 insertions(+), 62 deletions(-) diff --git a/src/test/java/com/linkurlshorter/urlshortener/user/UserControllerTest.java b/src/test/java/com/linkurlshorter/urlshortener/user/UserControllerTest.java index 15e64a2..47874e0 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/user/UserControllerTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/user/UserControllerTest.java @@ -1,28 +1,28 @@ package com.linkurlshorter.urlshortener.user; import com.fasterxml.jackson.databind.ObjectMapper; -import com.linkurlshorter.urlshortener.auth.AuthController; import com.linkurlshorter.urlshortener.auth.dto.AuthRequest; +import com.linkurlshorter.urlshortener.jwt.JwtUtil; +import com.linkurlshorter.urlshortener.security.CustomUserDetailsService; +import com.linkurlshorter.urlshortener.security.SecurityUserDetails; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; 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.testcontainers.service.connection.ServiceConnection; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; +import org.springframework.test.web.servlet.ResultActions; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; 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.result.MockMvcResultMatchers.*; /** * Unit tests for {@link UserController} class. @@ -30,90 +30,100 @@ * @author Anastasiia Usenko */ @SpringBootTest -@AutoConfigureMockMvc(addFilters = false) -@Testcontainers +@AutoConfigureMockMvc @ExtendWith(MockitoExtension.class) class UserControllerTest { - @Container - @ServiceConnection - static PostgreSQLContainer container = new PostgreSQLContainer<>("postgres:16.0-alpine"); - @Autowired private MockMvc mockMvc; @Autowired private ObjectMapper objectMapper; - @Autowired - private AuthController authController; - private AuthRequest authRequest; - private Authentication authentication; + @MockBean + private UserService userService; + + @MockBean + private JwtUtil jwtUtil; + + @MockBean + private CustomUserDetailsService customUserDetailsService; + + + private User user; /** * Test case for the {@link UserController#changePassword(ChangeUserPasswordRequest)} method. */ @Test - void changePasswordTest() throws Exception { + @WithMockUser + void testChangePassword() throws Exception { + given(jwtUtil.getEmailFromToken(any(String.class))).willReturn("test@example.com"); + given(userService.updateByEmailDynamically(any(User.class), any(String.class))).willReturn(1); + ChangeUserPasswordRequest request = new ChangeUserPasswordRequest("newPassword1"); - authRequest = new AuthRequest("test@email.com", "Password1"); - authentication = new UsernamePasswordAuthenticationToken(authRequest.getEmail(), authRequest.getPassword()); - SecurityContextHolder.getContext().setAuthentication(authentication); - authController.register(authRequest); - mockMvc.perform(post("/api/V1/user/change-password") - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(request))) - .andExpect(status().isOk()) + ResultActions resultActions = mockMvc.perform(post("/api/V1/user/change-password") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request))); + + resultActions.andExpect(status().isOk()) .andExpect(jsonPath("$.error").value("ok")); } + /** * Test case for the {@link UserController#changePassword(ChangeUserPasswordRequest)} method when the user with the * provided email is not found. */ @Test + @WithMockUser void changePasswordFailedTest() throws Exception { ChangeUserPasswordRequest request = new ChangeUserPasswordRequest("newPassword1"); - authentication = new UsernamePasswordAuthenticationToken("failed@email.com", "PAssWORD1"); - SecurityContextHolder.getContext().setAuthentication(authentication); + given(jwtUtil.getEmailFromToken(any(String.class))).willReturn("failed@email.com"); + given(userService.updateByEmailDynamically(any(User.class), any(String.class))).willReturn(0); - mockMvc.perform(post("/api/V1/user/change-password") + ResultActions resultActions = mockMvc.perform(post("/api/V1/user/change-password") .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(request))) - .andExpect(status().isNotFound()); + .content(objectMapper.writeValueAsString(request))); + + resultActions.andExpect(status().isNotFound()); } /** * Test case for the {@link UserController#changeEmail(ChangeUserEmailRequest)} method. */ - @Test - void changeEmailTest() throws Exception { - ChangeUserEmailRequest request = new ChangeUserEmailRequest("success@email.com"); - authRequest = new AuthRequest("test1@email.com", "Password1"); - authentication = new UsernamePasswordAuthenticationToken(authRequest.getEmail(), authRequest.getPassword()); - SecurityContextHolder.getContext().setAuthentication(authentication); - authController.register(authRequest); - - mockMvc.perform(post("/api/V1/user/change-email") - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(request))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.error").value("ok")); - } +// @Test +// @WithMockUser +// void changeEmailTest() throws Exception { +// ChangeUserEmailRequest request = new ChangeUserEmailRequest("newEmail@khj.gf"); +// User user = User.builder() +// .email(request.getNewEmail()) +// .build(); +// given(userService.findByEmail(anyString())).willReturn(user); +// given(userService.updateByEmailDynamically(any(User.class), anyString())).willReturn(1); +// +// ResultActions resultActions = mockMvc.perform(post("/api/V1/user/change-email") +// .contentType(MediaType.APPLICATION_JSON) +// .content(objectMapper.writeValueAsString(request))); +// +// resultActions.andExpect(status().isOk()); +// } - /** - * Test case for the {@link UserController#changeEmail(ChangeUserEmailRequest)} method when the user with the - * provided email is not found. - */ - @Test - void changeEmailFailedTest() throws Exception { - ChangeUserEmailRequest request = new ChangeUserEmailRequest("failed@email.com"); - authentication = new UsernamePasswordAuthenticationToken("user@email.com", "PAssWORD1"); - SecurityContextHolder.getContext().setAuthentication(authentication); - mockMvc.perform(post("/api/V1/user/change-email") - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(request))) - .andExpect(status().isNotFound()); - } +// +// /** +// * Test case for the {@link UserController#changeEmail(ChangeUserEmailRequest)} method when the user with the +// * provided email is not found. +// */ +// @Test +// void changeEmailFailedTest() throws Exception { +// ChangeUserEmailRequest request = new ChangeUserEmailRequest("failed@email.com"); +// authentication = new UsernamePasswordAuthenticationToken("user@email.com", "PAssWORD1"); +// SecurityContextHolder.getContext().setAuthentication(authentication); +// +// mockMvc.perform(post("/api/V1/user/change-email") +// .contentType(MediaType.APPLICATION_JSON) +// .content(objectMapper.writeValueAsString(request))) +// .andExpect(status().isNotFound()); +// } } From 4aeef7f6b9c480024c740f757276858fe3857433 Mon Sep 17 00:00:00 2001 From: nastiausenko Date: Wed, 17 Apr 2024 17:59:47 +0300 Subject: [PATCH 2/2] refactor change email tests --- .../urlshortener/user/UserControllerTest.java | 93 +++++++++++-------- 1 file changed, 55 insertions(+), 38 deletions(-) diff --git a/src/test/java/com/linkurlshorter/urlshortener/user/UserControllerTest.java b/src/test/java/com/linkurlshorter/urlshortener/user/UserControllerTest.java index 47874e0..46132c6 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/user/UserControllerTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/user/UserControllerTest.java @@ -1,7 +1,6 @@ package com.linkurlshorter.urlshortener.user; import com.fasterxml.jackson.databind.ObjectMapper; -import com.linkurlshorter.urlshortener.auth.dto.AuthRequest; import com.linkurlshorter.urlshortener.jwt.JwtUtil; import com.linkurlshorter.urlshortener.security.CustomUserDetailsService; import com.linkurlshorter.urlshortener.security.SecurityUserDetails; @@ -14,12 +13,16 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; +import java.util.UUID; + import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.BDDMockito.given; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @@ -48,16 +51,28 @@ class UserControllerTest { @MockBean private CustomUserDetailsService customUserDetailsService; - private User user; + /** + * Set up method to initialize test data before each test method. + */ + @BeforeEach + void setUp() { + user = User.builder() + .id(UUID.fromString("84991c79-f6a9-4b7b-b1b4-0d66c0b92c81")) + .email("test1@gmail.com") + .password("Password1") + .role(UserRole.USER) + .build(); + } + /** * Test case for the {@link UserController#changePassword(ChangeUserPasswordRequest)} method. */ @Test @WithMockUser void testChangePassword() throws Exception { - given(jwtUtil.getEmailFromToken(any(String.class))).willReturn("test@example.com"); + given(jwtUtil.getEmailFromToken(any(String.class))).willReturn(user.getEmail()); given(userService.updateByEmailDynamically(any(User.class), any(String.class))).willReturn(1); ChangeUserPasswordRequest request = new ChangeUserPasswordRequest("newPassword1"); @@ -70,7 +85,6 @@ void testChangePassword() throws Exception { .andExpect(jsonPath("$.error").value("ok")); } - /** * Test case for the {@link UserController#changePassword(ChangeUserPasswordRequest)} method when the user with the * provided email is not found. @@ -92,38 +106,41 @@ void changePasswordFailedTest() throws Exception { /** * Test case for the {@link UserController#changeEmail(ChangeUserEmailRequest)} method. */ -// @Test -// @WithMockUser -// void changeEmailTest() throws Exception { -// ChangeUserEmailRequest request = new ChangeUserEmailRequest("newEmail@khj.gf"); -// User user = User.builder() -// .email(request.getNewEmail()) -// .build(); -// given(userService.findByEmail(anyString())).willReturn(user); -// given(userService.updateByEmailDynamically(any(User.class), anyString())).willReturn(1); -// -// ResultActions resultActions = mockMvc.perform(post("/api/V1/user/change-email") -// .contentType(MediaType.APPLICATION_JSON) -// .content(objectMapper.writeValueAsString(request))); -// -// resultActions.andExpect(status().isOk()); -// } - - -// -// /** -// * Test case for the {@link UserController#changeEmail(ChangeUserEmailRequest)} method when the user with the -// * provided email is not found. -// */ -// @Test -// void changeEmailFailedTest() throws Exception { -// ChangeUserEmailRequest request = new ChangeUserEmailRequest("failed@email.com"); -// authentication = new UsernamePasswordAuthenticationToken("user@email.com", "PAssWORD1"); -// SecurityContextHolder.getContext().setAuthentication(authentication); -// -// mockMvc.perform(post("/api/V1/user/change-email") -// .contentType(MediaType.APPLICATION_JSON) -// .content(objectMapper.writeValueAsString(request))) -// .andExpect(status().isNotFound()); -// } + @Test + @WithMockUser + void changeEmailTest() throws Exception { + given(jwtUtil.getEmailFromToken(any(String.class))).willReturn(user.getEmail()); + given(userService.updateByEmailDynamically(any(User.class), anyString())).willReturn(1); + + ChangeUserEmailRequest request = new ChangeUserEmailRequest("newEmail@example.com"); + UserDetails userDetails = new SecurityUserDetails(user); + given(customUserDetailsService.loadUserByUsername(anyString())).willReturn(userDetails); + + ResultActions resultActions = mockMvc.perform(post("/api/V1/user/change-email") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request))); + + resultActions.andExpect(status().isOk()) + .andExpect(jsonPath("$.error").value("ok")); + } + + /** + * Test case for the {@link UserController#changeEmail(ChangeUserEmailRequest)} method when the user with the + * provided email is not found. + */ + @Test + @WithMockUser + void changeEmailFailedTest() throws Exception { + given(jwtUtil.getEmailFromToken(any(String.class))).willReturn("nonExistent@email.com"); + given(userService.updateByEmailDynamically(any(User.class), anyString())).willReturn(0); + + ChangeUserEmailRequest request = new ChangeUserEmailRequest("failed@email.com"); + UserDetails userDetails = new SecurityUserDetails(user); + given(customUserDetailsService.loadUserByUsername(anyString())).willReturn(userDetails); + + mockMvc.perform(post("/api/V1/user/change-email") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request))) + .andExpect(status().isNotFound()); + } }