1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import json
import threading
import time
import os
import inspect
from datetime import datetime, timedelta
from typing import Dict, Any, List, Optional
import requests
from azure.servicebus import ServiceBusClient
from azure.identity import AzureCliCredential
from devtools_testutils import AzureRecordedTestCase, is_live
from devtools_testutils.helpers import get_test_id
from azure.communication.callautomation import (
CommunicationIdentifierKind,
CallAutomationClient,
CallConnectionClient,
CommunicationIdentifier,
)
from azure.communication.callautomation._shared.models import identifier_from_raw_id
from azure.communication.identity import CommunicationIdentityClient
from azure.communication.phonenumbers import PhoneNumbersClient
class CallAutomationRecordedTestCase(AzureRecordedTestCase):
@classmethod
def setup_class(cls):
if is_live():
print("Live Test")
cls.connection_str = os.environ.get('COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING')
cls.servicebus_str = os.environ.get('SERVICEBUS_STRING')
cls.dispatcher_endpoint = os.environ.get('DISPATCHER_ENDPOINT')
cls.file_source_url = os.environ.get('FILE_SOURCE_URL')
cls.cognitive_service_endpoint = os.environ.get('COGNITIVE_SERVICE_ENDPOINT')
cls.transport_url = os.environ.get('TRANSPORT_URL')
else:
print("Recorded Test")
cls.connection_str = "endpoint=https://someEndpoint/;accesskey=someAccessKeyw=="
cls.servicebus_str = "redacted.servicebus.windows.net"
cls.dispatcher_endpoint = "https://REDACTED.azurewebsites.net"
cls.file_source_url = "https://REDACTED/prompt.wav"
cls.cognitive_service_endpoint = "https://sanitized/"
cls.transport_url ="wss://sanitized/ws"
cls.credential = AzureCliCredential()
cls.dispatcher_callback = cls.dispatcher_endpoint + "/api/servicebuscallback/events"
cls.identity_client = CommunicationIdentityClient.from_connection_string(cls.connection_str)
cls.phonenumber_client = PhoneNumbersClient.from_connection_string(cls.connection_str)
cls.service_bus_client = ServiceBusClient(
fully_qualified_namespace=cls.servicebus_str,
credential=cls.credential)
cls.wait_for_event_flags = []
cls.event_store: Dict[str, Dict[str, Any]] = {}
cls.event_to_save: Dict[str, Dict[str, Any]] = {}
cls.open_call_connections: Dict[str, CallConnectionClient] = {}
@classmethod
def teardown_class(cls):
cls.wait_for_event_flags.clear()
for cc in cls.open_call_connections.values():
cc.hang_up(is_for_everyone=True)
def setup_method(self, method):
self.test_name = get_test_id().split("/")[-1]
self.event_store: Dict[str, Dict[str, Any]] = {}
self.event_to_save: Dict[str, Dict[str, Any]] = {}
self._prepare_events_recording()
pass
def teardown_method(self, method):
self._record_method_events()
self.event_store: Dict[str, Dict[str, Any]] = {}
self.event_to_save: Dict[str, Dict[str, Any]] = {}
pass
@staticmethod
def _format_string(s) -> str:
s1 = f"{s[:12]}-{s[12:16]}-{s[16:20]}-{s[20:24]}-{s[24:36]}"
s2 = f"{s[36:44]}-{s[44:48]}-{s[48:52]}-{s[52:56]}-{s[56:]}"
return f"{s1}_{s2}"
@staticmethod
def _parse_ids_from_identifier(identifier: CommunicationIdentifier) -> str:
if identifier is None:
raise ValueError("Identifier cannot be None")
elif identifier.kind == CommunicationIdentifierKind.COMMUNICATION_USER:
return CallAutomationRecordedTestCase._format_string("".join(filter(str.isalnum, identifier.raw_id)))
elif identifier.kind == CommunicationIdentifierKind.PHONE_NUMBER:
return identifier.raw_id
else:
raise ValueError("Identifier type not supported")
@staticmethod
def _event_key_gen(event_type: str) -> str:
return event_type
@staticmethod
def _unique_key_gen(
caller_identifier: CommunicationIdentifier, receiver_identifier: CommunicationIdentifier
) -> str:
return CallAutomationRecordedTestCase._parse_ids_from_identifier(
caller_identifier
) + CallAutomationRecordedTestCase._parse_ids_from_identifier(receiver_identifier)
def _get_test_event_file_name(self):
script_dir = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(script_dir, "events", f"{self.test_name}.event.json")
return file_path
def _message_awaiter(self, unique_id) -> None:
service_bus_receiver = self.service_bus_client.get_queue_receiver(queue_name=unique_id)
while unique_id in self.wait_for_event_flags:
received_messages = service_bus_receiver.receive_messages(max_wait_time=20)
for msg in received_messages:
body_bytes = b"".join(msg.body)
body_str = body_bytes.decode("utf-8")
mapper = json.loads(body_str)
if "incomingCallContext" in mapper:
caller = identifier_from_raw_id(mapper["from"]["rawId"])
receiver = identifier_from_raw_id(mapper["to"]["rawId"])
unique_id = self._unique_key_gen(caller, receiver)
key = self._event_key_gen("IncomingCall")
print("EventRegistration(IncomingCall):" + key)
self.event_store[key] = mapper
self.event_to_save[key] = mapper
else:
if isinstance(mapper, list):
mapper = mapper[0]
if mapper["type"]:
key = self._event_key_gen(mapper["type"].split(".")[-1])
print("EventRegistration:" + key)
self.event_store[key] = mapper
self.event_to_save[key] = mapper
service_bus_receiver.complete_message(msg)
time.sleep(1)
return
def _prepare_events_recording(self) -> None:
# only load during playback
if not is_live():
file_path = self._get_test_event_file_name()
try:
with open(file_path, "r") as json_file:
self.event_store = json.load(json_file)
except IOError as e:
raise SystemExit(f"File write operation failed: {e}")
def _record_method_events(self) -> None:
# only save during live
if is_live():
file_path = self._get_test_event_file_name()
try:
keys_to_redact = ["incomingCallContext", "callerDisplayName"]
redacted_dict = self.redact_by_key(self.event_to_save, keys_to_redact)
with open(file_path, "w") as json_file:
json.dump(redacted_dict, json_file)
except IOError as e:
raise SystemExit(f"File write operation failed: {e}")
def check_for_event(self, event_type: str, call_connection_id: str, wait_time: timedelta) -> Any:
key = self._event_key_gen(event_type)
time_out_time = datetime.now() + wait_time
while datetime.now() < time_out_time:
popped_event = self.event_store.pop(key, None)
if popped_event is not None:
print(f"Matching Event Found [{key}]")
return popped_event
time.sleep(1)
return None
def establish_callconnection_voip(self, caller, target, *, cognitive_service_enabled: Optional[bool] = False) -> tuple:
return self._establish_callconnection(
caller, target, cognitive_service_enabled=cognitive_service_enabled
)
def establish_callconnection_pstn(self, caller, target) -> tuple:
return self._establish_callconnection(caller, target, is_pstn=True)
def establish_callconnection_voip_answercall_withcustomcontext(self, caller, target) -> tuple:
return self._establish_callconnection(caller, target, custom_context=True)
def establish_callconnection_voip_with_streaming_options(self, caller, target, options, is_transcription) -> tuple:
return self._establish_callconnection(
caller, target, options=options, is_transcription=is_transcription
)
def establish_callconnection_voip_connect_call(self, caller, target) -> tuple:
return self._establish_callconnection(caller, target, connect_call=True)
def _establish_callconnection(self, caller, target, cognitive_service_enabled: Optional[bool] = False, is_pstn: bool = False, custom_context: bool = False, options=None, is_transcription: bool = False, connect_call: bool = False) -> tuple:
call_automation_client_caller = self._create_call_automation_client(caller)
call_automation_client_target = self._create_call_automation_client(target)
unique_id = self._unique_key_gen(caller, target)
if is_live():
self._subscribe_to_dispatcher(unique_id)
callback_url = f"{self.dispatcher_callback}?q={unique_id}"
create_call_result = self._create_call(
call_automation_client_caller, target, unique_id, cognitive_service_enabled, is_pstn, options, is_transcription
)
caller_connection_id = self._validate_create_call_result(create_call_result)
incoming_call_context = self._wait_for_incoming_call(unique_id)
answer_call_result = self._answer_call(call_automation_client_target, incoming_call_context, custom_context)
call_connection_caller = self._create_call_connection_client(caller_connection_id)
call_connection_target = self._create_call_connection_client(answer_call_result.call_connection_id)
self.open_call_connections[unique_id] = call_connection_caller
if connect_call:
return unique_id, call_connection_caller, call_connection_target, call_automation_client_caller, callback_url
return unique_id, call_connection_caller, call_connection_target
def _create_call_automation_client(self, source):
return CallAutomationClient.from_connection_string(self.connection_str, source=source)
def _subscribe_to_dispatcher(self, unique_id):
dispatcher_url = f"{self.dispatcher_endpoint}/api/servicebuscallback/subscribe?q={unique_id}"
response = requests.post(dispatcher_url)
if response is None:
raise ValueError("Response cannot be None")
print(f"Subscription to dispatcher of {unique_id}: {response.status_code}")
self.wait_for_event_flags.append(unique_id)
thread = threading.Thread(target=self._message_awaiter, args=(unique_id,))
thread.start()
def _create_call(self, client, target, unique_id, cognitive_service_enabled, is_pstn, options, is_transcription):
if is_pstn:
return client.create_call(
target_participant=target,
source_caller_id_number=caller,
callback_url=f"{self.dispatcher_callback}?q={unique_id}",
)
elif options:
return client.create_call(
target_participant=target,
callback_url=f"{self.dispatcher_callback}?q={unique_id}",
media_streaming=options if not is_transcription else None,
transcription=options if is_transcription else None,
cognitive_services_endpoint=self.cognitive_service_endpoint if is_transcription else None
)
else:
return client.create_call(
target_participant=target,
callback_url=f"{self.dispatcher_callback}?q={unique_id}",
cognitive_services_endpoint=self.cognitive_service_endpoint if cognitive_service_enabled else None
)
def _validate_create_call_result(self, result):
if result is None:
raise ValueError("Invalid create_call_result")
caller_connection_id = result.call_connection_id
if caller_connection_id is None:
raise ValueError("Caller connection ID is None")
return caller_connection_id
def _wait_for_incoming_call(self, unique_id):
incoming_call_event = self.check_for_event("IncomingCall", unique_id, timedelta(seconds=30))
if incoming_call_event is None:
raise ValueError("incoming_call_event is None")
return incoming_call_event["incomingCallContext"]
def _answer_call(self, client, context, custom_context):
if custom_context:
result = client.answer_call(
incoming_call_context=context, callback_url=self.dispatcher_callback, voip_headers={"foo": "bar"}
)
else:
result = client.answer_call(
incoming_call_context=context, callback_url=self.dispatcher_callback
)
if result is None:
raise ValueError("Invalid answer_call result")
return result
def _create_call_connection_client(self, connection_id):
return CallConnectionClient.from_connection_string(self.connection_str, connection_id)
def terminate_call(self, unique_id) -> None:
try:
call_connection = self.open_call_connections.pop(unique_id, None)
if call_connection is not None:
call_connection.hang_up(is_for_everyone=True)
disconnected_event = self.check_for_event(
"CallDisconnected", call_connection._call_connection_id, timedelta(seconds=15)
)
if disconnected_event is None:
raise ValueError("Receiver CallDisconnected event is None")
finally:
while unique_id in self.wait_for_event_flags:
self.wait_for_event_flags.remove(unique_id)
pass
def redact_by_key(self, data: Dict[str, Dict[str, any]], keys_to_redact: List[str]) -> Dict[str, Dict[str, any]]:
for _, inner_dict in data.items():
for key in keys_to_redact:
if key in inner_dict:
inner_dict[key] = "REDACTED"
return data
|