From 9fa54bf15cabe41f8f555b3a842b92c21bf8e9ae Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Fri, 7 Jan 2022 14:29:14 -0300 Subject: [PATCH] Actually throw TolerableFailureException when *deleting* a contact Before, this was accidentally added to *listing* contacts. --- .../bramble/mailbox/MailboxApi.java | 16 ++++++++-------- .../bramble/mailbox/MailboxApiImpl.java | 6 +++--- .../bramble/mailbox/MailboxApiTest.java | 18 +++++++++--------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApi.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApi.java index 4040df48b..be08cc11a 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApi.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApi.java @@ -43,20 +43,20 @@ interface MailboxApi { /** * Deletes a contact from the mailbox. * This should get called after a contact was removed from Briar. - */ - void deleteContact(MailboxProperties properties, ContactId contactId) - throws IOException, ApiException; - - /** - * Gets a list of {@link ContactId}s from the mailbox. - * These are the contacts that the mailbox already knows about. * * @throws TolerableFailureException if response code is 404 * (contact probably was already deleted). */ - Collection getContacts(MailboxProperties properties) + void deleteContact(MailboxProperties properties, ContactId contactId) throws IOException, ApiException, TolerableFailureException; + /** + * Gets a list of {@link ContactId}s from the mailbox. + * These are the contacts that the mailbox already knows about. + */ + Collection getContacts(MailboxProperties properties) + throws IOException, ApiException; + @Immutable @JsonSerialize class MailboxContact { diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApiImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApiImpl.java index 3a42d9e6f..ba24a852d 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApiImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxApiImpl.java @@ -113,7 +113,7 @@ class MailboxApiImpl implements MailboxApi { @Override public void deleteContact(MailboxProperties properties, ContactId contactId) - throws IOException, ApiException { + throws IOException, ApiException, TolerableFailureException { if (!properties.isOwner()) throw new IllegalArgumentException(); String url = properties.getOnionAddress() + "/contacts/" + contactId.getInt(); @@ -123,15 +123,15 @@ class MailboxApiImpl implements MailboxApi { .build(); OkHttpClient client = httpClientProvider.get(); Response response = client.newCall(request).execute(); + if (response.code() == 404) throw new TolerableFailureException(); if (response.code() != 200) throw new ApiException(); } @Override public Collection getContacts(MailboxProperties properties) - throws IOException, ApiException, TolerableFailureException { + throws IOException, ApiException { if (!properties.isOwner()) throw new IllegalArgumentException(); Response response = sendGetRequest(properties, "/contacts"); - if (response.code() == 404) throw new TolerableFailureException(); if (response.code() != 200) throw new ApiException(); ResponseBody body = response.body(); diff --git a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxApiTest.java b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxApiTest.java index a62d012d6..c27e7ef42 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxApiTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/MailboxApiTest.java @@ -230,6 +230,7 @@ public class MailboxApiTest extends BrambleTestCase { server.enqueue(new MockResponse()); server.enqueue(new MockResponse().setResponseCode(205)); server.enqueue(new MockResponse().setResponseCode(401)); + server.enqueue(new MockResponse().setResponseCode(404)); server.start(); String baseUrl = getBaseUrl(server); MailboxProperties properties = @@ -257,6 +258,14 @@ public class MailboxApiTest extends BrambleTestCase { assertEquals("DELETE", request3.getMethod()); assertEquals("/contacts/" + contactId.getInt(), request3.getPath()); assertToken(request3, token); + + // tolerable 404 not found error + assertThrows(TolerableFailureException.class, + () -> api.deleteContact(properties, contactId)); + RecordedRequest request4 = server.takeRequest(); + assertEquals("/contacts/" + contactId.getInt(), request4.getPath()); + assertEquals("DELETE", request4.getMethod()); + assertToken(request4, token); } @Test @@ -286,7 +295,6 @@ public class MailboxApiTest extends BrambleTestCase { server.enqueue(new MockResponse().setBody(invalidResponse3)); server.enqueue(new MockResponse().setResponseCode(401)); server.enqueue(new MockResponse().setResponseCode(500)); - server.enqueue(new MockResponse().setResponseCode(404)); server.start(); String baseUrl = getBaseUrl(server); MailboxProperties properties = @@ -350,14 +358,6 @@ public class MailboxApiTest extends BrambleTestCase { assertEquals("/contacts", request8.getPath()); assertEquals("GET", request8.getMethod()); assertToken(request8, token); - - // tolerable 404 not found error - assertThrows(TolerableFailureException.class, - () -> api.getContacts(properties)); - RecordedRequest request9 = server.takeRequest(); - assertEquals("/contacts", request9.getPath()); - assertEquals("GET", request9.getMethod()); - assertToken(request9, token); } @Test