|
| 1 | +package com.nylas; |
| 2 | + |
| 3 | +import okhttp3.HttpUrl; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.lang.reflect.Field; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import static com.nylas.NylasClientTest.TEST_CLIENT_ID; |
| 13 | +import static com.nylas.NylasClientTest.TEST_CLIENT_SECRET; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 16 | +import static org.mockito.ArgumentMatchers.any; |
| 17 | +import static org.mockito.ArgumentMatchers.anyString; |
| 18 | +import static org.mockito.Mockito.mock; |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | + |
| 21 | +public class NylasApplicationTest { |
| 22 | + private NylasClient nylasClient; |
| 23 | + |
| 24 | + private NylasApplication nylasApplication; |
| 25 | + |
| 26 | + @BeforeEach |
| 27 | + private void init() { |
| 28 | + nylasClient = mock(NylasClient.class); |
| 29 | + nylasApplication = new NylasApplication(nylasClient, TEST_CLIENT_ID, TEST_CLIENT_SECRET); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testGetClient() { |
| 34 | + NylasClient client = nylasApplication.getClient(); |
| 35 | + |
| 36 | + assertNotNull(client); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testGetClientID() { |
| 41 | + String clientId = nylasApplication.getClientId(); |
| 42 | + |
| 43 | + assertEquals(clientId, TEST_CLIENT_ID); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testGetClientSecret() { |
| 48 | + String clientSecret = nylasApplication.getClientSecret(); |
| 49 | + |
| 50 | + assertEquals(clientSecret, TEST_CLIENT_SECRET); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testHostedAuthentication() { |
| 55 | + HostedAuthentication hostedAuthentication = nylasApplication.hostedAuthentication(); |
| 56 | + |
| 57 | + assertNotNull(hostedAuthentication); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testNativeAuthentication() { |
| 62 | + NativeAuthentication nativeAuthentication = nylasApplication.nativeAuthentication(); |
| 63 | + |
| 64 | + assertNotNull(nativeAuthentication); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testGetAccounts() { |
| 69 | + Accounts accounts = nylasApplication.accounts(); |
| 70 | + |
| 71 | + assertNotNull(accounts); |
| 72 | + assertEquals(accounts.authMethod, NylasClient.AuthMethod.BASIC); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testGetWebhooks (){ |
| 77 | + Webhooks webhooks = nylasApplication.webhooks(); |
| 78 | + |
| 79 | + assertNotNull(webhooks); |
| 80 | + assertEquals(webhooks.authMethod, NylasClient.AuthMethod.BASIC); |
| 81 | + assertEquals(webhooks.collectionPath, "a/kmsdv7809834n98fdvc/webhooks"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testGetComponents() { |
| 86 | + Components components = nylasApplication.components(); |
| 87 | + |
| 88 | + assertNotNull(components); |
| 89 | + assertEquals(components.authMethod, NylasClient.AuthMethod.BASIC); |
| 90 | + assertEquals(components.collectionPath, "component/kmsdv7809834n98fdvc"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testGetAuthentication() { |
| 95 | + Authentication authentication = nylasApplication.authentication(); |
| 96 | + |
| 97 | + assertNotNull(authentication); |
| 98 | + assertEquals(authentication.appName, "beta"); |
| 99 | + assertEquals(authentication.region, "us"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testGetApplicationDetail() throws NoSuchFieldException, IllegalAccessException, RequestFailedException, IOException { |
| 104 | + ApplicationDetail expected = new ApplicationDetail(); |
| 105 | + List<String> redirectUris = new ArrayList<>(); |
| 106 | + redirectUris.add("https://app.nylas.com/auth"); |
| 107 | + |
| 108 | + setField("application_name", "nylas-app", expected); |
| 109 | + setField("icon_url", "https://placehold.it/3x3", expected); |
| 110 | + setField("redirect_uris", redirectUris, expected); |
| 111 | + |
| 112 | + when(nylasClient.executeGet(anyString(), any(), any())).thenReturn(expected); |
| 113 | + when(nylasClient.newUrlBuilder()).thenReturn(new HttpUrl.Builder()); |
| 114 | + |
| 115 | + ApplicationDetail actual = nylasApplication.getApplicationDetail(); |
| 116 | + |
| 117 | + assertNotNull(actual); |
| 118 | + assertEquals(actual.getIconUrl(), expected.getIconUrl()); |
| 119 | + assertEquals(actual.getName(), expected.getName()); |
| 120 | + assertEquals(actual.getRedirectUris(), expected.getRedirectUris()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testSetApplicationName() throws NoSuchFieldException, IllegalAccessException, RequestFailedException, IOException { |
| 125 | + ApplicationDetail expected = new ApplicationDetail(); |
| 126 | + List<String> redirectUris = new ArrayList<>(); |
| 127 | + redirectUris.add("https://app.nylas.com/auth"); |
| 128 | + |
| 129 | + setField("application_name", "mew-nylas-app", expected); |
| 130 | + setField("icon_url", "https://placehold.it/3x3", expected); |
| 131 | + setField("redirect_uris", redirectUris, expected); |
| 132 | + |
| 133 | + when(nylasClient.executePut(anyString(), any(), any(), any())).thenReturn(expected); |
| 134 | + when(nylasClient.newUrlBuilder()).thenReturn(new HttpUrl.Builder()); |
| 135 | + |
| 136 | + ApplicationDetail actual = nylasApplication.setName("new-nylas-app"); |
| 137 | + |
| 138 | + assertEquals(actual.getName(), expected.getName()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testSetRedirectUris() throws NoSuchFieldException, IllegalAccessException, RequestFailedException, IOException { |
| 143 | + ApplicationDetail expected = new ApplicationDetail(); |
| 144 | + List<String> redirectUris = new ArrayList<>(); |
| 145 | + redirectUris.add("https://app.nylas.com/auth"); |
| 146 | + |
| 147 | + setField("application_name", "mew-nylas-app", expected); |
| 148 | + setField("icon_url", "https://placehold.it/3x3", expected); |
| 149 | + setField("redirect_uris", redirectUris, expected); |
| 150 | + |
| 151 | + when(nylasClient.executePut(anyString(), any(), any(), any())).thenReturn(expected); |
| 152 | + when(nylasClient.newUrlBuilder()).thenReturn(new HttpUrl.Builder()); |
| 153 | + |
| 154 | + ApplicationDetail actual = nylasApplication.setRedirectUris(redirectUris); |
| 155 | + |
| 156 | + assertEquals(actual.getRedirectUris(), expected.getRedirectUris()); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void testAddRedirectUri() throws NoSuchFieldException, IllegalAccessException, RequestFailedException, IOException { |
| 161 | + ApplicationDetail existing = new ApplicationDetail(); |
| 162 | + List<String> redirectUris = new ArrayList<>(); |
| 163 | + redirectUris.add("https://app.nylas.com/auth"); |
| 164 | + |
| 165 | + setField("application_name", "mew-nylas-app", existing); |
| 166 | + setField("icon_url", "https://placehold.it/3x3", existing); |
| 167 | + setField("redirect_uris", redirectUris, existing); |
| 168 | + |
| 169 | + |
| 170 | + ApplicationDetail updated = new ApplicationDetail(); |
| 171 | + List<String> updatedRedirectUris = new ArrayList<>(); |
| 172 | + updatedRedirectUris.add("https://app.nylas.com/auth"); |
| 173 | + updatedRedirectUris.add("https://app2.nylas.com/auth"); |
| 174 | + |
| 175 | + setField("application_name", "mew-nylas-app", updated); |
| 176 | + setField("icon_url", "https://placehold.it/3x3", updated); |
| 177 | + setField("redirect_uris", updatedRedirectUris, updated); |
| 178 | + |
| 179 | + // gets exsisting |
| 180 | + when(nylasClient.executeGet(anyString(), any(), any())).thenReturn(existing); |
| 181 | + when(nylasClient.newUrlBuilder()).thenReturn(new HttpUrl.Builder()); |
| 182 | + |
| 183 | + when(nylasClient.executePut(anyString(), any(), any(), any())).thenReturn(updated); |
| 184 | + when(nylasClient.newUrlBuilder()).thenReturn(new HttpUrl.Builder()); |
| 185 | + |
| 186 | + ApplicationDetail actual = nylasApplication.addRedirectUri("https://app2.nylas.com/auth"); |
| 187 | + |
| 188 | + assertEquals(actual.getRedirectUris().size(), 2); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + public void testAddRedirectUri_idempotency() throws NoSuchFieldException, IllegalAccessException, RequestFailedException, IOException { |
| 193 | + ApplicationDetail existing = new ApplicationDetail(); |
| 194 | + List<String> redirectUris = new ArrayList<>(); |
| 195 | + redirectUris.add("https://app.nylas.com/auth"); |
| 196 | + |
| 197 | + setField("application_name", "mew-nylas-app", existing); |
| 198 | + setField("icon_url", "https://placehold.it/3x3", existing); |
| 199 | + setField("redirect_uris", redirectUris, existing); |
| 200 | + |
| 201 | + |
| 202 | + // gets exsisting |
| 203 | + when(nylasClient.executeGet(anyString(), any(), any())).thenReturn(existing); |
| 204 | + when(nylasClient.newUrlBuilder()).thenReturn(new HttpUrl.Builder()); |
| 205 | + |
| 206 | + // we add the same url, should return the same app detail |
| 207 | + ApplicationDetail actual = nylasApplication.addRedirectUri("https://app.nylas.com/auth"); |
| 208 | + |
| 209 | + assertEquals(actual.getRedirectUris().size(), 1); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void testRemoveRedirecrUris() throws NoSuchFieldException, IllegalAccessException, RequestFailedException, IOException { |
| 214 | + ApplicationDetail existing = new ApplicationDetail(); |
| 215 | + List<String> redirectUris = new ArrayList<>(); |
| 216 | + redirectUris.add("https://app.nylas.com/auth"); |
| 217 | + redirectUris.add("https://app2.nylas.com/auth"); |
| 218 | + |
| 219 | + setField("application_name", "mew-nylas-app", existing); |
| 220 | + setField("icon_url", "https://placehold.it/3x3", existing); |
| 221 | + setField("redirect_uris", redirectUris, existing); |
| 222 | + |
| 223 | + |
| 224 | + ApplicationDetail updated = new ApplicationDetail(); |
| 225 | + List<String> updatedRedirectUris = new ArrayList<>(); |
| 226 | + updatedRedirectUris.add("https://app.nylas.com/auth"); |
| 227 | + |
| 228 | + setField("application_name", "mew-nylas-app", updated); |
| 229 | + setField("icon_url", "https://placehold.it/3x3", updated); |
| 230 | + setField("redirect_uris", updatedRedirectUris, updated); |
| 231 | + |
| 232 | + // gets exsisting |
| 233 | + when(nylasClient.executeGet(anyString(), any(), any())).thenReturn(existing); |
| 234 | + when(nylasClient.newUrlBuilder()).thenReturn(new HttpUrl.Builder()); |
| 235 | + |
| 236 | + when(nylasClient.executePut(anyString(), any(), any(), any())).thenReturn(updated); |
| 237 | + when(nylasClient.newUrlBuilder()).thenReturn(new HttpUrl.Builder()); |
| 238 | + |
| 239 | + ApplicationDetail actual = nylasApplication.removeRedirectUri("https://app2.nylas.com/auth"); |
| 240 | + |
| 241 | + assertEquals(actual.getRedirectUris().size(), 1); |
| 242 | + } |
| 243 | + |
| 244 | + @Test |
| 245 | + public void testRemoveRedirectUrls_removeInexistent() throws NoSuchFieldException, IllegalAccessException, RequestFailedException, IOException { |
| 246 | + ApplicationDetail existing = new ApplicationDetail(); |
| 247 | + List<String> redirectUris = new ArrayList<>(); |
| 248 | + redirectUris.add("https://app.nylas.com/auth"); |
| 249 | + |
| 250 | + setField("application_name", "mew-nylas-app", existing); |
| 251 | + setField("icon_url", "https://placehold.it/3x3", existing); |
| 252 | + setField("redirect_uris", redirectUris, existing); |
| 253 | + |
| 254 | + |
| 255 | + // gets exsisting urls |
| 256 | + when(nylasClient.executeGet(anyString(), any(), any())).thenReturn(existing); |
| 257 | + when(nylasClient.newUrlBuilder()).thenReturn(new HttpUrl.Builder()); |
| 258 | + |
| 259 | + // tries to remove an url that doesn't exist, returns the same app detail |
| 260 | + ApplicationDetail actual = nylasApplication.removeRedirectUri("https://google.com"); |
| 261 | + |
| 262 | + assertEquals(actual.getRedirectUris().size(), 1); |
| 263 | + } |
| 264 | + |
| 265 | + @Test |
| 266 | + public void testFetchIpAddressWhiteList() throws NoSuchFieldException, IllegalAccessException, RequestFailedException, IOException { |
| 267 | + IPAddressWhitelist expected = new IPAddressWhitelist(); |
| 268 | + List<String> ipAddresses = new ArrayList<>(); |
| 269 | + ipAddresses.add("192.169.11.2"); |
| 270 | + |
| 271 | + setField("ip_addresses", ipAddresses, expected); |
| 272 | + |
| 273 | + when(nylasClient.executeGet(anyString(), any(), any())).thenReturn(expected); |
| 274 | + when(nylasClient.newUrlBuilder()).thenReturn(new HttpUrl.Builder()); |
| 275 | + |
| 276 | + IPAddressWhitelist actual = nylasApplication.fetchIpAddressWhitelist(); |
| 277 | + |
| 278 | + assertEquals(expected.getIpAddresses(), actual.getIpAddresses()); |
| 279 | + } |
| 280 | + |
| 281 | + private void setField(String fieldName, Object fieldValue, Object o) throws NoSuchFieldException, IllegalAccessException { |
| 282 | + Field codeField = o.getClass().getDeclaredField(fieldName); |
| 283 | + codeField.setAccessible(true); |
| 284 | + codeField.set(o, fieldValue); |
| 285 | + } |
| 286 | +} |
0 commit comments