diff --git a/src/test/java/com/safetypin/authentication/controller/AuthenticationControllerTest.java b/src/test/java/com/safetypin/authentication/controller/AuthenticationControllerTest.java index 6be07bdf87b82f2d086dea329ed75403795e0be6..b847fbf259a73ddff0ab5f3e9af9aebbcd5c9a7f 100644 --- a/src/test/java/com/safetypin/authentication/controller/AuthenticationControllerTest.java +++ b/src/test/java/com/safetypin/authentication/controller/AuthenticationControllerTest.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; 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.model.Role; import com.safetypin.authentication.model.User; import com.safetypin.authentication.service.AuthenticationService; @@ -125,6 +127,21 @@ class AuthenticationControllerTest { .andExpect(jsonPath("$.data.tokenValue").value(token)); } + @Test + void testLoginEmail_InvalidCredentials() throws Exception { + String errorMessage = "Invalid email or password"; + Mockito.when(authenticationService.loginUser("wrong@example.com", "wrongpassword")) + .thenThrow(new InvalidCredentialsException(errorMessage)); + + mockMvc.perform(post("/api/auth/login-email") + .param("email", "wrong@example.com") + .param("password", "wrongpassword")) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.success").value(false)) + .andExpect(jsonPath("$.message").value(errorMessage)) + .andExpect(jsonPath("$.data").isEmpty()); + } + @Test void testLoginSocial() throws Exception { User user = new User(); @@ -148,6 +165,20 @@ class AuthenticationControllerTest { .andExpect(jsonPath("$.data.tokenValue").value(token)); } + @Test + void testLoginSocial_InvalidCredentials() throws Exception { + String errorMessage = "User with this email not found"; + Mockito.when(authenticationService.loginSocial("nonexistent@example.com")) + .thenThrow(new InvalidCredentialsException(errorMessage)); + + mockMvc.perform(post("/api/auth/login-social") + .param("email", "nonexistent@example.com")) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.success").value(false)) + .andExpect(jsonPath("$.message").value(errorMessage)) + .andExpect(jsonPath("$.data").isEmpty()); + } + @Test void testVerifyOTP_Success() throws Exception { Mockito.when(authenticationService.verifyOTP("email@example.com", "123456")).thenReturn(true); @@ -172,6 +203,17 @@ class AuthenticationControllerTest { .andExpect(jsonPath("$.message").value("OTP verification failed")); } + @Test + void testVerifyOTP_InvalidCredentials() throws Exception { + String errorMessage = "Invalid email or OTP"; + Mockito.when(authenticationService.verifyOTP("email@example.com", "invalid")) + .thenThrow(new InvalidCredentialsException(errorMessage)); + + mockMvc.perform(post("/api/auth/verify-otp") + .param("email", "Invalid OTP code or expired")) + .andExpect(status().isBadRequest()); + } + @Test void testForgotPassword() throws Exception { PasswordResetRequest request = new PasswordResetRequest(); @@ -186,6 +228,47 @@ class AuthenticationControllerTest { .andExpect(content().string("Password reset instructions have been sent to your email (simulated)")); } + @Test + void testVerifyJwtToken_Success() throws Exception { + String validToken = "valid.jwt.token"; + UUID userId = UUID.randomUUID(); + UserResponse userResponse = UserResponse.builder() + .id(userId) + .email("test@example.com") + .name("Test User") + .isVerified(true) + .role("REGISTERED_USER") + .birthdate(LocalDate.now().minusYears(25)) + .provider("EMAIL") + .build(); + + Mockito.when(authenticationService.getUserFromJwtToken(validToken)).thenReturn(userResponse); + + mockMvc.perform(post("/api/auth/verify-jwt") + .param("token", validToken)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.success").value(true)) + .andExpect(jsonPath("$.message").value("OK")) + .andExpect(jsonPath("$.data.email").value("test@example.com")) + .andExpect(jsonPath("$.data.name").value("Test User")); + } + + @Test + void testVerifyJwtToken_InvalidToken() throws Exception { + String invalidToken = "invalid.token"; + String errorMessage = "Invalid or expired JWT token"; + + Mockito.when(authenticationService.getUserFromJwtToken(invalidToken)) + .thenThrow(new InvalidCredentialsException(errorMessage)); + + mockMvc.perform(post("/api/auth/verify-jwt") + .param("token", invalidToken)) + .andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.success").value(false)) + .andExpect(jsonPath("$.message").value(errorMessage)) + .andExpect(jsonPath("$.data").isEmpty()); + } + @Test void testPostContent() throws Exception { Mockito.when(authenticationService.postContent("email@example.com", "Test Content")) @@ -211,14 +294,14 @@ class AuthenticationControllerTest { public AuthenticationService authenticationService() { return Mockito.mock(AuthenticationService.class); } - + @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http - .csrf(AbstractHttpConfigurer::disable) // Appropriate for JWT authentication - .authorizeHttpRequests(auth -> auth - .anyRequest().permitAll() - ); + .csrf(AbstractHttpConfigurer::disable) // Appropriate for JWT authentication + .authorizeHttpRequests(auth -> auth + .anyRequest().permitAll() + ); return http.build(); } }