Skip to content
Snippets Groups Projects
Verified Commit 2e5c99e7 authored by Torsten Grote's avatar Torsten Grote
Browse files

API endpoint for listing files in specified folder

parent 458c4d77
No related branches found
No related tags found
1 merge request!28Implement file management API
......@@ -60,9 +60,14 @@ class FileManager @Inject constructor(
randomIdManager.assertIsRandomId(folderId)
authManager.assertCanDownloadFromFolder(principal, folderId)
// TODO implement
call.respond(HttpStatusCode.OK, "get: Not yet implemented. folderId: $folderId")
val fileListResponse = withContext(Dispatchers.IO) {
val list = ArrayList<FileResponse>()
fileProvider.getFolder(folderId).listFiles()?.forEach { file ->
list.add(FileResponse(file.name, file.lastModified()))
}
FileListResponse(list)
}
call.respond(HttpStatusCode.OK, fileListResponse)
}
/**
......@@ -122,3 +127,6 @@ class FileManager @Inject constructor(
}
}
data class FileListResponse(val files: ArrayList<FileResponse>)
data class FileResponse(val name: String, val time: Long)
package org.briarproject.mailbox.core.files
import io.ktor.client.call.receive
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.readText
......@@ -53,14 +55,64 @@ class FileManagerIntegrationTest : IntegrationTest() {
}
@Test
fun `post new file creates new file`(): Unit = runBlocking {
fun `post new file, list, download and delete it`(): Unit = runBlocking {
// owner uploads the file
val response: HttpResponse = httpClient.post("$baseUrl/files/${contact1.inboxId}") {
authenticateWithToken(ownerToken)
body = bytes
}
assertEquals(HttpStatusCode.OK.value, response.status.value)
// contact can list the file
val listResponse: HttpResponse = httpClient.get("$baseUrl/files/${contact1.inboxId}") {
authenticateWithToken(contact1.token)
}
assertEquals(HttpStatusCode.OK.value, listResponse.status.value)
val fileList: FileListResponse = listResponse.receive()
assertEquals(1, fileList.files.size)
// TODO fetch the file later to see that it was uploaded correctly
val fileId = fileList.files[0].name
// TODO delete the file to clean up again
}
@Test
fun `list files rejects wrong token`(): Unit = runBlocking {
val response: HttpResponse = httpClient.get("$baseUrl/files/${getNewRandomId()}") {
authenticateWithToken(token)
body = bytes
}
assertEquals(HttpStatusCode.Unauthorized.value, response.status.value)
}
@Test
fun `list files rejects unauthorized folder ID`(): Unit = runBlocking {
val response: HttpResponse = httpClient.get("$baseUrl/files/${contact1.inboxId}") {
authenticateWithToken(ownerToken)
body = bytes
}
assertEquals(HttpStatusCode.Unauthorized.value, response.status.value)
}
@Test
fun `list files rejects invalid folder ID`(): Unit = runBlocking {
val response: HttpResponse = httpClient.get("$baseUrl/files/foo") {
authenticateWithToken(ownerToken)
body = bytes
}
assertEquals(HttpStatusCode.BadRequest.value, response.status.value)
assertEquals("Malformed ID: foo", response.readText())
}
@Test
fun `list files gives empty response for empty folder`(): Unit = runBlocking {
val response: HttpResponse = httpClient.get("$baseUrl/files/${contact1.outboxId}") {
authenticateWithToken(ownerToken)
body = bytes
}
assertEquals(HttpStatusCode.OK.value, response.status.value)
assertEquals("""{"files":[]}""", response.readText())
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment