Skip to content

Commit bc55edf

Browse files
committed
fix tests
1 parent 404d66a commit bc55edf

4 files changed

Lines changed: 31 additions & 126 deletions

File tree

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,14 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
173173
BigQueryJdbcUrlUtility.UNIVERSE_DOMAIN_OVERRIDE_PROPERTY_NAME, this.universeDomain);
174174
}
175175

176-
Boolean reqGoogleDriveScopeBool =
176+
this.reqGoogleDriveScope =
177177
BigQueryJdbcUrlUtility.convertIntToBoolean(
178178
String.valueOf(ds.getRequestGoogleDriveScope()),
179179
BigQueryJdbcUrlUtility.REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME);
180180

181181
this.credentials =
182182
BigQueryJdbcOAuthUtility.getCredentials(
183-
authProperties, overrideProperties, reqGoogleDriveScopeBool, this.connectionClassName);
183+
authProperties, overrideProperties, this.reqGoogleDriveScope, this.connectionClassName);
184184
String defaultDatasetString = ds.getDefaultDataset();
185185
if (defaultDatasetString == null || defaultDatasetString.trim().isEmpty()) {
186186
this.defaultDataset = null;

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtility.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ static GoogleCredentials getCredentials(
285285
case APPLICATION_DEFAULT_CREDENTIALS:
286286
// This auth method doesn't support service account impersonation
287287

288-
credentials = getApplicationDefaultCredentials(authProperties, callerClassName);
288+
credentials = getApplicationDefaultCredentials(callerClassName);
289289
break;
290290
case EXTERNAL_ACCOUNT_AUTH:
291291
// This auth method doesn't support service account impersonation
@@ -402,6 +402,9 @@ static UserAuthorizer getUserAuthorizer(
402402
throws URISyntaxException {
403403
LOG.finest("++enter++\t" + callerClassName);
404404

405+
List<String> scopes = new ArrayList<>();
406+
scopes.add("https://www.googleapis.com/auth/bigquery");
407+
405408
List<String> responseTypes = new ArrayList<>();
406409
responseTypes.add("code");
407410

@@ -412,6 +415,7 @@ static UserAuthorizer getUserAuthorizer(
412415
UserAuthorizer.Builder userAuthorizerBuilder =
413416
UserAuthorizer.newBuilder()
414417
.setClientId(clientId)
418+
.setScopes(scopes)
415419
.setCallbackUri(URI.create("http://localhost:" + port));
416420

417421
if (overrideProperties.containsKey(BigQueryJdbcUrlUtility.OAUTH2_TOKEN_URI_PROPERTY_NAME)) {

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryConnectionTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,28 @@ public void testBigQueryJobCreationMode_default() throws Exception {
413413
bq.getOptions().getDefaultJobCreationMode(), JobCreationMode.JOB_CREATION_OPTIONAL);
414414
}
415415
}
416+
417+
@Test
418+
public void testWithDriveScopeTrue() throws Exception {
419+
String url = BASE_URL + "RequestGoogleDriveScope=1;";
420+
try (BigQueryConnection connection = new BigQueryConnection(url)) {
421+
assertTrue(connection.reqGoogleDriveScope);
422+
}
423+
}
424+
425+
@Test
426+
public void testWithDriveScopeFalse() throws Exception {
427+
String url = BASE_URL + "RequestGoogleDriveScope=0;";
428+
try (BigQueryConnection connection = new BigQueryConnection(url)) {
429+
assertFalse(connection.reqGoogleDriveScope);
430+
}
431+
}
432+
433+
@Test
434+
public void testWithDriveScopeDefault() throws Exception {
435+
String url = BASE_URL;
436+
try (BigQueryConnection connection = new BigQueryConnection(url)) {
437+
assertFalse(connection.reqGoogleDriveScope);
438+
}
439+
}
416440
}

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtilityTest.java

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21-
import static org.junit.jupiter.api.Assertions.assertFalse;
2221
import static org.junit.jupiter.api.Assertions.assertNotNull;
2322
import static org.junit.jupiter.api.Assertions.assertNull;
2423
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -338,128 +337,6 @@ public void testParseBYOIDProps() {
338337
assertThat(result.get("BYOID_TokenUri")).isEqualTo("https://testuri.com/v1/token");
339338
}
340339

341-
@Test
342-
public void testParseOAuthProperties_UserAccount_RequestDriveScopeEnabled() {
343-
String url =
344-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
345-
+ "OAuthType=1;OAuthClientId=redactedClientId;OAuthClientSecret=redactedClientSecret;"
346-
+ "RequestGoogleDriveScope=1;";
347-
Map<String, String> properties =
348-
BigQueryJdbcOAuthUtility.parseOAuthProperties(
349-
DataSource.fromUrl(url), this.getClass().getName());
350-
assertEquals(
351-
String.valueOf(BigQueryJdbcOAuthUtility.AuthType.GOOGLE_USER_ACCOUNT),
352-
properties.get(BigQueryJdbcUrlUtility.OAUTH_TYPE_PROPERTY_NAME));
353-
assertEquals(
354-
"redactedClientId", properties.get(BigQueryJdbcUrlUtility.OAUTH_CLIENT_ID_PROPERTY_NAME));
355-
assertEquals(
356-
"redactedClientSecret",
357-
properties.get(BigQueryJdbcUrlUtility.OAUTH_CLIENT_SECRET_PROPERTY_NAME));
358-
assertEquals(
359-
"true", properties.get(BigQueryJdbcUrlUtility.REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME));
360-
}
361-
362-
@Test
363-
public void testParseOAuthProperties_UserAccount_RequestDriveScopeDisabled() {
364-
String url =
365-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
366-
+ "OAuthType=1;OAuthClientId=redactedClientId;OAuthClientSecret=redactedClientSecret;"
367-
+ "RequestGoogleDriveScope=0;";
368-
Map<String, String> properties =
369-
BigQueryJdbcOAuthUtility.parseOAuthProperties(
370-
DataSource.fromUrl(url), this.getClass().getName());
371-
assertEquals(
372-
"false", properties.get(BigQueryJdbcUrlUtility.REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME));
373-
}
374-
375-
@Test
376-
public void testParseOAuthProperties_UserAccount_RequestDriveScopeDefault() {
377-
String url =
378-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
379-
+ "OAuthType=1;OAuthClientId=redactedClientId;OAuthClientSecret=redactedClientSecret;";
380-
Map<String, String> properties =
381-
BigQueryJdbcOAuthUtility.parseOAuthProperties(
382-
DataSource.fromUrl(url), this.getClass().getName());
383-
assertEquals(
384-
"false", properties.get(BigQueryJdbcUrlUtility.REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME));
385-
}
386-
387-
@Test
388-
public void testGetUserAuthorizer_WithDriveScope() throws URISyntaxException {
389-
Map<String, String> authProperties = new HashMap<>();
390-
authProperties.put(BigQueryJdbcUrlUtility.OAUTH_CLIENT_ID_PROPERTY_NAME, "redactedClientId");
391-
authProperties.put(
392-
BigQueryJdbcUrlUtility.OAUTH_CLIENT_SECRET_PROPERTY_NAME, "redactedClientSecret");
393-
authProperties.put(BigQueryJdbcUrlUtility.REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME, "true");
394-
395-
UserAuthorizer authorizer =
396-
BigQueryJdbcOAuthUtility.getUserAuthorizer(
397-
authProperties, Collections.emptyMap(), 12345, this.getClass().getName());
398-
399-
assertTrue(authorizer.getScopes().contains("https://www.googleapis.com/auth/bigquery"));
400-
assertTrue(authorizer.getScopes().contains("https://www.googleapis.com/auth/drive.readonly"));
401-
assertEquals(2, authorizer.getScopes().size());
402-
}
403-
404-
@Test
405-
public void testGetUserAuthorizer_WithoutDriveScope() throws URISyntaxException {
406-
Map<String, String> authProperties = new HashMap<>();
407-
authProperties.put(BigQueryJdbcUrlUtility.OAUTH_CLIENT_ID_PROPERTY_NAME, "redactedClientId");
408-
authProperties.put(
409-
BigQueryJdbcUrlUtility.OAUTH_CLIENT_SECRET_PROPERTY_NAME, "redactedClientSecret");
410-
authProperties.put(BigQueryJdbcUrlUtility.REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME, "0");
411-
412-
UserAuthorizer authorizer =
413-
BigQueryJdbcOAuthUtility.getUserAuthorizer(
414-
authProperties, Collections.emptyMap(), 12345, this.getClass().getName());
415-
assertTrue(authorizer.getScopes().contains("https://www.googleapis.com/auth/bigquery"));
416-
assertFalse(authorizer.getScopes().contains("https://www.googleapis.com/auth/drive.readonly"));
417-
assertEquals(1, authorizer.getScopes().size());
418-
}
419-
420-
@Test
421-
public void testGetUserAuthorizer_InvalidDriveScopeValue() throws URISyntaxException {
422-
Map<String, String> authProperties = new HashMap<>();
423-
authProperties.put(BigQueryJdbcUrlUtility.OAUTH_CLIENT_ID_PROPERTY_NAME, "redactedClientId");
424-
authProperties.put(
425-
BigQueryJdbcUrlUtility.OAUTH_CLIENT_SECRET_PROPERTY_NAME, "redactedClientSecret");
426-
authProperties.put(
427-
BigQueryJdbcUrlUtility.REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME, "invalid_value");
428-
UserAuthorizer authorizer =
429-
BigQueryJdbcOAuthUtility.getUserAuthorizer(
430-
authProperties, Collections.emptyMap(), 12345, this.getClass().getName());
431-
assertFalse(authorizer.getScopes().contains("https://www.googleapis.com/auth/drive.readonly"));
432-
}
433-
434-
@Test
435-
public void testParseOAuthProperties_ServiceAccount_RequestDriveScopeEnabled() {
436-
String url =
437-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
438-
+ "OAuthType=0;OAuthServiceAcctEmail=dummy@email.com;OAuthPvtKey=key;"
439-
+ "RequestGoogleDriveScope=1;";
440-
Map<String, String> properties =
441-
BigQueryJdbcOAuthUtility.parseOAuthProperties(
442-
DataSource.fromUrl(url), this.getClass().getName());
443-
assertEquals(
444-
"true", properties.get(BigQueryJdbcUrlUtility.REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME));
445-
}
446-
447-
@Test
448-
public void testGetCredentialsForPreGeneratedToken_WithDriveScope() {
449-
Map<String, String> authProperties =
450-
BigQueryJdbcOAuthUtility.parseOAuthProperties(
451-
DataSource.fromUrl(
452-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
453-
+ "OAuthType=2;ProjectId=MyBigQueryProject;"
454-
+ "OAuthAccessToken=RedactedToken;"
455-
+ "RequestGoogleDriveScope=1;"),
456-
null);
457-
458-
GoogleCredentials credentials =
459-
BigQueryJdbcOAuthUtility.getCredentials(authProperties, Collections.EMPTY_MAP, true, null);
460-
assertThat(credentials).isNotNull();
461-
}
462-
463340
@Test
464341
public void testParseUserImpersonationDefault() {
465342
String connectionUri =

0 commit comments

Comments
 (0)