Skip to content

Commit

Permalink
[Graph] Support special characters in object names (#22739)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiasli authored Jun 14, 2022
1 parent 95a19f4 commit 2591276
Show file tree
Hide file tree
Showing 5 changed files with 424 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/azure-cli-testsdk/azure/cli/testsdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
from .checkers import (JMESPathCheck, JMESPathCheckExists, JMESPathCheckGreaterThan, NoneCheck, StringCheck,
StringContainCheck)
from .decorators import api_version_constraint
from .utilities import create_random_name, MSGraphUserReplacer
from .utilities import create_random_name, MSGraphNameReplacer
from .patches import MOCKED_USER_NAME

__all__ = ['ScenarioTest', 'LiveScenarioTest', 'ResourceGroupPreparer', 'StorageAccountPreparer',
'RoleBasedServicePrincipalPreparer', 'KeyVaultPreparer', 'ManagedHSMPreparer',
'ManagedApplicationPreparer', 'CliTestError', 'JMESPathCheck',
'JMESPathCheckExists', 'NoneCheck', 'live_only', 'record_only', 'StringCheck', 'StringContainCheck',
'get_sha1_hash', 'KeyVaultPreparer', 'JMESPathCheckGreaterThan', 'api_version_constraint',
'create_random_name', 'MOCKED_USER_NAME', 'MSGraphUserReplacer', 'LocalContextScenarioTest',
'create_random_name', 'MOCKED_USER_NAME', 'MSGraphNameReplacer', 'LocalContextScenarioTest',
'VirtualNetworkPreparer', 'VnetNicPreparer', 'LogAnalyticsWorkspacePreparer']


Expand Down
27 changes: 17 additions & 10 deletions src/azure-cli-testsdk/azure/cli/testsdk/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,26 +180,33 @@ def process_response(self, response):
return response


class MSGraphUserReplacer(RecordingProcessor):
def __init__(self, test_user, mock_user):
self.test_user = test_user
self.mock_user = mock_user
class MSGraphNameReplacer(RecordingProcessor):
"""If a name appears in URL, it should be percent-encoded, e.g.
- test-name+/ is encoded as test-name%2B%2F
- [email protected] is encoded as example%40example.com
Theoretically, GeneralNameReplacer should also follow this pattern, but since we haven't seen any ARM name that
is percent-encoded, we only replace percent-encoded names for MS Graph for better test performance.
"""
def __init__(self, test_name, mock_name):
from urllib.parse import quote
self.test_name = test_name
self.test_name_encoded = quote(test_name, safe='')
self.mock_name = mock_name
self.mock_name_encoded = quote(mock_name, safe='')

def process_request(self, request):
if self.test_user in request.uri:
request.uri = request.uri.replace(self.test_user, self.mock_user)
request.uri = request.uri.replace(self.test_name_encoded, self.mock_name_encoded)

if request.body:
body = _byte_to_str(request.body)
if self.test_user in body:
request.body = body.replace(self.test_user, self.mock_user)
request.body = body.replace(self.test_name, self.mock_name)

return request

def process_response(self, response):
if response['body']['string']:
response['body']['string'] = response['body']['string'].replace(self.test_user,
self.mock_user)
response['body']['string'] = response['body']['string'].replace(self.test_name, self.mock_name)
return response


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ def oauth2_permission_grant_delete(self, id):

def _filter_to_query(filter):
if filter is not None:
return "?$filter={}".format(filter)
# https://docs.microsoft.com/en-us/graph/query-parameters#encoding-query-parameters
# The values of query parameters should be percent-encoded.
from urllib.parse import quote
return "?$filter={}".format(quote(filter, safe=''))
return ''


Expand Down
Loading

0 comments on commit 2591276

Please sign in to comment.