2021-01-12 14:45:47 -05:00
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package externalaccount
|
|
|
|
|
|
|
|
import (
|
2021-01-13 15:38:24 -05:00
|
|
|
"context"
|
2021-01-12 14:45:47 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var testFileConfig = Config{
|
|
|
|
Audience: "32555940559.apps.googleusercontent.com",
|
|
|
|
SubjectTokenType: "urn:ietf:params:oauth:token-type:jwt",
|
|
|
|
TokenURL: "http://localhost:8080/v1/token",
|
|
|
|
TokenInfoURL: "http://localhost:8080/v1/tokeninfo",
|
|
|
|
ServiceAccountImpersonationURL: "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/service-gcs-admin@$PROJECT_ID.iam.gserviceaccount.com:generateAccessToken",
|
|
|
|
ClientSecret: "notsosecret",
|
|
|
|
ClientID: "rbrgnognrhongo3bi4gb9ghg9g",
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetrieveFileSubjectToken(t *testing.T) {
|
|
|
|
var fileSourceTests = []struct {
|
|
|
|
name string
|
|
|
|
cs CredentialSource
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "UntypedFileSource",
|
|
|
|
cs: CredentialSource{
|
2021-04-01 19:22:52 -04:00
|
|
|
File: textBaseCredPath,
|
2021-01-12 14:45:47 -05:00
|
|
|
},
|
|
|
|
want: "street123",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "TextFileSource",
|
|
|
|
cs: CredentialSource{
|
2021-04-01 19:22:52 -04:00
|
|
|
File: textBaseCredPath,
|
2021-01-12 14:45:47 -05:00
|
|
|
Format: format{Type: fileTypeText},
|
|
|
|
},
|
|
|
|
want: "street123",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "JSONFileSource",
|
|
|
|
cs: CredentialSource{
|
2021-04-01 19:22:52 -04:00
|
|
|
File: jsonBaseCredPath,
|
2021-01-12 14:45:47 -05:00
|
|
|
Format: format{Type: fileTypeJSON, SubjectTokenFieldName: "SubjToken"},
|
|
|
|
},
|
|
|
|
want: "321road",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range fileSourceTests {
|
|
|
|
test := test
|
|
|
|
tfc := testFileConfig
|
|
|
|
tfc.CredentialSource = test.cs
|
|
|
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2021-02-01 11:26:06 -05:00
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("parse() failed %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := base.subjectToken()
|
2021-01-12 14:45:47 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Method subjectToken() errored.")
|
|
|
|
} else if test.want != out {
|
|
|
|
t.Errorf("got %v but want %v", out, test.want)
|
|
|
|
}
|
|
|
|
|
2023-09-22 16:09:05 -04:00
|
|
|
if got, want := base.credentialSourceType(), "file"; got != want {
|
|
|
|
t.Errorf("got %v but want %v", got, want)
|
|
|
|
}
|
2021-01-12 14:45:47 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|