@@ -48,14 +48,8 @@ def force_progress_logging():
4848 az_logger .handlers [0 ].level = old_az_level
4949
5050
51- def _py3_byte_to_str (byte_or_str ):
52- import logging
53- logger = logging .getLogger ()
54- logger .warning (type (byte_or_str ))
55- try :
56- return str (byte_or_str , 'utf-8' ) if isinstance (byte_or_str , bytes ) else byte_or_str
57- except TypeError : # python 2 doesn't allow decoding through str
58- return str (byte_or_str )
51+ def _byte_to_str (byte_or_str ):
52+ return str (byte_or_str , 'utf-8' ) if isinstance (byte_or_str , bytes ) else byte_or_str
5953
6054
6155class StorageAccountKeyReplacer (RecordingProcessor ):
@@ -81,7 +75,7 @@ def process_request(self, request): # pylint: disable=no-self-use
8175 pass
8276 for candidate in self ._candidates :
8377 if request .body :
84- body_string = _py3_byte_to_str (request .body )
78+ body_string = _byte_to_str (request .body )
8579 request .body = body_string .replace (candidate , self .KEY_REPLACEMENT )
8680 return request
8781
@@ -99,7 +93,7 @@ def process_response(self, response):
9993 for candidate in self ._candidates :
10094 if response ['body' ]['string' ]:
10195 body = response ['body' ]['string' ]
102- response ['body' ]['string' ] = _py3_byte_to_str (body )
96+ response ['body' ]['string' ] = _byte_to_str (body )
10397 response ['body' ]['string' ] = response ['body' ]['string' ].replace (candidate , self .KEY_REPLACEMENT )
10498 return response
10599
@@ -127,11 +121,11 @@ def process_request(self, request): # pylint: disable=no-self-use
127121 pattern = r"[^/]+/applications$"
128122 if re .search (pattern , request .path , re .I ) and request .method .lower () == 'post' :
129123 self ._activated = True
130- body = _py3_byte_to_str (request .body )
124+ body = _byte_to_str (request .body )
131125 body = json .loads (body )
132126 for password_cred in body ['passwordCredentials' ]:
133127 if password_cred ['value' ]:
134- body_string = _py3_byte_to_str (request .body )
128+ body_string = _byte_to_str (request .body )
135129 request .body = body_string .replace (password_cred ['value' ], self .PWD_REPLACEMENT )
136130
137131 except (AttributeError , KeyError ):
@@ -157,18 +151,48 @@ def process_response(self, response):
157151 return response
158152
159153
160- class AADGraphUserReplacer :
154+ class MSGraphClientPasswordReplacer (RecordingProcessor ):
155+ """Replace 'secretText' property in 'addPassword' API's response."""
156+
157+ PWD_REPLACEMENT = 'replaced-microsoft-graph-password'
158+
159+ def __init__ (self ):
160+ self ._activated = False
161+
162+ def reset (self ):
163+ self ._activated = False
164+
165+ def process_request (self , request ):
166+ if request .body and self .PWD_REPLACEMENT in _byte_to_str (request .body ):
167+ return request
168+ if request .path .endswith ('/addPassword' ) and request .method .lower () == 'post' :
169+ self ._activated = True
170+ return request
171+
172+ def process_response (self , response ):
173+ if self ._activated :
174+ import json
175+
176+ body = json .loads (response ['body' ]['string' ])
177+ body ['secretText' ] = self .PWD_REPLACEMENT
178+
179+ response ['body' ]['string' ] = json .dumps (body )
180+ self ._activated = False
181+
182+ return response
183+
184+
185+ class MSGraphUserReplacer (RecordingProcessor ):
161186 def __init__ (self , test_user , mock_user ):
162187 self .test_user = test_user
163188 self .mock_user = mock_user
164189
165190 def process_request (self , request ):
166- test_user_encoded = self .test_user .replace ('@' , '%40' )
167- if test_user_encoded in request .uri :
168- request .uri = request .uri .replace (test_user_encoded , self .mock_user .replace ('@' , '%40' ))
191+ if self .test_user in request .uri :
192+ request .uri = request .uri .replace (self .test_user , self .mock_user )
169193
170194 if request .body :
171- body = _py3_byte_to_str (request .body )
195+ body = _byte_to_str (request .body )
172196 if self .test_user in body :
173197 request .body = body .replace (self .test_user , self .mock_user )
174198
0 commit comments