Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventGrid Event raise on binary type #14383

Merged
merged 9 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from typing import TYPE_CHECKING, Any
import hashlib
import hmac
import base64
from typing import TYPE_CHECKING, Any
import six
try:
from urllib.parse import quote
except ImportError:
Expand Down Expand Up @@ -92,3 +93,13 @@ def _is_cloud_event(event):
return all([_ in event for _ in required]) and event['specversion'] == "1.0"
except TypeError:
return False

def _eventgrid_data_typecheck(event):
try:
data = event.get('data')
except AttributeError:
data = event.data

if isinstance(data, six.binary_type):
raise TypeError("Data in EventGridEvent cannot be bytes. Please refer to"\
"https://docs.microsoft.com/en-us/azure/event-grid/event-schema")
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
)

from ._models import CloudEvent, EventGridEvent, CustomEvent
from ._helpers import _get_topic_hostname_only_fqdn, _get_authentication_policy, _is_cloud_event
from ._helpers import (
_get_topic_hostname_only_fqdn,
_get_authentication_policy,
_is_cloud_event,
_eventgrid_data_typecheck
)
from ._generated._event_grid_publisher_client import EventGridPublisherClient as EventGridPublisherClientImpl
from ._policies import CloudEventDistributedTracingPolicy
from ._version import VERSION
Expand Down Expand Up @@ -122,6 +127,8 @@ def send(self, events, **kwargs):
)
elif all(isinstance(e, EventGridEvent) for e in events) or all(isinstance(e, dict) for e in events):
kwargs.setdefault("content_type", "application/json; charset=utf-8")
for event in events:
_eventgrid_data_typecheck(event)
self._client.publish_events(self._topic_hostname, cast(List[InternalEventGridEvent], events), **kwargs)
elif all(isinstance(e, CustomEvent) for e in events):
serialized_events = [dict(e) for e in events] # type: ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
)
from .._policies import CloudEventDistributedTracingPolicy
from .._models import CloudEvent, EventGridEvent, CustomEvent
from .._helpers import _get_topic_hostname_only_fqdn, _get_authentication_policy, _is_cloud_event
from .._helpers import (
_get_topic_hostname_only_fqdn,
_get_authentication_policy,
_is_cloud_event,
_eventgrid_data_typecheck
)
from .._generated.aio import EventGridPublisherClient as EventGridPublisherClientAsync
from .._generated.models import CloudEvent as InternalCloudEvent, EventGridEvent as InternalEventGridEvent
from .._shared_access_signature_credential import EventGridSharedAccessSignatureCredential
Expand Down Expand Up @@ -126,6 +131,8 @@ async def send(
)
elif all(isinstance(e, EventGridEvent) for e in events) or all(isinstance(e, dict) for e in events):
kwargs.setdefault("content_type", "application/json; charset=utf-8")
for event in events:
_eventgrid_data_typecheck(event)
await self._client.publish_events(
self._topic_hostname,
cast(List[InternalEventGridEvent], events),
Expand Down
46 changes: 31 additions & 15 deletions sdk/eventgrid/azure-eventgrid/tests/test_eg_publisher_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
class EventGridPublisherClientTests(AzureMgmtTestCase):
FILTER_HEADERS = ReplayableTest.FILTER_HEADERS + ['aeg-sas-key']

@pytest.mark.liveTest
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='eventgridtest')
def test_send_event_grid_event_data_dict(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
Expand All @@ -39,41 +38,66 @@ def test_send_event_grid_event_data_dict(self, resource_group, eventgrid_topic,
)
client.send(eg_event)

@pytest.mark.liveTest
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='eventgridtest')
def test_send_event_grid_event_data_as_list(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
eg_event1 = EventGridEvent(
subject="sample",
data="eventgridevent",
data=u"eventgridevent",
event_type="Sample.EventGrid.Event",
data_version="2.0"
)
eg_event2 = EventGridEvent(
subject="sample2",
data="eventgridevent2",
data=u"eventgridevent2",
event_type="Sample.EventGrid.Event",
data_version="2.0"
)
client.send([eg_event1, eg_event2])

@pytest.mark.liveTest
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='eventgridtest')
def test_send_event_grid_event_data_str(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
eg_event = EventGridEvent(
subject="sample",
data="eventgridevent",
data=u"eventgridevent",
event_type="Sample.EventGrid.Event",
data_version="2.0"
)
client.send(eg_event)

@pytest.mark.liveTest
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='eventgridtest')
def test_send_event_grid_event_data_bytes(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
eg_event = EventGridEvent(
subject="sample",
data=b"eventgridevent",
event_type="Sample.EventGrid.Event",
data_version="2.0"
)
with pytest.raises(TypeError, match="Data in EventGridEvent cannot be bytes*"):
client.send(eg_event)

@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='eventgridtest')
def test_send_event_grid_event_dict_data_bytes(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
eg_event = {
"subject":"sample",
"data":b"eventgridevent",
"event_type":"Sample.EventGrid.Event",
"data_version":"2.0"
}
with pytest.raises(TypeError, match="Data in EventGridEvent cannot be bytes*"):
client.send(eg_event)

@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='cloudeventgridtest')
def test_send_cloud_event_data_dict(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
Expand All @@ -86,7 +110,6 @@ def test_send_cloud_event_data_dict(self, resource_group, eventgrid_topic, event
)
client.send(cloud_event)

@pytest.mark.liveTest
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='cloudeventgridtest')
def test_send_cloud_event_data_str(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
Expand All @@ -99,7 +122,6 @@ def test_send_cloud_event_data_str(self, resource_group, eventgrid_topic, eventg
)
client.send(cloud_event)

@pytest.mark.liveTest
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='cloudeventgridtest')
def test_send_cloud_event_data_bytes(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
Expand All @@ -112,7 +134,6 @@ def test_send_cloud_event_data_bytes(self, resource_group, eventgrid_topic, even
)
client.send(cloud_event)

@pytest.mark.liveTest
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='cloudeventgridtest')
def test_send_cloud_event_data_as_list(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
Expand All @@ -125,7 +146,6 @@ def test_send_cloud_event_data_as_list(self, resource_group, eventgrid_topic, ev
)
client.send([cloud_event])

@pytest.mark.liveTest
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='cloudeventgridtest')
def test_send_cloud_event_data_with_extensions(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
Expand All @@ -146,7 +166,6 @@ def test_send_cloud_event_data_with_extensions(self, resource_group, eventgrid_t
assert 'extension' in internal
assert internal['reason_code'] == 204

@pytest.mark.liveTest
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='cloudeventgridtest')
def test_send_cloud_event_dict(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
Expand All @@ -161,7 +180,6 @@ def test_send_cloud_event_dict(self, resource_group, eventgrid_topic, eventgrid_
}
client.send(cloud_event1)

@pytest.mark.liveTest
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='eventgridtest')
def test_send_signature_credential(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
Expand All @@ -177,7 +195,6 @@ def test_send_signature_credential(self, resource_group, eventgrid_topic, eventg
)
client.send(eg_event)

@pytest.mark.liveTest
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='customeventgridtest')
def test_send_custom_schema_event(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
Expand All @@ -195,7 +212,6 @@ def test_send_custom_schema_event(self, resource_group, eventgrid_topic, eventgr
)
client.send(custom_event)

@pytest.mark.liveTest
@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='customeventgridtest')
def test_send_custom_schema_event_as_list(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,36 @@ async def test_send_event_grid_event_data_str(self, resource_group, eventgrid_to
)
await client.send(eg_event)

@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='eventgridtest')
@pytest.mark.asyncio
async def test_send_event_grid_event_data_bytes(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
eg_event = EventGridEvent(
subject="sample",
data=b"eventgridevent",
event_type="Sample.EventGrid.Event",
data_version="2.0"
)
with pytest.raises(TypeError, match="Data in EventGridEvent cannot be bytes*"):
await client.send(eg_event)

@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='eventgridtest')
@pytest.mark.asyncio
async def test_send_event_grid_event_dict_data_bytes(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
eg_event = {
"subject":"sample",
"data":b"eventgridevent",
"event_type":"Sample.EventGrid.Event",
"data_version":"2.0"
}
with pytest.raises(TypeError, match="Data in EventGridEvent cannot be bytes*"):
await client.send(eg_event)

@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='cloudeventgridtest')
@pytest.mark.asyncio
Expand Down