Add method for deleting a file from a mailbox

This commit is contained in:
Torsten Grote
2022-01-21 12:08:52 -03:00
parent 0cb2dcf6b7
commit 482258fc92
3 changed files with 61 additions and 0 deletions

View File

@@ -88,6 +88,14 @@ interface MailboxApi {
void getFile(MailboxProperties properties, String folderId,
String fileId, File file) throws IOException, ApiException;
/**
* Used by owner and contacts to delete files.
* <p>
* Returns 200 OK (no exception) if deletion was successful.
*/
void deleteFile(MailboxProperties properties, String folderId,
String fileId) throws IOException, ApiException;
@Immutable
@JsonSerialize
class MailboxContact {

View File

@@ -221,6 +221,19 @@ class MailboxApiImpl implements MailboxApi {
copyAndClose(body.byteStream(), outputStream);
}
@Override
public void deleteFile(MailboxProperties properties, String folderId,
String fileId) throws IOException, ApiException {
String path = "/files/" + folderId + "/" + fileId;
Request request = getRequestBuilder(properties.getAuthToken())
.delete()
.url(properties.getOnionAddress() + path)
.build();
OkHttpClient client = httpClientProvider.get();
Response response = client.newCall(request).execute();
if (response.code() != 200) throw new ApiException();
}
/* Helper Functions */
private Response sendGetRequest(MailboxProperties properties, String path)

View File

@@ -582,6 +582,46 @@ public class MailboxApiTest extends BrambleTestCase {
assertEquals(0, readBytes(file3).length);
}
@Test
public void testDeleteFile() throws Exception {
String name = getMailboxSecret();
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse());
server.enqueue(new MockResponse().setResponseCode(205));
server.enqueue(new MockResponse().setResponseCode(401));
server.start();
String baseUrl = getBaseUrl(server);
MailboxProperties properties =
new MailboxProperties(baseUrl, token, true);
// file gets deleted as expected
api.deleteFile(properties, contactInboxId, name);
RecordedRequest request1 = server.takeRequest();
assertEquals("DELETE", request1.getMethod());
assertEquals("/files/" + contactInboxId + "/" + name,
request1.getPath());
assertToken(request1, token);
// request is not returning 200
assertThrows(ApiException.class, () ->
api.deleteFile(properties, contactInboxId, name));
RecordedRequest request2 = server.takeRequest();
assertEquals("DELETE", request2.getMethod());
assertEquals("/files/" + contactInboxId + "/" + name,
request2.getPath());
assertToken(request2, token);
// request is not authorized
assertThrows(ApiException.class, () ->
api.deleteFile(properties, contactInboxId, name));
RecordedRequest request3 = server.takeRequest();
assertEquals("DELETE", request3.getMethod());
assertEquals("/files/" + contactInboxId + "/" + name,
request3.getPath());
assertToken(request3, token);
}
private String getBaseUrl(MockWebServer server) {
String baseUrl = server.url("").toString();
return baseUrl.substring(0, baseUrl.length() - 1);