Spaces:
Sleeping
Sleeping
File size: 3,385 Bytes
1c8e50c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
package in.garvit.tasks.controller;
import in.garvit.tasks.exception.UserException;
import in.garvit.tasks.request.LoginRequest;
import in.garvit.tasks.request.SignupRequest;
import in.garvit.tasks.response.AuthResponse;
import in.garvit.tasks.service.UserService;
import in.garvit.tasks.taskSecurityConfig.JwtProvider;
import in.garvit.tasks.usermodel.User;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
class AuthControllerTest {
@Mock
private UserService userService;
@Mock
private AuthenticationManager authenticationManager;
@Mock
private JwtProvider jwtProvider;
@InjectMocks
private AuthController authController;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void register_ReturnsToken() throws UserException {
SignupRequest request = new SignupRequest("Jane Doe", "jane@example.com", "password123", "ROLE_USER", "1234567890");
User createdUser = new User();
createdUser.setEmail(request.getEmail());
Authentication authentication = new UsernamePasswordAuthenticationToken(createdUser.getEmail(), request.getPassword());
when(userService.register(any(SignupRequest.class))).thenReturn(createdUser);
when(authenticationManager.authenticate(any())).thenReturn(authentication);
when(jwtProvider.generateToken(authentication)).thenReturn("jwt-token");
ResponseEntity<AuthResponse> response = authController.register(request);
assertEquals(HttpStatus.CREATED, response.getStatusCode());
assertNotNull(response.getBody());
assertEquals("Register success", response.getBody().getMessage());
assertEquals("jwt-token", response.getBody().getJwt());
}
@Test
void signin_ReturnsToken() {
LoginRequest request = new LoginRequest("jane@example.com", "password123");
Authentication authentication = new UsernamePasswordAuthenticationToken(request.getEmail(), request.getPassword());
when(authenticationManager.authenticate(any())).thenReturn(authentication);
when(jwtProvider.generateToken(authentication)).thenReturn("jwt-token");
ResponseEntity<AuthResponse> response = authController.signin(request);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertNotNull(response.getBody());
assertEquals("Login success", response.getBody().getMessage());
}
@Test
void signin_InvalidCredentials_Propagates() {
LoginRequest request = new LoginRequest("jane@example.com", "wrong-password");
when(authenticationManager.authenticate(any()))
.thenThrow(new BadCredentialsException("Invalid credentials"));
assertThrows(BadCredentialsException.class, () -> authController.signin(request));
}
}
|