From 79f0ae80de64b42c51da53b042b1ad672286d39a Mon Sep 17 00:00:00 2001 From: Muhammad Raihan Akbar <ianakbar711@gmail.com> Date: Thu, 6 Mar 2025 21:09:57 +0700 Subject: [PATCH] [REFACTOR] Adjusted tests accordingly --- .../AuthenticationControllerTest.java | 153 +++++---- .../service/AuthenticationServiceTest.java | 304 ++++-------------- 2 files changed, 153 insertions(+), 304 deletions(-) diff --git a/src/test/java/com/safetypin/authentication/controller/AuthenticationControllerTest.java b/src/test/java/com/safetypin/authentication/controller/AuthenticationControllerTest.java index b0d152a..9323889 100644 --- a/src/test/java/com/safetypin/authentication/controller/AuthenticationControllerTest.java +++ b/src/test/java/com/safetypin/authentication/controller/AuthenticationControllerTest.java @@ -4,13 +4,14 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.safetypin.authentication.dto.GoogleAuthDTO; import com.safetypin.authentication.dto.PasswordResetRequest; import com.safetypin.authentication.dto.RegistrationRequest; -import com.safetypin.authentication.dto.SocialLoginRequest; +import com.safetypin.authentication.dto.UserResponse; +import com.safetypin.authentication.exception.InvalidCredentialsException; import com.safetypin.authentication.exception.UserAlreadyExistsException; import com.safetypin.authentication.model.User; import com.safetypin.authentication.service.AuthenticationService; import com.safetypin.authentication.service.GoogleAuthService; +import com.safetypin.authentication.service.JwtService; import org.junit.jupiter.api.Test; -import org.mockito.Mock; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -44,6 +45,9 @@ class AuthenticationControllerTest { @Autowired private GoogleAuthService googleAuthService; + @Autowired + private JwtService jwtService; + @Autowired private ObjectMapper objectMapper; @@ -58,6 +62,11 @@ class AuthenticationControllerTest { public GoogleAuthService googleAuthService() { return Mockito.mock(GoogleAuthService.class); } + + @Bean + public JwtService jwtService() { + return Mockito.mock(JwtService.class); + } } @TestConfiguration @@ -89,7 +98,7 @@ class AuthenticationControllerTest { UUID id = UUID.randomUUID(); user.setId(id); - String token = authenticationService.generateJwtToken(user.getId()); + String token = jwtService.generateToken(user.getId()); Mockito.when(authenticationService.registerUser(any(RegistrationRequest.class))).thenReturn(token); mockMvc.perform(post("/api/auth/register-email") @@ -99,37 +108,6 @@ class AuthenticationControllerTest { .andExpect(jsonPath("$.data.token").value(token)); } - @Test - void testRegisterSocial() throws Exception { - SocialLoginRequest request = new SocialLoginRequest(); - request.setProvider("GOOGLE"); - request.setSocialToken("token"); - request.setEmail("social@example.com"); - request.setName("Social User"); - request.setBirthdate(LocalDate.now().minusYears(25)); - request.setSocialId("social123"); - - User user = new User(); - user.setEmail("social@example.com"); - user.setPassword(null); - user.setName("Social User"); - user.setVerified(true); - user.setRole("USER"); - user.setBirthdate(request.getBirthdate()); - user.setProvider("GOOGLE"); - - UUID id = UUID.randomUUID(); - user.setId(id); - String token = authenticationService.generateJwtToken(user.getId()); - Mockito.when(authenticationService.socialLogin(any(SocialLoginRequest.class))).thenReturn(token); - - mockMvc.perform(post("/api/auth/register-social") - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(request))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.data.token").value(token)); - } - @Test void testLoginEmail() throws Exception { User user = new User(); @@ -143,7 +121,7 @@ class AuthenticationControllerTest { UUID id = UUID.randomUUID(); user.setId(id); - String token = authenticationService.generateJwtToken(user.getId()); + String token = jwtService.generateToken(user.getId()); Mockito.when(authenticationService.loginUser("email@example.com", "password")).thenReturn(token); mockMvc.perform(post("/api/auth/login-email") @@ -154,27 +132,26 @@ class AuthenticationControllerTest { } @Test - void testLoginSocial() throws Exception { - User user = new User(); - user.setEmail("social@example.com"); - user.setPassword(null); - user.setName("Social User"); - user.setVerified(true); - user.setRole("USER"); - user.setBirthdate(LocalDate.now().minusYears(25)); - user.setProvider("GOOGLE"); + void testLoginEmail_InvalidCredentials() throws Exception { + // Prepare test data + String email = "email@example.com"; + String password = "invalidPassword"; - UUID id = UUID.randomUUID(); - user.setId(id); - String token = authenticationService.generateJwtToken(user.getId()); - Mockito.when(authenticationService.loginSocial("social@example.com")).thenReturn(token); + // Mock the service method to throw InvalidCredentialsException + Mockito.when(authenticationService.loginUser(email, password)) + .thenThrow(new InvalidCredentialsException("Invalid email or password")); - mockMvc.perform(post("/api/auth/login-social") - .param("email", "social@example.com")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.data.token").value(token)); + // Perform the test + mockMvc.perform(post("/api/auth/login-email") + .param("email", email) + .param("password", password)) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.success").value(false)) + .andExpect(jsonPath("$.message").value("Invalid email or password")) + .andExpect(jsonPath("$.data").doesNotExist()); // No data expected in case of error } + @Test void testVerifyOTP_Success() throws Exception { Mockito.when(authenticationService.verifyOTP("email@example.com", "123456")).thenReturn(true); @@ -213,16 +190,29 @@ class AuthenticationControllerTest { .andExpect(content().string("Password reset instructions have been sent to your email (simulated)")); } + @Test - void testPostContent() throws Exception { - Mockito.when(authenticationService.postContent("email@example.com", "Test Content")) - .thenReturn("Content posted successfully"); + void testRegisterEmail_UserAlreadyExistsException() throws Exception { + // Prepare registration request + RegistrationRequest request = new RegistrationRequest(); + request.setEmail("existing@example.com"); + request.setPassword("password"); + request.setName("Test User"); + request.setBirthdate(LocalDate.now().minusYears(20)); - mockMvc.perform(post("/api/auth/post") - .param("email", "email@example.com") - .param("content", "Test Content")) - .andExpect(status().isOk()) - .andExpect(content().string("Content posted successfully")); + // Mock the service to throw UserAlreadyExistsException + String errorMessage = "User with email already exists"; + Mockito.when(authenticationService.registerUser(Mockito.any(RegistrationRequest.class))) + .thenThrow(new UserAlreadyExistsException(errorMessage)); + + // Perform the POST request to /register-email endpoint + mockMvc.perform(post("/api/auth/register-email") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request))) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.success").value(false)) + .andExpect(jsonPath("$.message").value(errorMessage)) + .andExpect(jsonPath("$.data").doesNotExist()); } @Test @@ -331,4 +321,47 @@ class AuthenticationControllerTest { .content("{}")) .andExpect(status().isBadRequest()); } + + @Test + void testVerifyJwtToken() throws Exception { + // Prepare test data + String validToken = "valid.jwt.token"; + + // Create a mock UserResponse + UserResponse userResponse = Mockito.mock(UserResponse.class); + UUID userId = UUID.randomUUID(); + + // Set up basic behavior for the mock + Mockito.when(userResponse.getId()).thenReturn(userId); + Mockito.when(userResponse.getEmail()).thenReturn("test@example.com"); + Mockito.when(userResponse.getName()).thenReturn("Test User"); + + // Mock the service method to return the mocked user response + Mockito.when(jwtService.getUserFromJwtToken(validToken)).thenReturn(userResponse); + + // Perform the test + mockMvc.perform(post("/api/auth/verify-jwt") + .param("token", validToken)) + .andExpect(jsonPath("$.success").value(true)) + .andExpect(jsonPath("$.message").value("OK")); + } + + @Test + void testVerifyJwtToken_InvalidToken() throws Exception { + // Prepare test data + String invalidToken = "invalid.jwt.token"; + + // Mock the service method to throw InvalidCredentialsException + Mockito.when(jwtService.getUserFromJwtToken(invalidToken)) + .thenThrow(new InvalidCredentialsException("Invalid token")); + + // Perform the test + mockMvc.perform(post("/api/auth/verify-jwt") + .param("token", invalidToken)) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.success").value(false)) + .andExpect(jsonPath("$.message").value("Invalid token")) + .andExpect(jsonPath("$.data").doesNotExist()); // No data expected in case of error + } + } diff --git a/src/test/java/com/safetypin/authentication/service/AuthenticationServiceTest.java b/src/test/java/com/safetypin/authentication/service/AuthenticationServiceTest.java index 66e3c5b..24bf8cb 100644 --- a/src/test/java/com/safetypin/authentication/service/AuthenticationServiceTest.java +++ b/src/test/java/com/safetypin/authentication/service/AuthenticationServiceTest.java @@ -1,22 +1,19 @@ package com.safetypin.authentication.service; import com.safetypin.authentication.dto.RegistrationRequest; -import com.safetypin.authentication.dto.SocialLoginRequest; -import com.safetypin.authentication.dto.UserResponse; import com.safetypin.authentication.exception.InvalidCredentialsException; import com.safetypin.authentication.exception.UserAlreadyExistsException; import com.safetypin.authentication.model.User; -import com.safetypin.authentication.repository.UserRepository; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.security.crypto.password.PasswordEncoder; import java.time.LocalDate; -import java.util.UUID; import java.util.Optional; +import java.util.UUID; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; @@ -26,7 +23,7 @@ import static org.mockito.Mockito.*; class AuthenticationServiceTest { @Mock - private UserRepository userRepository; + private UserService userService; @Mock private PasswordEncoder passwordEncoder; @@ -34,11 +31,15 @@ class AuthenticationServiceTest { @Mock private OTPService otpService; - @InjectMocks + @Mock + private JwtService jwtService; + private AuthenticationService authenticationService; - private final String JWT_SECRET_KEY = "5047c55bfe120155fd4e884845682bb8b8815c0048a686cc664d1ea6c8e094da"; - private final long EXPIRATION_TIME = 86400000; + @BeforeEach + void setUp() { + authenticationService = new AuthenticationService(userService, passwordEncoder, otpService, jwtService); + } // registerUser tests @@ -48,7 +49,7 @@ class AuthenticationServiceTest { request.setEmail("test@example.com"); request.setPassword("password"); request.setName("Test User"); - // set birthdate to 17 years old + // set birthdate to 15 years old request.setBirthdate(LocalDate.now().minusYears(15)); Exception exception = assertThrows(IllegalArgumentException.class, () -> @@ -65,7 +66,8 @@ class AuthenticationServiceTest { request.setName("Test User"); request.setBirthdate(LocalDate.now().minusYears(20)); - when(userRepository.findByEmail("test@example.com")).thenReturn(new User()); + User existingUser = new User(); + when(userService.findByEmail("test@example.com")).thenReturn(Optional.of(existingUser)); Exception exception = assertThrows(UserAlreadyExistsException.class, () -> authenticationService.registerUser(request) @@ -81,8 +83,9 @@ class AuthenticationServiceTest { request.setName("Test User"); request.setBirthdate(LocalDate.now().minusYears(20)); - when(userRepository.findByEmail("test@example.com")).thenReturn(null); + when(userService.findByEmail("test@example.com")).thenReturn(Optional.empty()); when(passwordEncoder.encode("password")).thenReturn("encodedPassword"); + User savedUser = new User(); savedUser.setEmail("test@example.com"); savedUser.setPassword("encodedPassword"); @@ -94,157 +97,33 @@ class AuthenticationServiceTest { UUID id = UUID.randomUUID(); savedUser.setId(id); - when(userRepository.save(any(User.class))).thenReturn(savedUser); - when(userRepository.findById(id)).thenReturn(Optional.of(savedUser)); - - String token = authenticationService.registerUser(request); - assertNotNull(token); - UserResponse userResponse = authenticationService.getUserFromJwtToken(token); - assertEquals("test@example.com", userResponse.getEmail()); - // OTPService should be invoked to generate OTP. - verify(otpService, times(1)).generateOTP("test@example.com"); - } - - // socialLogin tests - - @Test - void testSocialLogin_UnderAge() { - SocialLoginRequest request = new SocialLoginRequest(); - request.setEmail("social@example.com"); - request.setName("Social User"); - request.setBirthdate(LocalDate.now().minusYears(15)); - request.setProvider("GOOGLE"); - request.setSocialId("social123"); - request.setSocialToken("token"); - - Exception exception = assertThrows(IllegalArgumentException.class, () -> - authenticationService.socialLogin(request) - ); - assertEquals("User must be at least 16 years old", exception.getMessage()); - } - - @Test - void testSocialLogin_DuplicateEmailWithEmailProvider() { - SocialLoginRequest request = new SocialLoginRequest(); - request.setEmail("social@example.com"); - request.setName("Social User"); - request.setBirthdate(LocalDate.now().minusYears(25)); - request.setProvider("APPLE"); - request.setSocialId("social123"); - request.setSocialToken("token"); - - User existingUser = new User(); - existingUser.setEmail("social@example.com"); - existingUser.setPassword("encodedPassword"); - existingUser.setName("Existing User"); - existingUser.setVerified(false); - existingUser.setRole("USER"); - existingUser.setBirthdate(LocalDate.now().minusYears(30)); - existingUser.setProvider("EMAIL"); - - when(userRepository.findByEmail("social@example.com")).thenReturn(existingUser); - - Exception exception = assertThrows(UserAlreadyExistsException.class, () -> - authenticationService.socialLogin(request) - ); - assertTrue(exception.getMessage().contains("An account with this email exists")); - } - - @Test - void testSocialLogin_ExistingSocialUser() { - SocialLoginRequest request = new SocialLoginRequest(); - request.setEmail("social@example.com"); - request.setName("Social User"); - request.setBirthdate(LocalDate.now().minusYears(25)); - request.setProvider("GOOGLE"); - request.setSocialId("social123"); - request.setSocialToken("token"); - User existingUser = new User(); - existingUser.setEmail("social@example.com"); - existingUser.setPassword(null); - existingUser.setName("Social User"); - existingUser.setVerified(true); - existingUser.setRole("USER"); - existingUser.setBirthdate(LocalDate.now().minusYears(25)); - existingUser.setProvider("GOOGLE"); - UUID id = UUID.randomUUID(); - existingUser.setId(id); - - when(userRepository.findByEmail("social@example.com")).thenReturn(existingUser); - when(userRepository.findById(id)).thenReturn(Optional.of(existingUser)); - when(userRepository.save(any(User.class))).thenReturn(existingUser); - - String token = authenticationService.socialLogin(request); - assertNotNull(token); - UserResponse userResponse = authenticationService.getUserFromJwtToken(token); - - assertEquals("social@example.com", userResponse.getEmail()); - } - - @Test - void testSocialLogin_NewUser() { - SocialLoginRequest request = new SocialLoginRequest(); - request.setEmail("social@example.com"); - request.setName("Social User"); - request.setBirthdate(LocalDate.now().minusYears(25)); - request.setProvider("GOOGLE"); - request.setSocialId("social123"); - request.setSocialToken("token"); - - when(userRepository.findByEmail("social@example.com")).thenReturn(null); - User savedUser = new User(); - savedUser.setEmail("social@example.com"); - savedUser.setPassword(null); - savedUser.setName("Social User"); - savedUser.setVerified(true); - savedUser.setRole("USER"); - savedUser.setBirthdate(request.getBirthdate()); - savedUser.setProvider("GOOGLE"); + when(userService.save(any(User.class))).thenReturn(savedUser); + when(jwtService.generateToken(id)).thenReturn("jwtToken"); - UUID id = UUID.randomUUID(); - savedUser.setId(id); - when(userRepository.save(any(User.class))).thenReturn(savedUser); - when(userRepository.findById(id)).thenReturn(Optional.of(savedUser)); + String token = authenticationService.registerUser(request); - String token = authenticationService.socialLogin(request); assertNotNull(token); - UserResponse userResponse = authenticationService.getUserFromJwtToken(token); - assertEquals("social@example.com", userResponse.getEmail()); + assertEquals("jwtToken", token); + verify(otpService, times(1)).generateOTP("test@example.com"); + verify(userService, times(1)).save(any(User.class)); } // loginUser tests @Test void testLoginUser_EmailNotFound() { - when(userRepository.findByEmail("notfound@example.com")).thenReturn(null); + when(userService.findByEmail("notfound@example.com")).thenReturn(Optional.empty()); + Exception exception = assertThrows(InvalidCredentialsException.class, () -> authenticationService.loginUser("notfound@example.com", "password") ); - assertTrue(exception.getMessage().contains("Invalid email")); - } - - @Test - void testLoginUser_InvalidPassword_NullPassword() { - User user = new User(); - user.setEmail("test@example.com"); - user.setPassword(null); - user.setName("Test User"); - user.setVerified(true); - user.setRole("USER"); - user.setBirthdate(LocalDate.now().minusYears(20)); - user.setProvider("EMAIL"); - when(userRepository.findByEmail("test@example.com")).thenReturn(user); - - Exception exception = assertThrows(InvalidCredentialsException.class, () -> - authenticationService.loginUser("test@example.com", "password") - ); - assertTrue(exception.getMessage().contains("Invalid password")); + assertEquals("Invalid email", exception.getMessage()); } @Test - void testLoginUser_InvalidPassword_WrongMatch() { + void testLoginUser_InvalidPassword() { User user = new User(); user.setEmail("test@example.com"); user.setPassword("encodedPassword"); @@ -254,13 +133,14 @@ class AuthenticationServiceTest { user.setBirthdate(LocalDate.now().minusYears(20)); user.setProvider("EMAIL"); - when(userRepository.findByEmail("test@example.com")).thenReturn(user); + when(userService.findByEmail("test@example.com")).thenReturn(Optional.of(user)); when(passwordEncoder.matches("wrongPassword", "encodedPassword")).thenReturn(false); Exception exception = assertThrows(InvalidCredentialsException.class, () -> authenticationService.loginUser("test@example.com", "wrongPassword") ); - assertTrue(exception.getMessage().contains("Invalid password")); + + assertEquals("Invalid password", exception.getMessage()); } @Test @@ -275,51 +155,16 @@ class AuthenticationServiceTest { user.setProvider("EMAIL"); UUID id = UUID.randomUUID(); - user.setId(id - ); - when(userRepository.findByEmail("test@example.com")).thenReturn(user); + user.setId(id); + + when(userService.findByEmail("test@example.com")).thenReturn(Optional.of(user)); when(passwordEncoder.matches("password", "encodedPassword")).thenReturn(true); - when(userRepository.findById(id)).thenReturn(Optional.of(user)); + when(jwtService.generateToken(id)).thenReturn("jwtToken"); String token = authenticationService.loginUser("test@example.com", "password"); - assertNotNull(token); - UserResponse userResponse = authenticationService.getUserFromJwtToken(token); - assertEquals("test@example.com", userResponse.getEmail()); - } - - // loginSocial tests - - @Test - void testLoginSocial_UserNotFound() { - when(userRepository.findByEmail("notfound@example.com")).thenReturn(null); - Exception exception = assertThrows(InvalidCredentialsException.class, () -> - authenticationService.loginSocial("notfound@example.com") - ); - assertTrue(exception.getMessage().contains("Social login failed")); - } - - @Test - void testLoginSocial_Success() { - User user = new User(); - user.setEmail("social@example.com"); - user.setPassword(null); - user.setName("Social User"); - user.setVerified(true); - user.setRole("USER"); - user.setBirthdate(LocalDate.now().minusYears(25)); - user.setProvider("GOOGLE"); - - UUID id = UUID.randomUUID(); - user.setId(id); - when(userRepository.findByEmail("social@example.com")).thenReturn(user); - when(userRepository.findById(id)).thenReturn(Optional.of(user)); - - String token =authenticationService.loginSocial("social@example.com"); assertNotNull(token); - UserResponse userResponse = authenticationService.getUserFromJwtToken(token); - assertEquals("social@example.com", userResponse.getEmail()); - + assertEquals("jwtToken", token); } // verifyOTP tests @@ -328,6 +173,7 @@ class AuthenticationServiceTest { void testVerifyOTP_Success() { // OTPService returns true and user is found when(otpService.verifyOTP("test@example.com", "123456")).thenReturn(true); + User user = new User(); user.setEmail("test@example.com"); user.setPassword("encodedPassword"); @@ -337,32 +183,36 @@ class AuthenticationServiceTest { user.setBirthdate(LocalDate.now().minusYears(20)); user.setProvider("EMAIL"); - when(userRepository.findByEmail("test@example.com")).thenReturn(user); - when(userRepository.save(any(User.class))).thenReturn(user); + when(userService.findByEmail("test@example.com")).thenReturn(Optional.of(user)); + when(userService.save(any(User.class))).thenReturn(user); boolean result = authenticationService.verifyOTP("test@example.com", "123456"); + assertTrue(result); assertTrue(user.isVerified()); - verify(userRepository, times(1)).save(user); + verify(userService, times(1)).save(user); } @Test void testVerifyOTP_Success_UserNotFound() { // OTPService returns true but user is not found when(otpService.verifyOTP("nonexistent@example.com", "123456")).thenReturn(true); - when(userRepository.findByEmail("nonexistent@example.com")).thenReturn(null); + when(userService.findByEmail("nonexistent@example.com")).thenReturn(Optional.empty()); boolean result = authenticationService.verifyOTP("nonexistent@example.com", "123456"); + assertTrue(result); - verify(userRepository, never()).save(any(User.class)); + verify(userService, never()).save(any(User.class)); } @Test void testVerifyOTP_Failure() { when(otpService.verifyOTP("test@example.com", "000000")).thenReturn(false); + boolean result = authenticationService.verifyOTP("test@example.com", "000000"); + assertFalse(result); - verify(userRepository, never()).save(any(User.class)); + verify(userService, never()).save(any(User.class)); } // forgotPassword tests @@ -378,21 +228,24 @@ class AuthenticationServiceTest { user.setBirthdate(LocalDate.now().minusYears(20)); user.setProvider("EMAIL"); - when(userRepository.findByEmail("test@example.com")).thenReturn(user); + when(userService.findByEmail("test@example.com")).thenReturn(Optional.of(user)); assertDoesNotThrow(() -> authenticationService.forgotPassword("test@example.com")); } @Test - void testForgotPassword_Invalid() { - // Case 1: user not found - when(userRepository.findByEmail("notfound@example.com")).thenReturn(null); - Exception exception1 = assertThrows(IllegalArgumentException.class, () -> + void testForgotPassword_UserNotFound() { + when(userService.findByEmail("notfound@example.com")).thenReturn(Optional.empty()); + + Exception exception = assertThrows(IllegalArgumentException.class, () -> authenticationService.forgotPassword("notfound@example.com") ); - assertTrue(exception1.getMessage().contains("Password reset is only available for email-registered users.")); - // Case 2: user exists but provider is not EMAIL + assertTrue(exception.getMessage().contains("Password reset is only available for email-registered users")); + } + + @Test + void testForgotPassword_NonEmailProvider() { User user = new User(); user.setEmail("social@example.com"); user.setPassword(null); @@ -402,51 +255,14 @@ class AuthenticationServiceTest { user.setBirthdate(LocalDate.now().minusYears(25)); user.setProvider("GOOGLE"); - when(userRepository.findByEmail("social@example.com")).thenReturn(user); - Exception exception2 = assertThrows(IllegalArgumentException.class, () -> + when(userService.findByEmail("social@example.com")).thenReturn(Optional.of(user)); + + Exception exception = assertThrows(IllegalArgumentException.class, () -> authenticationService.forgotPassword("social@example.com") ); - assertTrue(exception2.getMessage().contains("Password reset is only available for email-registered users.")); - } - - // postContent tests - - @Test - void testPostContent_UserNotFound() { - when(userRepository.findByEmail("notfound@example.com")).thenReturn(null); - String response = authenticationService.postContent("notfound@example.com", "Content"); - assertEquals("User not found. Please register.", response); - } - - @Test - void testPostContent_UserNotVerified() { - User user = new User(); - user.setEmail("test@example.com"); - user.setPassword("encodedPassword"); - user.setName("Test User"); - user.setVerified(false); - user.setRole("USER"); - user.setBirthdate(LocalDate.now().minusYears(20)); - user.setProvider("EMAIL"); - when(userRepository.findByEmail("test@example.com")).thenReturn(user); - String response = authenticationService.postContent("test@example.com", "Content"); - assertTrue(response.contains("not verified")); + assertTrue(exception.getMessage().contains("Password reset is only available for email-registered users")); } - @Test - void testPostContent_UserVerified() { - User user = new User(); - user.setEmail("test@example.com"); - user.setPassword("encodedPassword"); - user.setName("Test User"); - user.setVerified(true); - user.setRole("USER"); - user.setBirthdate(LocalDate.now().minusYears(20)); - user.setProvider("EMAIL"); - - when(userRepository.findByEmail("test@example.com")).thenReturn(user); - String response = authenticationService.postContent("test@example.com", "Content"); - assertEquals("Content posted successfully", response); - } -} + // Add missing methods for other functionality if needed +} \ No newline at end of file -- GitLab