12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import google .auth .exceptions
16
- import pytest
17
15
from unittest .mock import patch , AsyncMock , MagicMock
18
16
17
+ import pytest
18
+ import google .auth .exceptions
19
+
19
20
from toolbox_core import auth_methods
20
21
22
+ # Constants for test values
23
+ MOCK_ASYNC_ID_TOKEN = "test_async_id_token_123"
24
+ MOCK_SYNC_ID_TOKEN = "test_sync_id_token_456"
25
+ MOCK_PROJECT_ID = "test-project"
26
+
27
+ # Error Messages
28
+ ADC_NOT_FOUND_MSG = "ADC not found"
29
+ TOKEN_REFRESH_FAILED_MSG = "Token refresh failed"
30
+ SYNC_ADC_NOT_FOUND_MSG = "Sync ADC not found"
31
+ SYNC_TOKEN_REFRESH_FAILED_MSG = "Sync token refresh failed"
32
+
33
+
21
34
@pytest .mark .asyncio
22
35
@patch ("toolbox_core.auth_methods.partial" )
23
36
@patch ("toolbox_core.auth_methods._aiohttp_requests.Request" )
@@ -30,22 +43,18 @@ async def test_aget_google_id_token_success(
30
43
mock_partial ,
31
44
):
32
45
"""
33
- Test aget_google_id_token successfully retrieves and formats a token using pytest .
46
+ Test aget_google_id_token successfully retrieves and formats a token.
34
47
"""
35
- # Setup mock for default_async() -> (creds, project_id)
36
48
mock_creds_instance = AsyncMock ()
37
- mock_creds_instance .id_token = "test_async_id_token_123"
38
- mock_default_async .return_value = (mock_creds_instance , "test-project" )
49
+ mock_creds_instance .id_token = MOCK_ASYNC_ID_TOKEN
50
+ mock_default_async .return_value = (mock_creds_instance , MOCK_PROJECT_ID )
39
51
40
- # Setup mock for _aiohttp_requests.Request()
41
52
mock_aio_request_instance = MagicMock ()
42
53
mock_aiohttp_request_class .return_value = mock_aio_request_instance
43
54
44
- # Setup mock for Credentials.before_request (class attribute used in partial)
45
55
mock_unbound_before_request = MagicMock ()
46
56
mock_credentials_class .before_request = mock_unbound_before_request
47
57
48
- # Setup mock for partial()
49
58
mock_partial_object = MagicMock ()
50
59
mock_partial .return_value = mock_partial_object
51
60
@@ -59,22 +68,21 @@ async def test_aget_google_id_token_success(
59
68
mock_unbound_before_request , mock_creds_instance
60
69
)
61
70
assert mock_creds_instance .before_request == mock_partial_object
62
-
63
- assert token == "Bearer test_async_id_token_123"
71
+ assert token == f"Bearer { MOCK_ASYNC_ID_TOKEN } "
64
72
65
73
66
74
@pytest .mark .asyncio
67
75
@patch ("toolbox_core.auth_methods.default_async" )
68
76
async def test_aget_google_id_token_default_credentials_error (mock_default_async ):
69
77
"""
70
- Test aget_google_id_token when default_async raises DefaultCredentialsError.
78
+ Test aget_google_id_token handles DefaultCredentialsError.
71
79
"""
72
80
mock_default_async .side_effect = google .auth .exceptions .DefaultCredentialsError (
73
- "ADC not found"
81
+ ADC_NOT_FOUND_MSG
74
82
)
75
83
76
84
with pytest .raises (
77
- google .auth .exceptions .DefaultCredentialsError , match = "ADC not found"
85
+ google .auth .exceptions .DefaultCredentialsError , match = ADC_NOT_FOUND_MSG
78
86
):
79
87
await auth_methods .aget_google_id_token ()
80
88
@@ -89,20 +97,19 @@ async def test_aget_google_id_token_refresh_error(
89
97
mock_aiohttp_request_class ,
90
98
):
91
99
"""
92
- Test aget_google_id_token when creds.refresh raises RefreshError.
93
- The `partial` call should not happen if refresh fails.
100
+ Test aget_google_id_token handles RefreshError.
94
101
"""
95
102
mock_creds_instance = AsyncMock ()
96
103
mock_creds_instance .refresh .side_effect = google .auth .exceptions .RefreshError (
97
- "Token refresh failed"
104
+ TOKEN_REFRESH_FAILED_MSG
98
105
)
99
- mock_default_async .return_value = (mock_creds_instance , "test-project" )
106
+ mock_default_async .return_value = (mock_creds_instance , MOCK_PROJECT_ID )
100
107
101
108
mock_aio_request_instance = MagicMock ()
102
109
mock_aiohttp_request_class .return_value = mock_aio_request_instance
103
110
104
111
with pytest .raises (
105
- google .auth .exceptions .RefreshError , match = "Token refresh failed"
112
+ google .auth .exceptions .RefreshError , match = TOKEN_REFRESH_FAILED_MSG
106
113
):
107
114
await auth_methods .aget_google_id_token ()
108
115
@@ -122,18 +129,15 @@ def test_get_google_id_token_success(
122
129
mock_request_class ,
123
130
):
124
131
"""
125
- Test get_google_id_token successfully retrieves and formats a token using pytest .
132
+ Test get_google_id_token successfully retrieves and formats a token.
126
133
"""
127
- # Setup mock for google.auth.default() -> (credentials, project_id)
128
134
mock_creds_instance = MagicMock ()
129
- mock_creds_instance .id_token = "test_sync_id_token_456"
130
- mock_google_auth_default .return_value = (mock_creds_instance , "test-project" )
135
+ mock_creds_instance .id_token = MOCK_SYNC_ID_TOKEN
136
+ mock_google_auth_default .return_value = (mock_creds_instance , MOCK_PROJECT_ID )
131
137
132
- # Setup mock for AuthorizedSession()
133
138
mock_session_instance = MagicMock ()
134
139
mock_authorized_session_class .return_value = mock_session_instance
135
140
136
- # Setup mock for Request()
137
141
mock_request_instance = MagicMock ()
138
142
mock_request_class .return_value = mock_request_instance
139
143
@@ -143,20 +147,20 @@ def test_get_google_id_token_success(
143
147
mock_authorized_session_class .assert_called_once_with (mock_creds_instance )
144
148
mock_request_class .assert_called_once_with (mock_session_instance )
145
149
mock_creds_instance .refresh .assert_called_once_with (mock_request_instance )
146
- assert token == "Bearer test_sync_id_token_456 "
150
+ assert token == f "Bearer { MOCK_SYNC_ID_TOKEN } "
147
151
148
152
149
153
@patch ("toolbox_core.auth_methods.google.auth.default" )
150
154
def test_get_google_id_token_default_credentials_error (mock_google_auth_default ):
151
155
"""
152
- Test get_google_id_token when google.auth.default raises DefaultCredentialsError.
156
+ Test get_google_id_token handles DefaultCredentialsError.
153
157
"""
154
158
mock_google_auth_default .side_effect = (
155
- google .auth .exceptions .DefaultCredentialsError ("Sync ADC not found" )
159
+ google .auth .exceptions .DefaultCredentialsError (SYNC_ADC_NOT_FOUND_MSG )
156
160
)
157
161
158
162
with pytest .raises (
159
- google .auth .exceptions .DefaultCredentialsError , match = "Sync ADC not found"
163
+ google .auth .exceptions .DefaultCredentialsError , match = SYNC_ADC_NOT_FOUND_MSG
160
164
):
161
165
auth_methods .get_google_id_token ()
162
166
@@ -172,13 +176,13 @@ def test_get_google_id_token_refresh_error(
172
176
mock_request_class ,
173
177
):
174
178
"""
175
- Test get_google_id_token when credentials.refresh raises RefreshError.
179
+ Test get_google_id_token handles RefreshError.
176
180
"""
177
181
mock_creds_instance = MagicMock ()
178
182
mock_creds_instance .refresh .side_effect = google .auth .exceptions .RefreshError (
179
- "Sync token refresh failed"
183
+ SYNC_TOKEN_REFRESH_FAILED_MSG
180
184
)
181
- mock_google_auth_default .return_value = (mock_creds_instance , "test-project" )
185
+ mock_google_auth_default .return_value = (mock_creds_instance , MOCK_PROJECT_ID )
182
186
183
187
mock_session_instance = MagicMock ()
184
188
mock_authorized_session_class .return_value = mock_session_instance
@@ -187,7 +191,7 @@ def test_get_google_id_token_refresh_error(
187
191
mock_request_class .return_value = mock_request_instance
188
192
189
193
with pytest .raises (
190
- google .auth .exceptions .RefreshError , match = "Sync token refresh failed"
194
+ google .auth .exceptions .RefreshError , match = SYNC_TOKEN_REFRESH_FAILED_MSG
191
195
):
192
196
auth_methods .get_google_id_token ()
193
197
0 commit comments