Tests on AuthenticationController
This commit is contained in:
parent
1dc37cca4b
commit
2f9bdd4a59
2 changed files with 104 additions and 0 deletions
|
@ -1,9 +1,13 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class JWTRequest {
|
||||
@NotNull private String usernameOrEmail;
|
||||
@NotNull private String password;
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.JWTTokenUtils;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTRequest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UnauthorizedException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UserNotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.JWTUserDetailsService;
|
||||
import java.security.Principal;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.DisabledException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@WithMockUser(username = "user")
|
||||
public class AuthenticationControllerTests {
|
||||
|
||||
@InjectMocks private AuthenticationController authenticationController;
|
||||
|
||||
@Mock private UserRepository userRepository;
|
||||
|
||||
@Mock private Principal principal;
|
||||
|
||||
@Mock private AuthenticationManager authenticationManager;
|
||||
|
||||
@Mock private JWTUserDetailsService jwtUserDetailsService;
|
||||
|
||||
@Mock private JWTTokenUtils jwtTokenUtils;
|
||||
|
||||
@Test
|
||||
public void testProfile() {
|
||||
final User u = new User();
|
||||
when(principal.getName()).thenReturn("user");
|
||||
when(userRepository.findByUsername("user")).thenReturn(u);
|
||||
assertThat(authenticationController.profile(principal)).isSameAs(u);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLogin() throws UnauthorizedException, UserNotFoundException {
|
||||
final UsernamePasswordAuthenticationToken u =
|
||||
new UsernamePasswordAuthenticationToken("username", "password");
|
||||
final UsernamePasswordAuthenticationToken v =
|
||||
new UsernamePasswordAuthenticationToken("disabled", "password");
|
||||
final UsernamePasswordAuthenticationToken z =
|
||||
new UsernamePasswordAuthenticationToken("username", "wrongpassword");
|
||||
when(authenticationManager.authenticate(u)).thenReturn(null);
|
||||
when(authenticationManager.authenticate(v)).thenThrow(DisabledException.class);
|
||||
when(authenticationManager.authenticate(z)).thenThrow(BadCredentialsException.class);
|
||||
|
||||
final UserDetails r = Mockito.mock(UserDetails.class);
|
||||
when(jwtUserDetailsService.loadUserByUsername("username")).thenReturn(r);
|
||||
when(jwtTokenUtils.generateToken(r)).thenReturn("token");
|
||||
|
||||
final User user = new User();
|
||||
user.setUsername("username");
|
||||
|
||||
when(userRepository.findByEmailIgnoreCase("email@example.com")).thenReturn(user);
|
||||
when(userRepository.findByEmailIgnoreCase("none@example.com")).thenReturn(null);
|
||||
|
||||
assertThatThrownBy(
|
||||
() ->
|
||||
authenticationController.login(
|
||||
new JWTRequest("none@example.com", "password")))
|
||||
.isInstanceOf(UserNotFoundException.class);
|
||||
assertThat(
|
||||
authenticationController
|
||||
.login(new JWTRequest("email@example.com", "password"))
|
||||
.getJwttoken())
|
||||
.isEqualTo("token");
|
||||
assertThatThrownBy(
|
||||
() ->
|
||||
authenticationController.login(
|
||||
new JWTRequest("disabled", "password")))
|
||||
.isInstanceOf(UnauthorizedException.class);
|
||||
assertThatThrownBy(
|
||||
() ->
|
||||
authenticationController.login(
|
||||
new JWTRequest("username", "wrongpassword")))
|
||||
.isInstanceOf(UnauthorizedException.class);
|
||||
assertThat(
|
||||
authenticationController
|
||||
.login(new JWTRequest("username", "password"))
|
||||
.getJwttoken())
|
||||
.isEqualTo("token");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue