Skip to content

Commit 7bc61d4

Browse files
authored
{Profile} az login: Handle missing tenantDisplayName (#29245)
1 parent d4147a7 commit 7bc61d4

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

src/azure-cli/azure/cli/command_modules/profile/_subscription_selector.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __call__(self):
9595

9696
@staticmethod
9797
def _get_tenant_string(subscription):
98-
try:
99-
return subscription[_TENANT_DISPLAY_NAME]
100-
except KeyError:
101-
return subscription[_TENANT_ID]
98+
if tenant_display_name := subscription.get(_TENANT_DISPLAY_NAME):
99+
return tenant_display_name
100+
# If _TENANT_DISPLAY_NAME doesn't exist or is None, return _TENANT_ID
101+
return subscription[_TENANT_ID]

src/azure-cli/azure/cli/command_modules/profile/tests/latest/test_subscription_selector.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@
4040
"environmentName": "AzureCloud",
4141
"isDefault": False
4242
},
43-
# 3: tenant account
43+
# 3: sub 4 without tenantDisplayName
44+
{
45+
"id": "00000000-0000-0000-0000-444444444444",
46+
"name": "Sub 4 without tenantDisplayName",
47+
"tenantDefaultDomain": "tenant1.onmicrosoft.com",
48+
"tenantDisplayName": None,
49+
"tenantId": "00000000-0000-0000-1111-111111111111",
50+
"environmentName": "AzureCloud",
51+
"isDefault": False
52+
},
53+
# 4: tenant account
4454
{
4555
"id": "00000000-0000-0000-1111-222222222222",
4656
"name": "N/A(tenant level account)",
@@ -54,11 +64,12 @@
5464

5565
EXPECTED_SUBSCRIPTION_TABLE = """\
5666
No Subscription name Subscription ID Tenant
57-
----- ------------------------------------ ------------------------------------ --------
67+
----- ------------------------------------ ------------------------------------ ------------------------------------
5868
[1] N/A(tenant level account) 00000000-0000-0000-1111-222222222222 Tenant 2
5969
[2] SUB 1 00000000-0000-0000-0000-111111111111 Tenant 1
6070
\x1b[96m[3]\x1b[0m * \x1b[96msub 2\x1b[0m \x1b[96m00000000-0000-0000-0000-222222222222\x1b[0m \x1b[96mTenant 1\x1b[0m
61-
[4] Sub 3 with long long long long lo... 00000000-0000-0000-0000-333333333333 Tenant 1"""
71+
[4] Sub 3 with long long long long lo... 00000000-0000-0000-0000-333333333333 Tenant 1
72+
[5] Sub 4 without tenantDisplayName 00000000-0000-0000-0000-444444444444 00000000-0000-0000-1111-111111111111"""
6273

6374
DUMMY_SUBSCRIPTIONS_NO_TENANT_INFO = [
6475
{
@@ -103,10 +114,12 @@ def test_format_subscription_table(self):
103114
with mock.patch.object(format_styled_text, 'theme', 'dark', create=True):
104115
selector = SubscriptionSelector(sub)
105116
assert (selector._index_to_subscription_map == {
106-
'1': sub[3],
117+
'1': sub[4],
107118
'2': sub[1],
108119
'3': sub[0],
109-
'4': sub[2]})
120+
'4': sub[2],
121+
'5': sub[3],
122+
})
110123
assert selector._table_str == EXPECTED_SUBSCRIPTION_TABLE
111124

112125
selector = SubscriptionSelector(DUMMY_SUBSCRIPTIONS_NO_TENANT_INFO)

0 commit comments

Comments
 (0)