mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 14:19:53 +01:00
Add method for deleting a contact from own mailbox
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package org.briarproject.bramble.mailbox;
|
package org.briarproject.bramble.mailbox;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||||
|
|
||||||
@@ -35,10 +37,17 @@ interface MailboxApi {
|
|||||||
* (contact was already added).
|
* (contact was already added).
|
||||||
*/
|
*/
|
||||||
void addContact(MailboxProperties properties, MailboxContact contact)
|
void addContact(MailboxProperties properties, MailboxContact contact)
|
||||||
throws IOException, ApiException,
|
throws IOException, ApiException, TolerableFailureException;
|
||||||
TolerableFailureException;
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
|
@JsonSerialize
|
||||||
class MailboxContact {
|
class MailboxContact {
|
||||||
public final int contactId;
|
public final int contactId;
|
||||||
public final String token, inboxId, outboxId;
|
public final String token, inboxId, outboxId;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.JsonNode;
|
|||||||
import com.fasterxml.jackson.databind.json.JsonMapper;
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.WeakSingletonProvider;
|
import org.briarproject.bramble.api.WeakSingletonProvider;
|
||||||
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
@@ -111,6 +112,21 @@ class MailboxApiImpl implements MailboxApi {
|
|||||||
if (!response.isSuccessful()) throw new ApiException();
|
if (!response.isSuccessful()) throw new ApiException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteContact(MailboxProperties properties, ContactId contactId)
|
||||||
|
throws IOException, ApiException {
|
||||||
|
if (!properties.isOwner()) throw new IllegalArgumentException();
|
||||||
|
String url = properties.getOnionAddress() + "/contacts/" +
|
||||||
|
contactId.getInt();
|
||||||
|
Request request = getRequestBuilder(properties.getAuthToken())
|
||||||
|
.delete()
|
||||||
|
.url(url)
|
||||||
|
.build();
|
||||||
|
OkHttpClient client = httpClientProvider.get();
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
if (response.code() != 200) throw new ApiException();
|
||||||
|
}
|
||||||
|
|
||||||
private Request.Builder getRequestBuilder(String token) {
|
private Request.Builder getRequestBuilder(String token) {
|
||||||
return new Request.Builder()
|
return new Request.Builder()
|
||||||
.addHeader("Authorization", "Bearer " + token);
|
.addHeader("Authorization", "Bearer " + token);
|
||||||
|
|||||||
@@ -220,6 +220,49 @@ public class MailboxApiTest extends BrambleTestCase {
|
|||||||
api.addContact(properties, mailboxContact));
|
api.addContact(properties, mailboxContact));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteContact() throws Exception {
|
||||||
|
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);
|
||||||
|
|
||||||
|
// contact gets deleted as expected
|
||||||
|
api.deleteContact(properties, contactId);
|
||||||
|
RecordedRequest request1 = server.takeRequest();
|
||||||
|
assertEquals("DELETE", request1.getMethod());
|
||||||
|
assertEquals("/contacts/" + contactId.getInt(), request1.getPath());
|
||||||
|
assertToken(request1, token);
|
||||||
|
|
||||||
|
// request is not returning 200
|
||||||
|
assertThrows(ApiException.class, () ->
|
||||||
|
api.deleteContact(properties, contactId));
|
||||||
|
RecordedRequest request2 = server.takeRequest();
|
||||||
|
assertEquals("DELETE", request2.getMethod());
|
||||||
|
assertEquals("/contacts/" + contactId.getInt(), request2.getPath());
|
||||||
|
assertToken(request2, token);
|
||||||
|
|
||||||
|
// request is not authorized
|
||||||
|
assertThrows(ApiException.class, () ->
|
||||||
|
api.deleteContact(properties, contactId));
|
||||||
|
RecordedRequest request3 = server.takeRequest();
|
||||||
|
assertEquals("DELETE", request3.getMethod());
|
||||||
|
assertEquals("/contacts/" + contactId.getInt(), request3.getPath());
|
||||||
|
assertToken(request3, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteContactOnlyForOwner() {
|
||||||
|
MailboxProperties properties =
|
||||||
|
new MailboxProperties("", token, false);
|
||||||
|
assertThrows(IllegalArgumentException.class, () ->
|
||||||
|
api.deleteContact(properties, contactId));
|
||||||
|
}
|
||||||
|
|
||||||
private String getBaseUrl(MockWebServer server) {
|
private String getBaseUrl(MockWebServer server) {
|
||||||
String baseUrl = server.url("").toString();
|
String baseUrl = server.url("").toString();
|
||||||
return baseUrl.substring(0, baseUrl.length() - 1);
|
return baseUrl.substring(0, baseUrl.length() - 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user