Hide private profile data and reissue tokens when users change their login

- GET /api/v1/users/{id} returns email and phone only to the user themself
  and to admins.
- When a user changes their own login, the PUT response carries a new token
  pair (login is the JWT subject, so the old tokens stop matching); old
  refresh tokens fail on their own because the stored login differs.
- The old avatar is deleted only after the profile is saved; login length is
  checked against the column size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SlimusMinus
2026-10-20 01:32:13 +03:00
parent 48351398b5
commit 477737c161
3 changed files with 138 additions and 23 deletions

View File

@@ -12,6 +12,7 @@ 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 com.krylov.refound.security.JwtService;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.AfterEach;
@@ -27,11 +28,15 @@ class UserServiceTest {
private UserRepository repository;
private UserService service;
private JwtService jwtService;
private RefreshTokenService refreshTokenService;
@BeforeEach
void setUp() {
repository = mock(UserRepository.class);
service = new UserService(repository, mock(FileStorageService.class));
jwtService = mock(JwtService.class);
refreshTokenService = mock(RefreshTokenService.class);
service = new UserService(repository, mock(FileStorageService.class), jwtService, refreshTokenService);
}
@AfterEach
@@ -92,6 +97,66 @@ class UserServiceTest {
verify(repository, never()).findByLogin(any());
}
@Test
void foreignProfileHidesEmailAndPhone() {
authenticate("alice", "ROLE_USER");
when(repository.findByLogin("alice")).thenReturn(Optional.of(user(1L, "alice")));
User bob = user(2L, "bob");
bob.setEmail("bob@example.com");
bob.setPhone("+7 900 000-00-00");
when(repository.findById(2L)).thenReturn(Optional.of(bob));
var dto = service.getUserById(2L);
assertThat(dto.getEmail()).isNull();
assertThat(dto.getPhone()).isNull();
assertThat(dto.getLogin()).isEqualTo("bob");
}
@Test
void ownProfileShowsEmailAndPhone() {
authenticate("alice", "ROLE_USER");
User alice = user(1L, "alice");
alice.setEmail("alice@example.com");
when(repository.findByLogin("alice")).thenReturn(Optional.of(alice));
when(repository.findById(1L)).thenReturn(Optional.of(alice));
assertThat(service.getUserById(1L).getEmail()).isEqualTo("alice@example.com");
}
@Test
void changingOwnLoginReturnsNewTokens() {
authenticate("alice", "ROLE_USER");
User alice = user(1L, "alice");
alice.setRole(com.krylov.refound.enums.Role.USER);
when(repository.findByLogin("alice")).thenReturn(Optional.of(alice));
when(repository.findById(1L)).thenReturn(Optional.of(alice));
when(jwtService.generateAccessToken("alice2", "USER")).thenReturn("new-access");
when(refreshTokenService.issue("alice2")).thenReturn("new-refresh");
UserUpdateDto dto = new UserUpdateDto();
dto.setLogin("alice2");
var response = service.updateUser(1L, dto, null);
assertThat(response.getLogin()).isEqualTo("alice2");
assertThat(response.getToken()).isEqualTo("new-access");
assertThat(response.getRefreshToken()).isEqualTo("new-refresh");
}
@Test
void adminChangingForeignLoginGetsNoTokens() {
authenticate("admin", "ROLE_ADMIN");
User bob = user(2L, "bob");
when(repository.findById(2L)).thenReturn(Optional.of(bob));
UserUpdateDto dto = new UserUpdateDto();
dto.setLogin("bob2");
var response = service.updateUser(2L, dto, null);
assertThat(response.getToken()).isNull();
verify(refreshTokenService, never()).issue(any());
}
private static void authenticate(String login, String authority) {
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(login, null, List.of(new SimpleGrantedAuthority(authority))));