|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package core |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "golang.org/x/oauth2" |
| 25 | + "google.golang.org/api/option" |
| 26 | +) |
| 27 | + |
| 28 | +// mockAuthTokenSource is a mock implementation of the oauth2.TokenSource interface. |
| 29 | +// It allows us to control the token and error returned during tests. |
| 30 | +type mockAuthTokenSource struct { |
| 31 | + tokenToReturn *oauth2.Token |
| 32 | + errorToReturn error |
| 33 | +} |
| 34 | + |
| 35 | +// Token is the method that satisfies the TokenSource interface. |
| 36 | +func (m *mockAuthTokenSource) Token() (*oauth2.Token, error) { |
| 37 | + return m.tokenToReturn, m.errorToReturn |
| 38 | +} |
| 39 | + |
| 40 | +// setup is a helper to reset the cache and the newTokenSource variable for each test. |
| 41 | +func setup(t *testing.T) { |
| 42 | + // Reset the global cache for a clean state. |
| 43 | + cacheMutex.Lock() |
| 44 | + tokenSourceCache = make(map[string]oauth2.TokenSource) |
| 45 | + cacheMutex.Unlock() |
| 46 | + |
| 47 | + // After the test, restore the original function. |
| 48 | + originalNewTokenSource := newTokenSource |
| 49 | + t.Cleanup(func() { |
| 50 | + newTokenSource = originalNewTokenSource |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +func TestGetGoogleIDToken_Success(t *testing.T) { |
| 55 | + setup(t) |
| 56 | + const mockToken = "mock-id-token-123" |
| 57 | + const audience = "https://test-service.com" |
| 58 | + |
| 59 | + // Replace the package-level variable with the mock function. |
| 60 | + newTokenSource = func(ctx context.Context, aud string, opts ...option.ClientOption) (oauth2.TokenSource, error) { |
| 61 | + // This mock will return our custom token source. |
| 62 | + return &mockAuthTokenSource{ |
| 63 | + tokenToReturn: &oauth2.Token{ |
| 64 | + AccessToken: mockToken, |
| 65 | + Expiry: time.Now().Add(time.Hour), |
| 66 | + }, |
| 67 | + }, nil |
| 68 | + } |
| 69 | + |
| 70 | + token, err := GetGoogleIDToken(context.Background(), audience) |
| 71 | + |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("Expected no error, but got: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + expectedToken := "Bearer " + mockToken |
| 77 | + if token != expectedToken { |
| 78 | + t.Errorf("Expected token '%s', but got '%s'", expectedToken, token) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestGetGoogleIDToken_Caching(t *testing.T) { |
| 83 | + setup(t) |
| 84 | + callCount := 0 |
| 85 | + |
| 86 | + // Replace the variable with a mock that tracks how many times it's called. |
| 87 | + newTokenSource = func(ctx context.Context, aud string, opts ...option.ClientOption) (oauth2.TokenSource, error) { |
| 88 | + callCount++ |
| 89 | + return &mockAuthTokenSource{ |
| 90 | + tokenToReturn: &oauth2.Token{AccessToken: "some-token"}, |
| 91 | + }, nil |
| 92 | + } |
| 93 | + |
| 94 | + _, _ = GetGoogleIDToken(context.Background(), "https://some-audience.com") |
| 95 | + _, _ = GetGoogleIDToken(context.Background(), "https://some-audience.com") |
| 96 | + |
| 97 | + // The mock should only be called once because the result is cached. |
| 98 | + if callCount != 1 { |
| 99 | + t.Errorf("Expected newTokenSource to be called 1 time due to caching, but was called %d times", callCount) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestGetGoogleIDToken_NewTokenSourceError(t *testing.T) { |
| 104 | + setup(t) |
| 105 | + expectedErr := errors.New("failed to create source") |
| 106 | + |
| 107 | + // This mock simulates an error during the creation of the token source itself. |
| 108 | + newTokenSource = func(ctx context.Context, aud string, opts ...option.ClientOption) (oauth2.TokenSource, error) { |
| 109 | + return nil, expectedErr |
| 110 | + } |
| 111 | + |
| 112 | + _, err := GetGoogleIDToken(context.Background(), "https://some-audience.com") |
| 113 | + |
| 114 | + if err == nil { |
| 115 | + t.Fatal("Expected an error, but got nil") |
| 116 | + } |
| 117 | + if !strings.Contains(err.Error(), expectedErr.Error()) { |
| 118 | + t.Errorf("Expected error message to contain '%s', but got: %v", expectedErr.Error(), err) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestGetGoogleIDToken_TokenFetchError(t *testing.T) { |
| 123 | + setup(t) |
| 124 | + expectedErr := errors.New("failed to fetch token") |
| 125 | + |
| 126 | + // This mock successfully creates a source, but the source itself will fail |
| 127 | + // when we try to get a token from it. |
| 128 | + newTokenSource = func(ctx context.Context, aud string, opts ...option.ClientOption) (oauth2.TokenSource, error) { |
| 129 | + return &mockAuthTokenSource{ |
| 130 | + errorToReturn: expectedErr, |
| 131 | + }, nil |
| 132 | + } |
| 133 | + |
| 134 | + _, err := GetGoogleIDToken(context.Background(), "https://some-audience.com") |
| 135 | + |
| 136 | + if err == nil { |
| 137 | + t.Fatal("Expected an error, but got nil") |
| 138 | + } |
| 139 | + if !strings.Contains(err.Error(), expectedErr.Error()) { |
| 140 | + t.Errorf("Expected error message to contain '%s', but got: %v", expectedErr.Error(), err) |
| 141 | + } |
| 142 | +} |
0 commit comments