Skip to content

Commit

Permalink
fix: UT
Browse files Browse the repository at this point in the history
  • Loading branch information
Hardik Singh Behl authored and Hardik Singh Behl committed Sep 28, 2023
1 parent dfc2500 commit 32ce6a5
Showing 1 changed file with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.behl.cerberus.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -17,6 +18,9 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.server.ResponseStatusException;

import com.behl.cerberus.configuration.JwtConfigurationProperties;
import com.behl.cerberus.configuration.JwtConfigurationProperties.JWT;
import com.behl.cerberus.configuration.JwtConfigurationProperties.JWT.RefreshToken;
import com.behl.cerberus.dto.RefreshTokenRequestDto;
import com.behl.cerberus.dto.TokenSuccessResponseDto;
import com.behl.cerberus.dto.UserLoginRequestDto;
Expand All @@ -26,17 +30,22 @@

class AuthenticationServiceTest {

private AuthenticationService authenticationService;
private JwtUtility jwtUtility;
private CacheService cacheService;
private UserRepository userRepository;
private PasswordEncoder passwordEncoder;
private JwtUtility jwtUtility;
private JwtConfigurationProperties jwtConfigurationProperties;

private AuthenticationService authenticationService;

@BeforeEach
void setUp() {
this.jwtUtility = mock(JwtUtility.class);
this.cacheService = mock(CacheService.class);
this.userRepository = mock(UserRepository.class);
this.passwordEncoder = mock(PasswordEncoder.class);
this.jwtUtility = mock(JwtUtility.class);
this.authenticationService = new AuthenticationService(userRepository, passwordEncoder, jwtUtility);
this.jwtConfigurationProperties = mock(JwtConfigurationProperties.class);
this.authenticationService = new AuthenticationService(jwtUtility, cacheService, userRepository, passwordEncoder, jwtConfigurationProperties);
}

@Test
Expand All @@ -54,12 +63,16 @@ void successfullLogin() {
when(userLoginRequestDto.getEmailId()).thenReturn(emailId);
when(userRepository.findByEmailId(emailId)).thenReturn(Optional.of(user));
when(passwordEncoder.matches(rawPassword, encryptedPassword)).thenReturn(true);

final var jwtProperties = mock(JWT.class);
final var refreshTokenProperties = mock(RefreshToken.class);
when(jwtConfigurationProperties.getJwt()).thenReturn(jwtProperties);
when(jwtProperties.getRefreshToken()).thenReturn(refreshTokenProperties);
when(refreshTokenProperties.getValidity()).thenReturn(20);

final String accessToken = "Access token JWT";
final String refreshToken = "Refresh token JWT";
final LocalDateTime expirationTime = LocalDateTime.now().plusMinutes(30);
when(jwtUtility.generateAccessToken(userId)).thenReturn(accessToken);
when(jwtUtility.generateRefreshToken(userId)).thenReturn(refreshToken);
when(jwtUtility.getExpirationTimestamp(accessToken)).thenReturn(expirationTime);

// Call
Expand All @@ -69,11 +82,9 @@ void successfullLogin() {
assertThat(response).isNotNull();
assertThat(response).isInstanceOf(TokenSuccessResponseDto.class);
assertThat(response.getAccessToken()).isEqualTo(accessToken);
assertThat(response.getRefreshToken()).isEqualTo(refreshToken);
assertThat(response.getExpiresAt()).isEqualTo(expirationTime);
verify(userRepository, times(1)).findByEmailId(emailId);
verify(jwtUtility, times(1)).generateAccessToken(userId);
verify(jwtUtility, times(1)).generateRefreshToken(userId);
verify(jwtUtility, times(1)).getExpirationTimestamp(accessToken);
verify(passwordEncoder, times(1)).matches(rawPassword, encryptedPassword);
}
Expand Down Expand Up @@ -125,14 +136,14 @@ void refreshTokenWithExpiredRefreshToken() {
final String refreshToken = "Refresh token JWT";
var refreshTokenRequestDto = mock(RefreshTokenRequestDto.class);
when(refreshTokenRequestDto.getRefreshToken()).thenReturn(refreshToken);
when(jwtUtility.isTokenExpired(refreshToken)).thenReturn(true);
when(cacheService.fetch(eq(refreshToken), eq(UUID.class))).thenReturn(Optional.empty());

// Call and Verify
final var errorResponse = Assertions.assertThrows(ResponseStatusException.class,
() -> authenticationService.refreshToken(refreshTokenRequestDto));
assertThat(errorResponse.getMessage()).contains("Token expired");
assertThat(errorResponse.getReason()).isEqualTo("Token expired");
assertThat(errorResponse.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
verify(jwtUtility, times(1)).isTokenExpired(refreshToken);
verify(cacheService, times(1)).fetch(eq(refreshToken), eq(UUID.class));
}

@Test
Expand All @@ -142,8 +153,7 @@ void tokenRefreshalSuccess() {
final UUID userId = UUID.randomUUID();
var refreshTokenRequestDto = mock(RefreshTokenRequestDto.class);
when(refreshTokenRequestDto.getRefreshToken()).thenReturn(refreshToken);
when(jwtUtility.isTokenExpired(refreshToken)).thenReturn(false);
when(jwtUtility.extractUserId(refreshToken)).thenReturn(userId);
when(cacheService.fetch(eq(refreshToken), eq(UUID.class))).thenReturn(Optional.of(userId));
final String accessToken = "Access token JWT";
final LocalDateTime expirationTime = LocalDateTime.now().plusMinutes(30);
when(jwtUtility.generateAccessToken(userId)).thenReturn(accessToken);
Expand All @@ -158,10 +168,7 @@ void tokenRefreshalSuccess() {
assertThat(response.getAccessToken()).isEqualTo(accessToken);
assertThat(response.getExpiresAt()).isEqualTo(expirationTime);
verify(jwtUtility, times(1)).generateAccessToken(userId);
verify(jwtUtility, times(0)).generateRefreshToken(userId);
verify(jwtUtility, times(1)).getExpirationTimestamp(accessToken);
verify(jwtUtility, times(1)).isTokenExpired(refreshToken);
verify(jwtUtility, times(1)).extractUserId(refreshToken);
}

}

0 comments on commit 32ce6a5

Please sign in to comment.