generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from Arquisoft/test/login
Added tests for the Authentication endpoints
- Loading branch information
Showing
11 changed files
with
287 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
api/src/main/java/lab/en2b/quizapi/auth/dtos/JwtResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
api/src/main/java/lab/en2b/quizapi/auth/dtos/RefreshTokenResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
api/src/main/java/lab/en2b/quizapi/commons/utils/TestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package lab.en2b.quizapi.commons.utils; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class TestUtils { | ||
public static String asJsonString(final Object obj) { | ||
try { | ||
return new ObjectMapper().writeValueAsString(obj); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
api/src/test/java/lab/en2b/quizapi/auth/AuthControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package lab.en2b.quizapi.auth; | ||
|
||
import lab.en2b.quizapi.auth.config.SecurityConfig; | ||
import lab.en2b.quizapi.auth.dtos.LoginDto; | ||
import lab.en2b.quizapi.auth.dtos.RefreshTokenDto; | ||
import lab.en2b.quizapi.auth.dtos.RegisterDto; | ||
import lab.en2b.quizapi.auth.jwt.JwtUtils; | ||
import lab.en2b.quizapi.commons.user.UserService; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultMatcher; | ||
|
||
import static lab.en2b.quizapi.commons.utils.TestUtils.asJsonString; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WebMvcTest(AuthController.class) | ||
@AutoConfigureMockMvc | ||
@Import(SecurityConfig.class) | ||
public class AuthControllerTest { | ||
@Autowired | ||
MockMvc mockMvc; | ||
@MockBean | ||
AuthService authService; | ||
@MockBean | ||
JwtUtils jwtUtils; | ||
@MockBean | ||
UserService userService; | ||
@Test | ||
void registerUserShouldReturn200() throws Exception { | ||
when(authService.register(any())).thenReturn(ResponseEntity.ok().build()); | ||
testRegister(asJsonString( new RegisterDto("[email protected]","test","testing")) | ||
,status().isOk()); | ||
} | ||
@Test | ||
void registerEmptyBodyShouldReturn400() throws Exception { | ||
testRegister("{}",status().isBadRequest()); | ||
} | ||
@Test | ||
void registerEmptyEmailShouldReturn400() throws Exception { | ||
testRegister(asJsonString( new RegisterDto("","test","testing")), | ||
status().isBadRequest()); | ||
} | ||
|
||
@Test | ||
void registerInvalidEmailShouldReturn400() throws Exception { | ||
testRegister(asJsonString( new RegisterDto("iAmAnInvalidEmail","test","testing")), | ||
status().isBadRequest()); | ||
} | ||
|
||
@Test | ||
void registerEmptyUsernameShouldReturn400() throws Exception { | ||
testRegister(asJsonString( new RegisterDto("[email protected]","","testing")), | ||
status().isBadRequest()); | ||
} | ||
|
||
@Test | ||
void registerEmptyPasswordShouldReturn400() throws Exception { | ||
testRegister(asJsonString( new RegisterDto("[email protected]","test","")), | ||
status().isBadRequest()); | ||
} | ||
|
||
@Test | ||
void loginUserShouldReturn200() throws Exception { | ||
when(authService.login(any())).thenReturn(ResponseEntity.ok().build()); | ||
testLogin(asJsonString( new LoginDto("[email protected]","password")) | ||
,status().isOk()); | ||
} | ||
|
||
@Test | ||
void loginEmptyBodyShouldReturn400() throws Exception { | ||
testLogin("{}",status().isBadRequest()); | ||
} | ||
@Test | ||
void loginEmptyEmailShouldReturn400() throws Exception { | ||
testLogin(asJsonString( new LoginDto("","password")), | ||
status().isBadRequest()); | ||
} | ||
|
||
@Test | ||
void loginInvalidEmailShouldReturn400() throws Exception { | ||
testLogin(asJsonString( new LoginDto("iAmAnInvalidEmail","password")), | ||
status().isBadRequest()); | ||
} | ||
|
||
@Test | ||
void refreshTokenShouldReturn200() throws Exception { | ||
when(authService.refreshToken(any())).thenReturn(ResponseEntity.ok().build()); | ||
testRefreshToken(asJsonString( new RefreshTokenDto("58ca95e9-c4ef-45fd-93cf-55c040aaff9c")) | ||
,status().isOk()); | ||
} | ||
|
||
@Test | ||
void refreshTokenEmptyBodyShouldReturn400() throws Exception { | ||
when(authService.refreshToken(any())).thenReturn(ResponseEntity.ok().build()); | ||
testRefreshToken("{}",status().isBadRequest()); | ||
} | ||
|
||
@Test | ||
void refreshTokenEmptyTokenShouldReturn400() throws Exception { | ||
when(authService.refreshToken(any())).thenReturn(ResponseEntity.ok().build()); | ||
testRefreshToken(asJsonString( new RefreshTokenDto("")), status().isBadRequest()); | ||
} | ||
|
||
private void testRegister(String content, ResultMatcher code) throws Exception { | ||
mockMvc.perform(post("/auth/register") | ||
.content(content) | ||
.contentType("application/json") | ||
.with(csrf())) | ||
.andExpect(code); | ||
} | ||
|
||
private void testLogin(String content, ResultMatcher code) throws Exception { | ||
mockMvc.perform(post("/auth/login") | ||
.content(content) | ||
.contentType("application/json") | ||
.with(csrf())) | ||
.andExpect(code); | ||
} | ||
|
||
private void testRefreshToken(String content, ResultMatcher code) throws Exception { | ||
mockMvc.perform(post("/auth/refresh-token") | ||
.content(content) | ||
.contentType("application/json") | ||
.with(csrf())) | ||
.andExpect(code); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
api/src/test/java/lab/en2b/quizapi/auth/AuthServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package lab.en2b.quizapi.auth; | ||
|
||
import ch.qos.logback.core.util.TimeUtil; | ||
import lab.en2b.quizapi.auth.config.UserDetailsImpl; | ||
import lab.en2b.quizapi.auth.dtos.*; | ||
import lab.en2b.quizapi.auth.jwt.JwtUtils; | ||
import lab.en2b.quizapi.commons.user.User; | ||
import lab.en2b.quizapi.commons.user.UserRepository; | ||
import lab.en2b.quizapi.commons.user.UserService; | ||
import lab.en2b.quizapi.commons.user.role.Role; | ||
import lab.en2b.quizapi.commons.user.role.RoleRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import javax.swing.text.html.Option; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith({MockitoExtension.class, SpringExtension.class}) | ||
public class AuthServiceTest { | ||
@InjectMocks | ||
AuthService authService; | ||
@Mock | ||
UserService userService; | ||
@Mock | ||
UserRepository userRepository; | ||
@Mock | ||
RoleRepository roleRepository; | ||
@Mock | ||
AuthenticationManager authenticationManager; | ||
@Mock | ||
JwtUtils jwtUtils; | ||
User defaultUser; | ||
@BeforeEach | ||
void setUp() { | ||
this.userService = new UserService(userRepository,roleRepository); | ||
this.authService = new AuthService(authenticationManager,userService,jwtUtils); | ||
this.defaultUser = User.builder() | ||
.id(1L) | ||
.email("[email protected]") | ||
.username("test") | ||
.roles(Set.of(new Role("user"))) | ||
.password("password") | ||
.refreshToken("token") | ||
.refreshExpiration(Instant.ofEpochSecond(TimeUtil.computeStartOfNextSecond(System.currentTimeMillis()+ 1000))) | ||
.build(); | ||
} | ||
@Test | ||
void testLogin(){ | ||
Authentication authentication = mock(Authentication.class); | ||
|
||
when(authenticationManager.authenticate(any())).thenReturn(authentication); | ||
when(authentication.getPrincipal()).thenReturn(UserDetailsImpl.build(defaultUser)); | ||
when(jwtUtils.generateJwtTokenUserPassword(authentication)).thenReturn("jwtToken"); | ||
when(userRepository.findById(any())).thenReturn(Optional.of(defaultUser)); | ||
|
||
ResponseEntity<JwtResponseDto> actual = authService.login(new LoginDto("test","password")); | ||
|
||
assertEquals(ResponseEntity.of(Optional.of( | ||
JwtResponseDto.builder() | ||
.userId(1L) | ||
.username(defaultUser.getUsername()) | ||
.email(defaultUser.getEmail()) | ||
.refreshToken(defaultUser.getRefreshToken()) | ||
.token("jwtToken") | ||
.roles(List.of("user")) | ||
.build())) | ||
,actual); | ||
|
||
} | ||
@Test | ||
void testRegister(){ | ||
|
||
when(userRepository.existsByEmail(any())).thenReturn(false); | ||
when(userRepository.existsByUsername(any())).thenReturn(false); | ||
when(userRepository.save(any())).thenAnswer(i -> i.getArguments()[0]); | ||
when(roleRepository.findByName(any())).thenReturn(Optional.of(new Role("user"))); | ||
|
||
ResponseEntity<?> actual = authService.register(new RegisterDto("test","username","password")); | ||
|
||
assertEquals(ResponseEntity.of(Optional.of("User registered successfully!")),actual); | ||
|
||
} | ||
|
||
@Test | ||
void testRefreshToken(){ | ||
|
||
when(userRepository.findByRefreshToken(any())).thenReturn(Optional.of(defaultUser)); | ||
when(jwtUtils.generateTokenFromEmail(any())).thenReturn("jwtToken"); | ||
|
||
ResponseEntity<?> actual = authService.refreshToken(new RefreshTokenDto("token")); | ||
|
||
assertEquals(ResponseEntity.of(Optional.of(new RefreshTokenResponseDto("jwtToken","token"))),actual); | ||
|
||
} | ||
} |