|
16 | 16 |
|
17 | 17 | from datetime import datetime, timedelta, timezone |
18 | 18 | import json |
19 | | -import os |
20 | 19 | import time |
21 | 20 | import pytest |
22 | 21 |
|
@@ -45,7 +44,8 @@ def setup_class(cls): |
45 | 44 | def teardown_class(cls): |
46 | 45 | testutils.cleanup_apps() |
47 | 46 |
|
48 | | - def _instrument_functions_service(self, app=None, status=200, payload=_DEFAULT_RESPONSE, mounted_url=_CLOUD_TASKS_URL): |
| 47 | + def _instrument_functions_service( |
| 48 | + self, app=None, status=200, payload=_DEFAULT_RESPONSE, mounted_url=_CLOUD_TASKS_URL): |
49 | 49 | if not app: |
50 | 50 | app = firebase_admin.get_app() |
51 | 51 | functions_service = functions._get_functions_service(app) |
@@ -214,37 +214,45 @@ def test_task_enqueue_with_emulator_host(self, monkeypatch): |
214 | 214 | emulator_host = 'localhost:8124' |
215 | 215 | emulator_url = f'http://{emulator_host}/' |
216 | 216 | request_url = emulator_url + _DEFAULT_TASK_PATH.replace('/tasks/test-task-id', '/tasks') |
217 | | - |
| 217 | + |
218 | 218 | monkeypatch.setenv('CLOUD_TASKS_EMULATOR_HOST', emulator_host) |
219 | | - app = firebase_admin.initialize_app(_utils.EmulatorAdminCredentials(), {'projectId': 'test-project'}, name='emulator-app') |
220 | | - |
221 | | - expected_response = json.dumps({'task': {'name': '/projects/test-project/locations/us-central1/queues/test-function-name/tasks/test-task-id'}}) |
222 | | - _, recorder = self._instrument_functions_service(app, payload=expected_response, mounted_url=emulator_url) |
| 219 | + app = firebase_admin.initialize_app( |
| 220 | + _utils.EmulatorAdminCredentials(), {'projectId': 'test-project'}, name='emulator-app') |
| 221 | + |
| 222 | + expected_task_name = ( |
| 223 | + '/projects/test-project/locations/us-central1' |
| 224 | + '/queues/test-function-name/tasks/test-task-id' |
| 225 | + ) |
| 226 | + expected_response = json.dumps({'task': {'name': expected_task_name}}) |
| 227 | + _, recorder = self._instrument_functions_service( |
| 228 | + app, payload=expected_response, mounted_url=emulator_url) |
223 | 229 |
|
224 | 230 | queue = functions.task_queue('test-function-name', app=app) |
225 | 231 | task_id = queue.enqueue(_DEFAULT_DATA) |
226 | | - |
| 232 | + |
227 | 233 | assert len(recorder) == 1 |
228 | 234 | assert recorder[0].method == 'POST' |
229 | 235 | assert recorder[0].url == request_url |
230 | 236 | assert recorder[0].headers['Content-Type'] == 'application/json' |
231 | | - |
| 237 | + |
232 | 238 | task = json.loads(recorder[0].body.decode())['task'] |
233 | 239 | assert task['httpRequest']['oidcToken'] == { |
234 | 240 | 'serviceAccountEmail': '[email protected]' |
235 | 241 | } |
236 | 242 | assert task_id == 'test-task-id' |
237 | 243 |
|
238 | 244 | def test_task_enqueue_without_emulator_host_error(self, monkeypatch): |
239 | | - app = firebase_admin.initialize_app(_utils.EmulatorAdminCredentials(), {'projectId': 'test-project'}, name='no-emulator-app') |
240 | | - |
| 245 | + app = firebase_admin.initialize_app( |
| 246 | + _utils.EmulatorAdminCredentials(), |
| 247 | + {'projectId': 'test-project'}, name='no-emulator-app') |
| 248 | + |
241 | 249 | _, recorder = self._instrument_functions_service(app) |
242 | 250 | monkeypatch.delenv('CLOUD_TASKS_EMULATOR_HOST', raising=False) |
243 | 251 | queue = functions.task_queue('test-function-name', app=app) |
244 | 252 | with pytest.raises(ValueError) as excinfo: |
245 | 253 | queue.enqueue(_DEFAULT_DATA) |
246 | 254 | assert "Failed to determine service account" in str(excinfo.value) |
247 | | - assert len(recorder) == 0 |
| 255 | + assert len(recorder) == 0 |
248 | 256 |
|
249 | 257 | def test_get_emulator_url_invalid_format(self, monkeypatch): |
250 | 258 | _, recorder = self._instrument_functions_service() |
|
0 commit comments