Skip to content

Commit 57388a2

Browse files
authored
revokeAccessToken returns a boolean now (#97)
This PR changes revokeAccessToken from being a void function to returning a boolean value, allowing the user to get more information on the call they make.
1 parent caf4d9b commit 57388a2

3 files changed

Lines changed: 41 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This section contains changes that have been committed but not yet released.
88

99
### Changed
1010

11+
* NylasAccount.revokeAccessToken() returns a boolean value now
12+
1113
### Deprecated
1214

1315
### Fixed

src/main/java/com/nylas/NylasAccount.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.nylas;
22

33
import java.io.IOException;
4+
import java.util.Map;
45

56
import okhttp3.HttpUrl;
67

@@ -91,10 +92,10 @@ public AccountDetail fetchAccountByAccessToken() throws IOException, RequestFail
9192
return client.executeGet(accessToken, accountUrl, AccountDetail.class);
9293
}
9394

94-
// TODO: Revoking an access token seems like an important op. We should return the result of the revoke operation.
95-
public void revokeAccessToken() throws IOException, RequestFailedException {
95+
public boolean revokeAccessToken() throws IOException, RequestFailedException {
9696
HttpUrl.Builder revokeUrl = client.newUrlBuilder().addPathSegments("oauth/revoke");
97-
client.executePost(accessToken, revokeUrl, null, null);
97+
Map<String, Boolean> response = client.executePost(accessToken, revokeUrl, null, Map.class);
98+
return response.get("success") != null && response.get("success");
9899
}
99100

100101
}

src/test/java/com/nylas/NylasAccountTest.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import org.junit.jupiter.api.Test;
66

77
import java.io.IOException;
8+
import java.util.Collections;
9+
import java.util.Map;
810

911
import static com.nylas.AccessTokenTest.TEST_ACCESS_TOKEN;
10-
import static org.junit.jupiter.api.Assertions.assertEquals;
11-
import static org.junit.jupiter.api.Assertions.assertNotNull;
12+
import static org.junit.jupiter.api.Assertions.*;
1213
import static org.mockito.ArgumentMatchers.any;
1314
import static org.mockito.Mockito.*;
1415

@@ -61,11 +62,42 @@ public void testFetchAccountByAccessToken() throws RequestFailedException, IOExc
6162
@Test
6263
public void testRevokeAccessToken() throws RequestFailedException, IOException {
6364
final NylasAccount nylasAccount = new NylasAccount(nylasClient, TEST_ACCESS_TOKEN);
65+
final Map<String, Boolean> revokeResponse = Collections.singletonMap("success", true);
6466

6567
when(nylasClient.newUrlBuilder()).thenReturn(new HttpUrl.Builder());
68+
when(nylasClient.executePost(anyString(), any(), any(), any())).thenReturn(revokeResponse);
6669

67-
nylasAccount.revokeAccessToken();
70+
boolean revokeAccessToken = nylasAccount.revokeAccessToken();
6871

6972
verify(nylasClient).executePost(anyString(), any(), any(), any());
73+
assertTrue(revokeAccessToken);
74+
}
75+
76+
@Test
77+
public void testRevokeAccessTokenSuccessFalse() throws RequestFailedException, IOException {
78+
final NylasAccount nylasAccount = new NylasAccount(nylasClient, TEST_ACCESS_TOKEN);
79+
final Map<String, Boolean> revokeResponse = Collections.singletonMap("success", false);
80+
81+
when(nylasClient.newUrlBuilder()).thenReturn(new HttpUrl.Builder());
82+
when(nylasClient.executePost(anyString(), any(), any(), any())).thenReturn(revokeResponse);
83+
84+
boolean revokeAccessToken = nylasAccount.revokeAccessToken();
85+
86+
verify(nylasClient).executePost(anyString(), any(), any(), any());
87+
assertFalse(revokeAccessToken);
88+
}
89+
90+
@Test
91+
public void testRevokeAccessTokenInvalidObjectReturnsFalse() throws RequestFailedException, IOException {
92+
final NylasAccount nylasAccount = new NylasAccount(nylasClient, TEST_ACCESS_TOKEN);
93+
final Map<String, Object> revokeResponse = Collections.singletonMap("invalidKey", "invalidValue");
94+
95+
when(nylasClient.newUrlBuilder()).thenReturn(new HttpUrl.Builder());
96+
when(nylasClient.executePost(anyString(), any(), any(), any())).thenReturn(revokeResponse);
97+
98+
boolean revokeAccessToken = nylasAccount.revokeAccessToken();
99+
100+
verify(nylasClient).executePost(anyString(), any(), any(), any());
101+
assertFalse(revokeAccessToken);
70102
}
71103
}

0 commit comments

Comments
 (0)