def test_client_sas_credential(self, servicebus_queue, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key, servicebus_namespace_connection_string, **kwargs): # This should "just work" to validate known-good. credential = ServiceBusSharedKeyCredential(servicebus_namespace_key_name, servicebus_namespace_primary_key) hostname = "{}.servicebus.windows.net".format(servicebus_namespace.name) auth_uri = "sb://{}/{}".format(hostname, servicebus_queue.name) token = credential.get_token(auth_uri).token # Finally let's do it with SAS token + conn str token_conn_str = "Endpoint=sb://{}/;SharedAccessSignature={};".format(hostname, token.decode()) client = ServiceBusClient.from_connection_string(token_conn_str) with client: assert len(client._handlers) == 0 with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(Message("foo")) # This is disabled pending UAMQP fix https://github.com/Azure/azure-uamqp-python/issues/170 # #token_conn_str_without_se = token_conn_str.split('se=')[0] + token_conn_str.split('se=')[1].split('&')[1] # #client = ServiceBusClient.from_connection_string(token_conn_str_without_se) #with client: # assert len(client._handlers) == 0 # with client.get_queue_sender(servicebus_queue.name) as sender: # sender.send_messages(Message("foo"))
def test_mgmt_queue_list_basic(self, servicebus_namespace_connection_string, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key): mgmt_service = ServiceBusAdministrationClient.from_connection_string( servicebus_namespace_connection_string) clear_queues(mgmt_service) queues = list(mgmt_service.list_queues()) assert len(queues) == 0 mgmt_service.create_queue("test_queue") queues = list(mgmt_service.list_queues()) assert len(queues) == 1 and queues[0].name == "test_queue" mgmt_service.delete_queue("test_queue") queues = list(mgmt_service.list_queues()) assert len(queues) == 0 fully_qualified_namespace = servicebus_namespace.name + '.servicebus.windows.net' mgmt_service = ServiceBusAdministrationClient( fully_qualified_namespace, credential=ServiceBusSharedKeyCredential( servicebus_namespace_key_name, servicebus_namespace_primary_key)) queues = list(mgmt_service.list_queues()) assert len(queues) == 0 mgmt_service.create_queue("test_queue") queues = list(mgmt_service.list_queues()) assert len(queues) == 1 and queues[0].name == "test_queue" mgmt_service.delete_queue("test_queue") queues = list(mgmt_service.list_queues()) assert len(queues) == 0
def test_mgmt_queue_list_with_negative_credential( self, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key): # invalid_conn_str = 'Endpoint=sb://invalid.servicebus.windows.net/;SharedAccessKeyName=invalid;SharedAccessKey=invalid' # mgmt_service = ServiceBusAdministrationClient.from_connection_string(invalid_conn_str) # with pytest.raises(ServiceRequestError): # list(mgmt_service.list_queues()) # TODO: This negative test makes replay test fail. Need more investigation. Live test has no problem. invalid_conn_str = 'Endpoint=sb://{}.servicebus.windows.net/;SharedAccessKeyName=invalid;SharedAccessKey=invalid'.format( servicebus_namespace.name) mgmt_service = ServiceBusAdministrationClient.from_connection_string( invalid_conn_str) with pytest.raises(HttpResponseError): list(mgmt_service.list_queues()) # fully_qualified_namespace = 'invalid.servicebus.windows.net' # mgmt_service = ServiceBusAdministrationClient( # fully_qualified_namespace, # credential=ServiceBusSharedKeyCredential(servicebus_namespace_key_name, servicebus_namespace_primary_key) # ) # with pytest.raises(ServiceRequestError): # list(mgmt_service.list_queues()) fully_qualified_namespace = servicebus_namespace.name + '.servicebus.windows.net' mgmt_service = ServiceBusAdministrationClient( fully_qualified_namespace, credential=ServiceBusSharedKeyCredential("invalid", "invalid")) with pytest.raises(HttpResponseError): list(mgmt_service.list_queues())
def test_client_credential(self, servicebus_queue, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key, servicebus_namespace_connection_string, **kwargs): # This should "just work" to validate known-good. credential = ServiceBusSharedKeyCredential( servicebus_namespace_key_name, servicebus_namespace_primary_key) hostname = "{}.servicebus.windows.net".format( servicebus_namespace.name) client = ServiceBusClient(hostname, credential) with client: assert len(client._handlers) == 0 with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo")) hostname = "sb://{}.servicebus.windows.net".format( servicebus_namespace.name) client = ServiceBusClient(hostname, credential) with client: assert len(client._handlers) == 0 with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo")) hostname = "https://{}.servicebus.windows.net \ ".format(servicebus_namespace.name) client = ServiceBusClient(hostname, credential) with client: assert len(client._handlers) == 0 with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo"))
def test_subscription_by_sas_token_credential_conn_str_send_basic( self, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key, servicebus_topic, servicebus_subscription, **kwargs): fully_qualified_namespace = servicebus_namespace.name + '.servicebus.windows.net' with ServiceBusClient( fully_qualified_namespace=fully_qualified_namespace, credential=ServiceBusSharedKeyCredential( policy=servicebus_namespace_key_name, key=servicebus_namespace_primary_key), logging_enable=False) as sb_client: with sb_client.get_topic_sender( topic_name=servicebus_topic.name) as sender: message = ServiceBusMessage(b"Sample topic message") sender.send_messages(message) with sb_client.get_subscription_receiver( topic_name=servicebus_topic.name, subscription_name=servicebus_subscription.name, max_wait_time=5) as receiver: count = 0 for message in receiver: count += 1 receiver.complete_message(message) assert count == 1
def test_mgmt_queue_basic_v2017_04(self, servicebus_namespace_connection_string, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key): mgmt_service = ServiceBusAdministrationClient.from_connection_string(servicebus_namespace_connection_string, api_version=ApiVersion.V2017_04) clear_queues(mgmt_service) mgmt_service.create_queue("test_queue") queues = list(mgmt_service.list_queues()) assert len(queues) == 1 and queues[0].name == "test_queue" queue = mgmt_service.get_queue("test_queue") assert queue.name == "test_queue" mgmt_service.delete_queue("test_queue") queues = list(mgmt_service.list_queues()) assert len(queues) == 0 with pytest.raises(HttpResponseError): mgmt_service.create_queue("queue_can_not_be_created", max_message_size_in_kilobytes=1024) fully_qualified_namespace = servicebus_namespace.name + '.servicebus.windows.net' mgmt_service = ServiceBusAdministrationClient( fully_qualified_namespace, credential=ServiceBusSharedKeyCredential(servicebus_namespace_key_name, servicebus_namespace_primary_key), api_version=ApiVersion.V2017_04 ) mgmt_service.create_queue("test_queue") queues = list(mgmt_service.list_queues()) assert len(queues) == 1 and queues[0].name == "test_queue" queue = mgmt_service.get_queue("test_queue") assert queue.name == "test_queue" mgmt_service.delete_queue("test_queue") queues = list(mgmt_service.list_queues()) assert len(queues) == 0 with pytest.raises(HttpResponseError): mgmt_service.create_queue("queue_can_not_be_created", max_message_size_in_kilobytes=1024)
def test_sb_client_bad_credentials(self, servicebus_namespace, servicebus_queue, **kwargs): client = ServiceBusClient( fully_qualified_namespace=servicebus_namespace.name + '.servicebus.windows.net', credential=ServiceBusSharedKeyCredential('invalid', 'invalid'), logging_enable=False) with client: with pytest.raises(ServiceBusAuthenticationError): with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(Message("test"))
def test_client_azure_named_key_credential( self, servicebus_queue, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key, servicebus_namespace_connection_string, **kwargs): # This should "just work" to validate known-good. credential = ServiceBusSharedKeyCredential( servicebus_namespace_key_name, servicebus_namespace_primary_key) hostname = "{}.servicebus.windows.net".format( servicebus_namespace.name) auth_uri = "sb://{}/{}".format(hostname, servicebus_queue.name) token = credential.get_token(auth_uri).token.decode() # Finally let's do it with AzureSasCredential credential = AzureSasCredential(token) client = ServiceBusClient(hostname, credential) with client: assert len(client._handlers) == 0
def test_sb_client_bad_namespace(self, **kwargs): client = ServiceBusClient( fully_qualified_namespace='invalid.servicebus.windows.net', credential=ServiceBusSharedKeyCredential('invalid', 'invalid'), logging_enable=False) with client: with pytest.raises(ServiceBusError): with client.get_queue_sender('invalidqueue') as sender: sender.send_messages(ServiceBusMessage("test"))
def test_client_sas_credential(self, servicebus_queue, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key, servicebus_namespace_connection_string, **kwargs): # This should "just work" to validate known-good. credential = ServiceBusSharedKeyCredential(servicebus_namespace_key_name, servicebus_namespace_primary_key) hostname = "{}.servicebus.windows.net".format(servicebus_namespace.name) auth_uri = "sb://{}/{}".format(hostname, servicebus_queue.name) token = credential.get_token(auth_uri).token # Finally let's do it with SAS token + conn str token_conn_str = "Endpoint=sb://{}/;SharedAccessSignature={};".format(hostname, token.decode()) client = ServiceBusClient.from_connection_string(token_conn_str) with client: assert len(client._handlers) == 0 with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo"))
def test_topic_by_sas_token_credential_conn_str_send_basic( self, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key, servicebus_topic, **kwargs): fully_qualified_namespace = servicebus_namespace.name + '.servicebus.windows.net' with ServiceBusClient( fully_qualified_namespace=fully_qualified_namespace, credential=ServiceBusSharedKeyCredential( policy=servicebus_namespace_key_name, key=servicebus_namespace_primary_key), logging_enable=False) as sb_client: with sb_client.get_topic_sender(servicebus_topic.name) as sender: message = ServiceBusMessage(b"Sample topic message") sender.send_messages(message)