17
17
from datetime import datetime , timedelta , timezone
18
18
import json
19
19
import time
20
- from unittest import mock
21
20
import pytest
22
21
23
22
import firebase_admin
@@ -125,6 +124,10 @@ def test_task_enqueue(self):
125
124
assert recorder [0 ].headers ['x-goog-api-client' ] == expected_metrics_header
126
125
assert task_id == 'test-task-id'
127
126
127
+ task = json .loads (recorder [0 ].body .decode ())['task' ]
128
+ assert task ['http_request' ]['oidc_token' ] == {'service_account_email' : 'mock-email' }
129
+ assert task ['http_request' ]['headers' ] == {'Content-Type' : 'application/json' }
130
+
128
131
def test_task_enqueue_with_extension (self ):
129
132
resource_name = (
130
133
'projects/test-project/locations/us-central1/queues/'
@@ -143,46 +146,68 @@ def test_task_enqueue_with_extension(self):
143
146
assert recorder [0 ].headers ['x-goog-api-client' ] == expected_metrics_header
144
147
assert task_id == 'test-task-id'
145
148
146
- def test_task_delete (self ):
147
- _ , recorder = self ._instrument_functions_service ()
148
- queue = functions .task_queue ('test-function-name' )
149
- queue .delete ('test-task-id' )
149
+ task = json .loads (recorder [0 ].body .decode ())['task' ]
150
+ assert task ['http_request' ]['oidc_token' ] == {'service_account_email' : 'mock-email' }
151
+ assert task ['http_request' ]['headers' ] == {'Content-Type' : 'application/json' }
152
+
153
+ def test_task_enqueue_compute_engine (self ):
154
+ app = firebase_admin .initialize_app (
155
+ testutils .MockComputeEngineCredential (),
156
+ options = {'projectId' : 'test-project' },
157
+ name = 'test-project-gce' )
158
+ _ , recorder = self ._instrument_functions_service (app )
159
+ queue = functions .task_queue ('test-function-name' , app = app )
160
+ task_id = queue .enqueue (_DEFAULT_DATA )
150
161
assert len (recorder ) == 1
151
- assert recorder [0 ].method == 'DELETE'
152
- assert recorder [0 ].url == _DEFAULT_TASK_URL
153
- expected_metrics_header = _utils .get_metrics_header () + ' mock-cred-metric-tag'
162
+ assert recorder [0 ].method == 'POST'
163
+ assert recorder [0 ].url == _DEFAULT_REQUEST_URL
164
+ assert recorder [0 ].headers ['Content-Type' ] == 'application/json'
165
+ assert recorder [0 ].headers ['Authorization' ] == 'Bearer mock-compute-engine-token'
166
+ expected_metrics_header = _utils .get_metrics_header () + ' mock-gce-cred-metric-tag'
154
167
assert recorder [0 ].headers ['x-goog-api-client' ] == expected_metrics_header
168
+ assert task_id == 'test-task-id'
155
169
156
- @mock .patch ('firebase_admin.functions.isinstance' )
157
- def test_task_enqueue_with_extension_refreshes_credential (self , mock_isinstance ):
158
- # Force the code to take the ComputeEngineCredentials path
159
- mock_isinstance .return_value = True
170
+ task = json .loads (recorder [0 ].body .decode ())['task' ]
171
+ assert task ['http_request' ]['oidc_token' ] == {'service_account_email' : 'mock-gce-email' }
172
+ assert task ['http_request' ]['headers' ] == {'Content-Type' : 'application/json' }
160
173
161
- # Create a custom response with the extension ID in the resource name
174
+ def test_task_enqueue_with_extension_compute_engine ( self ):
162
175
resource_name = (
163
176
'projects/test-project/locations/us-central1/queues/'
164
177
'ext-test-extension-id-test-function-name/tasks'
165
178
)
166
179
extension_response = json .dumps ({'name' : resource_name + '/test-task-id' })
180
+ app = firebase_admin .initialize_app (
181
+ testutils .MockComputeEngineCredential (),
182
+ options = {'projectId' : 'test-project' },
183
+ name = 'test-project-gce-extensions' )
184
+ _ , recorder = self ._instrument_functions_service (app , payload = extension_response )
185
+ queue = functions .task_queue ('test-function-name' , 'test-extension-id' , app )
186
+ task_id = queue .enqueue (_DEFAULT_DATA )
187
+ assert len (recorder ) == 1
188
+ assert recorder [0 ].method == 'POST'
189
+ assert recorder [0 ].url == _CLOUD_TASKS_URL + resource_name
190
+ assert recorder [0 ].headers ['Content-Type' ] == 'application/json'
191
+ assert recorder [0 ].headers ['Authorization' ] == 'Bearer mock-compute-engine-token'
192
+ expected_metrics_header = _utils .get_metrics_header () + ' mock-gce-cred-metric-tag'
193
+ assert recorder [0 ].headers ['x-goog-api-client' ] == expected_metrics_header
194
+ assert task_id == 'test-task-id'
167
195
168
- # Instrument the service and get the underlying credential mock
169
- functions_service , recorder = self ._instrument_functions_service (payload = extension_response )
170
- mock_credential = functions_service ._credential
171
- mock_credential .token = 'mock-id-token'
172
- mock_credential .refresh = mock .MagicMock ()
173
-
174
- # Create a TaskQueue with an extension ID
175
- queue = functions_service .task_queue ('test-function-name' , 'test-extension-id' )
176
-
177
- # Enqueue a task
178
- queue .enqueue (_DEFAULT_DATA )
179
-
180
- # Assert that the credential was refreshed
181
- mock_credential .refresh .assert_called_once ()
196
+ task = json .loads (recorder [0 ].body .decode ())['task' ]
197
+ assert 'oidc_token' not in task ['http_request' ]
198
+ assert task ['http_request' ]['headers' ] == {
199
+ 'Content-Type' : 'application/json' ,
200
+ 'Authorization' : 'Bearer mock-compute-engine-token' }
182
201
183
- # Assert that the correct token was used in the header
202
+ def test_task_delete (self ):
203
+ _ , recorder = self ._instrument_functions_service ()
204
+ queue = functions .task_queue ('test-function-name' )
205
+ queue .delete ('test-task-id' )
184
206
assert len (recorder ) == 1
185
- assert recorder [0 ].headers ['Authorization' ] == 'Bearer mock-id-token'
207
+ assert recorder [0 ].method == 'DELETE'
208
+ assert recorder [0 ].url == _DEFAULT_TASK_URL
209
+ expected_metrics_header = _utils .get_metrics_header () + ' mock-cred-metric-tag'
210
+ assert recorder [0 ].headers ['x-goog-api-client' ] == expected_metrics_header
186
211
187
212
class TestTaskQueueOptions :
188
213
0 commit comments