75
75
"data" : {
76
76
"message" : {
77
77
"data" : "10" ,
78
+ "publishTime" : "2020-05-18T12:13:19Z" ,
79
+ "messageId" : "1215011316659232" ,
78
80
},
79
81
},
80
82
}
@@ -122,7 +124,10 @@ def marshalled_pubsub_request():
122
124
def raw_pubsub_cloudevent_output (marshalled_pubsub_request ):
123
125
event = PUBSUB_CLOUD_EVENT .copy ()
124
126
# the data payload is more complex for the raw pubsub request
125
- event ["data" ] = {"message" : marshalled_pubsub_request ["data" ]}
127
+ data = marshalled_pubsub_request ["data" ]
128
+ data ["messageId" ] = event ["id" ]
129
+ data ["publishTime" ] = event ["time" ]
130
+ event ["data" ] = {"message" : data }
126
131
return from_json (json .dumps (event ))
127
132
128
133
@@ -138,6 +143,18 @@ def firebase_auth_cloudevent_output():
138
143
return from_json (f .read ())
139
144
140
145
146
+ @pytest .fixture
147
+ def firebase_db_background_input ():
148
+ with open (TEST_DATA_DIR / "firebase-db-legacy-input.json" , "r" ) as f :
149
+ return json .load (f )
150
+
151
+
152
+ @pytest .fixture
153
+ def firebase_db_cloudevent_output ():
154
+ with open (TEST_DATA_DIR / "firebase-db-cloudevent-output.json" , "r" ) as f :
155
+ return from_json (f .read ())
156
+
157
+
141
158
@pytest .fixture
142
159
def create_ce_headers ():
143
160
return lambda event_type , source : {
@@ -207,6 +224,41 @@ def test_firebase_auth_event_to_cloudevent_no_uid(
207
224
assert cloudevent == firebase_auth_cloudevent_output
208
225
209
226
227
+ def test_firebase_db_event_to_cloudevent_default_location (
228
+ firebase_db_background_input , firebase_db_cloudevent_output
229
+ ):
230
+ req = flask .Request .from_values (json = firebase_db_background_input )
231
+ cloudevent = event_conversion .background_event_to_cloudevent (req )
232
+ assert cloudevent == firebase_db_cloudevent_output
233
+
234
+
235
+ def test_firebase_db_event_to_cloudevent_location_subdomain (
236
+ firebase_db_background_input , firebase_db_cloudevent_output
237
+ ):
238
+ firebase_db_background_input ["domain" ] = "europe-west1.firebasedatabase.app"
239
+ firebase_db_cloudevent_output ["source" ] = firebase_db_cloudevent_output [
240
+ "source"
241
+ ].replace ("us-central1" , "europe-west1" )
242
+
243
+ req = flask .Request .from_values (json = firebase_db_background_input )
244
+ cloudevent = event_conversion .background_event_to_cloudevent (req )
245
+ assert cloudevent == firebase_db_cloudevent_output
246
+
247
+
248
+ def test_firebase_db_event_to_cloudevent_missing_domain (
249
+ firebase_db_background_input , firebase_db_cloudevent_output
250
+ ):
251
+ del firebase_db_background_input ["domain" ]
252
+ req = flask .Request .from_values (json = firebase_db_background_input )
253
+
254
+ with pytest .raises (EventConversionException ) as exc_info :
255
+ event_conversion .background_event_to_cloudevent (req )
256
+
257
+ assert (
258
+ "Invalid FirebaseDB event payload: missing 'domain'" in exc_info .value .args [0 ]
259
+ )
260
+
261
+
210
262
@pytest .mark .parametrize (
211
263
"background_resource" ,
212
264
[
@@ -299,34 +351,39 @@ def test_marshal_background_event_data_with_topic_path(
299
351
assert payload == marshalled_pubsub_request
300
352
301
353
354
+ @pytest .mark .parametrize (
355
+ "request_fixture, overrides" ,
356
+ [
357
+ (
358
+ "raw_pubsub_request" ,
359
+ {
360
+ "request_path" : "x/projects/sample-project/topics/gcf-test?pubsub_trigger=true" ,
361
+ },
362
+ ),
363
+ ("raw_pubsub_request" , {"source" : "//pubsub.googleapis.com/" }),
364
+ ("marshalled_pubsub_request" , {}),
365
+ ],
366
+ )
302
367
def test_pubsub_emulator_request_to_cloudevent (
303
- raw_pubsub_request , raw_pubsub_cloudevent_output
368
+ raw_pubsub_cloudevent_output , request_fixture , overrides , request
304
369
):
370
+ request_path = overrides .get ("request_path" , "/" )
371
+ payload = request .getfixturevalue (request_fixture )
305
372
req = flask .Request .from_values (
306
- json = raw_pubsub_request ,
307
- path = "x/projects/sample-project/topics/gcf-test?pubsub_trigger=true" ,
373
+ path = request_path ,
374
+ json = payload ,
308
375
)
309
376
cloudevent = event_conversion .background_event_to_cloudevent (req )
310
377
311
378
# Remove timestamps as they are generated on the fly.
312
379
del raw_pubsub_cloudevent_output ["time" ]
380
+ del raw_pubsub_cloudevent_output .data ["message" ]["publishTime" ]
313
381
del cloudevent ["time" ]
382
+ del cloudevent .data ["message" ]["publishTime" ]
314
383
315
- assert cloudevent == raw_pubsub_cloudevent_output
316
-
317
-
318
- def test_pubsub_emulator_request_to_cloudevent_without_topic_path (
319
- raw_pubsub_request , raw_pubsub_cloudevent_output
320
- ):
321
- req = flask .Request .from_values (json = raw_pubsub_request , path = "/" )
322
- cloudevent = event_conversion .background_event_to_cloudevent (req )
323
-
324
- # Remove timestamps as they are generated on the fly.
325
- del raw_pubsub_cloudevent_output ["time" ]
326
- del cloudevent ["time" ]
327
-
328
- # Default to the service name, when the topic is not configured subscription's pushEndpoint.
329
- raw_pubsub_cloudevent_output ["source" ] = "//pubsub.googleapis.com/"
384
+ if "source" in overrides :
385
+ # Default to the service name, when the topic is not configured subscription's pushEndpoint.
386
+ raw_pubsub_cloudevent_output ["source" ] = overrides ["source" ]
330
387
331
388
assert cloudevent == raw_pubsub_cloudevent_output
332
389
@@ -378,6 +435,18 @@ def test_pubsub_emulator_request_with_invalid_message(
378
435
"providers/firebase.auth/eventTypes/user.create" ,
379
436
"projects/my-project-id" ,
380
437
),
438
+ (
439
+ "google.firebase.database.document.v1.written" ,
440
+ "//firebasedatabase.googleapis.com/projects/_/locations/us-central1/instances/my-project-id" ,
441
+ "providers/google.firebase.database/eventTypes/ref.write" ,
442
+ "projects/_/instances/my-project-id/my/subject" ,
443
+ ),
444
+ (
445
+ "google.cloud.firestore.document.v1.written" ,
446
+ "//firestore.googleapis.com/projects/project-id/databases/(default)" ,
447
+ "providers/cloud.firestore/eventTypes/document.write" ,
448
+ "projects/project-id/databases/(default)/my/subject" ,
449
+ ),
381
450
],
382
451
)
383
452
def test_cloudevent_to_legacy_event (
@@ -406,7 +475,13 @@ def test_cloudevent_to_legacy_event_with_pubsub_message_payload(
406
475
"google.cloud.pubsub.topic.v1.messagePublished" ,
407
476
"//pubsub.googleapis.com/projects/sample-project/topics/gcf-test" ,
408
477
)
409
- data = {"message" : {"data" : "fizzbuzz" }}
478
+ data = {
479
+ "message" : {
480
+ "data" : "fizzbuzz" ,
481
+ "messageId" : "aaaaaa-1111-bbbb-2222-cccccccccccc" ,
482
+ "publishTime" : "2020-09-29T11:32:00.000Z" ,
483
+ }
484
+ }
410
485
req = flask .Request .from_values (headers = headers , json = data )
411
486
412
487
(res_data , res_context ) = event_conversion .cloudevent_to_background_event (req )
0 commit comments