Skip to content

Commit

Permalink
test: tests finished
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioqfeg1 committed Apr 14, 2024
1 parent d274ef1 commit 5a9ba95
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class UserService implements UserDetailsService {
private final UserRepository userRepository;
@Value("${REFRESH_TOKEN_DURATION_MS}")
private long refreshTokenDurationMs;
private UserResponseDtoMapper userResponseDtoMapper;
private final UserResponseDtoMapper userResponseDtoMapper;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
return UserDetailsImpl.build(userRepository.findByEmail(email).orElseThrow(() -> new InvalidAuthenticationException("Invalid email or password provided!")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class AuthServiceTest {
User defaultUser;
@BeforeEach
void setUp() {
this.userService = new UserService(userRepository);
this.userService = new UserService(userRepository,null);
this.authService = new AuthService(authenticationManager,userService,jwtUtils);
this.defaultUser = User.builder()
.id(1L)
Expand Down
22 changes: 16 additions & 6 deletions api/src/test/java/lab/en2b/quizapi/user/UserServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lab.en2b.quizapi.user;

import lab.en2b.quizapi.auth.config.UserDetailsImpl;
import lab.en2b.quizapi.commons.user.User;
import lab.en2b.quizapi.commons.user.UserRepository;
import lab.en2b.quizapi.commons.user.UserService;
Expand All @@ -12,9 +13,11 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.NoSuchElementException;
import java.util.Optional;

import static org.mockito.ArgumentMatchers.any;
Expand All @@ -30,16 +33,13 @@ public class UserServiceTest {
@Mock
private UserRepository userRepository;

@Mock
private UserResponseDtoMapper userResponseDtoMapper;

private User defaultUser;

private UserResponseDto defaultUserResponseDto;

@BeforeEach
public void setUp() {
userService = new UserService(userRepository);
userService = new UserService(userRepository, new UserResponseDtoMapper());
defaultUser = User.builder()
.id(1L)
.username("HordyJurtado")
Expand All @@ -57,8 +57,18 @@ public void setUp() {
@Test
public void getUserDetailsTest(){
Authentication authentication = mock(Authentication.class);
when(userService.getUserByAuthentication(any())).thenReturn(defaultUser);
Assertions.assertEquals(defaultUserResponseDto, userService.getUserDetailsByAuthentication(authentication));
when(authentication.getPrincipal()).thenReturn(UserDetailsImpl.build(defaultUser));
when(userRepository.findByEmail(any())).thenReturn(Optional.of(defaultUser));
UserResponseDto result = userService.getUserDetailsByAuthentication(authentication);
Assertions.assertEquals(defaultUserResponseDto, result);
}

@Test
public void getUserDetailsWhenNotFound() {
Authentication authentication = mock(Authentication.class);
when(authentication.getPrincipal()).thenReturn(UserDetailsImpl.build(defaultUser));
when(userRepository.findByEmail(any())).thenReturn(Optional.empty());
Assertions.assertThrows(NoSuchElementException.class, () -> userService.getUserDetailsByAuthentication(authentication));
}

}

0 comments on commit 5a9ba95

Please sign in to comment.