oauth2/google/internal/externalaccount/filecredsource_test.go
Patrick Jones 01de73cf58 google: base account credentials with file-sourcing
Implements the core functionality to allow 3rd party identities access to Google APIs.  Specifically, this PR implements the base account credential type and supports file-sourced credentials such as Kubernetes workloads.  Later updates will add support for URL-sourced credentials such as Microsoft Azure and support for AWS credentials.

Change-Id: I6e09a450f5221a1e06394b51374cff70ab3ab8a7
GitHub-Last-Rev: 3ab51622f8f7c6982a5e78ae9644675659318e7b
GitHub-Pull-Request: golang/oauth2#462
Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/276312
Reviewed-by: Tyler Bui-Palsulich <tbp@google.com>
Trust: Tyler Bui-Palsulich <tbp@google.com>
Trust: Cody Oss <codyoss@google.com>
Run-TryBot: Tyler Bui-Palsulich <tbp@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-01-12 20:04:29 +00:00

68 lines
1.8 KiB
Go

// 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 (
"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{
File: "./testdata/3pi_cred.txt",
},
want: "street123",
},
{
name: "TextFileSource",
cs: CredentialSource{
File: "./testdata/3pi_cred.txt",
Format: format{Type: fileTypeText},
},
want: "street123",
},
{
name: "JSONFileSource",
cs: CredentialSource{
File: "./testdata/3pi_cred.json",
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) {
out, err := tfc.parse().subjectToken()
if err != nil {
t.Errorf("Method subjectToken() errored.")
} else if test.want != out {
t.Errorf("got %v but want %v", out, test.want)
}
})
}
}