Files
nahodka-back/src/test/java/com/krylov/refound/service/PostDeletionServiceTest.java
SlimusMinus 6b647e0bbf Add tests for the review fixes and make the context test runnable
- ReFoundApplicationTests runs on PostgreSQL via Testcontainers with a test
  profile holding test-only JWT/Redis/MinIO values; it previously failed
  without JWT_SECRET. The test also checks Liquibase + ddl validate.
- Unit tests: JWT access/refresh separation, refresh rotation, admin
  authority, exception mapping, image signature checks, ad media Range
  responses, post deletion after commit, moderation races, campaign status,
  ad selection without HttpSession and with Redis down, target URL rules.
- resolveRange rejects ranges that start past the end of the file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 01:08:29 +03:00

107 lines
3.5 KiB
Java

package com.krylov.refound.service;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import com.krylov.refound.entity.Image;
import com.krylov.refound.entity.Post;
import com.krylov.refound.repository.ChatRepository;
import com.krylov.refound.repository.FavoriteRepository;
import com.krylov.refound.repository.PostRepository;
import com.krylov.refound.service.redis.PostCacheService;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
class PostDeletionServiceTest {
private PostRepository postRepository;
private FavoriteRepository favoriteRepository;
private ChatRepository chatRepository;
private FileStorageService fileStorageService;
private PostCacheService postCacheService;
private PostDeletionService service;
@BeforeEach
void setUp() {
postRepository = mock(PostRepository.class);
favoriteRepository = mock(FavoriteRepository.class);
chatRepository = mock(ChatRepository.class);
fileStorageService = mock(FileStorageService.class);
postCacheService = mock(PostCacheService.class);
service = new PostDeletionService(postRepository, favoriteRepository, chatRepository, fileStorageService,
postCacheService);
}
@AfterEach
void tearDown() {
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.clearSynchronization();
}
}
@Test
void removesFavoritesChatLinksPostsAndFiles() {
Post post = post(7L, "a.jpg", "b.jpg");
service.delete(List.of(post));
verify(favoriteRepository).deleteByPostIn(List.of(post));
verify(chatRepository).clearPostReferences(List.of(7L));
verify(postRepository).deleteAll(List.of(post));
verify(fileStorageService).deleteFile("a.jpg");
verify(fileStorageService).deleteFile("b.jpg");
verify(postCacheService).invalidatePosts();
}
@Test
void filesAreDeletedOnlyAfterCommit() {
TransactionSynchronizationManager.initSynchronization();
service.delete(List.of(post(7L, "a.jpg")));
verify(fileStorageService, never()).deleteFile(any());
List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
assertThat(synchronizations).hasSize(1);
synchronizations.forEach(TransactionSynchronization::afterCommit);
verify(fileStorageService).deleteFile("a.jpg");
}
@Test
void storageErrorsDoNotFailDeletion() {
doThrow(new RuntimeException("MinIO недоступен")).when(fileStorageService).deleteFile(any());
service.delete(List.of(post(7L, "a.jpg", "b.jpg")));
verify(fileStorageService).deleteFile("b.jpg");
}
@Test
void emptyListDoesNothing() {
service.delete(List.of());
verifyNoInteractions(postRepository, favoriteRepository, chatRepository, fileStorageService, postCacheService);
}
private static Post post(Long id, String... imageKeys) {
Post post = new Post();
post.setId(id);
for (String key : imageKeys) {
Image image = new Image();
image.setUrl(key);
image.setPost(post);
post.getImages().add(image);
}
return post;
}
}