Пример #1
0
    def test_cloud_event_serialization_extension_string(self, **kwargs):
        data = "cloudevent"
        cloud_event = CloudEvent(
                source="http://samplesource.dev",
                data=data,
                type="Sample.Cloud.Event",
                extensions={'e1':1, 'e2':2}
                )
        
        cloud_event.subject = "subject" # to test explicit setting of prop
        internal = _cloud_event_to_generated(cloud_event)

        assert internal.additional_properties is not None
        assert 'e1' in internal.additional_properties

        json  = internal.serialize()

        expected = {
            'source':'http://samplesource.dev',
            'data': data,
            'type':'Sample.Cloud.Event',
            'reason_code':204,
            'e1':1,
            'e2':2
        }

        self._assert_cloud_event_serialized(expected, json)
        if sys.version_info > (3, 5):
            assert expected['data'] == json['data']
        else:
            encoded = base64.b64encode(data).decode('utf-8')
            expected['data_base64'] = encoded
            assert expected['data_base64'] == json['data_base64']
            assert 'data' not in json
def test_data_and_base64_both_exist_raises():
    with pytest.raises(ValueError):
        CloudEvent.from_dict({
            "source": 'sample',
            "type": 'type',
            "data": 'data',
            "data_base64": 'Y2kQ=='
        })
Пример #3
0
def test_wrong_schema_raises_no_type():
    cloud_custom_dict = {
        "id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
        "data":{"team": "event grid squad"},
        "source":"Azure/Sdk/Sample",
        "time":"2020-08-07T02:06:08.11969Z",
        "specversion":"1.0",
    }
    with pytest.raises(ValueError, match="The event does not conform to the cloud event spec https://github.com/cloudevents/spec. The `source` and `type` params are required."):
        CloudEvent.from_dict(cloud_custom_dict)
Пример #4
0
def test_eventgrid_event_schema_raises():
    cloud_custom_dict = {
        "id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
        "data":{"team": "event grid squad"},
        "dataVersion": "1.0",
        "subject":"Azure.Sdk.Sample",
        "eventTime":"2020-08-07T02:06:08.11969Z",
        "eventType":"pull request",
    }
    with pytest.raises(ValueError, match="The event you are trying to parse follows the Eventgrid Schema. You can parse EventGrid events using EventGridEvent.from_dict method in the azure-eventgrid library."):
        CloudEvent.from_dict(cloud_custom_dict)
Пример #5
0
 def test_send_cloud_event_fails_on_providing_data_and_b64(self):
     with pytest.raises(ValueError,
                        match="Unexpected keyword arguments data_base64.*"):
         cloud_event = CloudEvent(source="http://samplesource.dev",
                                  data_base64=b'cloudevent',
                                  data="random data",
                                  type="Sample.Cloud.Event")
def test_cloud_event_constructor_NULL_data():
    event = CloudEvent(source='Azure.Core.Sample',
                       type='SampleType',
                       data=NULL)

    assert event.data == NULL
    assert event.data is NULL
Пример #7
0
def test_from_json_storage():
    cloud_storage_dict = """{
        "id":"a0517898-9fa4-4e70-b4a3-afda1dd68672",
        "source":"/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Storage/storageAccounts/{storage-account}",
        "data":{
            "api":"PutBlockList",
            "client_request_id":"6d79dbfb-0e37-4fc4-981f-442c9ca65760",
            "request_id":"831e1650-001e-001b-66ab-eeb76e000000",
            "e_tag":"0x8D4BCC2E4835CD0",
            "content_type":"application/octet-stream",
            "content_length":524288,
            "blob_type":"BlockBlob",
            "url":"https://oc2d2817345i60006.blob.core.windows.net/oc2d2817345i200097container/oc2d2817345i20002296blob",
            "sequencer":"00000000000004420000000000028963",
            "storage_diagnostics":{"batchId":"b68529f3-68cd-4744-baa4-3c0498ec19f0"}
        },
        "type":"Microsoft.Storage.BlobCreated",
        "time":"2021-02-18T20:18:10.581147898Z",
        "specversion":"1.0"
    }"""
    obj = MockQueueMessage(content=cloud_storage_dict)
    event = CloudEvent.from_json(obj)
    assert event.data == {
            "api":"PutBlockList",
            "client_request_id":"6d79dbfb-0e37-4fc4-981f-442c9ca65760",
            "request_id":"831e1650-001e-001b-66ab-eeb76e000000",
            "e_tag":"0x8D4BCC2E4835CD0",
            "content_type":"application/octet-stream",
            "content_length":524288,
            "blob_type":"BlockBlob",
            "url":"https://oc2d2817345i60006.blob.core.windows.net/oc2d2817345i200097container/oc2d2817345i20002296blob",
            "sequencer":"00000000000004420000000000028963",
            "storage_diagnostics":{"batchId":"b68529f3-68cd-4744-baa4-3c0498ec19f0"}
        }
Пример #8
0
def test_from_json_eh():
    obj = MockEventhubData(
        body=MockEhBody()
    )
    event = CloudEvent.from_json(obj)
    assert event.id == "f208feff-099b-4bda-a341-4afd0fa02fef"
    assert event.data == "Eventhub"
def test_cloud_event_repr():
    event = CloudEvent(source='Azure.Core.Sample',
                       type='SampleType',
                       data='cloudevent')

    assert repr(event).startswith(
        "CloudEvent(source=Azure.Core.Sample, type=SampleType, specversion=1.0,"
    )
Пример #10
0
 def test_send_cloud_event_data_as_list(
         self, variables, eventgrid_cloud_event_topic_endpoint):
     client = self.create_eg_publisher_client(
         eventgrid_cloud_event_topic_endpoint)
     cloud_event = CloudEvent(source="http://samplesource.dev",
                              data="cloudevent",
                              type="Sample.Cloud.Event")
     client.send([cloud_event])
def test_cloud_event_constructor_blank_data():
    event = CloudEvent(source='Azure.Core.Sample', type='SampleType', data='')

    assert event.specversion == '1.0'
    assert event.time.__class__ == datetime.datetime
    assert event.id is not None
    assert event.source == 'Azure.Core.Sample'
    assert event.data == ''
Пример #12
0
 async def test_send_cloud_event_data_none(self, variables, eventgrid_cloud_event_topic_endpoint):
     client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
     cloud_event = CloudEvent(
             source = "http://samplesource.dev",
             data = None,
             type="Sample.Cloud.Event"
             )
     await client.send(cloud_event)
Пример #13
0
def test_extensions_not_alphanumeric_value_error():	
    with pytest.raises(ValueError):	
        event = CloudEvent(	
            source='sample',	
            type='type',	
            data='data',	
            extensions={"lowercase123": "accepted", "not@lph@nu^^3ic": "not allowed"}	
        )
Пример #14
0
def test_extensions_upper_case_value_error():	
    with pytest.raises(ValueError):	
        event = CloudEvent(	
            source='sample',	
            type='type',	
            data='data',	
            extensions={"lowercase123": "accepted", "NOTlower123": "not allowed"}	
        )
Пример #15
0
def test_cloud_event_constructor_none_data():
    event = CloudEvent(
        source='Azure.Core.Sample',
        type='SampleType',
        data=None
        )

    assert event.data == None
def test_cloud_event_constructor_unexpected_keyword():
    with pytest.raises(ValueError) as e:
        event = CloudEvent(source='Azure.Core.Sample',
                           type='SampleType',
                           data='cloudevent',
                           unexpected_keyword="not allowed",
                           another_bad_kwarg="not allowed either")
        assert "unexpected_keyword" in e
        assert "another_bad_kwarg" in e
 async def test_send_cloud_event_data_none(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)
     cloud_event = CloudEvent(
             source = "http://samplesource.dev",
             data = None,
             type="Sample.Cloud.Event"
             )
     await client.send(cloud_event)
Пример #18
0
 async def test_send_and_close_async_session(self, variables, 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
         cloud_event = CloudEvent(
                 source = "http://samplesource.dev",
                 data = "cloudevent",
                 type="Sample.Cloud.Event"
                 )
         await client.send(cloud_event)
Пример #19
0
def test_from_json():
    json_str = '{"id": "de0fd76c-4ef4-4dfb-ab3a-8f24a307e033", "source": "https://egtest.dev/cloudcustomevent", "data": {"team": "event grid squad"}, "type": "Azure.Sdk.Sample", "time": "2020-08-07T02:06:08.11969Z", "specversion": "1.0"}'
    event = CloudEvent.from_json(json_str)

    assert event.data == {"team": "event grid squad"}
    assert event.time.year == 2020
    assert event.time.month == 8
    assert event.time.day == 7
    assert event.time.hour == 2
 async def test_send_and_close_async_session(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)
     async with client: # this throws if client can't close
         cloud_event = CloudEvent(
                 source = "http://samplesource.dev",
                 data = "cloudevent",
                 type="Sample.Cloud.Event"
                 )
         await client.send(cloud_event)
async def publish():
    credential = AzureKeyCredential(topic_key)
    client = EventGridPublisherClient(endpoint, credential)

    await client.send([
        CloudEvent(type="Contoso.Items.ItemReceived",
                   source="/contoso/items",
                   data={"itemSku": "Contoso Item SKU #1"},
                   subject="Door1")
    ])
def test_cloud_event_constructor_missing_data():
    event = CloudEvent(
        source='Azure.Core.Sample',
        type='SampleType',
    )

    assert event.data == None
    assert event.datacontenttype == None
    assert event.dataschema == None
    assert event.subject == None
def test_cloud_custom_dict_no_data():
    cloud_custom_dict_with_missing_data = {
        "id": "de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
        "source": "https://egtest.dev/cloudcustomevent",
        "type": "Azure.Sdk.Sample",
        "time": "2021-02-18T20:18:10+00:00",
        "specversion": "1.0",
    }
    event = CloudEvent.from_dict(cloud_custom_dict_with_missing_data)
    assert event.__class__ == CloudEvent
    assert event.data is None
Пример #24
0
    async def test_send_cloud_event_data_NULL(self, variables, eventgrid_cloud_event_topic_endpoint):
        client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
        cloud_event = CloudEvent(
                source = "http://samplesource.dev",
                data = NULL,
                type="Sample.Cloud.Event"
                )
        def callback(request):
            req = json.loads(request.http_request.body)
            assert req[0].get("data") is None

        await client.send(cloud_event, raw_request_hook=callback)
def test_cloud_custom_dict_both_data_and_base64():
    cloud_custom_dict_with_data_and_base64 = {
        "id": "de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
        "source": "https://egtest.dev/cloudcustomevent",
        "data": "abc",
        "data_base64": "Y2Wa==",
        "type": "Azure.Sdk.Sample",
        "time": "2021-02-18T20:18:10+00:00",
        "specversion": "1.0",
    }
    with pytest.raises(ValueError):
        event = CloudEvent.from_dict(cloud_custom_dict_with_data_and_base64)
    async def test_send_cloud_event_data_NULL(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)
        cloud_event = CloudEvent(
                source = "http://samplesource.dev",
                data = NULL,
                type="Sample.Cloud.Event"
                )
        def callback(request):
            req = json.loads(request.http_request.body)
            assert req[0].get("data") is None

        await client.send(cloud_event, raw_request_hook=callback)
Пример #27
0
def test_cloud_from_dict_with_invalid_extensions():
    cloud_custom_dict_with_extensions = {
        "id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
        "source":"https://egtest.dev/cloudcustomevent",
        "data":{"team": "event grid squad"},
        "type":"Azure.Sdk.Sample",
        "time":"2020-08-07T02:06:08.11969Z",
        "specversion":"1.0",
        "ext1": "example",
        "BADext2": "example2"
    }
    with pytest.raises(ValueError):
        event = CloudEvent.from_dict(cloud_custom_dict_with_extensions)
Пример #28
0
    def test_send_cloud_event_data_base64_using_data(
            self, variables, eventgrid_cloud_event_topic_endpoint):
        client = self.create_eg_publisher_client(
            eventgrid_cloud_event_topic_endpoint)
        cloud_event = CloudEvent(source="http://samplesource.dev",
                                 data=b'cloudevent',
                                 type="Sample.Cloud.Event")

        def callback(request):
            req = json.loads(request.http_request.body)
            assert req[0].get("data_base64") is not None
            assert req[0].get("data") is None

        client.send(cloud_event, raw_response_hook=callback)
def test_cloud_custom_dict_valid_optional_attrs():
    cloud_custom_dict_with_none_data = {
        "id": "de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
        "source": "https://egtest.dev/cloudcustomevent",
        "type": "Azure.Sdk.Sample",
        "data": None,
        "dataschema": "exists",
        "time": "2021-02-18T20:18:10+00:00",
        "specversion": "1.0",
    }
    event = CloudEvent.from_dict(cloud_custom_dict_with_none_data)
    assert event.__class__ == CloudEvent
    assert event.data is NULL
    assert event.dataschema == "exists"
Пример #30
0
 def test_send_cloud_event_data_with_extensions(
         self, variables, eventgrid_cloud_event_topic_endpoint):
     client = self.create_eg_publisher_client(
         eventgrid_cloud_event_topic_endpoint)
     cloud_event = CloudEvent(source="http://samplesource.dev",
                              data="cloudevent",
                              type="Sample.Cloud.Event",
                              extensions={
                                  'reasoncode': 204,
                                  'extension': 'hello'
                              })
     client.send([cloud_event])
     internal = _cloud_event_to_generated(cloud_event).serialize()
     assert 'reasoncode' in internal
     assert 'extension' in internal
     assert internal['reasoncode'] == 204