def connection_string_to_hostname(conn_str): """ Retrieves only the hostname from connection string. This will eventually give us the Linked IoT Hub """ conn_str_obj = ConnectionString(conn_str) return conn_str_obj.get("HostName")
def test_indexing_key_returns_corresponding_value(self): cs = ConnectionString( "HostName=my.host.name;SharedAccessKeyName=mykeyname;SharedAccessKey=Zm9vYmFy" ) assert cs["HostName"] == "my.host.name" assert cs["SharedAccessKeyName"] == "mykeyname" assert cs["SharedAccessKey"] == "Zm9vYmFy"
def connection_string_to_sas_token(conn_str): """ parse an IoTHub service connection string and return the host and a shared access signature that can be used to connect to the given hub """ conn_str_obj = ConnectionString(conn_str) signing_mechanism = SymmetricKeySigningMechanism(conn_str_obj.get("SharedAccessKey")) sas_token = RenewableSasToken( uri=conn_str_obj.get("HostName"), signing_mechanism=signing_mechanism, key_name=conn_str_obj.get("SharedAccessKeyName"), ) return {"host": conn_str_obj.get("HostName"), "sas": str(sas_token)}
def test_string_representation_of_object_is_the_input_string(self): string = "HostName=my.host.name;SharedAccessKeyName=mykeyname;SharedAccessKey=Zm9vYmFy" cs = ConnectionString(string) assert str(cs) == string
def test_raises_type_error_on_non_string_input(self, input_val): with pytest.raises(TypeError): ConnectionString(input_val)
def test_raises_value_error_on_invalid_input(self, input_string): with pytest.raises(ValueError): ConnectionString(input_string)
def test_instantiates_correctly_from_string(self, input_string): cs = ConnectionString(input_string) assert isinstance(cs, ConnectionString)
def test_calling_get_with_invalid_key_and_a_default_value_returns_default_value( self): cs = ConnectionString( "HostName=my.host.name;SharedAccessKeyName=mykeyname;SharedAccessKey=Zm9vYmFy" ) assert cs.get("invalidkey", "defaultval") == "defaultval"
def test_calling_get_with_key_returns_corresponding_value(self): cs = ConnectionString( "HostName=my.host.name;SharedAccessKeyName=mykeyname;SharedAccessKey=Zm9vYmFy" ) assert cs.get("HostName") == "my.host.name"
def test_indexing_key_raises_key_error_if_key_not_in_string(self): with pytest.raises(KeyError): cs = ConnectionString( "HostName=my.host.name;SharedAccessKeyName=mykeyname;SharedAccessKey=Zm9vYmFy" ) cs["SharedAccessSignature"]