Skip to content

Commit 0884d05

Browse files
committed
Add email address replacer for test framework
1 parent 6f3fa67 commit 0884d05

File tree

3 files changed

+326
-280
lines changed

3 files changed

+326
-280
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: 26 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,31 @@ def process_response(self, response):
210211
return response
211212

212213

214+
class EmailAddressReplacer(RecordingProcessor):
215+
"""Replace email address like [email protected] with [email protected]"""
216+
217+
EMAIL_REPLACEMENT = '[email protected]'
218+
219+
def process_request(self, request):
220+
return request
221+
222+
def process_response(self, response):
223+
if response['body']['string']:
224+
body = _byte_to_str(response['body']['string'])
225+
pattern = r'[\w.%#+-]+[%40|@|_]microsoft.com'
226+
index = 0
227+
replaced_body = ''
228+
for match in re.finditer(pattern, body):
229+
start = match.start()
230+
end = match.end()
231+
replaced_body += body[index:start] + self.EMAIL_REPLACEMENT
232+
index = end
233+
if index < len(body):
234+
replaced_body += body[index:]
235+
response['body']['string'] = replaced_body
236+
return response
237+
238+
213239
class AADAuthRequestFilter(RecordingProcessor):
214240
"""Remove oauth authentication requests and responses from recording.
215241
This is derived from OAuthRequestResponsesFilter.

0 commit comments

Comments
 (0)