Skip to content

Commit a0bb5a2

Browse files
Added record files
1 parent f504024 commit a0bb5a2

File tree

7 files changed

+105
-36
lines changed

7 files changed

+105
-36
lines changed

sdk/communication/azure-communication-sms/azure/communication/sms/_shared/user_credential.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
)
1212
import six
1313
from msrest.serialization import TZ_UTC
14-
from .utils import create_access_token, get_authentication_policy
14+
from .utils import create_access_token
1515

1616
class CommunicationUserCredential(object):
1717
"""Credential type used for authenticating to an Azure Communication service.

sdk/communication/azure-communication-sms/azure/communication/sms/_shared/utils.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,35 @@ def create_access_token(token):
6969
except ValueError:
7070
raise ValueError(token_parse_err_msg)
7171

72-
def get_authentication_policy(
73-
endpoint, # type: str
74-
credential # type: TokenCredential or str
75-
):
76-
# type: (...) -> BearerTokenCredentialPolicy or HMACCredentialPolicy
77-
"""Returns the correct authentication policy based
78-
on which credential is being passed.
79-
:param endpoint: The endpoint to which we are authenticating to.
80-
:type endpoint: str
81-
:param credential: The credential we use to authenticate to the service
82-
:type credential: TokenCredential or str
83-
:rtype: ~azure.core.pipeline.policies.BearerTokenCredentialPolicy
84-
~HMACCredentialsPolicy
85-
"""
72+
def get_authentication_policy(
73+
endpoint, # type: str
74+
credential, # type: TokenCredential or str
75+
is_async=False, # type: bool
76+
):
77+
# type: (...) -> BearerTokenCredentialPolicy or HMACCredentialPolicy
78+
"""Returns the correct authentication policy based
79+
on which credential is being passed.
8680
87-
if credential is None:
88-
raise ValueError("Parameter 'credential' must not be None.")
89-
if hasattr(credential, "get_token"):
90-
from azure.core.pipeline.policies import BearerTokenCredentialPolicy
91-
return BearerTokenCredentialPolicy(
92-
credential, "https://communication.azure.com//.default")
93-
if isinstance(credential, str):
94-
from .._shared.policy import HMACCredentialsPolicy
95-
return HMACCredentialsPolicy(endpoint, credential)
81+
:param endpoint: The endpoint to which we are authenticating to.
82+
:type endpoint: str
83+
:param credential: The credential we use to authenticate to the service
84+
:type credential: TokenCredential or str
85+
:param isAsync: For async clients there is a need to decode the url
86+
:type bool: isAsync or str
9687
97-
raise TypeError("Unsupported credential: {}. Use an access token string to use HMACCredentialsPolicy"
98-
"or a token credential from azure.identity".format(type(credential)))
88+
:rtype: ~azure.core.pipeline.policies.BearerTokenCredentialPolicy
89+
~HMACCredentialsPolicy
90+
"""
91+
92+
if credential is None:
93+
raise ValueError("Parameter 'credential' must not be None.")
94+
if hasattr(credential, "get_token"):
95+
from azure.core.pipeline.policies import BearerTokenCredentialPolicy
96+
return BearerTokenCredentialPolicy(
97+
credential, "https://communication.azure.com//.default")
98+
if isinstance(credential, str):
99+
from .._shared.policy import HMACCredentialsPolicy
100+
return HMACCredentialsPolicy(endpoint, credential, decode_url=is_async)
101+
102+
raise TypeError("Unsupported credential: {}. Use an access token string to use HMACCredentialsPolicy"
103+
"or a token credential from azure.identity".format(type(credential)))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-e ../../../tools/azure-sdk-tools
22
-e ../../../tools/azure-devtools
3+
-e ../../identity/azure-identity
34
../../core/azure-core
45
../azure-communication-nspkg
56
aiohttp>=3.0; python_version >= '3.5'

sdk/communication/azure-communication-sms/tests/recordings/test_sms_client_e2e.test_send_sms.yaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ interactions:
1414
Content-Type:
1515
- application/json
1616
Date:
17-
- Fri, 09 Oct 2020 23:20:19 GMT
17+
- Wed, 20 Jan 2021 18:11:23 GMT
1818
User-Agent:
19-
- azsdk-python-communication-sms/1.0.0b3 Python/3.8.6 (Windows-10-10.0.19041-SP0)
19+
- azsdk-python-communication-sms/1.0.0b5 Python/3.8.5 (Windows-10-10.0.19041-SP0)
2020
x-ms-return-client-request-id:
2121
- 'true'
2222
method: POST
@@ -27,13 +27,15 @@ interactions:
2727
content-type:
2828
- application/json; charset=utf-8
2929
date:
30-
- Fri, 09 Oct 2020 23:20:19 GMT
30+
- Wed, 20 Jan 2021 18:11:24 GMT
3131
ms-cv:
32-
- JCf4xQXQEkS2Tix1X3WyhA.0
32+
- 1y4+o6OmR0ulV50uPXHoEw.0
33+
request-context:
34+
- appId=
3335
transfer-encoding:
3436
- chunked
3537
x-processing-time:
36-
- 1071ms
38+
- 484ms
3739
status:
3840
code: 200
3941
message: OK

sdk/communication/azure-communication-sms/tests/recordings/test_sms_client_e2e_async.test_send_sms_async.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ interactions:
1010
Content-Type:
1111
- application/json
1212
Date:
13-
- Fri, 09 Oct 2020 23:20:20 GMT
13+
- Wed, 20 Jan 2021 18:11:24 GMT
1414
User-Agent:
15-
- azsdk-python-communication-sms/1.0.0b3 Python/3.8.6 (Windows-10-10.0.19041-SP0)
15+
- azsdk-python-communication-sms/1.0.0b5 Python/3.8.5 (Windows-10-10.0.19041-SP0)
1616
x-ms-return-client-request-id:
1717
- 'true'
1818
method: POST
@@ -21,10 +21,11 @@ interactions:
2121
body: '{"messageId": "sanitized"}'
2222
headers:
2323
content-type: application/json; charset=utf-8
24-
date: Fri, 09 Oct 2020 23:20:20 GMT
25-
ms-cv: Fh5qIzKnFUaqzbz+3C3fKQ.0
24+
date: Wed, 20 Jan 2021 18:11:24 GMT
25+
ms-cv: x7uY4q7a+EmmyTMUW8my+A.0
26+
request-context: appId=
2627
transfer-encoding: chunked
27-
x-processing-time: 815ms
28+
x-processing-time: 468ms
2829
status:
2930
code: 200
3031
message: OK
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
interactions:
2+
- request:
3+
body: '{"from": "sanitized", "to": "sanitized", "message": "Hello World via SMS",
4+
"sendSmsOptions": {"enableDeliveryReport": true}}'
5+
headers:
6+
Accept:
7+
- application/json
8+
Content-Length:
9+
- '132'
10+
Content-Type:
11+
- application/json
12+
User-Agent:
13+
- azsdk-python-communication-sms/1.0.0b5 Python/3.8.5 (Windows-10-10.0.19041-SP0)
14+
method: POST
15+
uri: https://sanitized.communication.azure.com/sms?api-version=2020-07-20-preview1
16+
response:
17+
body: '{"messageId": "sanitized"}'
18+
headers:
19+
content-type: application/json; charset=utf-8
20+
date: Wed, 20 Jan 2021 18:11:27 GMT
21+
ms-cv: 2alAVqPXy0O98kSmG+jzFA.0
22+
request-context: appId=
23+
transfer-encoding: chunked
24+
x-processing-time: 827ms
25+
status:
26+
code: 200
27+
message: OK
28+
url: https://sanitized.communication.azure.com/sms?api-version=2020-07-20-preview1
29+
version: 1

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66

77
import os
88
import pytest
9+
from azure.core.credentials import AccessToken
910
from azure.communication.sms.aio import SmsClient
1011
from azure.communication.sms import (
1112
PhoneNumber, SendSmsOptions
1213
)
14+
from azure.communication.sms._shared.utils import parse_connection_str
1315
from _shared.asynctestcase import AsyncCommunicationTestCase
1416
from _shared.testcase import (
1517
BodyReplacerProcessor, ResponseReplacerProcessor
1618
)
19+
from azure.identity import DefaultAzureCredential
1720

21+
22+
class FakeTokenCredential(object):
23+
def __init__(self):
24+
self.token = AccessToken("Fake Token", 0)
25+
26+
def get_token(self, *args):
27+
return self.token
1828
class SMSClientTestAsync(AsyncCommunicationTestCase):
1929
def __init__(self, method_name):
2030
super(SMSClientTestAsync, self).__init__(method_name)
@@ -46,3 +56,24 @@ async def test_send_sms_async(self):
4656
send_sms_options=SendSmsOptions(enable_delivery_report=True)) # optional property
4757

4858
assert sms_response.message_id is not None
59+
60+
@AsyncCommunicationTestCase.await_prepared_test
61+
@pytest.mark.live_test_only
62+
async def test_send_sms_async_from_managed_identity(self):
63+
endpoint, access_key = parse_connection_str(self.connection_str)
64+
from devtools_testutils import is_live
65+
if not is_live():
66+
credential = FakeTokenCredential()
67+
else:
68+
credential = DefaultAzureCredential()
69+
sms_client = SmsClient(endpoint, credential)
70+
print(sms_client)
71+
async with sms_client:
72+
# calling send() with sms values
73+
sms_response = await sms_client.send(
74+
from_phone_number=PhoneNumber(self.phone_number),
75+
to_phone_numbers=[PhoneNumber(self.phone_number)],
76+
message="Hello World via SMS",
77+
send_sms_options=SendSmsOptions(enable_delivery_report=True)) # optional property
78+
79+
assert sms_response.message_id is not None

0 commit comments

Comments
 (0)