- 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>
167 lines
6.1 KiB
Java
167 lines
6.1 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.ArgumentMatchers.anyList;
|
|
import static org.mockito.ArgumentMatchers.anyString;
|
|
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.ai.exception.AiBadRequestException;
|
|
import com.krylov.refound.ai.exception.ContentBlockedException;
|
|
import com.krylov.refound.ai.facade.ContentModerationFacade;
|
|
import com.krylov.refound.dto.DownloadedFile;
|
|
import com.krylov.refound.entity.Image;
|
|
import com.krylov.refound.entity.Post;
|
|
import com.krylov.refound.enums.ErrorCode;
|
|
import com.krylov.refound.enums.PostStatus;
|
|
import com.krylov.refound.exception.ApiException;
|
|
import com.krylov.refound.repository.PostRepository;
|
|
import com.krylov.refound.service.redis.PostCacheService;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.retry.ExhaustedRetryException;
|
|
import org.springframework.transaction.PlatformTransactionManager;
|
|
import org.springframework.transaction.support.SimpleTransactionStatus;
|
|
|
|
class PostModerationExecutorServiceTest {
|
|
|
|
private PostRepository postRepository;
|
|
private ContentModerationFacade facade;
|
|
private FileStorageService fileStorageService;
|
|
private PostCacheService postCacheService;
|
|
private PostModerationExecutorService service;
|
|
|
|
@BeforeEach
|
|
void setUp() {
|
|
postRepository = mock(PostRepository.class);
|
|
facade = mock(ContentModerationFacade.class);
|
|
fileStorageService = mock(FileStorageService.class);
|
|
postCacheService = mock(PostCacheService.class);
|
|
PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class);
|
|
when(transactionManager.getTransaction(any())).thenReturn(new SimpleTransactionStatus());
|
|
service = new PostModerationExecutorService(postRepository, facade, fileStorageService, postCacheService,
|
|
transactionManager);
|
|
|
|
when(fileStorageService.downloadFileRaw(anyString()))
|
|
.thenAnswer(inv -> new DownloadedFile("cat.jpg", "image/jpeg", new byte[] {1, 2, 3}));
|
|
}
|
|
|
|
@Test
|
|
void approvedPostBecomesActiveAndInvalidatesCache() {
|
|
Post post = post("Кот", "a.jpg");
|
|
when(postRepository.findById(1L)).thenReturn(Optional.of(post));
|
|
when(facade.moderate(any(), any(), anyList())).thenReturn(true);
|
|
|
|
service.moderateOne(1L);
|
|
|
|
assertThat(post.getStatus()).isEqualTo(PostStatus.ACTIVE);
|
|
verify(postCacheService).invalidatePosts();
|
|
}
|
|
|
|
@Test
|
|
void blockedContentIsRejected() {
|
|
Post post = post("Кот", "a.jpg");
|
|
when(postRepository.findById(1L)).thenReturn(Optional.of(post));
|
|
when(facade.moderate(any(), any(), anyList())).thenThrow(new ContentBlockedException("nsfw", List.of("nsfw")));
|
|
|
|
service.moderateOne(1L);
|
|
|
|
assertThat(post.getStatus()).isEqualTo(PostStatus.REJECTED);
|
|
verify(postCacheService, never()).invalidatePosts();
|
|
}
|
|
|
|
@Test
|
|
void editDuringModerationIsNotApproved() {
|
|
Post checked = post("Кот", "a.jpg");
|
|
Post edited = post("Кот и что-то ещё", "a.jpg");
|
|
// Первое чтение — снимок для AI, второе — перед установкой статуса: пост уже правили
|
|
when(postRepository.findById(1L)).thenReturn(Optional.of(checked), Optional.of(edited));
|
|
when(facade.moderate(any(), any(), anyList())).thenReturn(true);
|
|
|
|
service.moderateOne(1L);
|
|
|
|
assertThat(edited.getStatus()).isEqualTo(PostStatus.MODERATION);
|
|
verify(postCacheService, never()).invalidatePosts();
|
|
}
|
|
|
|
@Test
|
|
void imagesChangedDuringModerationAreNotApproved() {
|
|
Post checked = post("Кот", "a.jpg");
|
|
Post edited = post("Кот", "a.jpg", "b.jpg");
|
|
when(postRepository.findById(1L)).thenReturn(Optional.of(checked), Optional.of(edited));
|
|
when(facade.moderate(any(), any(), anyList())).thenReturn(true);
|
|
|
|
service.moderateOne(1L);
|
|
|
|
assertThat(edited.getStatus()).isEqualTo(PostStatus.MODERATION);
|
|
}
|
|
|
|
@Test
|
|
void storageOutageKeepsPostInModeration() {
|
|
Post post = post("Кот", "a.jpg");
|
|
when(postRepository.findById(1L)).thenReturn(Optional.of(post));
|
|
when(fileStorageService.downloadFileRaw("a.jpg")).thenThrow(new ApiException(ErrorCode.INTERNAL_ERROR,
|
|
"Ошибка при скачивании файла.", HttpStatus.INTERNAL_SERVER_ERROR));
|
|
|
|
service.moderateOne(1L);
|
|
|
|
assertThat(post.getStatus()).isEqualTo(PostStatus.MODERATION);
|
|
verify(facade, never()).moderate(any(), any(), anyList());
|
|
}
|
|
|
|
@Test
|
|
void aiUnavailableKeepsPostInModeration() {
|
|
Post post = post("Кот");
|
|
when(postRepository.findById(1L)).thenReturn(Optional.of(post));
|
|
when(facade.moderate(any(), any(), anyList())).thenReturn(null);
|
|
|
|
service.moderateOne(1L);
|
|
|
|
assertThat(post.getStatus()).isEqualTo(PostStatus.MODERATION);
|
|
}
|
|
|
|
@Test
|
|
void aiBadRequestWrappedByRetryIsRejected() {
|
|
Post post = post("Кот");
|
|
when(postRepository.findById(1L)).thenReturn(Optional.of(post));
|
|
when(facade.moderate(any(), any(), anyList()))
|
|
.thenThrow(new ExhaustedRetryException("no recoverer", new AiBadRequestException("unsupported image")));
|
|
|
|
service.moderateOne(1L);
|
|
|
|
assertThat(post.getStatus()).isEqualTo(PostStatus.REJECTED);
|
|
}
|
|
|
|
@Test
|
|
void postThatLeftModerationIsSkipped() {
|
|
Post post = post("Кот");
|
|
post.setStatus(PostStatus.ACTIVE);
|
|
when(postRepository.findById(1L)).thenReturn(Optional.of(post));
|
|
|
|
service.moderateOne(1L);
|
|
|
|
verify(facade, never()).moderate(any(), any(), anyList());
|
|
}
|
|
|
|
private static Post post(String title, String... imageKeys) {
|
|
Post post = new Post();
|
|
post.setId(1L);
|
|
post.setTitle(title);
|
|
post.setDescription("описание");
|
|
post.setStatus(PostStatus.MODERATION);
|
|
for (String key : imageKeys) {
|
|
Image image = new Image();
|
|
image.setUrl(key);
|
|
image.setPost(post);
|
|
post.getImages().add(image);
|
|
}
|
|
return post;
|
|
}
|
|
}
|