@@ -9,7 +9,20 @@ import (
99 "testing"
1010)
1111
12- var jwtJSONKeyUniverseDomain = []byte (`{
12+ var saJSONJWT = []byte (`{
13+ "type": "service_account",
14+ "project_id": "fake_project",
15+ "private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
16+ "private_key": "super secret key",
17+ "client_email": "[email protected] ", 18+ "client_id": "gopher.apps.googleusercontent.com",
19+ "auth_uri": "https://accounts.google.com/o/oauth2/auth",
20+ "token_uri": "https://oauth2.googleapis.com/token",
21+ "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
22+ "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gopher%40fake_project.iam.gserviceaccount.com"
23+ }` )
24+
25+ var saJSONJWTUniverseDomain = []byte (`{
1326 "type": "service_account",
1427 "project_id": "fake_project",
1528 "universe_domain": "example.com",
@@ -23,13 +36,49 @@ var jwtJSONKeyUniverseDomain = []byte(`{
2336 "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gopher%40fake_project.iam.gserviceaccount.com"
2437}` )
2538
26- func TestCredentialsFromJSONWithParams_UniverseDomain (t * testing.T ) {
39+ var userJSON = []byte (`{
40+ "client_id": "abc123.apps.googleusercontent.com",
41+ "client_secret": "shh",
42+ "refresh_token": "refreshing",
43+ "type": "authorized_user",
44+ "quota_project_id": "fake_project2"
45+ }` )
46+
47+ var userJSONUniverseDomain = []byte (`{
48+ "client_id": "abc123.apps.googleusercontent.com",
49+ "client_secret": "shh",
50+ "refresh_token": "refreshing",
51+ "type": "authorized_user",
52+ "quota_project_id": "fake_project2",
53+ "universe_domain": "example.com"
54+ }` )
55+
56+ func TestCredentialsFromJSONWithParams_SA (t * testing.T ) {
57+ ctx := context .Background ()
58+ scope := "https://www.googleapis.com/auth/cloud-platform"
59+ params := CredentialsParams {
60+ Scopes : []string {scope },
61+ }
62+ creds , err := CredentialsFromJSONWithParams (ctx , saJSONJWT , params )
63+ if err != nil {
64+ t .Fatal (err )
65+ }
66+
67+ if want := "fake_project" ; creds .ProjectID != want {
68+ t .Fatalf ("got %q, want %q" , creds .ProjectID , want )
69+ }
70+ if want := "googleapis.com" ; creds .UniverseDomain () != want {
71+ t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), want )
72+ }
73+ }
74+
75+ func TestCredentialsFromJSONWithParams_SA_UniverseDomain (t * testing.T ) {
2776 ctx := context .Background ()
2877 scope := "https://www.googleapis.com/auth/cloud-platform"
2978 params := CredentialsParams {
3079 Scopes : []string {scope },
3180 }
32- creds , err := CredentialsFromJSONWithParams (ctx , jwtJSONKeyUniverseDomain , params )
81+ creds , err := CredentialsFromJSONWithParams (ctx , saJSONJWTUniverseDomain , params )
3382 if err != nil {
3483 t .Fatal (err )
3584 }
@@ -41,3 +90,35 @@ func TestCredentialsFromJSONWithParams_UniverseDomain(t *testing.T) {
4190 t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), want )
4291 }
4392}
93+
94+ func TestCredentialsFromJSONWithParams_User (t * testing.T ) {
95+ ctx := context .Background ()
96+ scope := "https://www.googleapis.com/auth/cloud-platform"
97+ params := CredentialsParams {
98+ Scopes : []string {scope },
99+ }
100+ creds , err := CredentialsFromJSONWithParams (ctx , userJSON , params )
101+ if err != nil {
102+ t .Fatal (err )
103+ }
104+
105+ if want := "googleapis.com" ; creds .UniverseDomain () != want {
106+ t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), want )
107+ }
108+ }
109+
110+ func TestCredentialsFromJSONWithParams_User_UniverseDomain (t * testing.T ) {
111+ ctx := context .Background ()
112+ scope := "https://www.googleapis.com/auth/cloud-platform"
113+ params := CredentialsParams {
114+ Scopes : []string {scope },
115+ }
116+ creds , err := CredentialsFromJSONWithParams (ctx , userJSONUniverseDomain , params )
117+ if err != nil {
118+ t .Fatal (err )
119+ }
120+
121+ if want := "googleapis.com" ; creds .UniverseDomain () != want {
122+ t .Fatalf ("got %q, want %q" , creds .UniverseDomain (), want )
123+ }
124+ }
0 commit comments