|
20 | 20 |
|
21 | 21 | import okhttp3.Request; |
22 | 22 | import okhttp3.Response; |
| 23 | +import org.testng.SkipException; |
23 | 24 | import org.testng.annotations.AfterSuite; |
24 | 25 | import org.testng.annotations.BeforeSuite; |
25 | 26 |
|
|
29 | 30 | import com.dropbox.core.http.OkHttp3Requestor; |
30 | 31 | import com.dropbox.core.http.StandardHttpRequestor; |
31 | 32 | import com.dropbox.core.json.JsonReader; |
| 33 | +import com.dropbox.core.oauth.DbxCredential; |
32 | 34 | import com.dropbox.core.v1.DbxClientV1; |
33 | 35 | import com.dropbox.core.v2.DbxClientV2; |
| 36 | +import com.dropbox.core.v2.DbxTeamClientV2; |
34 | 37 | import com.dropbox.core.v2.files.DeleteErrorException; |
35 | 38 |
|
36 | 39 | // integration test utility class |
37 | 40 | public final class ITUtil { |
38 | 41 | private static final String AUTH_INFO_FILE_PROPERTY = "com.dropbox.test.authInfoFile"; |
39 | 42 | private static final String HTTP_REQUESTOR_PROPERTY = "com.dropbox.test.httpRequestor"; |
40 | 43 |
|
| 44 | + // Environment variables mirroring the credential model shared across the Dropbox SDKs |
| 45 | + // (see dropbox-sdk-python). Populated in CI from the "api-sdk-integration-test-creds" |
| 46 | + // AWS Secrets Manager secret. Scoped clients authenticate via refresh tokens. |
| 47 | + public static final String SCOPED_USER_CLIENT_ID = "SCOPED_USER_CLIENT_ID"; |
| 48 | + public static final String SCOPED_USER_CLIENT_SECRET = "SCOPED_USER_CLIENT_SECRET"; |
| 49 | + public static final String SCOPED_USER_REFRESH_TOKEN = "SCOPED_USER_REFRESH_TOKEN"; |
| 50 | + public static final String SCOPED_TEAM_CLIENT_ID = "SCOPED_TEAM_CLIENT_ID"; |
| 51 | + public static final String SCOPED_TEAM_CLIENT_SECRET = "SCOPED_TEAM_CLIENT_SECRET"; |
| 52 | + public static final String SCOPED_TEAM_REFRESH_TOKEN = "SCOPED_TEAM_REFRESH_TOKEN"; |
| 53 | + public static final String DROPBOX_SHARED_LINK = "DROPBOX_SHARED_LINK"; |
| 54 | + |
41 | 55 | private static final Random RAND = new Random(0L); |
42 | 56 | private static final long READ_TIMEOUT = TimeUnit.SECONDS.toMillis(20); |
43 | 57 | private static final int MAX_RETRIES = 3; |
@@ -196,6 +210,56 @@ public static DbxClientV2 newClientV2(DbxRequestConfig config) { |
196 | 210 | ); |
197 | 211 | } |
198 | 212 |
|
| 213 | + /** |
| 214 | + * Reads an environment variable, skipping the current test (rather than failing) if it is |
| 215 | + * not set. This lets the scoped-credential integration tests run only in environments that |
| 216 | + * supply the shared SDK credentials while remaining opt-in elsewhere. |
| 217 | + */ |
| 218 | + public static String valueFromEnvOrSkip(String name) { |
| 219 | + String value = System.getenv(name); |
| 220 | + if (value == null || value.isEmpty()) { |
| 221 | + throw new SkipException( |
| 222 | + "Environment variable \"" + name + "\" is not set; skipping test that requires " + |
| 223 | + "shared SDK integration credentials."); |
| 224 | + } |
| 225 | + return value; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Builds a {@link DbxCredential} from a refresh token plus app key/secret. The access token |
| 230 | + * is a placeholder with an {@code expiresAt} of {@code 0}, which forces the client to refresh |
| 231 | + * before the first request. |
| 232 | + */ |
| 233 | + static DbxCredential refreshCredential(String refreshToken, String appKey, String appSecret) { |
| 234 | + return new DbxCredential("placeholder-access-token", 0L, refreshToken, appKey, appSecret); |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * Creates a user {@link DbxClientV2} authenticated via the scoped user refresh token supplied |
| 239 | + * through environment variables. Skips the calling test if the credentials are not set. |
| 240 | + */ |
| 241 | + public static DbxClientV2 newScopedUserClientV2() { |
| 242 | + DbxCredential credential = refreshCredential( |
| 243 | + valueFromEnvOrSkip(SCOPED_USER_REFRESH_TOKEN), |
| 244 | + valueFromEnvOrSkip(SCOPED_USER_CLIENT_ID), |
| 245 | + valueFromEnvOrSkip(SCOPED_USER_CLIENT_SECRET) |
| 246 | + ); |
| 247 | + return new DbxClientV2(newRequestConfig().build(), credential); |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * Creates a {@link DbxTeamClientV2} authenticated via the scoped team refresh token supplied |
| 252 | + * through environment variables. Skips the calling test if the credentials are not set. |
| 253 | + */ |
| 254 | + public static DbxTeamClientV2 newScopedTeamClientV2() { |
| 255 | + DbxCredential credential = refreshCredential( |
| 256 | + valueFromEnvOrSkip(SCOPED_TEAM_REFRESH_TOKEN), |
| 257 | + valueFromEnvOrSkip(SCOPED_TEAM_CLIENT_ID), |
| 258 | + valueFromEnvOrSkip(SCOPED_TEAM_CLIENT_SECRET) |
| 259 | + ); |
| 260 | + return new DbxTeamClientV2(newRequestConfig().build(), credential); |
| 261 | + } |
| 262 | + |
199 | 263 | private static final class RootContainer { |
200 | 264 | public static String ROOT = "/test/dropbox-sdk-java/" + format(new Date()); |
201 | 265 | } |
|
0 commit comments