30
30
package com .google .api .gax .core ;
31
31
32
32
import static com .google .common .truth .Truth .assertThat ;
33
+ import static org .junit .Assert .assertTrue ;
33
34
34
35
import com .google .auth .Credentials ;
35
36
import com .google .auth .oauth2 .ServiceAccountCredentials ;
43
44
44
45
@ RunWith (JUnit4 .class )
45
46
public class GoogleCredentialsProviderTest {
47
+ ServiceAccountCredentials CreateServiceAccountCredentials () {
48
+ return ServiceAccountCredentials .newBuilder ()
49
+ .setClientId ("fake-client-id" )
50
+ .setClientEmail ("fake@example.com" )
51
+ .setPrivateKeyId ("fake-private-key" )
52
+ .setPrivateKey (Mockito .mock (PrivateKey .class ))
53
+ .build ();
54
+ }
55
+
46
56
@ Test
47
57
public void serviceAccountReplacedWithJwtTokens () throws Exception {
48
- ServiceAccountCredentials serviceAccountCredentials =
49
- ServiceAccountCredentials .newBuilder ()
50
- .setClientId ("fake-client-id" )
51
- .setClientEmail ("fake@example.com" )
52
- .setPrivateKeyId ("fake-private-key" )
53
- .setPrivateKey (Mockito .mock (PrivateKey .class ))
54
- .build ();
58
+ ServiceAccountCredentials serviceAccountCredentials = CreateServiceAccountCredentials ();
55
59
56
60
GoogleCredentialsProvider provider =
57
61
GoogleCredentialsProvider .newBuilder ()
@@ -71,13 +75,7 @@ public void serviceAccountReplacedWithJwtTokens() throws Exception {
71
75
72
76
@ Test
73
77
public void noJwtWithoutScopeMatch () throws Exception {
74
- ServiceAccountCredentials serviceAccountCredentials =
75
- ServiceAccountCredentials .newBuilder ()
76
- .setClientId ("fake-client-id" )
77
- .setClientEmail ("fake@example.com" )
78
- .setPrivateKeyId ("fake-private-key" )
79
- .setPrivateKey (Mockito .mock (PrivateKey .class ))
80
- .build ();
78
+ ServiceAccountCredentials serviceAccountCredentials = CreateServiceAccountCredentials ();
81
79
82
80
GoogleCredentialsProvider provider =
83
81
GoogleCredentialsProvider .newBuilder ()
@@ -100,4 +98,30 @@ public void noJwtWithoutScopeMatch() throws Exception {
100
98
.isEqualTo (serviceAccountCredentials .getPrivateKey ());
101
99
assertThat (serviceAccountCredentials2 .getScopes ()).containsExactly ("scope1" , "scope2" );
102
100
}
101
+
102
+ @ Test
103
+ public void useJwtAccessWithScope () throws Exception {
104
+ ServiceAccountCredentials serviceAccountCredentials = CreateServiceAccountCredentials ();
105
+
106
+ GoogleCredentialsProvider provider =
107
+ GoogleCredentialsProvider .newBuilder ()
108
+ .setScopesToApply (ImmutableList .of ("scope1" , "scope2" ))
109
+ .setOAuth2Credentials (serviceAccountCredentials )
110
+ .setUseJwtAccessWithScope (true )
111
+ .build ();
112
+
113
+ Credentials credentials = provider .getCredentials ();
114
+ assertThat (credentials ).isInstanceOf (ServiceAccountCredentials .class );
115
+
116
+ ServiceAccountCredentials serviceAccountCredentials2 = (ServiceAccountCredentials ) credentials ;
117
+ assertThat (serviceAccountCredentials2 .getClientId ())
118
+ .isEqualTo (serviceAccountCredentials .getClientId ());
119
+ assertThat (serviceAccountCredentials2 .getClientEmail ())
120
+ .isEqualTo (serviceAccountCredentials .getClientEmail ());
121
+ assertThat (serviceAccountCredentials2 .getPrivateKeyId ())
122
+ .isEqualTo (serviceAccountCredentials .getPrivateKeyId ());
123
+ assertThat (serviceAccountCredentials2 .getPrivateKey ())
124
+ .isEqualTo (serviceAccountCredentials .getPrivateKey ());
125
+ assertTrue (serviceAccountCredentials2 .getUseJwtAccessWithScope ());
126
+ }
103
127
}
0 commit comments