forked from Azure/azure-sdk-for-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_exceptions.py
More file actions
99 lines (88 loc) · 3.37 KB
/
test_exceptions.py
File metadata and controls
99 lines (88 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# 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
from azure.eventgrid import EventGridClient, EventGridEvent, ClientLevel
from azure.core.messaging import CloudEvent
from eventgrid_preparer import (
EventGridPreparer,
)
class TestEventGridPublisherClientExceptions(AzureMgmtRecordedTestCase):
def create_eg_publisher_client(self, endpoint):
credential = self.get_credential(EventGridClient)
client = self.create_client_from_credential(
EventGridClient, credential=credential, endpoint=endpoint, level=ClientLevel.BASIC
)
return client
@EventGridPreparer()
@recorded_by_proxy
def test_raise_on_auth_error(self, eventgrid_topic_endpoint):
akc_credential = AzureKeyCredential("bad credential")
client = EventGridClient(eventgrid_topic_endpoint, akc_credential, level=ClientLevel.BASIC)
eg_event = EventGridEvent(
subject="sample",
data={"sample": "eventgridevent"},
event_type="Sample.EventGrid.Event",
data_version="2.0",
)
with pytest.raises(
ClientAuthenticationError,
match="The request authorization key is not authorized for*",
):
client.send(eg_event)
@pytest.mark.skip("Fix during MQ - skip to unblock pipeline")
@pytest.mark.live_test_only
def test_raise_on_bad_resource(self):
credential = AzureKeyCredential(os.environ["EVENTGRID_TOPIC_KEY"])
client = EventGridClient(
"https://bad-resource.eastus-1.eventgrid.azure.net/api/events",
credential,
level=ClientLevel.BASIC,
)
eg_event = CloudEvent(
subject="sample",
data={"sample": "eventgridevent"},
source="source",
type="Sample.Cloud.Event",
)
with pytest.raises(HttpResponseError):
client.send(eg_event)
@EventGridPreparer()
@recorded_by_proxy
def test_raise_on_large_payload(self, eventgrid_topic_endpoint):
client = self.create_eg_publisher_client(eventgrid_topic_endpoint)
path = os.path.abspath(
os.path.join(os.path.abspath(__file__), "..", "./large_data.json")
)
with open(path) as json_file:
data = json.load(json_file)
eg_event = EventGridEvent(
subject="sample",
data=data,
event_type="Sample.EventGrid.Event",
data_version="2.0",
)
with pytest.raises(HttpResponseError) as err:
client.send(eg_event)
assert "The maximum size (1536000) has been exceeded." in err.value.message