package com.krylov.refound.service; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.krylov.refound.dto.user.UserUpdateDto; import com.krylov.refound.entity.User; import com.krylov.refound.exception.ApiException; import com.krylov.refound.repository.UserRepository; import java.util.List; import java.util.Optional; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.http.HttpStatus; import org.springframework.security.authentication.AnonymousAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; class UserServiceTest { private UserRepository repository; private UserService service; @BeforeEach void setUp() { repository = mock(UserRepository.class); service = new UserService(repository, mock(FileStorageService.class)); } @AfterEach void clearContext() { SecurityContextHolder.clearContext(); } @Test void adminIsRecognisedByRolePrefixedAuthority() { // Так роль кладёт JwtAuthenticationFilter authenticate("admin", "ROLE_ADMIN"); assertThat(service.isCurrentUserAdmin()).isTrue(); } @Test void adminCanModifyForeignProfile() { authenticate("admin", "ROLE_ADMIN"); User target = user(2L, "bob"); when(repository.findById(2L)).thenReturn(Optional.of(target)); UserUpdateDto dto = new UserUpdateDto(); dto.setFirstName("Боб"); service.updateUser(2L, dto, null); assertThat(target.getName()).isEqualTo("Боб"); } @Test void userCannotModifyForeignProfile() { authenticate("alice", "ROLE_USER"); when(repository.findByLogin("alice")).thenReturn(Optional.of(user(1L, "alice"))); assertThatThrownBy(() -> service.updateUser(2L, new UserUpdateDto(), null)) .isInstanceOfSatisfying(ApiException.class, e -> assertThat(e.getStatus()).isEqualTo(HttpStatus.FORBIDDEN)); verify(repository, never()).save(any()); } @Test void updateKeepsLoginWhenItIsNotSent() { authenticate("alice", "ROLE_USER"); User alice = user(1L, "alice"); when(repository.findByLogin("alice")).thenReturn(Optional.of(alice)); when(repository.findById(1L)).thenReturn(Optional.of(alice)); service.updateUser(1L, new UserUpdateDto(), null); assertThat(alice.getLogin()).isEqualTo("alice"); } @Test void anonymousRequestIsUnauthorizedNotNotFound() { SecurityContextHolder.getContext().setAuthentication(new AnonymousAuthenticationToken( "key", "anonymousUser", List.of(new SimpleGrantedAuthority("ROLE_ANONYMOUS")))); assertThatThrownBy(service::getCurrentUser) .isInstanceOfSatisfying(ApiException.class, e -> assertThat(e.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED)); verify(repository, never()).findByLogin(any()); } private static void authenticate(String login, String authority) { SecurityContextHolder.getContext().setAuthentication( new UsernamePasswordAuthenticationToken(login, null, List.of(new SimpleGrantedAuthority(authority)))); } private static User user(Long id, String login) { User user = new User(); user.setId(id); user.setLogin(login); return user; } }