Skip to content

Commit 34c73d0

Browse files
authored
{Test} Add email replacer for cli scenario test (#31262)
* Add email address replacer for test framework * add process request * fix * re.sub
1 parent 86a6cf5 commit 34c73d0

File tree

3 files changed

+320
-329
lines changed

3 files changed

+320
-329
lines changed

src/azure-cli-testsdk/azure/cli/testsdk/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
patch_progress_controller, patch_get_current_system_username)
2525
from .exceptions import CliExecutionError
2626
from .utilities import (find_recording_dir, StorageAccountKeyReplacer, GraphClientPasswordReplacer,
27-
MSGraphClientPasswordReplacer, AADAuthRequestFilter)
27+
MSGraphClientPasswordReplacer, AADAuthRequestFilter, EmailAddressReplacer)
2828
from .reverse_dependency import get_dummy_cli
2929

3030
logger = logging.getLogger('azure.cli.testsdk')
@@ -93,6 +93,7 @@ def __init__(self, method_name, config_file=None, recording_name=None,
9393
default_recording_processors = [
9494
SubscriptionRecordingProcessor(MOCKED_SUBSCRIPTION_ID),
9595
AADAuthRequestFilter(),
96+
EmailAddressReplacer(),
9697
LargeRequestBodyProcessor(),
9798
LargeResponseBodyProcessor(),
9899
DeploymentNameReplacer(),

src/azure-cli-testsdk/azure/cli/testsdk/utilities.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# --------------------------------------------------------------------------------------------
55

66
import os
7+
import re
78
from contextlib import contextmanager
89

910
from .scenario_tests import (create_random_name as create_random_name_base, RecordingProcessor)
@@ -210,6 +211,29 @@ def process_response(self, response):
210211
return response
211212

212213

214+
class EmailAddressReplacer(RecordingProcessor):
215+
"""Replace email address like xxx@microsoft.com with test@example.com"""
216+
217+
EMAIL_REPLACEMENT = 'test@example.com'
218+
219+
def _replace_email_address(self, text):
220+
pattern = r'[\w.%#+-]+[%40|@|_]microsoft.com'
221+
return re.sub(pattern, self.EMAIL_REPLACEMENT, text)
222+
223+
def process_request(self, request):
224+
request.uri = self._replace_email_address(request.uri)
225+
if request.body:
226+
body = _byte_to_str(request.body)
227+
request.body = self._replace_email_address(body)
228+
return request
229+
230+
def process_response(self, response):
231+
if response['body']['string']:
232+
body = _byte_to_str(response['body']['string'])
233+
response['body']['string'] = self._replace_email_address(body)
234+
return response
235+
236+
213237
class AADAuthRequestFilter(RecordingProcessor):
214238
"""Remove oauth authentication requests and responses from recording.
215239
This is derived from OAuthRequestResponsesFilter.

0 commit comments

Comments
 (0)