diff --git a/src/main/java/com/linkurlshorter/urlshortener/security/CustomUserDetailsService.java b/src/main/java/com/linkurlshorter/urlshortener/security/CustomUserDetailsService.java index 42d2f58..563e7be 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/security/CustomUserDetailsService.java +++ b/src/main/java/com/linkurlshorter/urlshortener/security/CustomUserDetailsService.java @@ -1,10 +1,11 @@ package com.linkurlshorter.urlshortener.security; -import com.linkurlshorter.urlshortener.user.model.User; -import com.linkurlshorter.urlshortener.user.UserService; + +import com.linkurlshorter.urlshortener.user.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; /** @@ -16,7 +17,7 @@ * * @author Egor Sivenko * @see org.springframework.security.core.userdetails.UserDetailsService - * @see UserService + * @see UserRepository * @see SecurityUserDetails */ @Service @@ -24,9 +25,9 @@ public class CustomUserDetailsService implements UserDetailsService { /** - * Service for managing user-related operations. + * Repository for managing user-related operations. */ - private final UserService userService; + private final UserRepository userRepository; /** * Loads user details by their email address. @@ -36,7 +37,9 @@ public class CustomUserDetailsService implements UserDetailsService { */ @Override public UserDetails loadUserByUsername(String email) { - User user = userService.findByEmail(email); - return new SecurityUserDetails(user); + return userRepository + .findByEmail(email) + .map(SecurityUserDetails::new) + .orElseThrow(() -> new UsernameNotFoundException(email)); } } diff --git a/src/test/java/com/linkurlshorter/urlshortener/TestConfig.java b/src/test/java/com/linkurlshorter/urlshortener/TestConfig.java index fb6841b..420c93c 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/TestConfig.java +++ b/src/test/java/com/linkurlshorter/urlshortener/TestConfig.java @@ -37,14 +37,14 @@ public JwtUtil jwtUtil() { } /** - * Creates a bean for CustomUserDetailsService with a mocked UserService dependency. + * Creates a bean for CustomUserDetailsService with a mocked UserRepository dependency. * - * @param userService UserService mock bean - * @return CustomUserDetailsService bean with mocked UserService dependency + * @param userRepository UserRepository mock bean + * @return CustomUserDetailsService bean with mocked UserRepository dependency */ @Bean - public CustomUserDetailsService customUserDetailsService(UserService userService) { - return new CustomUserDetailsService(userService); + public CustomUserDetailsService customUserDetailsService(UserRepository userRepository) { + return new CustomUserDetailsService(userRepository); } /** diff --git a/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java b/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java index af236b1..9c72c67 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java @@ -76,7 +76,7 @@ void loginFailedWhenUserDoesNotExistTest() throws Exception { .content(objectMapper.writeValueAsString(authRequest))) .andExpect(MockMvcResultMatchers.status().is4xxClientError()) .andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(401)) - .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("No user by provided email found")); + .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Bad credentials")); } /**