diff --git a/src/test/java/formflow/library/controllers/NoOpVirusScannerTest.java b/src/test/java/formflow/library/controllers/NoOpVirusScannerTest.java index 19d67c9b..33a896ed 100644 --- a/src/test/java/formflow/library/controllers/NoOpVirusScannerTest.java +++ b/src/test/java/formflow/library/controllers/NoOpVirusScannerTest.java @@ -11,6 +11,7 @@ import formflow.library.data.SubmissionRepositoryService; import formflow.library.data.UserFile; import formflow.library.data.UserFileRepositoryService; +import formflow.library.file.FileValidationService; import formflow.library.utilities.AbstractMockMvcTest; import java.util.HashMap; import java.util.Map; @@ -21,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; @@ -42,7 +44,9 @@ public class NoOpVirusScannerTest extends AbstractMockMvcTest { private UserFileRepositoryService userFileRepositoryService; @Autowired private FileController fileController; - + @SpyBean + FileValidationService fileValidationService; + @Override @BeforeEach public void setUp() throws Exception { @@ -72,6 +76,7 @@ public void setUp() throws Exception { void shouldBypassRealVirusScanningIfDisabled() throws Exception { MockMultipartFile testImage = new MockMultipartFile("file", "someImage.jpg", MediaType.IMAGE_JPEG_VALUE, "test".getBytes()); + when(fileValidationService.isAcceptedMimeType(testImage)).thenReturn(true); mockMvc.perform(MockMvcRequestBuilders.multipart("/file-upload") .file(testImage) .param("flow", "testFlow") diff --git a/src/test/java/formflow/library/controllers/ScreenControllerJourneyTest.java b/src/test/java/formflow/library/controllers/ScreenControllerJourneyTest.java index 499a0b9e..98a5e1dc 100644 --- a/src/test/java/formflow/library/controllers/ScreenControllerJourneyTest.java +++ b/src/test/java/formflow/library/controllers/ScreenControllerJourneyTest.java @@ -1,8 +1,11 @@ package formflow.library.controllers; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; +import formflow.library.file.FileValidationService; import formflow.library.utilities.AbstractBasePageTest; import java.io.IOException; @@ -10,6 +13,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.SpyBean; @SpringBootTest(properties = {"form-flow.path=flows-config/test-flow.yaml"}, webEnvironment = RANDOM_PORT) diff --git a/src/test/java/formflow/library/controllers/UploadBlockedIfVirusScanUnreachableTest.java b/src/test/java/formflow/library/controllers/UploadBlockedIfVirusScanUnreachableTest.java index 7934215f..05b67012 100644 --- a/src/test/java/formflow/library/controllers/UploadBlockedIfVirusScanUnreachableTest.java +++ b/src/test/java/formflow/library/controllers/UploadBlockedIfVirusScanUnreachableTest.java @@ -10,6 +10,7 @@ import formflow.library.data.SubmissionRepositoryService; import formflow.library.data.UserFileRepositoryService; import formflow.library.file.ClammitVirusScanner; +import formflow.library.file.FileValidationService; import formflow.library.utilities.AbstractMockMvcTest; import java.util.Locale; import java.util.UUID; @@ -33,22 +34,19 @@ public class UploadBlockedIfVirusScanUnreachableTest extends AbstractMockMvcTest { private MockMvc mockMvc; - @MockBean - private SubmissionRepositoryService submissionRepositoryService; @Autowired UserFileRepositoryService userFileRepositoryService; @Autowired private FileController fileController; @MockBean private ClammitVirusScanner clammitVirusScanner; + @MockBean + FileValidationService fileValidationService; @Override @BeforeEach public void setUp() throws Exception { - UUID submissionUUID = UUID.randomUUID(); mockMvc = MockMvcBuilders.standaloneSetup(fileController).build(); - Submission submission = Submission.builder().id(submissionUUID).build(); - //when(submissionRepositoryService.findOrCreate(any())).thenReturn(submission); super.setUp(); } @@ -58,6 +56,7 @@ void shouldPreventUploadAndShowAnErrorIfBlockIfUnreachableIsSetToTrue() throws E MediaType.IMAGE_JPEG_VALUE, "test".getBytes()); when(clammitVirusScanner.virusDetected(testImage)).thenThrow( new WebClientResponseException(500, "Failed!", null, null, null)); + when(fileValidationService.isAcceptedMimeType(testImage)).thenReturn(true); mockMvc.perform(MockMvcRequestBuilders.multipart("/file-upload") .file(testImage) .param("flow", "testFlow") diff --git a/src/test/java/formflow/library/file/FileValidationServiceTest.java b/src/test/java/formflow/library/file/FileValidationServiceTest.java index aeba0f62..318ef45d 100644 --- a/src/test/java/formflow/library/file/FileValidationServiceTest.java +++ b/src/test/java/formflow/library/file/FileValidationServiceTest.java @@ -3,6 +3,8 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -32,20 +34,20 @@ void acceptedFileTypesShouldReturnTheIntersectionOfDefaultTypesWithUserProvidedO private static Stream provideMultiPartFiles() { return Stream.of( - Arguments.of("image/jpeg", true), - Arguments.of("fake/nonsense", false), - Arguments.of(null, false), - Arguments.of("", false) + Arguments.of("test.jpeg", true), + Arguments.of("test-archive.zip", false), + Arguments.of("i-am-not-a-png.txt.png", false) ); } @ParameterizedTest @MethodSource("provideMultiPartFiles") - void isAcceptedMimeTypeReturnsTrueIfAccepted(String contentType, boolean assertion) throws IOException { + void isAcceptedMimeTypeReturnsTrueIfAccepted(String contentName, boolean assertion) throws IOException { FileValidationService fileValidationService = new FileValidationService(".jpeg,.bmp", 1); - MockMultipartFile testFile = new MockMultipartFile("test", "test", contentType, new byte[]{}); + ClassPathResource resource = new ClassPathResource(contentName); + MultipartFile file = new MockMultipartFile("file", contentName, Files.probeContentType(Path.of(contentName)), resource.getInputStream()); - assertThat(fileValidationService.isAcceptedMimeType(testFile)).isEqualTo(assertion); + assertThat(fileValidationService.isAcceptedMimeType(file)).isEqualTo(assertion); }