Я реализовал REST API с помощью токена JWT. Пользователь может зарегистрировать новую учетную запись или войти в систему с именем пользователя и паролем. У меня есть класс AuthController. Мне нужно протестировать два метода: login/ register. Я хочу использовать Mockito для тестирования этого класса. Как замокать аутентификацию (токен)?
@RestController
@RequestMapping(value = "/api/auth")
public class AuthController {
private final AuthenticationManager authenticationManager;
private final JwtTokenUtils jwtToken;
private final UserService userService;
private final UserRepository repository;
private final PasswordEncoder encoder;
private final RoleRepository roleRepository;
@Autowired
public AuthController(AuthenticationManager authenticationManager, JwtTokenUtils jwtToken, UserService userService, UserRepository repository, PasswordEncoder encoder, RoleRepository roleRepository) {
this.authenticationManager = authenticationManager;
this.jwtToken = jwtToken;
this.userService = userService;
this.repository = repository;
this.encoder = encoder;
this.roleRepository = roleRepository;
}
@PostMapping("/login")
public ResponseEntity<?> login(@RequestBody AuthDto requestDto) {
try {
String username = requestDto.getUsername();
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(requestDto.getUsername(), requestDto.getPassword()));
User user=userService.findByUsername(username);
SecurityContextHolder.getContext().setAuthentication(authentication);
String jwt = jwtToken.generateJwtToken(authentication);
UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
List<String> roles = userDetails.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.toList());
return ResponseEntity.ok(new JwtResponseDto(
jwt,
userDetails.getId(),
userDetails.getUsername(),
userDetails.getEmail(),
roles));
} catch (AuthenticationException e) {
throw new BadCredentialsException("Invalid username or password");
}
}
@PostMapping("/signup")
public ResponseEntity<?> registerUser(@RequestBody CustomerDto signUpAuthDto) {
if (repository.existsByUsername(signUpAuthDto.getUsername())) {
return ResponseEntity
.badRequest()
.body(new MessageResponse("Error: Username is already taken"));
}
if (repository.existsByEmail(signUpAuthDto.getEmail())) {
return ResponseEntity
.badRequest()
.body(new MessageResponse("Error: Email is already in use"));
}
if (signUpAuthDto.getPassword() !=null && !signUpAuthDto.getPassword().equals(signUpAuthDto.getConfirm())) {
return ResponseEntity
.badRequest()
.body(new MessageResponse("Error: You entered two different passwords. Please try again"));
}
User user = new User(signUpAuthDto.getUsername(),
signUpAuthDto.getEmail(),
encoder.encode(signUpAuthDto.getPassword()));
encoder.encode(signUpAuthDto.getConfirm());
Set<Role> strRoles = signUpAuthDto.getRole();
Set<Role> roles = new HashSet<>();
if (strRoles == null) {
Role userRole = roleRepository.findByName(EnumRole.ROLE_USER)
.orElseThrow(()-> new RuntimeException("Error: Role is not found"));
roles.add(userRole);
} else {
strRoles.forEach(role -> {
if ("admin".equals(role)) {
Role adminRole = roleRepository.findByName(EnumRole.ROLE_ADMIN)
.orElseThrow(()-> new RuntimeException("Error: Role is not found"));
roles.add(adminRole);
} else {
Role userRole = roleRepository.findByName(EnumRole.ROLE_USER)
.orElseThrow(()-> new RuntimeException("Error: Role is not found"));
roles.add(userRole);
}
});
}
user.setRoles(roles);
repository.save(user);
return ResponseEntity.ok(new MessageResponse("User registered successfully!"));
}
}