|
65 | 65 |
|
66 | 66 | BACKGROUND_RESOURCE_STRING = "projects/_/buckets/some-bucket/objects/folder/Test.cs"
|
67 | 67 |
|
| 68 | +PUBSUB_CLOUD_EVENT = { |
| 69 | + "specversion": "1.0", |
| 70 | + "id": "1215011316659232", |
| 71 | + "source": "//pubsub.googleapis.com/projects/sample-project/topics/gcf-test", |
| 72 | + "time": "2020-05-18T12:13:19Z", |
| 73 | + "type": "google.cloud.pubsub.topic.v1.messagePublished", |
| 74 | + "datacontenttype": "application/json", |
| 75 | + "data": { |
| 76 | + "message": { |
| 77 | + "data": "10", |
| 78 | + }, |
| 79 | + }, |
| 80 | +} |
| 81 | + |
68 | 82 |
|
69 | 83 | @pytest.fixture
|
70 | 84 | def pubsub_cloudevent_output():
|
71 |
| - event = { |
72 |
| - "specversion": "1.0", |
73 |
| - "id": "1215011316659232", |
74 |
| - "source": "//pubsub.googleapis.com/projects/sample-project/topics/gcf-test", |
75 |
| - "time": "2020-05-18T12:13:19Z", |
76 |
| - "type": "google.cloud.pubsub.topic.v1.messagePublished", |
77 |
| - "datacontenttype": "application/json", |
| 85 | + return from_json(json.dumps(PUBSUB_CLOUD_EVENT)) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.fixture |
| 89 | +def raw_pubsub_request(): |
| 90 | + return { |
| 91 | + "subscription": "projects/sample-project/subscriptions/gcf-test-sub", |
| 92 | + "message": { |
| 93 | + "data": "eyJmb28iOiJiYXIifQ==", |
| 94 | + "messageId": "1215011316659232", |
| 95 | + "attributes": {"test": "123"}, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | +@pytest.fixture |
| 101 | +def marshalled_pubsub_request(): |
| 102 | + return { |
78 | 103 | "data": {
|
79 |
| - "message": { |
80 |
| - "data": "10", |
| 104 | + "@type": "type.googleapis.com/google.pubsub.v1.PubsubMessage", |
| 105 | + "data": "eyJmb28iOiJiYXIifQ==", |
| 106 | + "attributes": {"test": "123"}, |
| 107 | + }, |
| 108 | + "context": { |
| 109 | + "eventId": "1215011316659232", |
| 110 | + "eventType": "google.pubsub.topic.publish", |
| 111 | + "resource": { |
| 112 | + "name": "projects/sample-project/topics/gcf-test", |
| 113 | + "service": "pubsub.googleapis.com", |
| 114 | + "type": "type.googleapis.com/google.pubsub.v1.PubsubMessage", |
81 | 115 | },
|
| 116 | + "timestamp": "2021-04-17T07:21:18.249Z", |
82 | 117 | },
|
83 | 118 | }
|
84 | 119 |
|
| 120 | + |
| 121 | +@pytest.fixture |
| 122 | +def raw_pubsub_cloudevent_output(marshalled_pubsub_request): |
| 123 | + event = PUBSUB_CLOUD_EVENT.copy() |
| 124 | + # the data payload is more complex for the raw pubsub request |
| 125 | + event["data"] = {"message": marshalled_pubsub_request["data"]} |
85 | 126 | return from_json(json.dumps(event))
|
86 | 127 |
|
87 | 128 |
|
@@ -212,3 +253,79 @@ def test_split_resource_no_resource_regex_match():
|
212 | 253 | with pytest.raises(EventConversionException) as exc_info:
|
213 | 254 | event_conversion._split_resource(context)
|
214 | 255 | assert "Resource regex did not match" in exc_info.value.args[0]
|
| 256 | + |
| 257 | + |
| 258 | +def test_marshal_background_event_data_without_topic_in_path( |
| 259 | + raw_pubsub_request, marshalled_pubsub_request |
| 260 | +): |
| 261 | + req = flask.Request.from_values(json=raw_pubsub_request, path="/myfunc/") |
| 262 | + payload = event_conversion.marshal_background_event_data(req) |
| 263 | + |
| 264 | + # Remove timestamps as they get generates on the fly |
| 265 | + del marshalled_pubsub_request["context"]["timestamp"] |
| 266 | + del payload["context"]["timestamp"] |
| 267 | + |
| 268 | + # Resource name is set to empty string when it cannot be parsed from the request path |
| 269 | + marshalled_pubsub_request["context"]["resource"]["name"] = "" |
| 270 | + |
| 271 | + assert payload == marshalled_pubsub_request |
| 272 | + |
| 273 | + |
| 274 | +def test_marshal_background_event_data_with_topic_path( |
| 275 | + raw_pubsub_request, marshalled_pubsub_request |
| 276 | +): |
| 277 | + req = flask.Request.from_values( |
| 278 | + json=raw_pubsub_request, |
| 279 | + path="x/projects/sample-project/topics/gcf-test?pubsub_trigger=true", |
| 280 | + ) |
| 281 | + payload = event_conversion.marshal_background_event_data(req) |
| 282 | + |
| 283 | + # Remove timestamps as they are generated on the fly. |
| 284 | + del marshalled_pubsub_request["context"]["timestamp"] |
| 285 | + del payload["context"]["timestamp"] |
| 286 | + |
| 287 | + assert payload == marshalled_pubsub_request |
| 288 | + |
| 289 | + |
| 290 | +def test_pubsub_emulator_request_to_cloudevent( |
| 291 | + raw_pubsub_request, raw_pubsub_cloudevent_output |
| 292 | +): |
| 293 | + req = flask.Request.from_values( |
| 294 | + json=raw_pubsub_request, |
| 295 | + path="x/projects/sample-project/topics/gcf-test?pubsub_trigger=true", |
| 296 | + ) |
| 297 | + cloudevent = event_conversion.background_event_to_cloudevent(req) |
| 298 | + |
| 299 | + # Remove timestamps as they are generated on the fly. |
| 300 | + del raw_pubsub_cloudevent_output["time"] |
| 301 | + del cloudevent["time"] |
| 302 | + |
| 303 | + assert cloudevent == raw_pubsub_cloudevent_output |
| 304 | + |
| 305 | + |
| 306 | +def test_pubsub_emulator_request_to_cloudevent_without_topic_path( |
| 307 | + raw_pubsub_request, raw_pubsub_cloudevent_output |
| 308 | +): |
| 309 | + req = flask.Request.from_values(json=raw_pubsub_request, path="/") |
| 310 | + cloudevent = event_conversion.background_event_to_cloudevent(req) |
| 311 | + |
| 312 | + # Remove timestamps as they are generated on the fly. |
| 313 | + del raw_pubsub_cloudevent_output["time"] |
| 314 | + del cloudevent["time"] |
| 315 | + |
| 316 | + # Default to the service name, when the topic is not configured subscription's pushEndpoint. |
| 317 | + raw_pubsub_cloudevent_output["source"] = "//pubsub.googleapis.com/" |
| 318 | + |
| 319 | + assert cloudevent == raw_pubsub_cloudevent_output |
| 320 | + |
| 321 | + |
| 322 | +def test_pubsub_emulator_request_with_invalid_message( |
| 323 | + raw_pubsub_request, raw_pubsub_cloudevent_output |
| 324 | +): |
| 325 | + # Create an invalid message payload |
| 326 | + raw_pubsub_request["message"] = None |
| 327 | + req = flask.Request.from_values(json=raw_pubsub_request, path="/") |
| 328 | + |
| 329 | + with pytest.raises(EventConversionException) as exc_info: |
| 330 | + cloudevent = event_conversion.background_event_to_cloudevent(req) |
| 331 | + assert "Failed to convert Pub/Sub payload to event" in exc_info.value.args[0] |
0 commit comments