From 2f9bdd4a5915b25d16c88f54a16340ca85d3eb14 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sat, 23 May 2020 18:36:35 +0200 Subject: [PATCH] Tests on AuthenticationController --- .../sanmarinoes/smarthut/dto/JWTRequest.java | 4 + .../AuthenticationControllerTests.java | 100 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AuthenticationControllerTests.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java index 48bb386..ef2dcb4 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java @@ -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; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AuthenticationControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AuthenticationControllerTests.java new file mode 100644 index 0000000..667f7f3 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AuthenticationControllerTests.java @@ -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"); + } +}