Skip to content

Commit 53cf763

Browse files
committed
Adds context tests
1 parent 74e6f6e commit 53cf763

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

google/externalaccount/aws_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ func TestAWSCredential_ProgrammaticAuthRegionError(t *testing.T) {
13711371
}
13721372
}
13731373

1374-
func TestAWSCredential_ProgrammaticAuthContext(t *testing.T) {
1374+
func TestAWSCredential_ProgrammaticAuthOptions(t *testing.T) {
13751375
tfc := testFileConfig
13761376
securityCredentials := AwsSecurityCredentials{
13771377
AccessKeyID: accessKeyID,
@@ -1396,6 +1396,31 @@ func TestAWSCredential_ProgrammaticAuthContext(t *testing.T) {
13961396
}
13971397
}
13981398

1399+
func TestAWSCredential_ProgrammaticAuthContext(t *testing.T) {
1400+
tfc := testFileConfig
1401+
securityCredentials := AwsSecurityCredentials{
1402+
AccessKeyID: accessKeyID,
1403+
SecretAccessKey: secretAccessKey,
1404+
}
1405+
ctx := context.Background()
1406+
1407+
tfc.AwsSecurityCredentialsSupplier = testAwsSupplier{
1408+
awsRegion: "us-east-2",
1409+
credentials: &securityCredentials,
1410+
expectedContext: ctx,
1411+
}
1412+
1413+
base, err := tfc.parse(ctx)
1414+
if err != nil {
1415+
t.Fatalf("parse() failed %v", err)
1416+
}
1417+
1418+
_, err = base.subjectToken()
1419+
if err != nil {
1420+
t.Fatalf("subjectToken() failed %v", err)
1421+
}
1422+
}
1423+
13991424
func TestAwsCredential_CredentialSourceType(t *testing.T) {
14001425
server := createDefaultAwsTestServer()
14011426
ts := httptest.NewServer(server)
@@ -1419,6 +1444,7 @@ type testAwsSupplier struct {
14191444
awsRegion string
14201445
credentials *AwsSecurityCredentials
14211446
expectedOptions *SupplierOptions
1447+
expectedContext context.Context
14221448
}
14231449

14241450
func (supp testAwsSupplier) AwsRegion(ctx context.Context, options SupplierOptions) (string, error) {
@@ -1433,6 +1459,11 @@ func (supp testAwsSupplier) AwsRegion(ctx context.Context, options SupplierOptio
14331459
return "", errors.New("Audience does not match")
14341460
}
14351461
}
1462+
if supp.expectedContext != nil {
1463+
if supp.expectedContext != ctx {
1464+
return "", errors.New("Context does not match")
1465+
}
1466+
}
14361467
return supp.awsRegion, nil
14371468
}
14381469

@@ -1448,5 +1479,10 @@ func (supp testAwsSupplier) AwsSecurityCredentials(ctx context.Context, options
14481479
return nil, errors.New("Audience does not match")
14491480
}
14501481
}
1482+
if supp.expectedContext != nil {
1483+
if supp.expectedContext != ctx {
1484+
return nil, errors.New("Context does not match")
1485+
}
1486+
}
14511487
return supp.credentials, nil
14521488
}

google/externalaccount/programmaticrefreshcredsource_test.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ func TestRetrieveSubjectToken_ProgrammaticAuthFails(t *testing.T) {
5454
}
5555
}
5656

57-
func TestRetrieveSubjectToken_ProgrammaticAuthContext(t *testing.T) {
57+
func TestRetrieveSubjectToken_ProgrammaticAuthOptions(t *testing.T) {
5858
tfc := testConfig
5959
expectedOptions := SupplierOptions{Audience: tfc.Audience, SubjectTokenType: tfc.SubjectTokenType}
6060

6161
tfc.SubjectTokenSupplier = testSubjectTokenSupplier{
6262
subjectToken: "subjectToken",
63-
expectedContext: &expectedOptions,
63+
expectedOptions: &expectedOptions,
6464
}
6565

6666
base, err := tfc.parse(context.Background())
@@ -74,23 +74,49 @@ func TestRetrieveSubjectToken_ProgrammaticAuthContext(t *testing.T) {
7474
}
7575
}
7676

77+
func TestRetrieveSubjectToken_ProgrammaticAuthContext(t *testing.T) {
78+
tfc := testConfig
79+
ctx := context.Background()
80+
81+
tfc.SubjectTokenSupplier = testSubjectTokenSupplier{
82+
subjectToken: "subjectToken",
83+
expectedContext: ctx,
84+
}
85+
86+
base, err := tfc.parse(ctx)
87+
if err != nil {
88+
t.Fatalf("parse() failed %v", err)
89+
}
90+
91+
_, err = base.subjectToken()
92+
if err != nil {
93+
t.Fatalf("retrieveSubjectToken() failed: %v", err)
94+
}
95+
}
96+
7797
type testSubjectTokenSupplier struct {
7898
err error
7999
subjectToken string
80-
expectedContext *SupplierOptions
100+
expectedOptions *SupplierOptions
101+
expectedContext context.Context
81102
}
82103

83104
func (supp testSubjectTokenSupplier) SubjectToken(ctx context.Context, options SupplierOptions) (string, error) {
84105
if supp.err != nil {
85106
return "", supp.err
86107
}
87-
if supp.expectedContext != nil {
88-
if supp.expectedContext.Audience != options.Audience {
108+
if supp.expectedOptions != nil {
109+
if supp.expectedOptions.Audience != options.Audience {
89110
return "", errors.New("Audience does not match")
90111
}
91-
if supp.expectedContext.SubjectTokenType != options.SubjectTokenType {
112+
if supp.expectedOptions.SubjectTokenType != options.SubjectTokenType {
92113
return "", errors.New("Audience does not match")
93114
}
94115
}
116+
if supp.expectedContext != nil {
117+
if supp.expectedContext != ctx {
118+
return "", errors.New("Context does not match")
119+
}
120+
}
95121
return supp.subjectToken, nil
96122
}

0 commit comments

Comments
 (0)