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): hostname = "{}.servicebus.windows.net".format( servicebus_namespace.name) credential = AzureNamedKeyCredential(servicebus_namespace_key_name, servicebus_namespace_primary_key) client = ServiceBusClient(hostname, credential) with client: with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo")) credential.update("foo", "bar") with pytest.raises(Exception): with client: with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo")) # update back to the right key again credential.update(servicebus_namespace_key_name, servicebus_namespace_primary_key) with client: with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("foo"))
def test_sb_client_incorrect_queue_conn_str(self, servicebus_queue_authorization_rule_connection_string, servicebus_queue, wrong_queue, **kwargs): client = ServiceBusClient.from_connection_string(servicebus_queue_authorization_rule_connection_string) with client: # Validate that the wrong queue with the right credentials fails. with pytest.raises(ServiceBusAuthenticationError): with client.get_queue_sender(wrong_queue.name) as sender: sender.send_messages(ServiceBusMessage("test")) # But that the correct one works. with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("test")) # Now do the same but with direct connstr initialization. with pytest.raises(ServiceBusAuthenticationError): with ServiceBusSender.from_connection_string( servicebus_queue_authorization_rule_connection_string, queue_name=wrong_queue.name, ) as sender: sender.send_messages(ServiceBusMessage("test")) with ServiceBusSender.from_connection_string( servicebus_queue_authorization_rule_connection_string, queue_name=servicebus_queue.name, ) as sender: sender.send_messages(ServiceBusMessage("test"))
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_sb_client_incorrect_queue_conn_str( self, servicebus_queue_authorization_rule_connection_string, servicebus_queue, wrong_queue, **kwargs): client = ServiceBusClient.from_connection_string( servicebus_queue_authorization_rule_connection_string) with client: # Validate that the wrong sender/receiver queues with the right credentials fail. with pytest.raises(ValueError): with client.get_queue_sender(wrong_queue.name) as sender: sender.send_messages(ServiceBusMessage("test")) with pytest.raises(ValueError): with client.get_queue_receiver(wrong_queue.name) as receiver: messages = receiver.receive_messages(max_message_count=1, max_wait_time=1) # But that the correct ones work. with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("test")) with client.get_queue_receiver(servicebus_queue.name) as receiver: messages = receiver.receive_messages(max_message_count=1, max_wait_time=1) # Now do the same but with direct connstr initialization. with pytest.raises(ValueError): with ServiceBusSender._from_connection_string( servicebus_queue_authorization_rule_connection_string, queue_name=wrong_queue.name, ) as sender: sender.send_messages(ServiceBusMessage("test")) with pytest.raises(ValueError): with ServiceBusReceiver._from_connection_string( servicebus_queue_authorization_rule_connection_string, queue_name=wrong_queue.name, ) as receiver: messages = receiver.receive_messages(max_message_count=1, max_wait_time=1) with ServiceBusSender._from_connection_string( servicebus_queue_authorization_rule_connection_string, queue_name=servicebus_queue.name, ) as sender: sender.send_messages(ServiceBusMessage("test")) with ServiceBusReceiver._from_connection_string( servicebus_queue_authorization_rule_connection_string, queue_name=servicebus_queue.name, ) as receiver: messages = receiver.receive_messages(max_message_count=1, max_wait_time=1)
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")) # 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(ServiceBusMessage("foo"))
def test_sb_client_bad_entity(self, servicebus_namespace_connection_string, **kwargs): client = ServiceBusClient.from_connection_string(servicebus_namespace_connection_string) with client: with pytest.raises(ServiceBusAuthenticationError): with client.get_queue_sender("invalid") as sender: sender.send_messages(ServiceBusMessage("test"))
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(ServiceBusMessage("test"))
def test_sb_client_readonly_credentials(self, servicebus_authorization_rule_connection_string, servicebus_queue, **kwargs): client = ServiceBusClient.from_connection_string(servicebus_authorization_rule_connection_string) with client: with client.get_queue_receiver(servicebus_queue.name) as receiver: messages = receiver.receive_messages(max_message_count=1, max_wait_time=1) with pytest.raises(ServiceBusAuthorizationError): with client.get_queue_sender(servicebus_queue.name) as sender: sender.send_messages(ServiceBusMessage("test"))
def test_topic_by_servicebus_client_conn_str_send_basic( self, servicebus_namespace_connection_string, servicebus_topic, **kwargs): with ServiceBusClient.from_connection_string( servicebus_namespace_connection_string, 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)
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)
def test_client_azure_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.decode() # Finally let's do it with AzureSasCredential credential = AzureSasCredential(token) 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_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_sb_client_bad_entity(self, servicebus_namespace_connection_string, **kwargs): client = ServiceBusClient.from_connection_string( servicebus_namespace_connection_string) with client: with pytest.raises(ServiceBusAuthenticationError): with client.get_queue_sender("invalid") as sender: sender.send_messages(ServiceBusMessage("test")) fake_str = "Endpoint=sb://mock.servicebus.windows.net/;" \ "SharedAccessKeyName=mock;SharedAccessKey=mock;EntityPath=mockentity" fake_client = ServiceBusClient.from_connection_string(fake_str) with pytest.raises(ValueError): fake_client.get_queue_sender('queue') with pytest.raises(ValueError): fake_client.get_queue_receiver('queue') with pytest.raises(ValueError): fake_client.get_topic_sender('topic') with pytest.raises(ValueError): fake_client.get_subscription_receiver('topic', 'subscription') fake_client.get_queue_sender('mockentity') fake_client.get_queue_receiver('mockentity') fake_client.get_topic_sender('mockentity') fake_client.get_subscription_receiver('mockentity', 'subscription') fake_str = "Endpoint=sb://mock.servicebus.windows.net/;" \ "SharedAccessKeyName=mock;SharedAccessKey=mock" fake_client = ServiceBusClient.from_connection_string(fake_str) fake_client.get_queue_sender('queue') fake_client.get_queue_receiver('queue') fake_client.get_topic_sender('topic') fake_client.get_subscription_receiver('topic', 'subscription')