@@ -20,33 +20,48 @@ import (
2020func TestTokenSource_serviceAccount (t * testing.T ) {
2121 ctx := context .Background ()
2222 tests := []struct {
23- name string
24- targetPrincipal string
25- scopes []string
26- lifetime time.Duration
27- wantErr bool
23+ name string
24+ config CredentialsConfig
25+ opts option.ClientOption
26+ wantErr error
2827 }{
2928 {
3029 name : "missing targetPrincipal" ,
31- wantErr : true ,
30+ wantErr : errMissingTargetPrincipal ,
3231 },
3332 {
34- name : "missing scopes" ,
35- targetPrincipal :
"[email protected] " ,
36- wantErr : true ,
33+ name : "missing scopes" ,
34+ config : CredentialsConfig {
35+ TargetPrincipal :
"[email protected] " ,
36+ },
37+ wantErr : errMissingScopes ,
3738 },
3839 {
39- name : "lifetime over max" ,
40- targetPrincipal :
"[email protected] " ,
41- scopes : []string {"scope" },
42- lifetime : 13 * time .Hour ,
43- wantErr : true ,
40+ name : "lifetime over max" ,
41+ config : CredentialsConfig {
42+ TargetPrincipal :
"[email protected] " ,
43+ Scopes : []string {"scope" },
44+ Lifetime : 13 * time .Hour ,
45+ },
46+ wantErr : errLifetimeOverMax ,
4447 },
4548 {
46- name : "works" ,
47- targetPrincipal :
"[email protected] " ,
48- scopes : []string {"scope" },
49- wantErr : false ,
49+ name : "works" ,
50+ config : CredentialsConfig {
51+ TargetPrincipal :
"[email protected] " ,
52+ Scopes : []string {"scope" },
53+ },
54+ wantErr : nil ,
55+ },
56+ {
57+ name : "universe domain" ,
58+ config : CredentialsConfig {
59+ TargetPrincipal :
"[email protected] " ,
60+ Scopes : []string {"scope" },
61+ 62+ },
63+ opts : option .WithUniverseDomain ("example.com" ),
64+ wantErr : errUniverseNotSupportedDomainWideDelegation ,
5065 },
5166 }
5267
@@ -74,23 +89,26 @@ func TestTokenSource_serviceAccount(t *testing.T) {
7489 return nil
7590 }),
7691 }
77- ts , err := CredentialsTokenSource (ctx , CredentialsConfig {
78- TargetPrincipal : tt .targetPrincipal ,
79- Scopes : tt .scopes ,
80- Lifetime : tt .lifetime ,
81- }, option .WithHTTPClient (client ))
82- if tt .wantErr && err != nil {
83- return
92+ opts := []option.ClientOption {
93+ option .WithHTTPClient (client ),
8494 }
85- if err != nil {
86- t . Fatal ( err )
95+ if tt . opts != nil {
96+ opts = append ( opts , tt . opts )
8797 }
88- tok , err := ts .Token ()
98+ ts , err := CredentialsTokenSource (ctx , tt .config , opts ... )
99+
89100 if err != nil {
90- t .Fatal (err )
91- }
92- if tok .AccessToken != saTok {
93- t .Fatalf ("got %q, want %q" , tok .AccessToken , saTok )
101+ if err != tt .wantErr {
102+ t .Fatalf ("%s: err: %v" , tt .name , err )
103+ }
104+ } else {
105+ tok , err := ts .Token ()
106+ if err != nil {
107+ t .Fatal (err )
108+ }
109+ if tok .AccessToken != saTok {
110+ t .Fatalf ("got %q, want %q" , tok .AccessToken , saTok )
111+ }
94112 }
95113 })
96114 }
0 commit comments