Skip to content

Commit

Permalink
Fix failing tests 2
Browse files Browse the repository at this point in the history
  • Loading branch information
spokenbird committed Aug 29, 2024
1 parent c7fc755 commit bdbee02
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
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;

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,20 +34,20 @@ void acceptedFileTypesShouldReturnTheIntersectionOfDefaultTypesWithUserProvidedO

private static Stream<Arguments> 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);
}


Expand Down

0 comments on commit bdbee02

Please sign in to comment.