Skip to content

Commit f19ce6f

Browse files
Copilotxiangyan99
andcommitted
Fix black formatting issues in test files
Co-authored-by: xiangyan99 <[email protected]>
1 parent a1bc1ca commit f19ce6f

File tree

2 files changed

+180
-64
lines changed

2 files changed

+180
-64
lines changed

sdk/identity/azure-identity/tests/test_powershell_credential.py

Lines changed: 86 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
)
2424
import pytest
2525

26-
from credscan_ignore import POWERSHELL_INVALID_OPERATION_EXCEPTION, POWERSHELL_NOT_LOGGED_IN_ERROR
26+
from credscan_ignore import (
27+
POWERSHELL_INVALID_OPERATION_EXCEPTION,
28+
POWERSHELL_NOT_LOGGED_IN_ERROR,
29+
)
2730
from helpers import INVALID_CHARACTERS, GET_TOKEN_METHODS
2831

2932

@@ -35,7 +38,9 @@
3538

3639

3740
def raise_called_process_error(return_code, stdout, stderr="", cmd="..."):
38-
error = subprocess.CalledProcessError(return_code, cmd=cmd, output=stdout, stderr=stderr)
41+
error = subprocess.CalledProcessError(
42+
return_code, cmd=cmd, output=stdout, stderr=stderr
43+
)
3944
return Mock(side_effect=error)
4045

4146

@@ -57,7 +62,9 @@ def test_multiple_scopes(get_token_method):
5762
"""The credential should raise ValueError when get_token is called with more than one scope"""
5863

5964
with pytest.raises(ValueError):
60-
getattr(AzurePowerShellCredential(), get_token_method)("one scope", "and another")
65+
getattr(AzurePowerShellCredential(), get_token_method)(
66+
"one scope", "and another"
67+
)
6168

6269

6370
@pytest.mark.parametrize("get_token_method", GET_TOKEN_METHODS)
@@ -93,7 +100,9 @@ def test_invalid_scopes(get_token_method):
93100
getattr(AzurePowerShellCredential(), get_token_method)("scope" + c)
94101

95102

96-
@pytest.mark.parametrize("stderr,get_token_method", product(("", PREPARING_MODULES), GET_TOKEN_METHODS))
103+
@pytest.mark.parametrize(
104+
"stderr,get_token_method", product(("", PREPARING_MODULES), GET_TOKEN_METHODS)
105+
)
97106
def test_get_token(stderr, get_token_method):
98107
"""The credential should parse Azure PowerShell's output to an AccessToken"""
99108

@@ -126,7 +135,9 @@ def test_get_token(stderr, get_token_method):
126135
args, kwargs = Popen().communicate.call_args
127136

128137

129-
@pytest.mark.parametrize("stderr,get_token_method", product(("", PREPARING_MODULES), GET_TOKEN_METHODS))
138+
@pytest.mark.parametrize(
139+
"stderr,get_token_method", product(("", PREPARING_MODULES), GET_TOKEN_METHODS)
140+
)
130141
def test_get_token_tenant_id(stderr, get_token_method):
131142
expected_access_token = "access"
132143
expected_expires_on = 1617923581
@@ -149,7 +160,9 @@ def test_ignores_extraneous_stdout_content(get_token_method):
149160
expected_access_token = "access"
150161
expected_expires_on = 1617923581
151162
motd = "MOTD: Customize your experience: save your profile to $HOME/.config/PowerShell\n"
152-
Popen = get_mock_Popen(stdout=motd + "azsdk%{}%{}".format(expected_access_token, expected_expires_on))
163+
Popen = get_mock_Popen(
164+
stdout=motd + "azsdk%{}%{}".format(expected_access_token, expected_expires_on)
165+
)
153166

154167
with patch(POPEN, Popen):
155168
token = getattr(AzurePowerShellCredential(), get_token_method)("scope")
@@ -198,7 +211,10 @@ def test_powershell_not_installed_sh(get_token_method):
198211

199212
@pytest.mark.parametrize(
200213
"stderr,get_token_method",
201-
product((POWERSHELL_INVALID_OPERATION_EXCEPTION, POWERSHELL_NOT_LOGGED_IN_ERROR), GET_TOKEN_METHODS),
214+
product(
215+
(POWERSHELL_INVALID_OPERATION_EXCEPTION, POWERSHELL_NOT_LOGGED_IN_ERROR),
216+
GET_TOKEN_METHODS,
217+
),
202218
)
203219
def test_not_logged_in(stderr, get_token_method):
204220
"""The credential should raise CredentialUnavailableError when a user isn't logged in to Azure PowerShell"""
@@ -217,7 +233,9 @@ def test_blocked_by_execution_policy(get_token_method):
217233
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><S S="Error">Import-Module : Errors occurred while loading the format data file: _x000D__x000A_</S><S S="Error">C:\Users\foo\Documents\WindowsPowerShell\Modules\Az.Accounts\2.2.7\Accounts.format.ps1xml, , C:\Users\foo\Documents\WindowsPowerShell\Modules\Az.Accounts\2.2.7\Accounts.format.ps1xml: The file was skipped because of the _x000D__x000A_</S><S S="Error">following validation exception: AuthorizationManager check failed.._x000D__x000A_</S><S S="Error">C:\Users\foo\Documents\WindowsPowerShell\Modules\Az.Accounts\2.2.7\Accounts.generated.format.ps1xml, , C:\Users\foo\Documents\WindowsPowerShell\Modules\Az.Accounts\2.2.7\Accounts.generated.format.ps1xml: The file was skipped _x000D__x000A_</S><S S="Error">because of the following validation exception: AuthorizationManager check failed.._x000D__x000A_</S><S S="Error">At line:4 char:6_x000D__x000A_</S><S S="Error">+ $m = Import-Module Az.Accounts -MinimumVersion $minimumVersion -PassT ..._x000D__x000A_</S><S S="Error">+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~_x000D__x000A_</S><S S="Error"> + CategoryInfo : InvalidOperation: (:) [Import-Module], RuntimeException_x000D__x000A_</S><S S="Error"> + FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand_x000D__x000A_</S><S S="Error"> _x000D__x000A_</S></Objs>"""
218234
Popen = get_mock_Popen(return_code=1, stderr=stderr)
219235
with patch(POPEN, Popen):
220-
with pytest.raises(CredentialUnavailableError, match=BLOCKED_BY_EXECUTION_POLICY):
236+
with pytest.raises(
237+
CredentialUnavailableError, match=BLOCKED_BY_EXECUTION_POLICY
238+
):
221239
getattr(AzurePowerShellCredential(), get_token_method)("scope")
222240

223241

@@ -230,7 +248,9 @@ def test_timeout(get_token_method):
230248
proc = Mock(communicate=Mock(side_effect=TimeoutExpired("", 42)), returncode=None)
231249
with patch(POPEN, Mock(return_value=proc)):
232250
with pytest.raises(CredentialUnavailableError):
233-
getattr(AzurePowerShellCredential(process_timeout=42), get_token_method)("scope")
251+
getattr(AzurePowerShellCredential(process_timeout=42), get_token_method)(
252+
"scope"
253+
)
234254

235255
assert proc.communicate.call_count == 1
236256
# Ensure custom timeout is passed to subprocess
@@ -294,17 +314,23 @@ def Popen(args, **kwargs):
294314
stderr = error_message
295315
return_code = 9009
296316
else:
297-
assert args[-1].startswith("powershell"), 'credential should fall back to "powershell"'
317+
assert args[-1].startswith(
318+
"powershell"
319+
), 'credential should fall back to "powershell"'
298320
stdout = NO_AZ_ACCOUNT_MODULE
299321
stderr = ""
300322
return_code = 0
301323

302-
return Mock(communicate=Mock(return_value=(stdout, stderr)), returncode=return_code)
324+
return Mock(
325+
communicate=Mock(return_value=(stdout, stderr)), returncode=return_code
326+
)
303327

304328
with patch(AzurePowerShellCredential.__module__ + ".sys.platform", "win32"):
305329
with patch.dict("os.environ", {"SYSTEMROOT": "foo"}):
306330
with patch(POPEN, Popen):
307-
with pytest.raises(CredentialUnavailableError, match=AZ_ACCOUNT_NOT_INSTALLED):
331+
with pytest.raises(
332+
CredentialUnavailableError, match=AZ_ACCOUNT_NOT_INSTALLED
333+
):
308334
getattr(AzurePowerShellCredential(), get_token_method)("scope")
309335

310336
assert Fake.calls == 2
@@ -317,7 +343,9 @@ def test_multitenant_authentication(get_token_method):
317343
second_token = first_token * 2
318344

319345
def fake_Popen(command, **_):
320-
assert command[-1].startswith("pwsh -NoProfile -NonInteractive -EncodedCommand ")
346+
assert command[-1].startswith(
347+
"pwsh -NoProfile -NonInteractive -EncodedCommand "
348+
)
321349
match = re.search(r"-EncodedCommand\s+(\S+)", command[-1])
322350
assert match, "couldn't find encoded script in command line"
323351
encoded_script = match.groups()[0]
@@ -326,7 +354,9 @@ def fake_Popen(command, **_):
326354
assert match
327355
tenant = match.group(1)
328356

329-
assert not tenant or tenant == second_tenant, 'unexpected tenant "{}"'.format(tenant)
357+
assert not tenant or tenant == second_tenant, 'unexpected tenant "{}"'.format(
358+
tenant
359+
)
330360
token = first_token if not tenant else second_token
331361
stdout = "azsdk%{}%{}".format(token, int(time.time()) + 3600)
332362

@@ -354,7 +384,9 @@ def test_multitenant_authentication_not_allowed(get_token_method):
354384
expected_token = "***"
355385

356386
def fake_Popen(command, **_):
357-
assert command[-1].startswith("pwsh -NoProfile -NonInteractive -EncodedCommand ")
387+
assert command[-1].startswith(
388+
"pwsh -NoProfile -NonInteractive -EncodedCommand "
389+
)
358390
match = re.search(r"-EncodedCommand\s+(\S+)", command[-1])
359391
assert match, "couldn't find encoded script in command line"
360392
encoded_script = match.groups()[0]
@@ -374,7 +406,10 @@ def fake_Popen(command, **_):
374406
token = getattr(credential, get_token_method)("scope")
375407
assert token.token == expected_token
376408

377-
with patch.dict("os.environ", {EnvironmentVariables.AZURE_IDENTITY_DISABLE_MULTITENANTAUTH: "true"}):
409+
with patch.dict(
410+
"os.environ",
411+
{EnvironmentVariables.AZURE_IDENTITY_DISABLE_MULTITENANTAUTH: "true"},
412+
):
378413
kwargs = {"tenant_id": "12345"}
379414
if get_token_method == "get_token_info":
380415
kwargs = {"options": kwargs}
@@ -385,47 +420,67 @@ def fake_Popen(command, **_):
385420
@pytest.mark.parametrize("get_token_method", GET_TOKEN_METHODS)
386421
def test_claims_challenge_error(get_token_method):
387422
"""The credential should raise CredentialUnavailableError when claims challenge is provided"""
388-
423+
389424
if get_token_method == "get_token":
390425
# Test claims parameter in get_token method
391426
with pytest.raises(CredentialUnavailableError) as exc_info:
392-
getattr(AzurePowerShellCredential(), get_token_method)("scope", claims="some-claims")
393-
assert "Fail to get token, please run Connect-AzAccount --ClaimsChallenge some-claims" in str(exc_info.value)
427+
getattr(AzurePowerShellCredential(), get_token_method)(
428+
"scope", claims="some-claims"
429+
)
430+
assert (
431+
"Fail to get token, please run Connect-AzAccount --ClaimsChallenge some-claims"
432+
in str(exc_info.value)
433+
)
394434
else:
395435
# Test claims in options for get_token_info method
396436
with pytest.raises(CredentialUnavailableError) as exc_info:
397-
getattr(AzurePowerShellCredential(), get_token_method)("scope", options={"claims": "some-claims"})
398-
assert "Fail to get token, please run Connect-AzAccount --ClaimsChallenge some-claims" in str(exc_info.value)
437+
getattr(AzurePowerShellCredential(), get_token_method)(
438+
"scope", options={"claims": "some-claims"}
439+
)
440+
assert (
441+
"Fail to get token, please run Connect-AzAccount --ClaimsChallenge some-claims"
442+
in str(exc_info.value)
443+
)
399444

400445

401446
@pytest.mark.parametrize("get_token_method", GET_TOKEN_METHODS)
402447
def test_empty_claims_no_error(get_token_method):
403448
"""The credential should not raise error for empty or None claims"""
404-
449+
405450
# Mock successful token response
406451
expected_access_token = "access"
407452
expected_expires_on = 1617923581
408453
stdout = "azsdk%{}%{}".format(expected_access_token, expected_expires_on)
409-
454+
410455
Popen = get_mock_Popen(stdout=stdout)
411456
with patch(POPEN, Popen):
412457
if get_token_method == "get_token":
413458
# Test None claims parameter in get_token method
414-
token = getattr(AzurePowerShellCredential(), get_token_method)("scope", claims=None)
459+
token = getattr(AzurePowerShellCredential(), get_token_method)(
460+
"scope", claims=None
461+
)
415462
assert token.token == expected_access_token
416-
463+
417464
# Test empty string claims
418-
token = getattr(AzurePowerShellCredential(), get_token_method)("scope", claims="")
465+
token = getattr(AzurePowerShellCredential(), get_token_method)(
466+
"scope", claims=""
467+
)
419468
assert token.token == expected_access_token
420469
else:
421470
# Test None claims in options for get_token_info method
422-
token = getattr(AzurePowerShellCredential(), get_token_method)("scope", options={"claims": None})
471+
token = getattr(AzurePowerShellCredential(), get_token_method)(
472+
"scope", options={"claims": None}
473+
)
423474
assert token.token == expected_access_token
424-
475+
425476
# Test empty string claims in options
426-
token = getattr(AzurePowerShellCredential(), get_token_method)("scope", options={"claims": ""})
477+
token = getattr(AzurePowerShellCredential(), get_token_method)(
478+
"scope", options={"claims": ""}
479+
)
427480
assert token.token == expected_access_token
428-
481+
429482
# Test missing claims key in options
430-
token = getattr(AzurePowerShellCredential(), get_token_method)("scope", options={})
483+
token = getattr(AzurePowerShellCredential(), get_token_method)(
484+
"scope", options={}
485+
)
431486
assert token.token == expected_access_token

0 commit comments

Comments
 (0)