|
| 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 auth |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + firebaseauth "firebase.google.com/go/v4/auth" |
| 24 | +) |
| 25 | + |
| 26 | +// TestGCIPAuthenticator tests the GCIPAuthenticator functions. |
| 27 | +func TestGCIPAuthenticator(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + idToken string |
| 31 | + mockVerifyFn func(context.Context, string) (*firebaseauth.Token, error) |
| 32 | + expectedUser *User |
| 33 | + expectedError bool |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "Successful authentication", |
| 37 | + idToken: "valid_id_token", |
| 38 | + mockVerifyFn: func(_ context.Context, _ string) (*firebaseauth.Token, error) { |
| 39 | + // nolint:exhaustruct // WONTFIX -third part struct used for testing |
| 40 | + return &firebaseauth.Token{UID: "123"}, nil |
| 41 | + }, |
| 42 | + expectedUser: &User{ID: "123"}, |
| 43 | + expectedError: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "Authentication failure", |
| 47 | + idToken: "invalid_id_token", |
| 48 | + mockVerifyFn: func(_ context.Context, _ string) (*firebaseauth.Token, error) { |
| 49 | + return nil, errors.New("invalid ID token") |
| 50 | + }, |
| 51 | + expectedUser: nil, |
| 52 | + expectedError: true, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tc := range tests { |
| 57 | + t.Run(tc.name, func(t *testing.T) { |
| 58 | + // Create a mock UserAuthClient. |
| 59 | + mockUserAuthClient := &MockUserAuthClient{ |
| 60 | + verifyIDTokenFn: tc.mockVerifyFn, |
| 61 | + } |
| 62 | + |
| 63 | + // Create a GCIPAuthenticator using the mock client. |
| 64 | + authenticator := NewGCIPAuthenticator(mockUserAuthClient) |
| 65 | + |
| 66 | + // Authenticate the user. |
| 67 | + user, err := authenticator.Authenticate(context.Background(), tc.idToken) |
| 68 | + |
| 69 | + // Check if the error matches the expected outcome. |
| 70 | + if tc.expectedError && err == nil { |
| 71 | + t.Fatal("Expected authentication to fail, but it succeeded") |
| 72 | + } else if !tc.expectedError && err != nil { |
| 73 | + t.Fatalf("Failed to authenticate: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + // Check if the user matches the expected value. |
| 77 | + if !reflect.DeepEqual(tc.expectedUser, user) { |
| 78 | + t.Errorf("Expected user to be '%+v', got '%+v'", tc.expectedUser, user) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// MockUserAuthClient is a mock implementation of the UserAuthClient interface. |
| 85 | +type MockUserAuthClient struct { |
| 86 | + verifyIDTokenFn func(context.Context, string) (*firebaseauth.Token, error) |
| 87 | +} |
| 88 | + |
| 89 | +// VerifyIDToken verifies an ID token. |
| 90 | +func (m *MockUserAuthClient) VerifyIDToken(ctx context.Context, idToken string) (*firebaseauth.Token, error) { |
| 91 | + if m.verifyIDTokenFn == nil { |
| 92 | + panic("verifyIDTokenFn not set") |
| 93 | + } |
| 94 | + |
| 95 | + return m.verifyIDTokenFn(ctx, idToken) |
| 96 | +} |
0 commit comments