diff --git a/sdk/eventgrid/azure-eventgrid/assets.json b/sdk/eventgrid/azure-eventgrid/assets.json index 884eb4ab595a..a00be8330ac3 100644 --- a/sdk/eventgrid/azure-eventgrid/assets.json +++ b/sdk/eventgrid/azure-eventgrid/assets.json @@ -2,5 +2,5 @@ "AssetsRepo": "Azure/azure-sdk-assets", "AssetsRepoPrefixPath": "python", "TagPrefix": "python/eventgrid/azure-eventgrid", - "Tag": "python/eventgrid/azure-eventgrid_d176c3a3b4" + "Tag": "python/eventgrid/azure-eventgrid_6f56978914" } diff --git a/sdk/eventgrid/azure-eventgrid/tests/conftest.py b/sdk/eventgrid/azure-eventgrid/tests/conftest.py index 4f8ea9e61368..c69c75af758e 100644 --- a/sdk/eventgrid/azure-eventgrid/tests/conftest.py +++ b/sdk/eventgrid/azure-eventgrid/tests/conftest.py @@ -23,32 +23,50 @@ # IN THE SOFTWARE. # # -------------------------------------------------------------------------- -import platform -import sys +import os import pytest -from devtools_testutils import test_proxy +from urllib.parse import urlparse +from devtools_testutils import add_general_string_sanitizer, test_proxy, add_body_key_sanitizer, add_header_regex_sanitizer, add_oauth_response_sanitizer, add_body_regex_sanitizer from devtools_testutils.sanitizers import ( add_remove_header_sanitizer, add_general_regex_sanitizer, set_custom_default_matcher, ) -# Ignore async tests for Python < 3.5 -collect_ignore_glob = [] -if sys.version_info < (3, 5): - collect_ignore_glob.append("*_async.py") - collect_ignore_glob.append("test_cncf*") - - @pytest.fixture(scope="session", autouse=True) -def add_aeg_sanitizer(test_proxy): +def add_sanitizers(test_proxy): # this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers set_custom_default_matcher( compare_bodies=False, excluded_headers="Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id", ) - add_remove_header_sanitizer(headers="aeg-sas-key, aeg-sas-token") + add_remove_header_sanitizer(headers="aeg-sas-key, aeg-sas-token, aeg-channel-name") add_general_regex_sanitizer( value="fakeresource", - regex="(?<=\\/\\/)[a-z-]+(?=\\.westus2-1\\.eventgrid\\.azure\\.net/api/events)", + regex="(?<=\\/\\/)[.*]+(?=\\.eastus-1\\.eventgrid\\.azure\\.net/api/events)", ) + + add_oauth_response_sanitizer() + add_header_regex_sanitizer(key="Set-Cookie", value="[set-cookie;]") + add_header_regex_sanitizer(key="Cookie", value="cookie;") + + add_body_key_sanitizer(json_path="$..id", value="id") + + client_id = os.getenv("AZURE_CLIENT_ID", "sanitized") + client_secret = os.getenv("AZURE_CLIENT_SECRET", "sanitized") + eventgrid_client_id = os.getenv("EVENTGRID_CLIENT_ID", "sanitized") + eventgrid_client_secret = os.getenv("EVENTGRID_CLIENT_SECRET", "sanitized") + tenant_id = os.getenv("AZURE_TENANT_ID", "sanitized") + eventgrid_topic_endpoint = os.getenv("EVENTGRID_TOPIC_ENDPOINT", "sanitized") + # Need to santize namespace for eventgrid_topic: + try: + eventgrid_hostname = urlparse(eventgrid_topic_endpoint).hostname + add_general_string_sanitizer(target=eventgrid_hostname.upper(), value="sanitized.eastus-1.eventgrid.azure.net") + except: + pass + add_general_string_sanitizer(target=client_id, value="00000000-0000-0000-0000-000000000000") + add_general_string_sanitizer(target=client_secret, value="sanitized") + add_general_string_sanitizer(target=eventgrid_client_id, value="00000000-0000-0000-0000-000000000000") + add_general_string_sanitizer(target=eventgrid_client_secret, value="sanitized") + add_general_string_sanitizer(target=tenant_id, value="00000000-0000-0000-0000-000000000000") + diff --git a/sdk/eventgrid/azure-eventgrid/tests/eventgrid_preparer.py b/sdk/eventgrid/azure-eventgrid/tests/eventgrid_preparer.py index a1236092d58a..1343179da503 100644 --- a/sdk/eventgrid/azure-eventgrid/tests/eventgrid_preparer.py +++ b/sdk/eventgrid/azure-eventgrid/tests/eventgrid_preparer.py @@ -1,5 +1,5 @@ import functools -from devtools_testutils import PowerShellPreparer +from devtools_testutils import PowerShellPreparer, EnvironmentVariableLoader from azure.mgmt.eventgrid.models import ( Topic, @@ -8,12 +8,9 @@ JsonField, JsonFieldWithDefault, ) -from azure_devtools.scenario_tests.exceptions import AzureTestError - -from devtools_testutils.resource_testcase import RESOURCE_GROUP_PARAM EVENTGRID_TOPIC_PARAM = "eventgrid_topic" -EVENTGRID_TOPIC_LOCATION = "westus" +EVENTGRID_TOPIC_LOCATION = "eastus" CLOUD_EVENT_SCHEMA = InputSchema.cloud_event_schema_v1_0 CUSTOM_EVENT_SCHEMA = InputSchema.custom_event_schema ID_JSON_FIELD = JsonField(source_field="customId") @@ -40,14 +37,21 @@ EventGridPreparer = functools.partial( PowerShellPreparer, "eventgrid", - eventgrid_topic_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events", + eventgrid_topic_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events", eventgrid_topic_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=", - eventgrid_domain_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events", + eventgrid_domain_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events", eventgrid_domain_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=", - eventgrid_cloud_event_topic_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events", + eventgrid_cloud_event_topic_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events", eventgrid_cloud_event_topic_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=", - eventgrid_cloud_event_domain_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events", + eventgrid_cloud_event_domain_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events", eventgrid_cloud_event_domain_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=", - eventgrid_custom_event_topic_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events", + eventgrid_custom_event_topic_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events", eventgrid_custom_event_topic_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=", -) + eventgrid_partner_channel_name="fakechannel", + eventgrid_partner_namespace_topic_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events", + eventgrid_partner_namespace_topic_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=", + eventgrid_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events", + eventgrid_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=", + eventgrid_topic_name="faketopic", + eventgrid_event_subscription_name="fakesub", +) \ No newline at end of file diff --git a/sdk/eventgrid/azure-eventgrid/tests/test_cncf_events.py b/sdk/eventgrid/azure-eventgrid/tests/test_cncf_events.py index 00dffb933cf4..dca0af806cb3 100644 --- a/sdk/eventgrid/azure-eventgrid/tests/test_cncf_events.py +++ b/sdk/eventgrid/azure-eventgrid/tests/test_cncf_events.py @@ -20,8 +20,8 @@ def create_eg_publisher_client(self, endpoint): @EventGridPreparer() @recorded_by_proxy - def test_send_cloud_event_data_dict( - self, variables, eventgrid_cloud_event_topic_endpoint + def test_send_cncf_data_dict( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) attributes = { @@ -42,8 +42,8 @@ def callback(request): @EventGridPreparer() @recorded_by_proxy - def test_send_cloud_event_data_base64_using_data( - self, variables, eventgrid_cloud_event_topic_endpoint + def test_send_cncf_data_base64_using_data( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) attributes = { @@ -62,8 +62,8 @@ def callback(request): @EventGridPreparer() @recorded_by_proxy - def test_send_cloud_event_data_none( - self, variables, eventgrid_cloud_event_topic_endpoint + def test_send_cncf_data_none( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) attributes = { @@ -76,8 +76,8 @@ def test_send_cloud_event_data_none( @EventGridPreparer() @recorded_by_proxy - def test_send_cloud_event_data_str( - self, variables, eventgrid_cloud_event_topic_endpoint + def test_send_cncf_data_str( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) attributes = { @@ -96,8 +96,8 @@ def callback(request): @EventGridPreparer() @recorded_by_proxy - def test_send_cloud_event_data_as_list( - self, variables, eventgrid_cloud_event_topic_endpoint + def test_send_cncf_data_as_list( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) attributes = { @@ -110,8 +110,8 @@ def test_send_cloud_event_data_as_list( @EventGridPreparer() @recorded_by_proxy - def test_send_cloud_event_data_with_extensions( - self, variables, eventgrid_cloud_event_topic_endpoint + def test_send_cncf_data_with_extensions( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) attributes = { diff --git a/sdk/eventgrid/azure-eventgrid/tests/test_cncf_events_async.py b/sdk/eventgrid/azure-eventgrid/tests/test_cncf_events_async.py index 30bbd3eb7f0c..a421181f4d82 100644 --- a/sdk/eventgrid/azure-eventgrid/tests/test_cncf_events_async.py +++ b/sdk/eventgrid/azure-eventgrid/tests/test_cncf_events_async.py @@ -23,8 +23,8 @@ def create_eg_publisher_client(self, endpoint): @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_data_dict( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cncf_data_dict_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) attributes = { @@ -46,8 +46,8 @@ def callback(request): @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_data_base64_using_data( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cncf_data_base64_using_data_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) attributes = { @@ -67,8 +67,8 @@ def callback(request): @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_data_none( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cncf_data_none_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) attributes = { @@ -82,8 +82,8 @@ async def test_send_cloud_event_data_none( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_data_str( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cncf_data_str_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) attributes = { @@ -103,8 +103,8 @@ def callback(request): @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_data_as_list( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cncf_data_as_list_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) attributes = { @@ -118,8 +118,8 @@ async def test_send_cloud_event_data_as_list( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_data_with_extensions( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cncf_data_with_extensions_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) attributes = { diff --git a/sdk/eventgrid/azure-eventgrid/tests/test_eg_client.py b/sdk/eventgrid/azure-eventgrid/tests/test_eg_client.py new file mode 100644 index 000000000000..6f848c7c28a0 --- /dev/null +++ b/sdk/eventgrid/azure-eventgrid/tests/test_eg_client.py @@ -0,0 +1,78 @@ +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +import pytest +import os +import time +from devtools_testutils import AzureRecordedTestCase, recorded_by_proxy +from azure.eventgrid import EventGridClient +from azure.eventgrid.models import * +from azure.core.messaging import CloudEvent +from azure.core.credentials import AzureKeyCredential + +from eventgrid_preparer import EventGridPreparer + + +class TestEGClientExceptions(AzureRecordedTestCase): + def create_eg_client(self, endpoint, key): + client = EventGridClient( + endpoint=endpoint, credential=AzureKeyCredential(key) + ) + return client + + @pytest.mark.skip("need to update conftest") + @EventGridPreparer() + @recorded_by_proxy + def test_publish_receive_cloud_event(self, eventgrid_endpoint, eventgrid_key, eventgrid_topic_name, eventgrid_event_subscription_name): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) + + event = CloudEvent( + type="Contoso.Items.ItemReceived", + source="source", + subject="MySubject", + data=b'this is binary data', + ) + + client.publish_cloud_events( + eventgrid_topic_name, body=[event] + ) + + time.sleep(5) + + events = client.receive_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name,max_events=1) + lock_token = events.value[0].broker_properties.lock_token + + ack = client.acknowledge_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name, lock_tokens=AcknowledgeOptions(lock_tokens=[lock_token])) + assert len(ack.succeeded_lock_tokens) == 1 + assert len(ack.failed_lock_tokens) == 0 + + @pytest.mark.skip("need to update conftest") + @EventGridPreparer() + @recorded_by_proxy + def test_publish_release_cloud_event(self, eventgrid_endpoint, eventgrid_key, eventgrid_topic_name, eventgrid_event_subscription_name): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) + + event = CloudEvent( + type="Contoso.Items.ItemReceived", + source="source", + subject="MySubject", + data=b'this is binary data', + ) + + client.publish_cloud_events( + eventgrid_topic_name, body=[event] + ) + + time.sleep(5) + + events = client.receive_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name, max_events=1) + lock_token = events.value[0].broker_properties.lock_token + + ack = client.release_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name, lock_tokens=ReleaseOptions(lock_tokens=[lock_token])) + assert len(ack.succeeded_lock_tokens) == 1 + assert len(ack.failed_lock_tokens) == 0 + + events = client.receive_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name, max_events=1) + assert events.value[0].broker_properties.delivery_count > 1 \ No newline at end of file diff --git a/sdk/eventgrid/azure-eventgrid/tests/test_eg_client_exceptions.py b/sdk/eventgrid/azure-eventgrid/tests/test_eg_client_exceptions.py index e385f206653e..7f5f8ffa4a91 100644 --- a/sdk/eventgrid/azure-eventgrid/tests/test_eg_client_exceptions.py +++ b/sdk/eventgrid/azure-eventgrid/tests/test_eg_client_exceptions.py @@ -6,27 +6,27 @@ import pytest import os import time +from devtools_testutils import AzureRecordedTestCase, recorded_by_proxy from azure.eventgrid import EventGridClient from azure.eventgrid.models import * from azure.core.messaging import CloudEvent from azure.core.credentials import AzureKeyCredential from azure.core.exceptions import HttpResponseError, ResourceNotFoundError +from eventgrid_preparer import EventGridPreparer -class TestEGClientExceptions: - def create_eg_client(self, endpoint): - eventgrid_key = os.environ["EVENTGRID_KEY"] + +class TestEGClientExceptions(AzureRecordedTestCase): + def create_eg_client(self, endpoint, key): client = EventGridClient( - endpoint=endpoint, credential=AzureKeyCredential(eventgrid_key) + endpoint=endpoint, credential=AzureKeyCredential(key) ) return client - @pytest.mark.live_test_only - def test_publish_cloud_event_bad_request(self): - eventgrid_endpoint = os.environ["EVENTGRID_ENDPOINT"] - topic_name = os.environ["TOPIC_NAME"] - - client = self.create_eg_client(eventgrid_endpoint) + @EventGridPreparer() + @recorded_by_proxy + def test_publish_cloud_event_bad_request(self, eventgrid_endpoint, eventgrid_key): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) event = CloudEvent( type="Contoso.Items.ItemReceived", source=None, @@ -35,13 +35,12 @@ def test_publish_cloud_event_bad_request(self): ) with pytest.raises(HttpResponseError): - client.publish_cloud_events(topic_name, [event]) + client.publish_cloud_events("testtopic1", [event]) - @pytest.mark.live_test_only - def test_publish_cloud_event_not_found(self): - eventgrid_endpoint = os.environ["EVENTGRID_ENDPOINT"] - - client = self.create_eg_client(eventgrid_endpoint) + @EventGridPreparer() + @recorded_by_proxy + def test_publish_cloud_event_not_found(self, eventgrid_endpoint, eventgrid_key): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) event = CloudEvent( type="Contoso.Items.ItemReceived", source=None, @@ -52,152 +51,123 @@ def test_publish_cloud_event_not_found(self): with pytest.raises(ResourceNotFoundError): client.publish_cloud_events("faketopic", [event]) - @pytest.mark.live_test_only - def test_receive_cloud_event_not_found(self): - eventgrid_endpoint = os.environ["EVENTGRID_ENDPOINT"] - event_subscription_name = os.environ["EVENT_SUBSCRIPTION_NAME"] - - client = self.create_eg_client(eventgrid_endpoint) + @EventGridPreparer() + @recorded_by_proxy + def test_receive_cloud_event_not_found(self, eventgrid_endpoint, eventgrid_key, eventgrid_event_subscription_name): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) with pytest.raises(ResourceNotFoundError): - client.receive_cloud_events("faketopic", event_subscription_name) + client.receive_cloud_events("faketopic", eventgrid_event_subscription_name) - @pytest.mark.live_test_only - def test_receive_cloud_event_max_events_negative(self): - eventgrid_endpoint = os.environ["EVENTGRID_ENDPOINT"] - topic_name = os.environ["TOPIC_NAME"] - event_subscription_name = os.environ["EVENT_SUBSCRIPTION_NAME"] - - client = self.create_eg_client(eventgrid_endpoint) + @EventGridPreparer() + @recorded_by_proxy + def test_receive_cloud_event_max_events_negative(self, eventgrid_endpoint, eventgrid_key, eventgrid_topic_name, eventgrid_event_subscription_name): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) with pytest.raises(HttpResponseError): client.receive_cloud_events( - topic_name, event_subscription_name, max_events=-20 + eventgrid_topic_name, eventgrid_event_subscription_name, max_events=-20 ) - @pytest.mark.live_test_only - def test_receive_cloud_event_timeout_negative(self): - eventgrid_endpoint = os.environ["EVENTGRID_ENDPOINT"] - topic_name = os.environ["TOPIC_NAME"] - event_subscription_name = os.environ["EVENT_SUBSCRIPTION_NAME"] - - client = self.create_eg_client(eventgrid_endpoint) + @EventGridPreparer() + @recorded_by_proxy + def test_receive_cloud_event_timeout_negative(self, eventgrid_endpoint, eventgrid_key, eventgrid_topic_name, eventgrid_event_subscription_name): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) with pytest.raises(HttpResponseError): client.receive_cloud_events( - topic_name, event_subscription_name, max_wait_time=-20 + eventgrid_topic_name, eventgrid_event_subscription_name, max_wait_time=-20 ) - @pytest.mark.live_test_only - def test_receive_cloud_event_timeout_max_value(self): - eventgrid_endpoint = os.environ["EVENTGRID_ENDPOINT"] - topic_name = os.environ["TOPIC_NAME"] - event_subscription_name = os.environ["EVENT_SUBSCRIPTION_NAME"] - - client = self.create_eg_client(eventgrid_endpoint) + @EventGridPreparer() + @recorded_by_proxy + def test_receive_cloud_event_timeout_max_value(self, eventgrid_endpoint, eventgrid_key, eventgrid_topic_name, eventgrid_event_subscription_name): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) with pytest.raises(HttpResponseError): client.receive_cloud_events( - topic_name, event_subscription_name, max_wait_time=121 + eventgrid_topic_name, eventgrid_event_subscription_name, max_wait_time=121 ) - @pytest.mark.live_test_only - def test_receive_cloud_event_timeout_min_value(self): - eventgrid_endpoint = os.environ["EVENTGRID_ENDPOINT"] - topic_name = os.environ["TOPIC_NAME"] - event_subscription_name = os.environ["EVENT_SUBSCRIPTION_NAME"] - - client = self.create_eg_client(eventgrid_endpoint) + @EventGridPreparer() + @recorded_by_proxy + def test_receive_cloud_event_timeout_min_value(self, eventgrid_endpoint, eventgrid_key, eventgrid_topic_name, eventgrid_event_subscription_name): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) with pytest.raises(HttpResponseError): client.receive_cloud_events( - topic_name, event_subscription_name, max_wait_time=9 + eventgrid_topic_name, eventgrid_event_subscription_name, max_wait_time=9 ) - @pytest.mark.live_test_only - def test_acknowledge_cloud_event_not_found(self): - eventgrid_endpoint = os.environ["EVENTGRID_ENDPOINT"] - event_subscription_name = os.environ["EVENT_SUBSCRIPTION_NAME"] - - client = self.create_eg_client(eventgrid_endpoint) + @EventGridPreparer() + @recorded_by_proxy + def test_acknowledge_cloud_event_not_found(self, eventgrid_endpoint, eventgrid_key, eventgrid_event_subscription_name): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) with pytest.raises(ResourceNotFoundError): lock_tokens = AcknowledgeOptions(lock_tokens=["faketoken"]) client.acknowledge_cloud_events( - "faketopic", event_subscription_name, lock_tokens=lock_tokens + "faketopic", eventgrid_event_subscription_name, lock_tokens=lock_tokens ) - @pytest.mark.live_test_only - def test_release_cloud_event_not_found(self): - eventgrid_endpoint = os.environ["EVENTGRID_ENDPOINT"] - event_subscription_name = os.environ["EVENT_SUBSCRIPTION_NAME"] - - client = self.create_eg_client(eventgrid_endpoint) + @EventGridPreparer() + @recorded_by_proxy + def test_release_cloud_event_not_found(self, eventgrid_endpoint, eventgrid_key, eventgrid_event_subscription_name): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) with pytest.raises(ResourceNotFoundError): lock_tokens = ReleaseOptions(lock_tokens=["faketoken"]) client.release_cloud_events( - "faketopic", event_subscription_name, lock_tokens=lock_tokens + "faketopic", eventgrid_event_subscription_name, lock_tokens=lock_tokens ) - @pytest.mark.live_test_only - def test_reject_cloud_event_not_found(self): - eventgrid_endpoint = os.environ["EVENTGRID_ENDPOINT"] - event_subscription_name = os.environ["EVENT_SUBSCRIPTION_NAME"] - - client = self.create_eg_client(eventgrid_endpoint) + @EventGridPreparer() + @recorded_by_proxy + def test_reject_cloud_event_not_found(self, eventgrid_endpoint, eventgrid_key, eventgrid_event_subscription_name): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) lock_tokens = RejectOptions(lock_tokens=["faketoken"]) with pytest.raises(ResourceNotFoundError): client.reject_cloud_events( - "faketopic", event_subscription_name, lock_tokens=lock_tokens + "faketopic", eventgrid_event_subscription_name, lock_tokens=lock_tokens ) - @pytest.mark.live_test_only - def test_acknowledge_cloud_event_invalid_token(self): - eventgrid_endpoint = os.environ["EVENTGRID_ENDPOINT"] - topic_name = os.environ["TOPIC_NAME"] - event_subscription_name = os.environ["EVENT_SUBSCRIPTION_NAME"] - - client = self.create_eg_client(eventgrid_endpoint) + @EventGridPreparer() + @recorded_by_proxy + def test_acknowledge_cloud_event_invalid_token(self, eventgrid_endpoint, eventgrid_key, eventgrid_topic_name, eventgrid_event_subscription_name): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) lock_tokens = AcknowledgeOptions(lock_tokens=["faketoken"]) ack = client.acknowledge_cloud_events( - topic_name, event_subscription_name, lock_tokens=lock_tokens + eventgrid_topic_name, eventgrid_event_subscription_name, lock_tokens=lock_tokens ) assert type(ack) == AcknowledgeResult assert ack.succeeded_lock_tokens == [] assert type(ack.failed_lock_tokens[0]) == FailedLockToken assert ack.failed_lock_tokens[0].lock_token == "faketoken" - @pytest.mark.live_test_only - def test_release_cloud_event_invalid_token(self): - eventgrid_endpoint = os.environ["EVENTGRID_ENDPOINT"] - topic_name = os.environ["TOPIC_NAME"] - event_subscription_name = os.environ["EVENT_SUBSCRIPTION_NAME"] - - client = self.create_eg_client(eventgrid_endpoint) + @EventGridPreparer() + @recorded_by_proxy + def test_release_cloud_event_invalid_token(self, eventgrid_endpoint, eventgrid_key, eventgrid_topic_name, eventgrid_event_subscription_name): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) lock_tokens = ReleaseOptions(lock_tokens=["faketoken"]) release = client.release_cloud_events( - topic_name, event_subscription_name, lock_tokens=lock_tokens + eventgrid_topic_name, eventgrid_event_subscription_name, lock_tokens=lock_tokens ) assert type(release) == ReleaseResult assert release.succeeded_lock_tokens == [] assert type(release.failed_lock_tokens[0]) == FailedLockToken assert release.failed_lock_tokens[0].lock_token == "faketoken" - @pytest.mark.live_test_only - def test_reject_cloud_event_invalid_token(self): - eventgrid_endpoint = os.environ["EVENTGRID_ENDPOINT"] - topic_name = os.environ["TOPIC_NAME"] - event_subscription_name = os.environ["EVENT_SUBSCRIPTION_NAME"] - - client = self.create_eg_client(eventgrid_endpoint) + @EventGridPreparer() + @recorded_by_proxy + def test_reject_cloud_event_invalid_token(self, eventgrid_endpoint, eventgrid_key, eventgrid_topic_name, eventgrid_event_subscription_name): + client = self.create_eg_client(eventgrid_endpoint, eventgrid_key) lock_tokens = RejectOptions(lock_tokens=["faketoken"]) reject = client.reject_cloud_events( - topic_name, event_subscription_name, lock_tokens=lock_tokens + eventgrid_topic_name, eventgrid_event_subscription_name, lock_tokens=lock_tokens ) assert type(reject) == RejectResult assert reject.succeeded_lock_tokens == [] diff --git a/sdk/eventgrid/azure-eventgrid/tests/test_eg_publisher_client.py b/sdk/eventgrid/azure-eventgrid/tests/test_eg_publisher_client.py index 47c49d74bbc1..9133c1b74e4a 100644 --- a/sdk/eventgrid/azure-eventgrid/tests/test_eg_publisher_client.py +++ b/sdk/eventgrid/azure-eventgrid/tests/test_eg_publisher_client.py @@ -55,7 +55,7 @@ def test_send_event_grid_event_data_dict(self, variables, eventgrid_topic_endpoi @EventGridPreparer() @recorded_by_proxy def test_send_event_grid_event_fails_without_full_url( - self, variables, eventgrid_topic_key, eventgrid_topic_endpoint + self, eventgrid_topic_key, eventgrid_topic_endpoint ): akc_credential = AzureKeyCredential(eventgrid_topic_key) parsed_url = urlparse(eventgrid_topic_endpoint) @@ -104,7 +104,7 @@ def test_send_event_grid_event_data_str(self, variables, eventgrid_topic_endpoin @EventGridPreparer() @recorded_by_proxy def test_send_event_grid_event_data_bytes( - self, variables, eventgrid_topic_endpoint + self, eventgrid_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_topic_endpoint) eg_event = EventGridEvent( @@ -119,7 +119,7 @@ def test_send_event_grid_event_data_bytes( @EventGridPreparer() @recorded_by_proxy def test_send_event_grid_event_dict_data_bytes( - self, variables, eventgrid_topic_endpoint + self, eventgrid_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_topic_endpoint) eg_event = { @@ -154,7 +154,7 @@ def test_send_event_grid_event_dict_data_dict( @EventGridPreparer() @recorded_by_proxy def test_send_cloud_event_data_dict( - self, variables, eventgrid_cloud_event_topic_endpoint + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -168,7 +168,7 @@ def test_send_cloud_event_data_dict( @EventGridPreparer() @recorded_by_proxy def test_send_cloud_event_data_NULL( - self, variables, eventgrid_cloud_event_topic_endpoint + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -184,7 +184,7 @@ def callback(request): @EventGridPreparer() @recorded_by_proxy def test_send_cloud_event_data_base64_using_data( - self, variables, eventgrid_cloud_event_topic_endpoint + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -214,7 +214,7 @@ def test_send_cloud_event_fails_on_providing_data_and_b64(self): @EventGridPreparer() @recorded_by_proxy def test_send_cloud_event_data_none( - self, variables, eventgrid_cloud_event_topic_endpoint + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -225,7 +225,7 @@ def test_send_cloud_event_data_none( @EventGridPreparer() @recorded_by_proxy def test_send_cloud_event_data_str( - self, variables, eventgrid_cloud_event_topic_endpoint + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -238,7 +238,7 @@ def test_send_cloud_event_data_str( @EventGridPreparer() @recorded_by_proxy def test_send_cloud_event_data_bytes( - self, variables, eventgrid_cloud_event_topic_endpoint + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -251,7 +251,7 @@ def test_send_cloud_event_data_bytes( @EventGridPreparer() @recorded_by_proxy def test_send_cloud_event_data_as_list( - self, variables, eventgrid_cloud_event_topic_endpoint + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -264,7 +264,7 @@ def test_send_cloud_event_data_as_list( @EventGridPreparer() @recorded_by_proxy def test_send_cloud_event_data_with_extensions( - self, variables, eventgrid_cloud_event_topic_endpoint + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -282,7 +282,7 @@ def test_send_cloud_event_data_with_extensions( @EventGridPreparer() @recorded_by_proxy def test_send_cloud_event_dict( - self, variables, eventgrid_cloud_event_topic_endpoint + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event1 = { @@ -315,7 +315,7 @@ def test_send_signature_credential( @EventGridPreparer() @recorded_by_proxy - def test_send_NONE_credential(self, variables, eventgrid_topic_endpoint): + def test_send_NONE_credential(self, eventgrid_topic_endpoint): with pytest.raises( ValueError, match="Parameter 'self._credential' must not be None." ): @@ -324,7 +324,7 @@ def test_send_NONE_credential(self, variables, eventgrid_topic_endpoint): @EventGridPreparer() @recorded_by_proxy def test_send_custom_schema_event( - self, variables, eventgrid_custom_event_topic_endpoint + self, eventgrid_custom_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_custom_event_topic_endpoint) custom_event = { @@ -340,7 +340,7 @@ def test_send_custom_schema_event( @EventGridPreparer() @recorded_by_proxy def test_send_custom_schema_event_as_list( - self, variables, eventgrid_custom_event_topic_endpoint + self, eventgrid_custom_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_custom_event_topic_endpoint) custom_event1 = { @@ -372,7 +372,7 @@ def test_send_throws_with_bad_credential(self): @pytest.mark.live_test_only @EventGridPreparer() @recorded_by_proxy - def test_send_token_credential(self, variables, eventgrid_topic_endpoint): + def test_send_token_credential(self, eventgrid_topic_endpoint): credential = self.get_credential(EventGridPublisherClient) client = EventGridPublisherClient(eventgrid_topic_endpoint, credential) eg_event = EventGridEvent( @@ -386,17 +386,10 @@ def test_send_token_credential(self, variables, eventgrid_topic_endpoint): @pytest.mark.live_test_only @EventGridPreparer() @recorded_by_proxy - def test_send_partner_namespace(self, variables): - eventgrid_partner_namespace_endpoint = os.environ[ - "EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT" - ] - eventgrid_partner_namespace_key = os.environ[ - "EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY" - ] - channel_name = os.environ["EVENTGRID_PARTNER_CHANNEL_NAME"] - credential = AzureKeyCredential(eventgrid_partner_namespace_key) + def test_send_partner_namespace(self, eventgrid_partner_namespace_topic_endpoint, eventgrid_partner_namespace_topic_key, eventgrid_partner_channel_name): + credential = AzureKeyCredential(eventgrid_partner_namespace_topic_key) client = EventGridPublisherClient( - eventgrid_partner_namespace_endpoint, eventgrid_partner_namespace_key + eventgrid_partner_namespace_topic_endpoint, credential ) cloud_event = CloudEvent( source="http://samplesource.dev", @@ -405,7 +398,7 @@ def test_send_partner_namespace(self, variables): ) def callback(request): - req = json.loads(request.http_request.headers) - assert req.get("aeg-channel-name") == channel_name + req = request.http_request.headers + assert req.get("aeg-channel-name") == eventgrid_partner_channel_name - client.send(cloud_event, channel_name=channel_name, raw_request_hook=callback) + client.send(cloud_event, channel_name=eventgrid_partner_channel_name, raw_request_hook=callback) diff --git a/sdk/eventgrid/azure-eventgrid/tests/test_eg_publisher_client_async.py b/sdk/eventgrid/azure-eventgrid/tests/test_eg_publisher_client_async.py index 246eb4f72c00..c0a7e93be95a 100644 --- a/sdk/eventgrid/azure-eventgrid/tests/test_eg_publisher_client_async.py +++ b/sdk/eventgrid/azure-eventgrid/tests/test_eg_publisher_client_async.py @@ -39,7 +39,7 @@ def create_eg_publisher_client(self, endpoint): @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_event_grid_event_data_dict( + async def test_send_event_grid_event_data_dict_async( self, variables, eventgrid_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_topic_endpoint) @@ -54,7 +54,7 @@ async def test_send_event_grid_event_data_dict( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_event_grid_event_data_as_list( + async def test_send_event_grid_event_data_as_list_async( self, variables, eventgrid_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_topic_endpoint) @@ -75,8 +75,8 @@ async def test_send_event_grid_event_data_as_list( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_event_grid_event_fails_without_full_url( - self, variables, eventgrid_topic_key, eventgrid_topic_endpoint + async def test_send_event_grid_event_fails_without_full_url_async( + self, eventgrid_topic_key, eventgrid_topic_endpoint ): akc_credential = AzureKeyCredential(eventgrid_topic_key) parsed_url = urlparse(eventgrid_topic_endpoint) @@ -93,7 +93,7 @@ async def test_send_event_grid_event_fails_without_full_url( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_event_grid_event_data_str( + async def test_send_event_grid_event_data_str_async( self, variables, eventgrid_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_topic_endpoint) @@ -108,8 +108,8 @@ async def test_send_event_grid_event_data_str( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_event_grid_event_data_bytes( - self, variables, eventgrid_topic_endpoint + async def test_send_event_grid_event_data_bytes_async( + self, eventgrid_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_topic_endpoint) eg_event = EventGridEvent( @@ -124,8 +124,8 @@ async def test_send_event_grid_event_data_bytes( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_event_grid_event_dict_data_bytes( - self, variables, eventgrid_topic_endpoint + async def test_send_event_grid_event_dict_data_bytes_async( + self, eventgrid_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_topic_endpoint) eg_event = { @@ -142,8 +142,8 @@ async def test_send_event_grid_event_dict_data_bytes( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_data_dict( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cloud_event_data_dict_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -156,8 +156,8 @@ async def test_send_cloud_event_data_dict( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_data_str( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cloud_event_data_str_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -170,8 +170,8 @@ async def test_send_cloud_event_data_str( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_data_bytes( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cloud_event_data_bytes_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -184,8 +184,8 @@ async def test_send_cloud_event_data_bytes( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_data_as_list( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cloud_event_data_as_list_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -198,8 +198,8 @@ async def test_send_cloud_event_data_as_list( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_data_with_extensions( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cloud_event_data_with_extensions_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -217,8 +217,8 @@ async def test_send_cloud_event_data_with_extensions( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_dict( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cloud_event_dict_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event1 = { @@ -233,8 +233,8 @@ async def test_send_cloud_event_dict( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_data_none( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cloud_event_data_none_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -246,8 +246,8 @@ async def test_send_cloud_event_data_none( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_cloud_event_data_NULL( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_cloud_event_data_NULL_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) cloud_event = CloudEvent( @@ -263,7 +263,7 @@ def callback(request): @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_signature_credential( + async def test_send_signature_credential_async( self, variables, eventgrid_topic_key, eventgrid_topic_endpoint ): expiration_date_utc = dt.datetime.now(UTC()) + timedelta(hours=1) @@ -283,8 +283,8 @@ async def test_send_signature_credential( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_custom_schema_event( - self, variables, eventgrid_custom_event_topic_endpoint + async def test_send_custom_schema_event_async( + self, eventgrid_custom_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_custom_event_topic_endpoint) custom_event = { @@ -300,8 +300,8 @@ async def test_send_custom_schema_event( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_custom_schema_event_as_list( - self, variables, eventgrid_custom_event_topic_endpoint + async def test_send_custom_schema_event_as_list_async( + self, eventgrid_custom_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_custom_event_topic_endpoint) custom_event1 = { @@ -325,8 +325,8 @@ async def test_send_custom_schema_event_as_list( @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_and_close_async_session( - self, variables, eventgrid_cloud_event_topic_endpoint + async def test_send_and_close_async_session_async( + self, eventgrid_cloud_event_topic_endpoint ): client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint) async with client: # this throws if client can't close @@ -340,7 +340,7 @@ async def test_send_and_close_async_session( @pytest.mark.skip() @EventGridPreparer() @recorded_by_proxy_async - def test_send_NONE_credential_async(self, variables, eventgrid_topic_endpoint): + def test_send_NONE_credential_async(self, eventgrid_topic_endpoint): with pytest.raises( ValueError, match="Parameter 'self._credential' must not be None." ): @@ -350,7 +350,7 @@ def test_send_NONE_credential_async(self, variables, eventgrid_topic_endpoint): @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_send_token_credential(self, variables, eventgrid_topic_endpoint): + async def test_send_token_credential_async(self, eventgrid_topic_endpoint): credential = self.get_credential(EventGridPublisherClient) client = EventGridPublisherClient(eventgrid_topic_endpoint, credential) eg_event = EventGridEvent( @@ -364,17 +364,10 @@ async def test_send_token_credential(self, variables, eventgrid_topic_endpoint): @pytest.mark.live_test_only @EventGridPreparer() @recorded_by_proxy_async - async def test_send_partner_namespace(self, variables): - eventgrid_partner_namespace_endpoint = os.environ[ - "EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT" - ] - eventgrid_partner_namespace_key = os.environ[ - "EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY" - ] - channel_name = os.environ["EVENTGRID_PARTNER_CHANNEL_NAME"] - credential = AzureKeyCredential(eventgrid_partner_namespace_key) + async def test_send_partner_namespace_async(self, eventgrid_partner_namespace_topic_endpoint, eventgrid_partner_namespace_topic_key, eventgrid_partner_channel_name): + credential = AzureKeyCredential(eventgrid_partner_namespace_topic_key) client = EventGridPublisherClient( - eventgrid_partner_namespace_endpoint, eventgrid_partner_namespace_key + eventgrid_partner_namespace_topic_endpoint, credential ) cloud_event = CloudEvent( source="http://samplesource.dev", @@ -383,9 +376,9 @@ async def test_send_partner_namespace(self, variables): ) def callback(request): - req = json.loads(request.http_request.headers) - assert req.get("aeg-channel-name") == channel_name + req = request.http_request.headers + assert req.get("aeg-channel-name") == eventgrid_partner_channel_name await client.send( - cloud_event, channel_name=channel_name, raw_request_hook=callback + cloud_event, channel_name=eventgrid_partner_channel_name, raw_request_hook=callback ) diff --git a/sdk/eventgrid/azure-eventgrid/tests/test_exceptions.py b/sdk/eventgrid/azure-eventgrid/tests/test_exceptions.py index 007779455832..7253ebc28323 100644 --- a/sdk/eventgrid/azure-eventgrid/tests/test_exceptions.py +++ b/sdk/eventgrid/azure-eventgrid/tests/test_exceptions.py @@ -26,12 +26,9 @@ from devtools_testutils import AzureMgmtRecordedTestCase, recorded_by_proxy -from azure_devtools.scenario_tests import ReplayableTest -from azure.core.credentials import AzureKeyCredential, AzureSasCredential +from azure.core.credentials import AzureKeyCredential +from azure.eventgrid import EventGridPublisherClient, EventGridEvent from azure.core.messaging import CloudEvent -from azure.core.serialization import NULL -from azure.eventgrid import EventGridPublisherClient, EventGridEvent, generate_sas -from azure.eventgrid._legacy._helpers import _cloud_event_to_generated from eventgrid_preparer import ( EventGridPreparer, @@ -63,19 +60,20 @@ def test_raise_on_auth_error(self, variables, eventgrid_topic_endpoint): ): client.send(eg_event) + @pytest.mark.skip("TestProxy recording gives ServiceRequestError") @EventGridPreparer() @recorded_by_proxy - def test_raise_on_bad_resource(self, variables, eventgrid_topic_key): - akc_credential = AzureKeyCredential(eventgrid_topic_key) + def test_raise_on_bad_resource(self): + credential = self.get_credential(EventGridPublisherClient) client = EventGridPublisherClient( - "https://bad-resource.westus-1.eventgrid.azure.net/api/events", - akc_credential, + "https://bad-resource.eastus-1.eventgrid.azure.net/api/events", + credential, ) - eg_event = EventGridEvent( + eg_event = CloudEvent( subject="sample", data={"sample": "eventgridevent"}, - event_type="Sample.EventGrid.Event", - data_version="2.0", + source="source", + type="Sample.Cloud.Event", ) with pytest.raises(HttpResponseError): client.send(eg_event) diff --git a/sdk/eventgrid/azure-eventgrid/tests/test_exceptions_async.py b/sdk/eventgrid/azure-eventgrid/tests/test_exceptions_async.py index 4065ef0c745f..30a99652376e 100644 --- a/sdk/eventgrid/azure-eventgrid/tests/test_exceptions_async.py +++ b/sdk/eventgrid/azure-eventgrid/tests/test_exceptions_async.py @@ -66,14 +66,15 @@ async def test_raise_on_auth_error(self, variables, eventgrid_topic_endpoint): ): await client.send(eg_event) + @pytest.mark.skip("TestProxy recording gives ServiceRequestError") @EventGridPreparer() @recorded_by_proxy_async @pytest.mark.asyncio - async def test_raise_on_bad_resource(self, variables, eventgrid_topic_key): - akc_credential = AzureKeyCredential(eventgrid_topic_key) + async def test_raise_on_bad_resource(self): + credential = self.get_credential(EventGridPublisherClient, is_async=True) client = EventGridPublisherClient( - "https://bad-resource.westus-1.eventgrid.azure.net/api/events", - akc_credential, + "https://bad-resource.eastus-1.eventgrid.azure.net/api/events", + credential, ) eg_event = EventGridEvent( subject="sample", diff --git a/sdk/eventgrid/test-resources.json b/sdk/eventgrid/test-resources.json index 88e3eab75b9d..e41dbdec3be1 100644 --- a/sdk/eventgrid/test-resources.json +++ b/sdk/eventgrid/test-resources.json @@ -26,9 +26,19 @@ "metadata": { "description": "The application client secret used to run tests." } + }, + "location": { + "type": "string", + "defaultValue": "eastus", + "metadata": { + "description": "The resource location" + } } }, "variables": { + "namespaceName": "[format('{0}-2', parameters('baseName'))]", + "topicName": "testtopic1", + "subscriptionName": "testsubscription1", "apiVersion": "2022-06-15", "eventGridTopicName": "[concat(parameters('baseName'), 'topic')]", "eventGridDomainName": "[concat(parameters('baseName'), 'domain')]", @@ -43,6 +53,55 @@ "eventGridDataSenderRoleId": "d5a91429-5739-47e2-a06b-3470a27159e7", }, "resources": [ + { + "type": "Microsoft.EventGrid/namespaces", + "apiVersion": "2023-06-01-preview", + "name": "[variables('namespaceName')]", + "location": "[parameters('location')]", + "sku": { + "name": "Standard", + "capacity": 1 + }, + "properties": { + "isZoneRedundant": true, + "publicNetworkAccess": "Enabled" + } + }, + { + "type": "Microsoft.EventGrid/namespaces/topics", + "apiVersion": "2023-06-01-preview", + "name": "[format('{0}/{1}', variables('namespaceName'), variables('topicName'))]", + "properties": { + "publisherType": "Custom", + "inputSchema": "CloudEventSchemaV1_0", + "eventRetentionInDays": 1 + }, + "dependsOn": [ + "[resourceId('Microsoft.EventGrid/namespaces', variables('namespaceName'))]" + ] + }, + { + "type": "Microsoft.EventGrid/namespaces/topics/eventSubscriptions", + "apiVersion": "2023-06-01-preview", + "name": "[format('{0}/{1}/{2}', variables('namespaceName'), variables('topicName'), variables('subscriptionName'))]", + "properties": { + "deliveryConfiguration": { + "deliveryMode": "Queue", + "queue": { + "receiveLockDurationInSeconds": 60, + "maxDeliveryCount": 10, + "eventTimeToLive": "P1D" + } + }, + "eventDeliverySchema": "CloudEventSchemaV1_0", + "filtersConfiguration": { + "includedEventTypes": [] + } + }, + "dependsOn": [ + "[resourceId('Microsoft.EventGrid/namespaces/topics', variables('namespaceName'), variables('topicName'))]" + ] + }, { "type": "Microsoft.EventGrid/topics", "apiVersion": "[variables('apiVersion')]", @@ -227,6 +286,30 @@ "EVENTGRID_PARTNER_CHANNEL_NAME": { "type": "string", "value": "[variables('partnerChannelName')]" + }, + "EVENTGRID_KEY": { + "type": "string", + "value": "[listKeys(resourceId('Microsoft.EventGrid/namespaces', variables('namespaceName')), '2023-06-01-preview').key1]" + }, + "EVENTGRID_ENDPOINT": { + "type": "string", + "value": "[format('https://{0}', reference(resourceId('Microsoft.EventGrid/namespaces', variables('namespaceName')), '2023-06-01-preview').topicsConfiguration.hostname)]" + }, + "EVENTGRID_TOPIC_NAME": { + "type": "string", + "value": "[variables('topicName')]" + }, + "EVENTGRID_EVENT_SUBSCRIPTION_NAME": { + "type": "string", + "value": "[variables('subscriptionName')]" + }, + "RESOURCE_GROUP": { + "type": "string", + "value": "[resourceGroup().name]" + }, + "AZURE_SUBSCRIPTION_ID": { + "type": "string", + "value": "[subscription().subscriptionId]" } } }