@@ -30,33 +30,47 @@ import (
3030func TestNewCredentials_serviceAccount (t * testing.T ) {
3131 ctx := context .Background ()
3232 tests := []struct {
33- name string
34- targetPrincipal string
35- scopes []string
36- lifetime time.Duration
37- wantErr bool
33+ name string
34+ config CredentialsOptions
35+ wantErr error
3836 }{
3937 {
4038 name : "missing targetPrincipal" ,
41- wantErr : true ,
39+ wantErr : errMissingTargetPrincipal ,
4240 },
4341 {
44- name : "missing scopes" ,
45- targetPrincipal : "foo@project-id.iam.gserviceaccount.com" ,
46- wantErr : true ,
42+ name : "missing scopes" ,
43+ config : CredentialsOptions {
44+ TargetPrincipal : "foo@project-id.iam.gserviceaccount.com" ,
45+ },
46+ wantErr : errMissingScopes ,
4747 },
4848 {
49- name : "lifetime over max" ,
50- targetPrincipal : "foo@project-id.iam.gserviceaccount.com" ,
51- scopes : []string {"scope" },
52- lifetime : 13 * time .Hour ,
53- wantErr : true ,
49+ name : "lifetime over max" ,
50+ config : CredentialsOptions {
51+ TargetPrincipal : "foo@project-id.iam.gserviceaccount.com" ,
52+ Scopes : []string {"scope" },
53+ Lifetime : 13 * time .Hour ,
54+ },
55+ wantErr : errLifetimeOverMax ,
5456 },
5557 {
56- name : "works" ,
57- targetPrincipal : "foo@project-id.iam.gserviceaccount.com" ,
58- scopes : []string {"scope" },
59- wantErr : false ,
58+ name : "works" ,
59+ config : CredentialsOptions {
60+ TargetPrincipal : "foo@project-id.iam.gserviceaccount.com" ,
61+ Scopes : []string {"scope" },
62+ },
63+ wantErr : nil ,
64+ },
65+ {
66+ name : "universe domain" ,
67+ config : CredentialsOptions {
68+ TargetPrincipal : "foo@project-id.iam.gserviceaccount.com" ,
69+ Scopes : []string {"scope" },
70+ Subject : "admin@example.com" ,
71+ UniverseDomain : "example.com" ,
72+ },
73+ wantErr : errUniverseNotSupportedDomainWideDelegation ,
6074 },
6175 }
6276
@@ -76,11 +90,11 @@ func TestNewCredentials_serviceAccount(t *testing.T) {
7690 if err := json .Unmarshal (b , & r ); err != nil {
7791 t .Error (err )
7892 }
79- if ! cmp .Equal (r .Scope , tt .scopes ) {
80- t .Errorf ("got %v, want %v" , r .Scope , tt .scopes )
93+ if ! cmp .Equal (r .Scope , tt .config . Scopes ) {
94+ t .Errorf ("got %v, want %v" , r .Scope , tt .config . Scopes )
8195 }
82- if ! strings .Contains (req .URL .Path , tt .targetPrincipal ) {
83- t .Errorf ("got %q, want %q" , req .URL .Path , tt .targetPrincipal )
96+ if ! strings .Contains (req .URL .Path , tt .config . TargetPrincipal ) {
97+ t .Errorf ("got %q, want %q" , req .URL .Path , tt .config . TargetPrincipal )
8498 }
8599
86100 resp := generateAccessTokenResponse {
@@ -100,24 +114,20 @@ func TestNewCredentials_serviceAccount(t *testing.T) {
100114 return nil
101115 }),
102116 }
103- ts , err := NewCredentials (& CredentialsOptions {
104- TargetPrincipal : tt .targetPrincipal ,
105- Scopes : tt .scopes ,
106- Lifetime : tt .lifetime ,
107- Client : client ,
108- })
109- if tt .wantErr && err != nil {
110- return
111- }
112- if err != nil {
113- t .Fatal (err )
114- }
115- tok , err := ts .Token (ctx )
117+ tt .config .Client = client
118+ ts , err := NewCredentials (& tt .config )
116119 if err != nil {
117- t .Fatal (err )
118- }
119- if tok .Value != saTok {
120- t .Fatalf ("got %q, want %q" , tok .Value , saTok )
120+ if err != tt .wantErr {
121+ t .Fatalf ("err: %v" , err )
122+ }
123+ } else {
124+ tok , err := ts .Token (ctx )
125+ if err != nil {
126+ t .Fatal (err )
127+ }
128+ if tok .Value != saTok {
129+ t .Fatalf ("got %q, want %q" , tok .Value , saTok )
130+ }
121131 }
122132 })
123133 }
@@ -126,3 +136,45 @@ func TestNewCredentials_serviceAccount(t *testing.T) {
126136type RoundTripFn func (req * http.Request ) * http.Response
127137
128138func (f RoundTripFn ) RoundTrip (req * http.Request ) (* http.Response , error ) { return f (req ), nil }
139+
140+ func TestCredentialsOptions_UniverseDomain (t * testing.T ) {
141+ testCases := []struct {
142+ name string
143+ opts * CredentialsOptions
144+ wantUniverseDomain string
145+ wantIsGDU bool
146+ }{
147+ {
148+ name : "empty" ,
149+ opts : & CredentialsOptions {},
150+ wantUniverseDomain : "googleapis.com" ,
151+ wantIsGDU : true ,
152+ },
153+ {
154+ name : "defaults" ,
155+ opts : & CredentialsOptions {
156+ UniverseDomain : "googleapis.com" ,
157+ },
158+ wantUniverseDomain : "googleapis.com" ,
159+ wantIsGDU : true ,
160+ },
161+ {
162+ name : "non-GDU" ,
163+ opts : & CredentialsOptions {
164+ UniverseDomain : "example.com" ,
165+ },
166+ wantUniverseDomain : "example.com" ,
167+ wantIsGDU : false ,
168+ },
169+ }
170+ for _ , tc := range testCases {
171+ t .Run (tc .name , func (t * testing.T ) {
172+ if got := tc .opts .getUniverseDomain (); got != tc .wantUniverseDomain {
173+ t .Errorf ("got %v, want %v" , got , tc .wantUniverseDomain )
174+ }
175+ if got := tc .opts .isUniverseDomainGDU (); got != tc .wantIsGDU {
176+ t .Errorf ("got %v, want %v" , got , tc .wantIsGDU )
177+ }
178+ })
179+ }
180+ }
0 commit comments