|
23 | 23 | from google.auth import transport
|
24 | 24 | import google.auth.compute_engine._metadata
|
25 | 25 | from google.oauth2 import id_token
|
| 26 | +from google.oauth2 import service_account |
26 | 27 |
|
27 | 28 | SERVICE_ACCOUNT_FILE = os.path.join(
|
28 | 29 | os.path.dirname(__file__), "../data/service_account.json"
|
@@ -134,62 +135,93 @@ def test_verify_firebase_token(verify_token):
|
134 | 135 | )
|
135 | 136 |
|
136 | 137 |
|
137 |
| -def test_fetch_id_token_from_metadata_server(): |
| 138 | +def test_fetch_id_token_from_metadata_server(monkeypatch): |
| 139 | + monkeypatch.delenv(environment_vars.CREDENTIALS, raising=False) |
| 140 | + |
138 | 141 | def mock_init(self, request, audience, use_metadata_identity_endpoint):
|
139 | 142 | assert use_metadata_identity_endpoint
|
140 | 143 | self.token = "id_token"
|
141 | 144 |
|
142 |
| - with mock.patch.multiple( |
143 |
| - google.auth.compute_engine.IDTokenCredentials, |
144 |
| - __init__=mock_init, |
145 |
| - refresh=mock.Mock(), |
146 |
| - ): |
147 |
| - request = mock.Mock() |
148 |
| - token = id_token.fetch_id_token(request, "https://pubsub.googleapis.com") |
149 |
| - assert token == "id_token" |
| 145 | + with mock.patch("google.auth.compute_engine._metadata.ping", return_value=True): |
| 146 | + with mock.patch.multiple( |
| 147 | + google.auth.compute_engine.IDTokenCredentials, |
| 148 | + __init__=mock_init, |
| 149 | + refresh=mock.Mock(), |
| 150 | + ): |
| 151 | + request = mock.Mock() |
| 152 | + token = id_token.fetch_id_token(request, "https://pubsub.googleapis.com") |
| 153 | + assert token == "id_token" |
150 | 154 |
|
151 | 155 |
|
152 |
| -@mock.patch.object( |
153 |
| - google.auth.compute_engine.IDTokenCredentials, |
154 |
| - "__init__", |
155 |
| - side_effect=exceptions.TransportError(), |
156 |
| -) |
157 |
| -def test_fetch_id_token_from_explicit_cred_json_file(mock_init, monkeypatch): |
| 156 | +def test_fetch_id_token_from_explicit_cred_json_file(monkeypatch): |
158 | 157 | monkeypatch.setenv(environment_vars.CREDENTIALS, SERVICE_ACCOUNT_FILE)
|
159 | 158 |
|
160 | 159 | def mock_refresh(self, request):
|
161 | 160 | self.token = "id_token"
|
162 | 161 |
|
163 |
| - with mock.patch.object( |
164 |
| - google.oauth2.service_account.IDTokenCredentials, "refresh", mock_refresh |
165 |
| - ): |
| 162 | + with mock.patch.object(service_account.IDTokenCredentials, "refresh", mock_refresh): |
166 | 163 | request = mock.Mock()
|
167 | 164 | token = id_token.fetch_id_token(request, "https://pubsub.googleapis.com")
|
168 | 165 | assert token == "id_token"
|
169 | 166 |
|
170 | 167 |
|
171 |
| -@mock.patch.object( |
172 |
| - google.auth.compute_engine.IDTokenCredentials, |
173 |
| - "__init__", |
174 |
| - side_effect=exceptions.TransportError(), |
175 |
| -) |
176 |
| -def test_fetch_id_token_no_cred_json_file(mock_init, monkeypatch): |
| 168 | +def test_fetch_id_token_no_cred_exists(monkeypatch): |
177 | 169 | monkeypatch.delenv(environment_vars.CREDENTIALS, raising=False)
|
178 | 170 |
|
179 |
| - with pytest.raises(exceptions.DefaultCredentialsError): |
| 171 | + with mock.patch( |
| 172 | + "google.auth.compute_engine._metadata.ping", |
| 173 | + side_effect=exceptions.TransportError(), |
| 174 | + ): |
| 175 | + with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
| 176 | + request = mock.Mock() |
| 177 | + id_token.fetch_id_token(request, "https://pubsub.googleapis.com") |
| 178 | + assert excinfo.match( |
| 179 | + r"Neither metadata server or valid service account credentials are found." |
| 180 | + ) |
| 181 | + |
| 182 | + with mock.patch("google.auth.compute_engine._metadata.ping", return_value=False): |
| 183 | + with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
| 184 | + request = mock.Mock() |
| 185 | + id_token.fetch_id_token(request, "https://pubsub.googleapis.com") |
| 186 | + assert excinfo.match( |
| 187 | + r"Neither metadata server or valid service account credentials are found." |
| 188 | + ) |
| 189 | + |
| 190 | + |
| 191 | +def test_fetch_id_token_invalid_cred_file_type(monkeypatch): |
| 192 | + user_credentials_file = os.path.join( |
| 193 | + os.path.dirname(__file__), "../data/authorized_user.json" |
| 194 | + ) |
| 195 | + monkeypatch.setenv(environment_vars.CREDENTIALS, user_credentials_file) |
| 196 | + |
| 197 | + with mock.patch("google.auth.compute_engine._metadata.ping", return_value=False): |
| 198 | + with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
| 199 | + request = mock.Mock() |
| 200 | + id_token.fetch_id_token(request, "https://pubsub.googleapis.com") |
| 201 | + assert excinfo.match( |
| 202 | + r"Neither metadata server or valid service account credentials are found." |
| 203 | + ) |
| 204 | + |
| 205 | + |
| 206 | +def test_fetch_id_token_invalid_json(monkeypatch): |
| 207 | + not_json_file = os.path.join(os.path.dirname(__file__), "../data/public_cert.pem") |
| 208 | + monkeypatch.setenv(environment_vars.CREDENTIALS, not_json_file) |
| 209 | + |
| 210 | + with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
180 | 211 | request = mock.Mock()
|
181 | 212 | id_token.fetch_id_token(request, "https://pubsub.googleapis.com")
|
| 213 | + assert excinfo.match( |
| 214 | + r"GOOGLE_APPLICATION_CREDENTIALS is not valid service account credentials." |
| 215 | + ) |
182 | 216 |
|
183 | 217 |
|
184 |
| -@mock.patch.object( |
185 |
| - google.auth.compute_engine.IDTokenCredentials, |
186 |
| - "__init__", |
187 |
| - side_effect=exceptions.TransportError(), |
188 |
| -) |
189 |
| -def test_fetch_id_token_invalid_cred_file(mock_init, monkeypatch): |
190 |
| - not_json_file = os.path.join(os.path.dirname(__file__), "../data/public_cert.pem") |
| 218 | +def test_fetch_id_token_invalid_cred_path(monkeypatch): |
| 219 | + not_json_file = os.path.join(os.path.dirname(__file__), "../data/not_exists.json") |
191 | 220 | monkeypatch.setenv(environment_vars.CREDENTIALS, not_json_file)
|
192 | 221 |
|
193 |
| - with pytest.raises(exceptions.DefaultCredentialsError): |
| 222 | + with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
194 | 223 | request = mock.Mock()
|
195 | 224 | id_token.fetch_id_token(request, "https://pubsub.googleapis.com")
|
| 225 | + assert excinfo.match( |
| 226 | + r"GOOGLE_APPLICATION_CREDENTIALS path is either not found or invalid." |
| 227 | + ) |
0 commit comments