Skip to content

Commit cff747c

Browse files
dinazavyrDina Saparbaeva
andauthored
[communication]-SMS-Replaced DefaultAzureCredential with get_credential() (Azure#38941)
* Replaced DefaultAzureCredential with get_credential() to get all available auth options * Testing env variables for AzurePipelinesCredential * Added logging * Adjusted logging to warning * Trying to see values of system variables * Added system access token as env variable * Removed system access token as it is defined already * Removed logging lines * Reverted changes in shared files. * Reverted changes in shared tests utils.py. --------- Co-authored-by: Dina Saparbaeva <[email protected]>
1 parent 24da367 commit cff747c

File tree

4 files changed

+46
-32
lines changed

4 files changed

+46
-32
lines changed

sdk/communication/azure-communication-sms/samples/sms_token_credential_auth_sample.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ class SmsTokenCredentialAuthSample(object):
3131

3232
connection_string = os.getenv("COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING")
3333
phone_number = os.getenv("SMS_PHONE_NUMBER")
34+
client_id = os.getenv("AZURE_CLIENT_ID")
35+
client_secret = os.getenv("AZURE_CLIENT_SECRET")
36+
tenant_id = os.getenv("AZURE_TENANT_ID")
3437

3538
def sms_token_credential_auth(self):
3639
# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have
3740
# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
38-
endpoint, _ = parse_connection_str(self.connection_string)
39-
sms_client = SmsClient(endpoint, DefaultAzureCredential())
41+
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
42+
endpoint, _ = parse_connection_str(self.connection_string)
43+
sms_client = SmsClient(endpoint, DefaultAzureCredential())
44+
else:
45+
sms_client = SmsClient.from_connection_string(self.connection_string)
4046

4147
# calling send() with sms values
4248
sms_responses = sms_client.send(from_=self.phone_number, to=self.phone_number, message="Hello World via SMS")

sdk/communication/azure-communication-sms/samples/sms_token_credential_auth_sample_async.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,38 @@ class SmsTokenCredentialAuthSampleAsync(object):
3232

3333
connection_string = os.getenv("COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING")
3434
phone_number = os.getenv("SMS_PHONE_NUMBER")
35+
client_id = os.getenv("AZURE_CLIENT_ID")
36+
client_secret = os.getenv("AZURE_CLIENT_SECRET")
37+
tenant_id = os.getenv("AZURE_TENANT_ID")
3538

3639
async def sms_token_credential_auth_async(self):
3740
# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have
3841
# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
39-
endpoint, _ = parse_connection_str(self.connection_string)
40-
sms_client = SmsClient(endpoint, DefaultAzureCredential())
42+
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
43+
endpoint, _ = parse_connection_str(self.connection_string)
44+
sms_client = SmsClient(endpoint, DefaultAzureCredential())
45+
else:
46+
sms_client = SmsClient.from_connection_string(self.connection_string)
4147

4248
async with sms_client:
43-
try:
44-
# calling send() with sms values
45-
sms_responses = await sms_client.send(
46-
from_=self.phone_number, to=self.phone_number, message="Hello World via SMS"
47-
)
48-
sms_response = sms_responses[0]
49+
# calling send() with sms values
50+
sms_responses = await sms_client.send(
51+
from_=self.phone_number, to=self.phone_number, message="Hello World via SMS"
52+
)
53+
sms_response = sms_responses[0]
4954

50-
if sms_response.successful:
51-
print(
52-
"Message with message id {} was successful sent to {}".format(
53-
sms_response.message_id, sms_response.to
54-
)
55+
if sms_response.successful:
56+
print(
57+
"Message with message id {} was successful sent to {}".format(
58+
sms_response.message_id, sms_response.to
5559
)
56-
else:
57-
print(
58-
"Message failed to send to {} with the status code {} and error: {}".format(
59-
sms_response.to, sms_response.http_status_code, sms_response.error_message
60-
)
60+
)
61+
else:
62+
print(
63+
"Message failed to send to {} with the status code {} and error: {}".format(
64+
sms_response.to, sms_response.http_status_code, sms_response.error_message
6165
)
62-
except Exception:
63-
print(Exception)
64-
pass
66+
)
6567

6668

6769
if __name__ == "__main__":

sdk/communication/azure-communication-sms/tests/test_sms_client_e2e.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
# Licensed under the MIT License. See License.txt in the project root for
44
# license information.
55
# --------------------------------------------------------------------------
6+
import os
7+
import logging
8+
import sys
9+
610
import pytest
7-
from devtools_testutils import AzureRecordedTestCase, is_live, recorded_by_proxy, set_bodiless_matcher
8-
from _shared.utils import get_http_logging_policy
911
from devtools_testutils.fake_credentials import FakeTokenCredential
12+
from devtools_testutils import get_credential, is_live, recorded_by_proxy, set_bodiless_matcher
13+
from _shared.utils import get_http_logging_policy
1014
from azure.core.exceptions import HttpResponseError
11-
from azure.identity import DefaultAzureCredential
1215
from acs_sms_test_case import ACSSMSTestCase
1316
from azure.communication.sms import SmsClient
1417

@@ -52,7 +55,7 @@ def test_send_sms_from_managed_identity(self):
5255
if not is_live():
5356
credential = FakeTokenCredential()
5457
else:
55-
credential = DefaultAzureCredential()
58+
credential = get_credential()
5659
sms_client = SmsClient(self.endpoint, credential, http_logging_policy=get_http_logging_policy())
5760

5861
# calling send() with sms values

sdk/communication/azure-communication-sms/tests/test_sms_client_e2e_async.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
# Licensed under the MIT License. See License.txt in the project root for
44
# license information.
55
# --------------------------------------------------------------------------
6+
import os
7+
import logging
8+
import sys
9+
610
import pytest
711
from devtools_testutils.aio import recorded_by_proxy_async
8-
from devtools_testutils import is_live
12+
from devtools_testutils.fake_credentials_async import AsyncFakeCredential
13+
from devtools_testutils import get_credential, is_live
914
from azure.communication.sms.aio import SmsClient
1015
from azure.core.exceptions import HttpResponseError
11-
from _shared.utils import get_http_logging_policy
16+
from _shared.utils import async_create_token_credential, get_http_logging_policy
1217
from acs_sms_test_case import ACSSMSTestCase
13-
from azure.identity.aio import DefaultAzureCredential
14-
from devtools_testutils.fake_credentials_async import AsyncFakeCredential
1518

1619

1720
@pytest.mark.asyncio
@@ -57,7 +60,7 @@ async def test_send_sms_from_managed_identity_async(self):
5760
if not is_live():
5861
credential = AsyncFakeCredential()
5962
else:
60-
credential = DefaultAzureCredential()
63+
credential = get_credential(is_async=True)
6164
sms_client = SmsClient(self.endpoint, credential, http_logging_policy=get_http_logging_policy())
6265

6366
async with sms_client:

0 commit comments

Comments
 (0)