Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequ
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

Expand Down Expand Up @@ -658,4 +659,40 @@ class WorkspaceControllerTests : ControllerTestBase() {
document(
"organizations/{organization_id}/workspaces/{workspace_id}/files/download/GET"))
}

@Test
@WithMockOauth2User
fun `download workspace file with wrong file name`() {

val workspaceId =
createWorkspaceAndReturnId(
mvc, organizationId, constructWorkspaceCreateRequest(solutionId = solutionId))

val fileName = "test.txt"
val fileToUpload =
this::class.java.getResourceAsStream("/workspace/$fileName")
?: throw IllegalStateException(
"$fileName file used for organizations/{organization_id}/workspaces/{workspace_id}/files/POST endpoint documentation cannot be null")

val mockFile =
MockMultipartFile(
"file", fileName, MediaType.TEXT_PLAIN_VALUE, IOUtils.toByteArray(fileToUpload))

val destination = "path/to/a/directory/"
mvc.perform(
multipart("/organizations/$organizationId/workspaces/$workspaceId/files")
.file(mockFile)
.param("overwrite", "true")
.param("destination", destination)
.accept(MediaType.APPLICATION_JSON)
.with(csrf()))

mvc.perform(
get("/organizations/$organizationId/workspaces/$workspaceId/files/download")
.param("file_name", "Wrong file name")
.accept(MediaType.APPLICATION_OCTET_STREAM))
.andExpect(status().is4xxClientError)
.andExpect(jsonPath("$.detail").value("The specified key does not exist."))
.andDo(MockMvcResultHandlers.print())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice
import org.springframework.web.context.request.WebRequest
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
import org.springframework.web.util.BindErrorUtils
import software.amazon.awssdk.services.s3.model.NoSuchKeyException

@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice
Expand Down Expand Up @@ -160,4 +161,15 @@ open class CsmExceptionHandling : ResponseEntityExceptionHandler() {
}
return response
}

@ExceptionHandler(NoSuchKeyException::class)
fun handleNoSuchKeyException(exception: NoSuchKeyException): ProblemDetail {
val response = ProblemDetail.forStatus(HttpStatus.NOT_FOUND)
val notImplementedErrorStatus = HttpStatus.NOT_FOUND
response.type = URI.create(httpStatusCodeTypePrefix + notImplementedErrorStatus.value())
if (exception.awsErrorDetails().errorMessage() != null) {
response.detail = exception.awsErrorDetails().errorMessage()
}
return response
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,24 @@ class WorkspaceServiceIntegrationTest : CsmTestBase() {
assertEquals(expectedText, retrievedText)
}

@Test
fun `test get workspace file with wrong name`() {
val resourceTestFile = resourceLoader.getResource("classpath:/$fileName").file
val input = FileInputStream(resourceTestFile)
val multipartFile =
MockMultipartFile(
"file", resourceTestFile.getName(), "text/plain", IOUtils.toByteArray(input))
workspaceApiService.createWorkspaceFile(
organizationSaved.id, workspaceSaved.id, multipartFile, true, null)

val exception =
assertThrows<NoSuchKeyException> {
workspaceApiService.getWorkspaceFile(organizationSaved.id, workspaceSaved.id, "WrongName")
}

assertEquals("The specified key does not exist.", exception.awsErrorDetails().errorMessage())
}

@Test
fun `test list workspace files`() {
every { getCurrentAuthenticatedRoles(any()) } returns listOf("Platform.Admin")
Expand Down
Loading