|
18 | 18 |
|
19 | 19 | import pytest |
20 | 20 |
|
21 | | - |
| 21 | +from google.auth.transport import mtls |
22 | 22 | from google.api_core.gapic_v1.client_cert import ( |
23 | 23 | get_client_cert_source, |
24 | 24 | use_client_cert_effective, |
25 | 25 | ) |
26 | 26 |
|
27 | 27 |
|
28 | | -@mock.patch("google.auth.transport.mtls.should_use_client_cert", create=True) |
29 | | -def test_use_client_cert_effective_with_google_auth(mock_method): |
30 | | - # Test when google-auth supports the method |
31 | | - mock_method.return_value = True |
32 | | - assert use_client_cert_effective() is True |
33 | | - |
34 | | - mock_method.return_value = False |
35 | | - assert use_client_cert_effective() is False |
36 | | - |
37 | | - |
38 | | -@mock.patch.dict(os.environ, {}, clear=True) |
39 | | -def test_use_client_cert_effective_fallback(): |
40 | | - # We must patch hasattr to simulate google-auth lacking the method |
41 | | - with mock.patch("google.api_core.gapic_v1.client_cert.hasattr", return_value=False): |
42 | | - # Default is false |
43 | | - assert use_client_cert_effective() is False |
44 | | - |
45 | | - env_true = {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} |
46 | | - with mock.patch.dict(os.environ, env_true): |
47 | | - assert use_client_cert_effective() is True |
48 | | - |
49 | | - with mock.patch.dict( |
50 | | - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"} |
51 | | - ): |
52 | | - assert use_client_cert_effective() is False |
53 | | - |
54 | | - with mock.patch.dict( |
55 | | - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"} |
56 | | - ): |
57 | | - match_str = "must be either `true` or `false`" |
58 | | - with pytest.raises(ValueError, match=match_str): |
59 | | - use_client_cert_effective() |
60 | | - |
61 | | - |
62 | | -@mock.patch( |
63 | | - "google.auth.transport.mtls.has_default_client_cert_source", create=True |
64 | | -) # noqa: E501 |
65 | | -@mock.patch( |
66 | | - "google.auth.transport.mtls.default_client_cert_source", create=True |
67 | | -) # noqa: E501 |
68 | | -def test_get_client_cert_source(mock_default, mock_has_default): |
69 | | - mock_default.return_value = b"default_cert" |
70 | | - mock_has_default.return_value = True |
71 | | - |
72 | | - # When use_cert_flag is False, return None |
73 | | - assert get_client_cert_source(b"provided", False) is None |
74 | | - |
75 | | - # When provided_cert_source is given, return provided |
76 | | - assert get_client_cert_source(b"provided", True) == b"provided" # noqa: E501 |
77 | | - |
78 | | - # When no provided cert but default is available |
79 | | - assert get_client_cert_source(None, True) == b"default_cert" |
| 28 | +@pytest.mark.parametrize( |
| 29 | + "has_method, method_val, env_val, expected", |
| 30 | + [ |
| 31 | + # should_use_client_cert is available cases |
| 32 | + (True, True, None, "true"), |
| 33 | + (True, False, None, "false"), |
| 34 | + (True, False, "unsupported", "false"), |
| 35 | + # should_use_client_cert is unavailable cases |
| 36 | + (False, None, "true", "true"), |
| 37 | + (False, None, "false", "false"), |
| 38 | + (False, None, "True", "true"), |
| 39 | + (False, None, "False", "false"), |
| 40 | + (False, None, "TRUE", "true"), |
| 41 | + (False, None, "FALSE", "false"), |
| 42 | + (False, None, None, "false"), |
| 43 | + (False, None, "unsupported", "value_error"), |
| 44 | + ], |
| 45 | + ids=[ |
| 46 | + "google_auth_true", |
| 47 | + "google_auth_false", |
| 48 | + "google_auth_false_env_unsupported", |
| 49 | + "fallback_env_true_lowercase", |
| 50 | + "fallback_env_false_lowercase", |
| 51 | + "fallback_env_true_titlecase", |
| 52 | + "fallback_env_false_titlecase", |
| 53 | + "fallback_env_true_uppercase", |
| 54 | + "fallback_env_false_uppercase", |
| 55 | + "fallback_env_unset", |
| 56 | + "fallback_env_unsupported", |
| 57 | + ] |
| 58 | +) |
| 59 | +def test_use_client_cert_effective(has_method, method_val, env_val, expected): |
| 60 | + # Mock hasattr to control whether should_use_client_cert exists |
| 61 | + original_hasattr = hasattr |
| 62 | + def custom_hasattr(obj, name): |
| 63 | + if obj is mtls and name == "should_use_client_cert": |
| 64 | + return has_method |
| 65 | + return original_hasattr(obj, name) |
| 66 | + |
| 67 | + with mock.patch("google.api_core.gapic_v1.client_cert.hasattr", custom_hasattr): |
| 68 | + with mock.patch("google.auth.transport.mtls.should_use_client_cert", create=True, return_value=method_val): |
| 69 | + env = {} |
| 70 | + if env_val is not None: |
| 71 | + env["GOOGLE_API_USE_CLIENT_CERTIFICATE"] = env_val |
| 72 | + with mock.patch.dict(os.environ, env, clear=True): |
| 73 | + if expected == "value_error": |
| 74 | + with pytest.raises(ValueError, match="must be either `true` or `false`"): |
| 75 | + use_client_cert_effective() |
| 76 | + else: |
| 77 | + assert use_client_cert_effective() is (expected == "true") |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.parametrize( |
| 81 | + "provided, use_cert, has_default_avail, default_val, expected", |
| 82 | + [ |
| 83 | + (None, False, True, b"default", None), |
| 84 | + (b"provided", False, True, b"default", None), |
| 85 | + (b"provided", True, True, b"default", b"provided"), |
| 86 | + (None, True, True, b"default", b"default"), |
| 87 | + (None, True, False, b"default", None), |
| 88 | + ], |
| 89 | + ids=[ |
| 90 | + "use_cert_false_no_provided", |
| 91 | + "use_cert_false_with_provided", |
| 92 | + "use_cert_true_with_provided", |
| 93 | + "use_cert_true_no_provided_default_avail", |
| 94 | + "use_cert_true_no_provided_default_unavail", |
| 95 | + ] |
| 96 | +) |
| 97 | +def test_get_client_cert_source(provided, use_cert, has_default_avail, default_val, expected): |
| 98 | + original_hasattr = hasattr |
| 99 | + def custom_hasattr(obj, name): |
| 100 | + if obj is mtls and name == "has_default_client_cert_source": |
| 101 | + return has_default_avail |
| 102 | + return original_hasattr(obj, name) |
| 103 | + |
| 104 | + with mock.patch("google.api_core.gapic_v1.client_cert.hasattr", custom_hasattr): |
| 105 | + with mock.patch("google.auth.transport.mtls.has_default_client_cert_source", create=True, return_value=has_default_avail): |
| 106 | + with mock.patch("google.auth.transport.mtls.default_client_cert_source", create=True, return_value=default_val): |
| 107 | + assert get_client_cert_source(provided, use_cert) == expected |
0 commit comments