Actually throw TolerableFailureException when *deleting* a contact

Before, this was accidentally added to *listing* contacts.
This commit is contained in:
Torsten Grote
2022-01-07 14:29:14 -03:00
parent af3389e0e1
commit 9fa54bf15c
3 changed files with 20 additions and 20 deletions

View File

@@ -43,20 +43,20 @@ interface MailboxApi {
/** /**
* Deletes a contact from the mailbox. * Deletes a contact from the mailbox.
* This should get called after a contact was removed from Briar. * 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 * @throws TolerableFailureException if response code is 404
* (contact probably was already deleted). * (contact probably was already deleted).
*/ */
Collection<ContactId> getContacts(MailboxProperties properties) void deleteContact(MailboxProperties properties, ContactId contactId)
throws IOException, ApiException, TolerableFailureException; 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<ContactId> getContacts(MailboxProperties properties)
throws IOException, ApiException;
@Immutable @Immutable
@JsonSerialize @JsonSerialize
class MailboxContact { class MailboxContact {

View File

@@ -113,7 +113,7 @@ class MailboxApiImpl implements MailboxApi {
@Override @Override
public void deleteContact(MailboxProperties properties, ContactId contactId) public void deleteContact(MailboxProperties properties, ContactId contactId)
throws IOException, ApiException { throws IOException, ApiException, TolerableFailureException {
if (!properties.isOwner()) throw new IllegalArgumentException(); if (!properties.isOwner()) throw new IllegalArgumentException();
String url = properties.getOnionAddress() + "/contacts/" + String url = properties.getOnionAddress() + "/contacts/" +
contactId.getInt(); contactId.getInt();
@@ -123,15 +123,15 @@ class MailboxApiImpl implements MailboxApi {
.build(); .build();
OkHttpClient client = httpClientProvider.get(); OkHttpClient client = httpClientProvider.get();
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
if (response.code() == 404) throw new TolerableFailureException();
if (response.code() != 200) throw new ApiException(); if (response.code() != 200) throw new ApiException();
} }
@Override @Override
public Collection<ContactId> getContacts(MailboxProperties properties) public Collection<ContactId> getContacts(MailboxProperties properties)
throws IOException, ApiException, TolerableFailureException { throws IOException, ApiException {
if (!properties.isOwner()) throw new IllegalArgumentException(); if (!properties.isOwner()) throw new IllegalArgumentException();
Response response = sendGetRequest(properties, "/contacts"); Response response = sendGetRequest(properties, "/contacts");
if (response.code() == 404) throw new TolerableFailureException();
if (response.code() != 200) throw new ApiException(); if (response.code() != 200) throw new ApiException();
ResponseBody body = response.body(); ResponseBody body = response.body();

View File

@@ -230,6 +230,7 @@ public class MailboxApiTest extends BrambleTestCase {
server.enqueue(new MockResponse()); server.enqueue(new MockResponse());
server.enqueue(new MockResponse().setResponseCode(205)); server.enqueue(new MockResponse().setResponseCode(205));
server.enqueue(new MockResponse().setResponseCode(401)); server.enqueue(new MockResponse().setResponseCode(401));
server.enqueue(new MockResponse().setResponseCode(404));
server.start(); server.start();
String baseUrl = getBaseUrl(server); String baseUrl = getBaseUrl(server);
MailboxProperties properties = MailboxProperties properties =
@@ -257,6 +258,14 @@ public class MailboxApiTest extends BrambleTestCase {
assertEquals("DELETE", request3.getMethod()); assertEquals("DELETE", request3.getMethod());
assertEquals("/contacts/" + contactId.getInt(), request3.getPath()); assertEquals("/contacts/" + contactId.getInt(), request3.getPath());
assertToken(request3, token); 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 @Test
@@ -286,7 +295,6 @@ public class MailboxApiTest extends BrambleTestCase {
server.enqueue(new MockResponse().setBody(invalidResponse3)); server.enqueue(new MockResponse().setBody(invalidResponse3));
server.enqueue(new MockResponse().setResponseCode(401)); server.enqueue(new MockResponse().setResponseCode(401));
server.enqueue(new MockResponse().setResponseCode(500)); server.enqueue(new MockResponse().setResponseCode(500));
server.enqueue(new MockResponse().setResponseCode(404));
server.start(); server.start();
String baseUrl = getBaseUrl(server); String baseUrl = getBaseUrl(server);
MailboxProperties properties = MailboxProperties properties =
@@ -350,14 +358,6 @@ public class MailboxApiTest extends BrambleTestCase {
assertEquals("/contacts", request8.getPath()); assertEquals("/contacts", request8.getPath());
assertEquals("GET", request8.getMethod()); assertEquals("GET", request8.getMethod());
assertToken(request8, token); 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 @Test