File: filecredsource_test.go

package info (click to toggle)
golang-golang-x-oauth2 0.15.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 748 kB
  • sloc: makefile: 15
file content (76 lines) | stat: -rw-r--r-- 2,028 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 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 (
	"context"
	"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: textBaseCredPath,
			},
			want: "street123",
		},
		{
			name: "TextFileSource",
			cs: CredentialSource{
				File:   textBaseCredPath,
				Format: format{Type: fileTypeText},
			},
			want: "street123",
		},
		{
			name: "JSONFileSource",
			cs: CredentialSource{
				File:   jsonBaseCredPath,
				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) {
			base, err := tfc.parse(context.Background())
			if err != nil {
				t.Fatalf("parse() failed %v", err)
			}

			out, err := base.subjectToken()
			if err != nil {
				t.Errorf("Method subjectToken() errored.")
			} else if test.want != out {
				t.Errorf("got %v but want %v", out, test.want)
			}

			if got, want := base.credentialSourceType(), "file"; got != want {
				t.Errorf("got %v but want %v", got, want)
			}
		})
	}
}