Skip to content

Commit

Permalink
[EG] Update tests (#35752)
Browse files Browse the repository at this point in the history
* test

* typo

* update recordings

* mark live

* kwarg fix

* updates

* revert

* kwargs

* continue skip
  • Loading branch information
l0lawrence authored May 24, 2024
1 parent 3a7af26 commit 2565a94
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 79 deletions.
2 changes: 1 addition & 1 deletion sdk/eventgrid/azure-eventgrid/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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_3ae14bceff"
}
2 changes: 1 addition & 1 deletion sdk/eventgrid/azure-eventgrid/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ def add_aeg_sanitizer(test_proxy):
add_remove_header_sanitizer(headers="aeg-sas-key, aeg-sas-token")
add_general_regex_sanitizer(
value="fakeresource",
regex="(?<=\\/\\/)[a-z-]+(?=\\.westus2-1\\.eventgrid\\.azure\\.net/api/events)"
regex="(?<=\\/\\/)[a-z-]+(?=\\.eastus-1\\.eventgrid\\.azure\\.net/api/events)"
)
19 changes: 10 additions & 9 deletions sdk/eventgrid/azure-eventgrid/tests/eventgrid_preparer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import functools
from devtools_testutils import PowerShellPreparer
from devtools_testutils import EnvironmentVariableLoader

from azure.mgmt.eventgrid.models import Topic, InputSchema, JsonInputSchemaMapping, JsonField, JsonFieldWithDefault

from devtools_testutils.resource_testcase import RESOURCE_GROUP_PARAM

EVENTGRID_TOPIC_PARAM = 'eventgrid_topic'
EVENTGRID_TOPIC_LOCATION = 'westus'
CLOUD_EVENT_SCHEMA = InputSchema.cloud_event_schema_v1_0
Expand All @@ -18,15 +16,18 @@
CUSTOM_JSON_INPUT_SCHEMA_MAPPING = JsonInputSchemaMapping(id=ID_JSON_FIELD, topic=TOPIC_JSON_FIELD, event_time=EVENT_TIME_JSON_FIELD, event_type=EVENT_TYPE_JSON_FIELD_WITH_DEFAULT, subject=SUBJECT_JSON_FIELD_WITH_DEFAULT, data_version=DATA_VERSION_JSON_FIELD_WITH_DEFAULT)

EventGridPreparer = functools.partial(
PowerShellPreparer, "eventgrid",
eventgrid_topic_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events",
EnvironmentVariableLoader, "eventgrid",
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_namespace_topic_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
eventgrid_partner_namespace_topic_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
eventgrid_partner_channel_name="fake_channel_name"
)
1 change: 0 additions & 1 deletion sdk/eventgrid/azure-eventgrid/tests/test_cncf_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
from devtools_testutils import AzureRecordedTestCase, recorded_by_proxy

from azure.core.credentials import AzureKeyCredential, AzureSasCredential
from azure.eventgrid import EventGridPublisherClient
from cloudevents.http import CloudEvent

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

import json
import pytest
from devtools_testutils import AzureRecordedTestCase, CachedResourceGroupPreparer
from devtools_testutils import AzureRecordedTestCase
from devtools_testutils.aio import recorded_by_proxy_async

from azure.core.credentials import AzureKeyCredential, AzureSasCredential
from azure.eventgrid.aio import EventGridPublisherClient
from cloudevents.http import CloudEvent

Expand Down
28 changes: 14 additions & 14 deletions sdk/eventgrid/azure-eventgrid/tests/test_eg_publisher_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from devtools_testutils import AzureRecordedTestCase, recorded_by_proxy

from azure.core.credentials import AzureKeyCredential, AzureSasCredential
from azure.core.credentials import AzureSasCredential
from azure.core.messaging import CloudEvent
from azure.core.serialization import NULL
from azure.eventgrid import EventGridPublisherClient, EventGridEvent, generate_sas
Expand Down Expand Up @@ -51,10 +51,10 @@ def test_send_event_grid_event_data_dict(self, eventgrid_topic_endpoint):

@EventGridPreparer()
@recorded_by_proxy
def test_send_event_grid_event_fails_without_full_url(self, eventgrid_topic_key, eventgrid_topic_endpoint):
akc_credential = AzureKeyCredential(eventgrid_topic_key)
def test_send_event_grid_event_fails_without_full_url(self,eventgrid_topic_endpoint):
credential = self.get_credential(EventGridPublisherClient)
parsed_url = urlparse(eventgrid_topic_endpoint)
client = EventGridPublisherClient(parsed_url.netloc, akc_credential)
client = EventGridPublisherClient(parsed_url.netloc, credential)
eg_event = EventGridEvent(
subject="sample",
data={"sample": "eventgridevent"},
Expand Down Expand Up @@ -269,9 +269,11 @@ def test_send_cloud_event_dict(self, eventgrid_cloud_event_topic_endpoint):
}
client.send(cloud_event1)

@pytest.mark.live_test_only
@EventGridPreparer()
@recorded_by_proxy
def test_send_signature_credential(self, eventgrid_topic_key, eventgrid_topic_endpoint):
def test_send_signature_credential(self, **kwargs):
eventgrid_topic_endpoint = kwargs.pop("eventgrid_topic_endpoint")
eventgrid_topic_key = kwargs.pop("eventgrid_topic_key")
expiration_date_utc = dt.datetime.now(UTC()) + timedelta(hours=1)
signature = generate_sas(eventgrid_topic_endpoint, eventgrid_topic_key, expiration_date_utc)
credential = AzureSasCredential(signature)
Expand Down Expand Up @@ -346,12 +348,10 @@ def test_send_token_credential(self, eventgrid_topic_endpoint):
client.send(eg_event)

@pytest.mark.live_test_only
def test_send_partner_namespace(self):
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)
client = EventGridPublisherClient(eventgrid_partner_namespace_endpoint, credential)
@EventGridPreparer()
@recorded_by_proxy
def test_send_partner_namespace(self, eventgrid_partner_namespace_topic_endpoint, eventgrid_partner_channel_name):
client = self.create_eg_publisher_client(eventgrid_partner_namespace_topic_endpoint)
cloud_event = CloudEvent(
source = "http://samplesource.dev",
data = "cloudevent",
Expand All @@ -360,6 +360,6 @@ def test_send_partner_namespace(self):

def callback(request):
req = request.http_request.headers
assert req.get("aeg-channel-name") == channel_name
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)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from devtools_testutils import AzureRecordedTestCase
from devtools_testutils.aio import recorded_by_proxy_async

from azure.core.credentials import AzureKeyCredential, AzureSasCredential
from azure.core.credentials import AzureSasCredential
from azure.core.messaging import CloudEvent
from azure.core.serialization import NULL
from azure.eventgrid import EventGridEvent, generate_sas
Expand Down Expand Up @@ -72,10 +72,10 @@ async def test_send_event_grid_event_data_as_list(self, eventgrid_topic_endpoint
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_event_grid_event_fails_without_full_url(self, eventgrid_topic_key, eventgrid_topic_endpoint):
akc_credential = AzureKeyCredential(eventgrid_topic_key)
async def test_send_event_grid_event_fails_without_full_url(self, eventgrid_topic_endpoint):
credential = self.get_credential(EventGridPublisherClient, is_async=True)
parsed_url = urlparse(eventgrid_topic_endpoint)
client = EventGridPublisherClient(parsed_url.netloc, akc_credential)
client = EventGridPublisherClient(parsed_url.netloc, credential)
eg_event = EventGridEvent(
subject="sample",
data={"sample": "eventgridevent"},
Expand Down Expand Up @@ -242,10 +242,12 @@ def callback(request):

await client.send(cloud_event, raw_request_hook=callback)

@pytest.mark.live_test_only
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_signature_credential(self, eventgrid_topic_key, eventgrid_topic_endpoint):
async def test_send_signature_credential(self, **kwargs):
eventgrid_topic_endpoint = kwargs.pop("eventgrid_topic_endpoint")
eventgrid_topic_key = kwargs.pop("eventgrid_topic_key")
expiration_date_utc = dt.datetime.now(UTC()) + timedelta(hours=1)
signature = generate_sas(eventgrid_topic_endpoint, eventgrid_topic_key, expiration_date_utc)
credential = AzureSasCredential(signature)
Expand Down Expand Up @@ -334,19 +336,18 @@ async def test_send_token_credential(self, eventgrid_topic_endpoint):
await client.send(eg_event)

@pytest.mark.live_test_only
async def test_send_partner_namespace(self):
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)
client = EventGridPublisherClient(eventgrid_partner_namespace_endpoint, credential)
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_partner_namespace(self, eventgrid_partner_namespace_topic_endpoint, eventgrid_partner_channel_name):
client = self.create_eg_publisher_client(eventgrid_partner_namespace_topic_endpoint)
cloud_event = CloudEvent(
source = "http://samplesource.dev",
data = "cloudevent",
type="Sample.Cloud.Event"
)
def callback(request):
req = request.http_request.headers
assert req.get("aeg-channel-name") == channel_name
assert req.get("aeg-channel-name") == eventgrid_partner_channel_name

await client.send(cloud_event, channel_name=channel_name, raw_request_hook=callback)
await client.send(cloud_event, channel_name=eventgrid_partner_channel_name, raw_request_hook=callback)
27 changes: 7 additions & 20 deletions sdk/eventgrid/azure-eventgrid/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,18 @@
# license information.
#--------------------------------------------------------------------------

import logging
import sys
import os
import json
import pytest
import uuid
from datetime import datetime, timedelta
from azure.core.exceptions import (
HttpResponseError,
ClientAuthenticationError,
ServiceRequestError
)
from msrest.serialization import UTC
import datetime as dt

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse

from devtools_testutils import AzureMgmtRecordedTestCase, recorded_by_proxy

from azure.core.credentials import AzureKeyCredential, AzureSasCredential
from azure.core.messaging import CloudEvent
from azure.core.serialization import NULL
from azure.eventgrid import EventGridPublisherClient, EventGridEvent, generate_sas
from azure.eventgrid._helpers import _cloud_event_to_generated
from azure.core.credentials import AzureKeyCredential
from azure.eventgrid import EventGridPublisherClient, EventGridEvent

from eventgrid_preparer import (
EventGridPreparer,
Expand All @@ -42,9 +27,10 @@ def create_eg_publisher_client(self, endpoint):
client = self.create_client_from_credential(EventGridPublisherClient, credential=credential, endpoint=endpoint)
return client

@pytest.mark.live_test_only
@EventGridPreparer()
@recorded_by_proxy
def test_raise_on_auth_error(self, eventgrid_topic_endpoint):
def test_raise_on_auth_error(self, **kwargs):
eventgrid_topic_endpoint = kwargs.pop("eventgrid_topic_endpoint")
akc_credential = AzureKeyCredential("bad credential")
client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
eg_event = EventGridEvent(
Expand All @@ -59,7 +45,8 @@ def test_raise_on_auth_error(self, eventgrid_topic_endpoint):
@pytest.mark.skip("Fix during MQ - skip to unblock pipeline")
@pytest.mark.live_test_only
@EventGridPreparer()
def test_raise_on_bad_resource(self, eventgrid_topic_key):
def test_raise_on_bad_resource(self, **kwargs):
eventgrid_topic_key = kwargs.pop("eventgrid_topic_key")
akc_credential = AzureKeyCredential(eventgrid_topic_key)
client = EventGridPublisherClient("https://bad-resource.westus-1.eventgrid.azure.net/api/events", akc_credential)
eg_event = EventGridEvent(
Expand Down
25 changes: 8 additions & 17 deletions sdk/eventgrid/azure-eventgrid/tests/test_exceptions_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,20 @@
# license information.
#--------------------------------------------------------------------------

import logging
import sys
import os
import json
import pytest
import uuid
from datetime import datetime, timedelta
from azure.core.exceptions import HttpResponseError, ClientAuthenticationError, ServiceRequestError
from azure.core.exceptions import HttpResponseError, ClientAuthenticationError
from msrest.serialization import UTC
import datetime as dt

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse

from devtools_testutils import AzureRecordedTestCase
from devtools_testutils.aio import recorded_by_proxy_async

from azure.core.credentials import AzureKeyCredential, AzureSasCredential
from azure.core.messaging import CloudEvent
from azure.core.serialization import NULL
from azure.eventgrid import EventGridEvent, generate_sas
from azure.core.credentials import AzureKeyCredential
from azure.eventgrid import EventGridEvent
from azure.eventgrid.aio import EventGridPublisherClient
from azure.eventgrid._helpers import _cloud_event_to_generated

from eventgrid_preparer import (
EventGridPreparer,
Expand All @@ -40,10 +29,11 @@ def create_eg_publisher_client(self, endpoint):
client = self.create_client_from_credential(EventGridPublisherClient, credential=credential, endpoint=endpoint)
return client

@pytest.mark.live_test_only
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_raise_on_auth_error(self, eventgrid_topic_endpoint):
async def test_raise_on_auth_error(self, **kwargs):
eventgrid_topic_endpoint = kwargs.pop("eventgrid_topic_endpoint")
akc_credential = AzureKeyCredential("bad credential")
client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
eg_event = EventGridEvent(
Expand All @@ -59,7 +49,8 @@ async def test_raise_on_auth_error(self, eventgrid_topic_endpoint):
@pytest.mark.live_test_only
@EventGridPreparer()
@pytest.mark.asyncio
async def test_raise_on_bad_resource(self, eventgrid_topic_key):
async def test_raise_on_bad_resource(self, **kwargs):
eventgrid_topic_key = kwargs.pop("eventgrid_topic_key")
akc_credential = AzureKeyCredential(eventgrid_topic_key)
client = EventGridPublisherClient("https://bad-resource.westus-1.eventgrid.azure.net/api/events", akc_credential)
eg_event = EventGridEvent(
Expand Down

0 comments on commit 2565a94

Please sign in to comment.