1313
1414import requests
1515from azure .servicebus import ServiceBusClient
16+ from azure .identity import AzureCliCredential
1617from devtools_testutils import AzureRecordedTestCase , is_live
1718from devtools_testutils .helpers import get_test_id
1819
2627from azure .communication .identity import CommunicationIdentityClient
2728from azure .communication .phonenumbers import PhoneNumbersClient
2829
29-
3030class CallAutomationRecordedTestCase (AzureRecordedTestCase ):
3131 @classmethod
3232 def setup_class (cls ):
3333 if is_live ():
3434 print ("Live Test" )
3535 cls .connection_str = os .environ .get ('COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING' )
36- cls .servicebus_connection_str = os .environ .get ('SERVICEBUS_STRING' )
36+ cls .servicebus_str = os .environ .get ('SERVICEBUS_STRING' )
3737 cls .dispatcher_endpoint = os .environ .get ('DISPATCHER_ENDPOINT' )
3838 cls .file_source_url = os .environ .get ('FILE_SOURCE_URL' )
3939 cls .cognitive_service_endpoint = os .environ .get ('COGNITIVE_SERVICE_ENDPOINT' )
4040 cls .transport_url = os .environ .get ('TRANSPORT_URL' )
4141 else :
4242 print ("Recorded Test" )
4343 cls .connection_str = "endpoint=https://someEndpoint/;accesskey=someAccessKeyw=="
44- cls .servicebus_connection_str = (
45- "Endpoint=sb://someEndpoint/;SharedAccessKeyName=somekey;SharedAccessKey=someAccessKey="
46- )
44+ cls .servicebus_str = "redacted.servicebus.windows.net"
4745 cls .dispatcher_endpoint = "https://REDACTED.azurewebsites.net"
4846 cls .file_source_url = "https://REDACTED/prompt.wav"
4947 cls .cognitive_service_endpoint = "https://sanitized/"
50- cls .transport_url = "wss://REDACTED"
48+ cls .transport_url = "wss://sanitized/ws"
49+
5150
51+ cls .credential = AzureCliCredential ()
5252 cls .dispatcher_callback = cls .dispatcher_endpoint + "/api/servicebuscallback/events"
5353 cls .identity_client = CommunicationIdentityClient .from_connection_string (cls .connection_str )
5454 cls .phonenumber_client = PhoneNumbersClient .from_connection_string (cls .connection_str )
55- cls .service_bus_client = ServiceBusClient .from_connection_string (cls .servicebus_connection_str )
55+ cls .service_bus_client = ServiceBusClient (
56+ fully_qualified_namespace = cls .servicebus_str ,
57+ credential = cls .credential )
5658
5759 cls .wait_for_event_flags = []
5860 cls .event_store : Dict [str , Dict [str , Any ]] = {}
@@ -84,18 +86,14 @@ def _format_string(s) -> str:
8486 s2 = f"{ s [36 :44 ]} -{ s [44 :48 ]} -{ s [48 :52 ]} -{ s [52 :56 ]} -{ s [56 :]} "
8587 return f"{ s1 } _{ s2 } "
8688
87- @staticmethod
88- def _format_phonenumber_string (s ) -> str :
89- return s .replace (":+" , "u002B" )
90-
9189 @staticmethod
9290 def _parse_ids_from_identifier (identifier : CommunicationIdentifier ) -> str :
9391 if identifier is None :
9492 raise ValueError ("Identifier cannot be None" )
9593 elif identifier .kind == CommunicationIdentifierKind .COMMUNICATION_USER :
9694 return CallAutomationRecordedTestCase ._format_string ("" .join (filter (str .isalnum , identifier .raw_id )))
9795 elif identifier .kind == CommunicationIdentifierKind .PHONE_NUMBER :
98- return CallAutomationRecordedTestCase . _format_phonenumber_string ( identifier .raw_id )
96+ return identifier .raw_id
9997 else :
10098 raise ValueError ("Identifier type not supported" )
10199
@@ -159,8 +157,10 @@ def _record_method_events(self) -> None:
159157 if is_live ():
160158 file_path = self ._get_test_event_file_name ()
161159 try :
160+ keys_to_redact = ["incomingCallContext" , "callerDisplayName" ]
161+ redacted_dict = self .redact_by_key (self .event_to_save , keys_to_redact )
162162 with open (file_path , "w" ) as json_file :
163- json .dump (self . event_to_save , json_file )
163+ json .dump (redacted_dict , json_file )
164164 except IOError as e :
165165 raise SystemExit (f"File write operation failed: { e } " )
166166
@@ -394,4 +394,11 @@ def establish_callconnection_voip_connect_call(self, caller, target) -> tuple:
394394 call_connection_target = CallConnectionClient .from_connection_string (self .connection_str , answer_call_result .call_connection_id )
395395 self .open_call_connections [unique_id ] = call_connection_caller
396396
397- return unique_id , call_connection_caller , call_connection_target , call_automation_client_caller , callback_url
397+ return unique_id , call_connection_caller , call_connection_target , call_automation_client_caller , callback_url
398+
399+ def redact_by_key (self , data : Dict [str , Dict [str , any ]], keys_to_redact : List [str ]) -> Dict [str , Dict [str , any ]]:
400+ for _ , inner_dict in data .items ():
401+ for key in keys_to_redact :
402+ if key in inner_dict :
403+ inner_dict [key ] = "REDACTED"
404+ return data
0 commit comments