async def sms_token_credential_auth_async(self): # To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have # AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables. endpoint, _ = parse_connection_str(self.connection_string) sms_client = SmsClient(endpoint, DefaultAzureCredential()) async with sms_client: try: # calling send() with sms values sms_responses = await sms_client.send( from_=self.phone_number, to=self.phone_number, message="Hello World via SMS") sms_response = sms_responses[0] if (sms_response.successful): print( "Message with message id {} was successful sent to {}". format(sms_response.message_id, sms_response.to)) else: print( "Message failed to send to {} with the status code {} and error: {}" .format(sms_response.to, sms_response.http_status_code, sms_response.error_message)) except Exception: print(Exception) pass
async def send_sms_to_single_recipient_async(self): sms_client = SmsClient.from_connection_string(self.connection_string) async with sms_client: try: # calling send() with sms values sms_responses = await sms_client.send( from_="<leased-phone-number>", to="<to-phone-number>", message="Hello World via SMS", enable_delivery_report=True, # optional property tag="custom-tag") # optional property sms_response = sms_responses[0] if (sms_response.successful): print( "Message with message id {} was successful sent to {}". format(sms_response.message_id, sms_response.to)) else: print( "Message failed to send to {} with the status code {} and error: {}" .format(sms_response.to, sms_response.http_status_code, sms_response.error_message)) except Exception: print(Exception) pass
async def test_send_sms_unauthorized_from_phone_number_async(self): sms_client = SmsClient.from_connection_string(self.connection_str) with pytest.raises(HttpResponseError) as ex: async with sms_client: # calling send() with sms values await sms_client.send(from_="+14255550123", to=[self.phone_number], message="Hello World via SMS") assert ex.value.message is not None
async def test_send_sms_fake_from_phone_number_async(self): sms_client = SmsClient.from_connection_string(self.connection_str) with pytest.raises(HttpResponseError) as ex: async with sms_client: # calling send() with sms values await sms_client.send(from_="+15550000000", to=[self.phone_number], message="Hello World via SMS") assert str(ex.value.status_code) == "400" assert ex.value.message is not None
async def test_send_sms_async(self): sms_client = SmsClient.from_connection_string(self.connection_str) async with sms_client: # calling send() with sms values sms_response = await sms_client.send( from_phone_number=PhoneNumber(self.phone_number), to_phone_numbers=[PhoneNumber(self.phone_number)], message="Hello World via SMS", send_sms_options=SendSmsOptions(enable_delivery_report=True)) # optional property assert sms_response.message_id is not None
async def test_send_sms_single_async(self): sms_client = SmsClient.from_connection_string(self.connection_str) async with sms_client: # calling send() with sms values sms_responses = await sms_client.send( from_=self.phone_number, to=self.phone_number, message="Hello World via SMS") assert len(sms_responses) == 1 self.verify_successful_sms_response(sms_responses[0])
async def test_send_sms_multiple_with_options_async(self): sms_client = SmsClient.from_connection_string(self.connection_str) async with sms_client: # calling send() with sms values sms_responses = await sms_client.send( from_=self.phone_number, to=[self.phone_number, self.phone_number], message="Hello World via SMS", enable_delivery_report=True, # optional property tag="custom-tag") # optional property assert len(sms_responses) == 2 self.verify_successful_sms_response(sms_responses[0]) self.verify_successful_sms_response(sms_responses[1])
async def send_sms_async(self): connection_string = "COMMUNICATION_SERVICES_CONNECTION_STRING" sms_client = SmsClient.from_connection_string(connection_string) async with sms_client: try: # calling send() with constructed request object smsresponse = await sms_client.send( from_phone_number=PhoneNumberIdentifier("<leased-phone-number>"), to_phone_numbers=[PhoneNumberIdentifier("<to-phone-number>")], message="Hello World via SMS", send_sms_options=SendSmsOptions(enable_delivery_report=True)) # optional property except Exception: print(Exception) pass print(smsresponse)
async def test_send_sms_single_async(self): sms_client = SmsClient.from_connection_string(self.connection_str) async with sms_client: # calling send() with sms values sms_responses = await sms_client.send( from_=self.phone_number, to=self.phone_number, message="Hello World via SMS", enable_delivery_report=True, # optional property tag="custom-tag") # optional property assert len(sms_responses) is 1 for sms_response in sms_responses: self.verify_sms_response(sms_response)
async def send_sms_async(self): connection_string = "COMMUNICATION_SERVICES_CONNECTION_STRING" sms_client = SmsClient.from_connection_string(connection_string) async with sms_client: try: # calling send() with sms values sms_responses = await sms_client.send( from_="<leased-phone-number>", to=[ "<to-phone-number-1>", "<to-phone-number-2>", "<to-phone-number-3>" ], message="Hello World via SMS", enable_delivery_report=True, # optional property tag="custom-tag") # optional property except Exception: print(Exception) pass failed_recipients = [] for sms_response in sms_responses: if (sms_response.successful): print( "Message with message id {} was successful sent to {}". format(sms_response.message_id, sms_response.to)) else: print( "Message failed to send to {} with the status code {} and error: {}" .format(sms_response.to, sms_response.http_status_code, sms_response.error_message)) if (sms_response.http_status_code != 400): failed_recipients.append(sms_response.to) try: # calling send() with failed recipients sms_responses = await sms_client.send( from_="<leased-phone-number>", to=failed_recipients, message="Hello World via SMS", enable_delivery_report=True, # optional property tag="custom-tag") # optional property except Exception: print(Exception) pass
async def test_send_sms_invalid_to_phone_number_async(self): sms_client = SmsClient.from_connection_string(self.connection_str) async with sms_client: # calling send() with sms values sms_responses = await sms_client.send( from_=self.phone_number, to=["+1234567891011"], message="Hello World via SMS", enable_delivery_report=True, # optional property tag="custom-tag") # optional property assert len(sms_responses) is 1 for sms_response in sms_responses: assert sms_response.http_status_code == 400 assert not sms_response.successful
async def test_send_sms_fake_to_phone_number_async(self): sms_client = SmsClient.from_connection_string(self.connection_str) async with sms_client: # calling send() with sms values sms_responses = await sms_client.send( from_=self.phone_number, to=["+15550000000"], message="Hello World via SMS") assert len(sms_responses) == 1 assert sms_responses[0].message_id is None assert sms_responses[0].http_status_code == 400 assert sms_responses[ 0].error_message == "Invalid To phone number format." assert not sms_responses[0].successful
async def test_send_sms_async_from_managed_identity(self): endpoint, access_key = parse_connection_str(self.connection_str) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() sms_client = SmsClient(endpoint, credential) print(sms_client) async with sms_client: # calling send() with sms values sms_response = await sms_client.send( from_phone_number=PhoneNumber(self.phone_number), to_phone_numbers=[PhoneNumber(self.phone_number)], message="Hello World via SMS", send_sms_options=SendSmsOptions(enable_delivery_report=True) ) # optional property assert sms_response.message_id is not None
async def test_send_sms_from_managed_identity_async(self): endpoint, access_key = parse_connection_str(self.connection_str) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() sms_client = SmsClient(endpoint, credential) async with sms_client: # calling send() with sms values sms_responses = await sms_client.send( from_=self.phone_number, to=[self.phone_number], message="Hello World via SMS") assert len(sms_responses) == 1 self.verify_successful_sms_response(sms_responses[0])
async def test_send_sms_unique_message_ids_async(self): sms_client = SmsClient.from_connection_string(self.connection_str) async with sms_client: # calling send() with sms values sms_responses_1 = await sms_client.send( from_=self.phone_number, to=[self.phone_number], message="Hello World via SMS") # calling send() again with the same sms values sms_responses_2 = await sms_client.send( from_=self.phone_number, to=[self.phone_number], message="Hello World via SMS") assert sms_responses_1[0].message_id != sms_responses_2[ 0].message_id
async def test_send_message_async(self): phone_number = "+14255550123" raised = False async def mock_send(*_, **__): return mock_response(status_code=202, json_payload={ "value": [{ "to": phone_number, "messageId": "id", "httpStatusCode": "202", "errorMessage": "null", "repeatabilityResult": "accepted", "successful": "true" }] }) sms_client = SmsClient("https://endpoint", FakeTokenCredential(), transport=Mock(send=mock_send)) sms_response = None try: sms_responses = await sms_client.send( from_=phone_number, to=[phone_number], message="Hello World via SMS", enable_delivery_report=True, tag="custom-tag") sms_response = sms_responses[0] except: raised = True raise self.assertFalse(raised, 'Expected is no excpetion raised') self.assertEqual(phone_number, sms_response.to) self.assertIsNotNone(sms_response.message_id) self.assertEqual(202, sms_response.http_status_code) self.assertIsNotNone(sms_response.error_message) self.assertTrue(sms_response.successful)
async def test_send_sms_unique_message_ids_async(self): sms_client = SmsClient.from_connection_string(self.connection_str) async with sms_client: # calling send() with sms values sms_responses_1 = await sms_client.send( from_=self.phone_number, to=[self.phone_number], message="Hello World via SMS") # calling send() again with the same sms values sms_responses_2 = await sms_client.send( from_=self.phone_number, to=[self.phone_number], message="Hello World via SMS") self.verify_successful_sms_response(sms_responses_1[0]) self.verify_successful_sms_response(sms_responses_2[0]) # message ids should be unique due to having a different idempotency key assert sms_responses_1[0].message_id != sms_responses_2[ 0].message_id
async def test_send_sms_async_from_managed_identity(self): endpoint, access_key = parse_connection_str(self.connection_str) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() sms_client = SmsClient(endpoint, credential) async with sms_client: # calling send() with sms values sms_responses = await sms_client.send( from_=self.phone_number, to=[self.phone_number], message="Hello World via SMS", enable_delivery_report=True, # optional property tag="custom-tag") # optional property assert len(sms_responses) is 1 for sms_response in sms_responses: self.verify_sms_response(sms_response)
async def test_send_message_parameters_async(self, mock_send): phone_number = "+14255550123" msg = "Hello World via SMS" tag = "custom-tag" sms_client = SmsClient("https://endpoint", FakeTokenCredential()) await sms_client.send(from_=phone_number, to=[phone_number], message=msg, enable_delivery_report=True, tag=tag) send_message_request = mock_send.call_args[0][0] self.assertEqual(phone_number, send_message_request.from_property) self.assertEqual(phone_number, send_message_request.sms_recipients[0].to) self.assertIsNotNone( send_message_request.sms_recipients[0].repeatability_request_id) self.assertIsNotNone( send_message_request.sms_recipients[0].repeatability_first_sent) self.assertTrue( send_message_request.sms_send_options.enable_delivery_report) self.assertEqual(tag, send_message_request.sms_send_options.tag)