Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 0d45640

Browse files
fix: Add test cases for helper method
Signed-off-by: Radhika Agrawal <agrawalradhika@google.com>
1 parent f1f7c45 commit 0d45640

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import os
2+
import pytest
3+
from unittest import mock
4+
from google.auth import exceptions
5+
# Assuming the provided code is in a file named google/auth/transport/aio/mtls_helper.py
6+
from google.auth.transport.aio import mtls_helper
7+
8+
CERT_DATA = b"client-cert"
9+
KEY_DATA = b"client-key"
10+
11+
class TestMTLSHelper:
12+
13+
@mock.patch("os.path.expanduser")
14+
@mock.patch("os.path.exists")
15+
def test__check_config_path_exists(self, mock_exists, mock_expand):
16+
mock_expand.side_effect = lambda x: x.replace("~", "/home/user")
17+
mock_exists.return_value = True
18+
19+
path = "/home/user/config.json"
20+
result = mtls_helper._check_config_path("~/config.json")
21+
22+
assert result == path
23+
mock_exists.assert_called_with(path)
24+
25+
@mock.patch("os.path.exists", return_value=False)
26+
def test__check_config_path_not_found(self, mock_exists):
27+
result = mtls_helper._check_config_path("nonexistent.json")
28+
assert result is None
29+
30+
@mock.patch("google.auth.transport.aio.mtls_helper._check_config_path")
31+
@mock.patch("os.getenv")
32+
def test_has_default_client_cert_source_default_path(self, mock_getenv, mock_check):
33+
# Case 1: Default config path exists
34+
mock_check.side_effect = lambda x: x if x == mtls_helper.CERTIFICATE_CONFIGURATION_DEFAULT_PATH else None
35+
36+
assert mtls_helper.has_default_client_cert_source() is True
37+
38+
@mock.patch("google.auth.transport.aio.mtls_helper._check_config_path")
39+
@mock.patch("os.getenv")
40+
def test_has_default_client_cert_source_env_var(self, mock_getenv, mock_check):
41+
# Case 2: Default path doesn't exist, but env var path does
42+
custom_path = "/custom/path.json"
43+
mock_check.side_effect = lambda x: x if x == custom_path else None
44+
mock_getenv.return_value = custom_path
45+
46+
assert mtls_helper.has_default_client_cert_source() is True
47+
48+
@mock.patch("google.auth.transport.aio.mtls_helper._check_config_path", return_value=None)
49+
def test_has_default_client_cert_source_none(self, mock_check):
50+
assert mtls_helper.has_default_client_cert_source() is False
51+
52+
@mock.patch("google.auth.transport._mtls_helper._get_workload_cert_and_key")
53+
def test_get_client_ssl_credentials_success(self, mock_workload):
54+
mock_workload.return_value = (CERT_DATA, KEY_DATA)
55+
56+
success, cert, key, passphrase = mtls_helper.get_client_ssl_credentials()
57+
58+
assert success is True
59+
assert cert == CERT_DATA
60+
assert key == KEY_DATA
61+
assert passphrase is None
62+
63+
@mock.patch("google.auth.transport._mtls_helper._get_workload_cert_and_key", return_value=(None, None))
64+
def test_get_client_ssl_credentials_fail(self, mock_workload):
65+
success, cert, key, passphrase = mtls_helper.get_client_ssl_credentials()
66+
assert success is False
67+
assert cert is None
68+
69+
def test_get_client_cert_and_key_callback(self):
70+
# Callback should take priority
71+
callback = mock.Mock(return_value=(CERT_DATA, KEY_DATA))
72+
73+
success, cert, key = mtls_helper.get_client_cert_and_key(callback)
74+
75+
assert success is True
76+
assert cert == CERT_DATA
77+
assert key == KEY_DATA
78+
callback.assert_called_once()
79+
80+
@mock.patch("google.auth.transport.aio.mtls_helper.get_client_ssl_credentials")
81+
def test_get_client_cert_and_key_default(self, mock_get_ssl):
82+
mock_get_ssl.return_value = (True, CERT_DATA, KEY_DATA, None)
83+
84+
success, cert, key = mtls_helper.get_client_cert_and_key(None)
85+
86+
assert success is True
87+
assert cert == CERT_DATA
88+
assert key == KEY_DATA

0 commit comments

Comments
 (0)