From 3868bc745c07500ef6b06baf0cfd6aa8abcab9a6 Mon Sep 17 00:00:00 2001 From: open-schnick <60597856+open-schnick@users.noreply.github.com> Date: Sat, 22 May 2021 12:07:00 +0200 Subject: [PATCH 1/4] Added create new folder endpoint --- pom.xml | 2 +- .../business/FileSystemUploadService.java | 56 +++++++++++++++++++ .../data/dto/upload/CreateNewFolder.java | 19 +++++++ .../rest/FileSystemRestController.java | 11 ++++ .../rest/FileSystemRestService.java | 11 ++++ .../rest/FileSystemRestServiceInterface.java | 3 + src/main/resources/application.properties | 4 +- 7 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 src/main/java/de/filefighter/rest/domain/filesystem/data/dto/upload/CreateNewFolder.java diff --git a/pom.xml b/pom.xml index 429c5435..be7d496e 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ de.filefighter rest - 0.0.9 + 0.1.0 RestApi RestApi for FileFighter diff --git a/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java b/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java index b47aa93b..077b327b 100644 --- a/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java +++ b/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java @@ -5,6 +5,7 @@ import de.filefighter.rest.domain.common.exceptions.RequestDidntMeetFormalRequirementsException; import de.filefighter.rest.domain.filesystem.data.InteractionType; import de.filefighter.rest.domain.filesystem.data.dto.FileSystemItem; +import de.filefighter.rest.domain.filesystem.data.dto.upload.CreateNewFolder; import de.filefighter.rest.domain.filesystem.data.dto.upload.FileSystemUpload; import de.filefighter.rest.domain.filesystem.data.dto.upload.FileSystemUploadPreflightResponse; import de.filefighter.rest.domain.filesystem.data.dto.upload.PreflightResponse; @@ -386,4 +387,59 @@ public PreflightResponse handlePreflightEntity(String absolutePath, String curre } } } + + public FileSystemItem createNewFolder(long parentId, CreateNewFolder newFolderRequest, User authenticatedUser) { + FileSystemEntity parent = fileSystemRepository.findByFileSystemId(parentId); + if (null == parent) + throw new FileSystemItemCouldNotBeUploadedException("Could not find parent entity or you are not allowed to see it"); + + if (!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(parent, authenticatedUser, InteractionType.READ)) + throw new FileSystemItemCouldNotBeUploadedException("Could not find parent entity or you are not allowed to see it"); + + if (!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(parent, authenticatedUser, InteractionType.CHANGE)) + throw new FileSystemItemCouldNotBeUploadedException("You dont have write permissions in that directory."); + + // check for already existing folder. + List children = fileSystemHelperService.getFolderContentsOfEntityAndPermissions(parent, authenticatedUser, false, false); + Optional entityWithSameName = children.stream().filter(child -> child.getName().equalsIgnoreCase(newFolderRequest.getName())).findFirst(); + if (entityWithSameName.isPresent()) + throw new FileSystemItemCouldNotBeUploadedException("A Entity with the same name already exists in this directory"); + + long timeStamp = fileSystemHelperService.getCurrentTimeStamp(); + FileSystemEntity newFolder = FileSystemEntity.builder() + .path(parent.getPath() + "/" + newFolderRequest.getName().toLowerCase()) + .name(newFolderRequest.getName()) + .fileSystemId(idGenerationService.consumeNext()) + .ownerId(parent.getOwnerId()) + .editableForUserIds(parent.getEditableForUserIds()) + .editableFoGroupIds(parent.getEditableFoGroupIds()) + .visibleForGroupIds(parent.getVisibleForGroupIds()) + .visibleForUserIds(parent.getVisibleForUserIds()) + .size(0) + .typeId(FileSystemType.FOLDER.getId()) + .isFile(false) + .lastUpdatedBy(authenticatedUser.getUserId()) + .lastUpdated(timeStamp) + .build(); + + fileSystemRepository.insert(newFolder); + + // add folder to children of parent. + long[] childrenWithNewFolder = fileSystemHelperService.addLongToLongArray(parent.getItemIds(), newFolder.getFileSystemId()); + Query query = new Query().addCriteria(Criteria.where("fileSystemId").is(parent.getFileSystemId())); + Update update = new Update().set("itemIds", childrenWithNewFolder); + mongoTemplate.findAndModify(query, update, FileSystemEntity.class); + + // update timestamps. + fileSystemHelperService.recursivlyUpdateTimeStamps(parent, authenticatedUser, timeStamp); + + User owner; + try { + owner = userBusinessService.findUserById(parent.getOwnerId()); + } catch (UserNotFoundException ex) { + throw new FileFighterDataException("Could not find the owner of the entity with id: " + parentId); + } + String path = "/" + owner.getUsername() + parent.getPath() + newFolderRequest.getName().toLowerCase(); + return fileSystemHelperService.createDTO(newFolder, authenticatedUser, path); + } } \ No newline at end of file diff --git a/src/main/java/de/filefighter/rest/domain/filesystem/data/dto/upload/CreateNewFolder.java b/src/main/java/de/filefighter/rest/domain/filesystem/data/dto/upload/CreateNewFolder.java new file mode 100644 index 00000000..052cb35d --- /dev/null +++ b/src/main/java/de/filefighter/rest/domain/filesystem/data/dto/upload/CreateNewFolder.java @@ -0,0 +1,19 @@ +package de.filefighter.rest.domain.filesystem.data.dto.upload; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Builder; +import lombok.Getter; +import lombok.ToString; + +@Builder +@ToString +@Getter +public class CreateNewFolder { + private final String name; + + @JsonCreator + public CreateNewFolder(@JsonProperty("name") String name) { + this.name = name; + } +} diff --git a/src/main/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestController.java b/src/main/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestController.java index fbb00af5..ca8d67d8 100644 --- a/src/main/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestController.java +++ b/src/main/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestController.java @@ -3,6 +3,7 @@ import de.filefighter.rest.domain.common.Pair; import de.filefighter.rest.domain.filesystem.data.dto.FileSystemItem; import de.filefighter.rest.domain.filesystem.data.dto.FileSystemItemUpdate; +import de.filefighter.rest.domain.filesystem.data.dto.upload.CreateNewFolder; import de.filefighter.rest.domain.filesystem.data.dto.upload.FileSystemUpload; import de.filefighter.rest.domain.filesystem.data.dto.upload.FileSystemUploadPreflightResponse; import io.swagger.v3.oas.annotations.tags.Tag; @@ -66,6 +67,16 @@ public ResponseEntity> downloadFileOrFolder( return fileSystemRestService.downloadFileSystemEntity(ids, new Pair<>(cookieValue, accessToken)); } + @PostMapping(FS_BASE_URI + "{fsItemId}/folder/create") + public ResponseEntity createNewFolder( + @PathVariable long fsItemId, + @RequestBody CreateNewFolder newFolder, + @RequestHeader(value = "Authorization") String accessToken) { + + log.info("Tried creating new Folder {}", newFolder); + return fileSystemRestService.createNewFolder(fsItemId, newFolder, accessToken); + } + @PostMapping(FS_BASE_URI + "{fsItemId}/upload") public ResponseEntity> uploadFileOrFolder( @PathVariable long fsItemId, diff --git a/src/main/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestService.java b/src/main/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestService.java index 8e2b9234..6e94bfb1 100644 --- a/src/main/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestService.java +++ b/src/main/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestService.java @@ -8,6 +8,7 @@ import de.filefighter.rest.domain.filesystem.business.FileSystemUploadService; import de.filefighter.rest.domain.filesystem.data.dto.FileSystemItem; import de.filefighter.rest.domain.filesystem.data.dto.FileSystemItemUpdate; +import de.filefighter.rest.domain.filesystem.data.dto.upload.CreateNewFolder; import de.filefighter.rest.domain.filesystem.data.dto.upload.FileSystemUpload; import de.filefighter.rest.domain.filesystem.data.dto.upload.FileSystemUploadPreflightResponse; import de.filefighter.rest.domain.user.data.dto.User; @@ -61,6 +62,16 @@ public ResponseEntity> downloadFileSystemEntity(List return new ResponseEntity<>(listStringPair.getFirst(), responseHeaders, HttpStatus.OK); } + @Override + public ResponseEntity createNewFolder(long parentId, CreateNewFolder newFolder, String accessToken) { + User authenticatedUser = authenticationService.bearerAuthenticationWithAccessToken(accessToken); + String sanitizedName = inputSanitizerService.sanitizeString(newFolder.getName()); + newFolder = CreateNewFolder.builder().name(sanitizedName).build(); + + FileSystemItem folderItem = fileSystemUploadService.createNewFolder(parentId, newFolder, authenticatedUser); + return new ResponseEntity<>(folderItem, HttpStatus.CREATED); + } + @Override public ResponseEntity> uploadFileSystemItemWithAccessToken(long rootItemId, FileSystemUpload fileSystemUpload, String accessToken) { User authenticatedUser = authenticationService.bearerAuthenticationWithAccessToken(accessToken); diff --git a/src/main/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestServiceInterface.java b/src/main/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestServiceInterface.java index 4c81d358..877c1d0f 100644 --- a/src/main/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestServiceInterface.java +++ b/src/main/java/de/filefighter/rest/domain/filesystem/rest/FileSystemRestServiceInterface.java @@ -3,6 +3,7 @@ import de.filefighter.rest.domain.common.Pair; import de.filefighter.rest.domain.filesystem.data.dto.FileSystemItem; import de.filefighter.rest.domain.filesystem.data.dto.FileSystemItemUpdate; +import de.filefighter.rest.domain.filesystem.data.dto.upload.CreateNewFolder; import de.filefighter.rest.domain.filesystem.data.dto.upload.FileSystemUpload; import de.filefighter.rest.domain.filesystem.data.dto.upload.FileSystemUploadPreflightResponse; import org.springframework.http.ResponseEntity; @@ -25,4 +26,6 @@ public interface FileSystemRestServiceInterface { ResponseEntity> deleteFileSystemItemWithIdAndAccessToken(long fsItemId, String accessToken); ResponseEntity> downloadFileSystemEntity(List fsItemIds, Pair authPair); + + ResponseEntity createNewFolder(long parentId, CreateNewFolder newFolder, String accessToken); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8a03ff07..034d6957 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -16,6 +16,6 @@ spring.data.mongodb.host=localhost spring.data.mongodb.port=20000 spring.data.mongodb.auto-index-creation=true #------------------- Custom ----------------------- -filefighter.version=0.0.9 -filefighter.date=18.05.2021 +filefighter.version=0.1.0 +filefighter.date=22.05.2021 filefighter.disable-password-check=false From b97e2c146ab5791d5a9133e0e936918ee3826e53 Mon Sep 17 00:00:00 2001 From: open-schnick <60597856+open-schnick@users.noreply.github.com> Date: Sat, 22 May 2021 15:36:58 +0200 Subject: [PATCH 2/4] Unit Tests --- .../business/FileSystemUploadService.java | 8 +- .../FileSystemUploadServiceUnitTest.java | 91 +++++++++++++++++++ 2 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/test/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadServiceUnitTest.java diff --git a/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java b/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java index 077b327b..83092d35 100644 --- a/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java +++ b/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java @@ -391,10 +391,10 @@ public PreflightResponse handlePreflightEntity(String absolutePath, String curre public FileSystemItem createNewFolder(long parentId, CreateNewFolder newFolderRequest, User authenticatedUser) { FileSystemEntity parent = fileSystemRepository.findByFileSystemId(parentId); if (null == parent) - throw new FileSystemItemCouldNotBeUploadedException("Could not find parent entity or you are not allowed to see it"); + throw new FileSystemItemCouldNotBeUploadedException("Could not find parent entity or you are not allowed to see it."); if (!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(parent, authenticatedUser, InteractionType.READ)) - throw new FileSystemItemCouldNotBeUploadedException("Could not find parent entity or you are not allowed to see it"); + throw new FileSystemItemCouldNotBeUploadedException("Could not find parent entity or you are not allowed to see it."); if (!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(parent, authenticatedUser, InteractionType.CHANGE)) throw new FileSystemItemCouldNotBeUploadedException("You dont have write permissions in that directory."); @@ -403,7 +403,7 @@ public FileSystemItem createNewFolder(long parentId, CreateNewFolder newFolderRe List children = fileSystemHelperService.getFolderContentsOfEntityAndPermissions(parent, authenticatedUser, false, false); Optional entityWithSameName = children.stream().filter(child -> child.getName().equalsIgnoreCase(newFolderRequest.getName())).findFirst(); if (entityWithSameName.isPresent()) - throw new FileSystemItemCouldNotBeUploadedException("A Entity with the same name already exists in this directory"); + throw new FileSystemItemCouldNotBeUploadedException("A Entity with the same name already exists in this directory."); long timeStamp = fileSystemHelperService.getCurrentTimeStamp(); FileSystemEntity newFolder = FileSystemEntity.builder() @@ -439,7 +439,7 @@ public FileSystemItem createNewFolder(long parentId, CreateNewFolder newFolderRe } catch (UserNotFoundException ex) { throw new FileFighterDataException("Could not find the owner of the entity with id: " + parentId); } - String path = "/" + owner.getUsername() + parent.getPath() + newFolderRequest.getName().toLowerCase(); + String path = "/" + owner.getUsername() + parent.getPath() + "/" + newFolderRequest.getName().toLowerCase(); return fileSystemHelperService.createDTO(newFolder, authenticatedUser, path); } } \ No newline at end of file diff --git a/src/test/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadServiceUnitTest.java b/src/test/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadServiceUnitTest.java new file mode 100644 index 00000000..04de86c6 --- /dev/null +++ b/src/test/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadServiceUnitTest.java @@ -0,0 +1,91 @@ +package de.filefighter.rest.domain.filesystem.business; + +import de.filefighter.rest.domain.common.InputSanitizerService; +import de.filefighter.rest.domain.filesystem.data.InteractionType; +import de.filefighter.rest.domain.filesystem.data.dto.FileSystemItem; +import de.filefighter.rest.domain.filesystem.data.dto.upload.CreateNewFolder; +import de.filefighter.rest.domain.filesystem.data.persistence.FileSystemEntity; +import de.filefighter.rest.domain.filesystem.data.persistence.FileSystemRepository; +import de.filefighter.rest.domain.filesystem.exceptions.FileSystemItemCouldNotBeUploadedException; +import de.filefighter.rest.domain.filesystem.type.FileSystemTypeRepository; +import de.filefighter.rest.domain.user.business.UserBusinessService; +import de.filefighter.rest.domain.user.data.dto.User; +import org.junit.jupiter.api.Test; +import org.springframework.data.mongodb.core.MongoTemplate; + +import java.util.Collections; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class FileSystemUploadServiceUnitTest { + + private final FileSystemRepository fileSystemRepositoryMock = mock(FileSystemRepository.class); + private final FileSystemHelperService fileSystemHelperServiceMock = mock(FileSystemHelperService.class); + private final InputSanitizerService inputSanitizerServiceMock = mock(InputSanitizerService.class); + private final MongoTemplate mongoTemplateMock = mock(MongoTemplate.class); + private final FileSystemTypeRepository fileSystemTypeRepositoryMock = mock(FileSystemTypeRepository.class); + private final UserBusinessService userBusinessServiceMock = mock(UserBusinessService.class); + private final IdGenerationService idGenerationServiceMock = mock(IdGenerationService.class); + + private final FileSystemUploadService fileSystemUploadService = new FileSystemUploadService(fileSystemRepositoryMock, fileSystemHelperServiceMock, inputSanitizerServiceMock, fileSystemTypeRepositoryMock, mongoTemplateMock, userBusinessServiceMock, idGenerationServiceMock); + + @Test + void createNewFolderThrows() { + long parentId = 420; + String folderName = "Kevin"; + CreateNewFolder createNewFolder = new CreateNewFolder(folderName); + User autheticatedUser = User.builder().build(); + + FileSystemItemCouldNotBeUploadedException ex = assertThrows(FileSystemItemCouldNotBeUploadedException.class, + () -> fileSystemUploadService.createNewFolder(parentId, createNewFolder, autheticatedUser)); + assertEquals(FileSystemItemCouldNotBeUploadedException.getErrorMessagePrefix() + " Could not find parent entity or you are not allowed to see it.", ex.getMessage()); + + FileSystemEntity parent = FileSystemEntity.builder().build(); + when(fileSystemRepositoryMock.findByFileSystemId(parentId)).thenReturn(parent); + + ex = assertThrows(FileSystemItemCouldNotBeUploadedException.class, + () -> fileSystemUploadService.createNewFolder(parentId, createNewFolder, autheticatedUser)); + assertEquals(FileSystemItemCouldNotBeUploadedException.getErrorMessagePrefix() + " Could not find parent entity or you are not allowed to see it.", ex.getMessage()); + + when(fileSystemHelperServiceMock.userIsAllowedToInteractWithFileSystemEntity(parent, autheticatedUser, InteractionType.READ)).thenReturn(true); + + ex = assertThrows(FileSystemItemCouldNotBeUploadedException.class, + () -> fileSystemUploadService.createNewFolder(parentId, createNewFolder, autheticatedUser)); + assertEquals(FileSystemItemCouldNotBeUploadedException.getErrorMessagePrefix() + " You dont have write permissions in that directory.", ex.getMessage()); + + when(fileSystemHelperServiceMock.userIsAllowedToInteractWithFileSystemEntity(parent, autheticatedUser, InteractionType.CHANGE)).thenReturn(true); + when(fileSystemHelperServiceMock.getFolderContentsOfEntityAndPermissions(parent, autheticatedUser, false, false)).thenReturn(Collections.singletonList(FileSystemEntity.builder().name(folderName.toUpperCase()).build())); + + ex = assertThrows(FileSystemItemCouldNotBeUploadedException.class, + () -> fileSystemUploadService.createNewFolder(parentId, createNewFolder, autheticatedUser)); + assertEquals(FileSystemItemCouldNotBeUploadedException.getErrorMessagePrefix() + " A Entity with the same name already exists in this directory.", ex.getMessage()); + } + + @Test + void createNewFolderWorks() { + long parentId = 420; + String folderName = "Kevin"; + CreateNewFolder createNewFolder = new CreateNewFolder(folderName); + long userId = 420; + User autheticatedUser = User.builder().build(); + + FileSystemEntity parent = FileSystemEntity.builder().path("/parent").ownerId(userId).build(); + when(fileSystemRepositoryMock.findByFileSystemId(parentId)).thenReturn(parent); + when(fileSystemHelperServiceMock.userIsAllowedToInteractWithFileSystemEntity(parent, autheticatedUser, InteractionType.READ)).thenReturn(true); + when(fileSystemHelperServiceMock.userIsAllowedToInteractWithFileSystemEntity(parent, autheticatedUser, InteractionType.CHANGE)).thenReturn(true); + when(fileSystemHelperServiceMock.getFolderContentsOfEntityAndPermissions(parent, autheticatedUser, false, false)).thenReturn(Collections.singletonList(FileSystemEntity.builder().name("a name").build())); + when(userBusinessServiceMock.findUserById(userId)).thenReturn(User.builder().username(folderName).build()); + + FileSystemItem item = FileSystemItem.builder().build(); + String path = "/" + folderName + "/parent/" + folderName.toLowerCase(); + when(fileSystemHelperServiceMock.createDTO(any(), eq(autheticatedUser), eq(path))).thenReturn(item); + + FileSystemItem actual = fileSystemUploadService.createNewFolder(parentId, createNewFolder, autheticatedUser); + assertEquals(item, actual); + } +} \ No newline at end of file From a6e9d235672f6df213f3d79c87967ad9cdc8439b Mon Sep 17 00:00:00 2001 From: open-schnick <60597856+open-schnick@users.noreply.github.com> Date: Sun, 23 May 2021 13:17:28 +0200 Subject: [PATCH 3/4] Fixed a bug with the calculation of paths --- .../business/FileSystemUploadService.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java b/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java index 83092d35..7f071c25 100644 --- a/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java +++ b/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java @@ -406,8 +406,16 @@ public FileSystemItem createNewFolder(long parentId, CreateNewFolder newFolderRe throw new FileSystemItemCouldNotBeUploadedException("A Entity with the same name already exists in this directory."); long timeStamp = fileSystemHelperService.getCurrentTimeStamp(); + + String dbPath; + if (parent.getPath().equals("/")) { + dbPath = parent.getPath() + newFolderRequest.getName().toLowerCase(); + } else { + dbPath = parent.getPath() + "/" + newFolderRequest.getName().toLowerCase(); + } + FileSystemEntity newFolder = FileSystemEntity.builder() - .path(parent.getPath() + "/" + newFolderRequest.getName().toLowerCase()) + .path(dbPath) .name(newFolderRequest.getName()) .fileSystemId(idGenerationService.consumeNext()) .ownerId(parent.getOwnerId()) @@ -439,7 +447,15 @@ public FileSystemItem createNewFolder(long parentId, CreateNewFolder newFolderRe } catch (UserNotFoundException ex) { throw new FileFighterDataException("Could not find the owner of the entity with id: " + parentId); } - String path = "/" + owner.getUsername() + parent.getPath() + "/" + newFolderRequest.getName().toLowerCase(); - return fileSystemHelperService.createDTO(newFolder, authenticatedUser, path); + + StringBuilder absolutePathBuilder = new StringBuilder("/").append(owner.getUsername()); + if (parent.getPath().equals("/")) { + absolutePathBuilder.append(parent.getPath()); + } else { + absolutePathBuilder.append(parent.getPath()).append("/"); + } + absolutePathBuilder.append(newFolderRequest.getName().toLowerCase()); + + return fileSystemHelperService.createDTO(newFolder, authenticatedUser, absolutePathBuilder.toString()); } } \ No newline at end of file From 894bbbdf2127aff3e2a34de4ddd66c61da33ab43 Mon Sep 17 00:00:00 2001 From: qvalentin Date: Sun, 23 May 2021 13:50:28 +0200 Subject: [PATCH 4/4] FF-426 add Integration tests for new folder --- .../rest/cucumber/FileSystemUploadSteps.java | 18 +++++ src/test/resources/NewFolder.feature | 69 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/test/resources/NewFolder.feature diff --git a/src/test/java/de/filefighter/rest/cucumber/FileSystemUploadSteps.java b/src/test/java/de/filefighter/rest/cucumber/FileSystemUploadSteps.java index f2496bf1..3f5c311c 100644 --- a/src/test/java/de/filefighter/rest/cucumber/FileSystemUploadSteps.java +++ b/src/test/java/de/filefighter/rest/cucumber/FileSystemUploadSteps.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import de.filefighter.rest.RestApplicationIntegrationTest; +import de.filefighter.rest.domain.filesystem.data.dto.upload.CreateNewFolder; import de.filefighter.rest.domain.filesystem.data.dto.upload.FileSystemUpload; import de.filefighter.rest.domain.filesystem.data.dto.upload.PreflightResponse; import io.cucumber.java.en.Then; @@ -110,4 +111,21 @@ public void theUserWithTokenWantsToUploadAFileWithTheNamePathMimeTypeAndSizeToTh executeRestApiCall(HttpMethod.POST, url, header, jsonBody); } + + @When("the user with token {string} wants to create a folder with name {string} in the the folder with the id {long}") + public void theUserWithTokenWantsToCreateAFolderWithNameInTheTheFolderWithTheId(String accessToken, String name, long parentId ) throws JsonProcessingException { + + String authHeaderString = AUTHORIZATION_BEARER_PREFIX + accessToken; + String url = BASE_API_URI + FS_BASE_URI + parentId + "/folder/create"; + + HashMap header = new HashMap<>(); + header.put("Authorization", authHeaderString); + + String jsonBody = objectMapper.writeValueAsString(CreateNewFolder.builder() + .name(name) + .build()); + + executeRestApiCall(HttpMethod.POST, url, header, jsonBody); + + } } diff --git a/src/test/resources/NewFolder.feature b/src/test/resources/NewFolder.feature new file mode 100644 index 00000000..2955a015 --- /dev/null +++ b/src/test/resources/NewFolder.feature @@ -0,0 +1,69 @@ +Feature: Create new folders + As a user + I want to be able to create new folders to be able to upload file to them + + Background: + Given database is empty + And runtime user exists + And user with userId 1234 exists and has username "Richard", password "badPassword" + And user with userId 420 exists and has username "Nasir", password "AlsoBadPassword" + And accessToken with value "1234" exists for user 1234 + And accessToken with value "420" exists for user 420 + And user with userId 1234 has HomeFolder with Id 1234 + And user with userId 420 has HomeFolder with Id 420 + And fileSystemItem with the fileSystemId 420 is a folder and contains the fileSystemId 42 + And fileSystemItem with the fileSystemId 42 exists, has owner with userId 420 has the path "/gebäude" and name "gebäude" + And fileSystemItem with the fileSystemId 42 is a folder and contains the fileSystemId 72 + And fileSystemItem with the fileSystemId 72 exists, has owner with userId 420 and name "Bergfried.avi" + + Scenario: Successful interaction, creation of folder in personal folder + When the user with token "420" wants to create a folder with name "thefolder" in the the folder with the id 42 + Then response status code is 201 + Then response contains key "path" and value "/nasir/gebäude/thefolder" + Then response contains key "name" and value "thefolder" + When user with token "420" wants to see the content of folder with path "/Nasir/gebäude" + Then the response contains the folder with name "thefolder" + + Scenario: Successful interaction, creation of folder in home folder + When the user with token "420" wants to create a folder with name "thefolder" in the the folder with the id 420 + Then response status code is 201 + Then response contains key "path" and value "/nasir/thefolder" + Then response contains key "name" and value "thefolder" + When user with token "420" wants to see the content of folder with path "/Nasir" + Then the response contains the folder with name "thefolder" + + Scenario: Successful interaction, creation of folder in shared folder + Given user with the userId 1234 is allowed to EDIT the fileSystemItem with the fileSystemId 42 + And user with the userId 1234 is allowed to VIEW the fileSystemItem with the fileSystemId 42 + When the user with token "1234" wants to create a folder with name "thefolder" in the the folder with the id 42 + Then response status code is 201 + Then response contains key "path" and value "/nasir/gebäude/thefolder" + Then response contains key "name" and value "thefolder" + When user with token "1234" wants to see the content of folder with path "/Nasir/gebäude" + Then the response contains the folder with name "thefolder" + + Scenario: Folder does not exist + When the user with token "420" wants to create a folder with name "thefolder" in the the folder with the id 422 + Then response status code is 400 + And response contains key "message" and value "FileSystemEntity could not be uploaded. Could not find parent entity or you are not allowed to see it." + + Scenario: Folder already exists + When the user with token "420" wants to create a folder with name "gebäudE" in the the folder with the id 420 + Then response status code is 400 + And response contains key "message" and value "FileSystemEntity could not be uploaded. A Entity with the same name already exists in this directory." + + Scenario: File with same name already exists + When the user with token "420" wants to create a folder with name "Bergfried.avi" in the the folder with the id 42 + Then response status code is 400 + And response contains key "message" and value "FileSystemEntity could not be uploaded. A Entity with the same name already exists in this directory." + + Scenario: insufficient authorization (can't view) + When the user with token "1234" wants to create a folder with name "thefolder" in the the folder with the id 420 + Then response status code is 400 + And response contains key "message" and value "FileSystemEntity could not be uploaded. Could not find parent entity or you are not allowed to see it." + + Scenario: insufficient authorization (can't edit) + Given user with the userId 1234 is allowed to VIEW the fileSystemItem with the fileSystemId 420 + When the user with token "1234" wants to create a folder with name "thefolder" in the the folder with the id 420 + Then response status code is 400 + And response contains key "message" and value "FileSystemEntity could not be uploaded. You dont have write permissions in that directory."