7575 "data" : {
7676 "message" : {
7777 "data" : "10" ,
78+ "publishTime" : "2020-05-18T12:13:19Z" ,
79+ "messageId" : "1215011316659232" ,
7880 },
7981 },
8082}
@@ -122,7 +124,10 @@ def marshalled_pubsub_request():
122124def raw_pubsub_cloudevent_output (marshalled_pubsub_request ):
123125 event = PUBSUB_CLOUD_EVENT .copy ()
124126 # 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 }
126131 return from_json (json .dumps (event ))
127132
128133
@@ -138,6 +143,18 @@ def firebase_auth_cloudevent_output():
138143 return from_json (f .read ())
139144
140145
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+
141158@pytest .fixture
142159def create_ce_headers ():
143160 return lambda event_type , source : {
@@ -207,6 +224,41 @@ def test_firebase_auth_event_to_cloudevent_no_uid(
207224 assert cloudevent == firebase_auth_cloudevent_output
208225
209226
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+
210262@pytest .mark .parametrize (
211263 "background_resource" ,
212264 [
@@ -299,34 +351,39 @@ def test_marshal_background_event_data_with_topic_path(
299351 assert payload == marshalled_pubsub_request
300352
301353
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+ )
302367def test_pubsub_emulator_request_to_cloudevent (
303- raw_pubsub_request , raw_pubsub_cloudevent_output
368+ raw_pubsub_cloudevent_output , request_fixture , overrides , request
304369):
370+ request_path = overrides .get ("request_path" , "/" )
371+ payload = request .getfixturevalue (request_fixture )
305372 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 ,
308375 )
309376 cloudevent = event_conversion .background_event_to_cloudevent (req )
310377
311378 # Remove timestamps as they are generated on the fly.
312379 del raw_pubsub_cloudevent_output ["time" ]
380+ del raw_pubsub_cloudevent_output .data ["message" ]["publishTime" ]
313381 del cloudevent ["time" ]
382+ del cloudevent .data ["message" ]["publishTime" ]
314383
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" ]
330387
331388 assert cloudevent == raw_pubsub_cloudevent_output
332389
@@ -378,6 +435,18 @@ def test_pubsub_emulator_request_with_invalid_message(
378435 "providers/firebase.auth/eventTypes/user.create" ,
379436 "projects/my-project-id" ,
380437 ),
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+ ),
381450 ],
382451)
383452def test_cloudevent_to_legacy_event (
@@ -406,7 +475,13 @@ def test_cloudevent_to_legacy_event_with_pubsub_message_payload(
406475 "google.cloud.pubsub.topic.v1.messagePublished" ,
407476 "//pubsub.googleapis.com/projects/sample-project/topics/gcf-test" ,
408477 )
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+ }
410485 req = flask .Request .from_values (headers = headers , json = data )
411486
412487 (res_data , res_context ) = event_conversion .cloudevent_to_background_event (req )
0 commit comments