diff --git a/sdk/communication/azure-communication-administration/tests/_shared/fake_token_credential.py b/sdk/communication/azure-communication-administration/tests/_shared/fake_token_credential.py new file mode 100644 index 0000000000000..b4fc2e6eaccfa --- /dev/null +++ b/sdk/communication/azure-communication-administration/tests/_shared/fake_token_credential.py @@ -0,0 +1,13 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +from azure.core.credentials import AccessToken + +class FakeTokenCredential(object): + def __init__(self): + self.token = AccessToken("Fake Token", 0) + + def get_token(self, *args): + return self.token \ No newline at end of file diff --git a/sdk/communication/azure-communication-administration/tests/_shared/utils.py b/sdk/communication/azure-communication-administration/tests/_shared/utils.py new file mode 100644 index 0000000000000..4fcf96d313ffa --- /dev/null +++ b/sdk/communication/azure-communication-administration/tests/_shared/utils.py @@ -0,0 +1,14 @@ +# ------------------------------------------------------------------------ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# ------------------------------------------------------------------------- +from .fake_token_credential import FakeTokenCredential +from azure.identity import DefaultAzureCredential + +def create_token_credential(): + # type: () -> FakeTokenCredential or DefaultAzureCredential + from devtools_testutils import is_live + if not is_live(): + return FakeTokenCredential() + return DefaultAzureCredential() diff --git a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py index 30b3480ec193c..449a709936420 100644 --- a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py +++ b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client.py @@ -16,18 +16,12 @@ from phone_number_helper import PhoneNumberUriReplacer from phone_number_testcase import PhoneNumberCommunicationTestCase from _shared.testcase import BodyReplacerProcessor -from azure.identity import DefaultAzureCredential +from _shared.utils import create_token_credential from azure.communication.administration._shared.utils import parse_connection_str SKIP_PHONE_NUMBER_TESTS = True PHONE_NUMBER_TEST_SKIP_REASON= "Phone Number Administration live tests infra not ready yet" -class FakeTokenCredential(object): - def __init__(self): - self.token = AccessToken("Fake Token", 0) - - def get_token(self, *args): - return self.token class PhoneNumberAdministrationClientTest(PhoneNumberCommunicationTestCase): def setUp(self): super(PhoneNumberAdministrationClientTest, self).setUp() @@ -137,11 +131,7 @@ def setUp(self): @pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON) def test_list_all_phone_numbers_from_managed_identity(self): endpoint, access_key = parse_connection_str(self.connection_str) - from devtools_testutils import is_live - if not is_live(): - credential = FakeTokenCredential() - else: - credential = DefaultAzureCredential() + credential = create_token_credential() phone_number_client = PhoneNumberAdministrationClient(endpoint, credential) pages = phone_number_client.list_all_phone_numbers() assert pages.next() diff --git a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py index 09e60b2ad48df..dccf1ae64fc94 100644 --- a/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py +++ b/sdk/communication/azure-communication-administration/tests/test_phone_number_administration_client_async.py @@ -5,7 +5,6 @@ # license information. # -------------------------------------------------------------------------- import pytest -from azure.core.credentials import AccessToken from azure.communication.administration.aio import PhoneNumberAdministrationClient from azure.communication.administration._shared.utils import parse_connection_str from azure.communication.administration import ( @@ -16,18 +15,12 @@ from phone_number_helper import PhoneNumberUriReplacer from phone_number_testcase_async import AsyncPhoneNumberCommunicationTestCase from _shared.testcase import BodyReplacerProcessor, ResponseReplacerProcessor -from azure.identity import DefaultAzureCredential +from _shared.utils import create_token_credential import os SKIP_PHONE_NUMBER_TESTS = True PHONE_NUMBER_TEST_SKIP_REASON= "Phone Number Administration live tests infra not ready yet" -class FakeTokenCredential(object): - def __init__(self): - self.token = AccessToken("Fake Token", 0) - - def get_token(self, *args): - return self.token class PhoneNumberAdministrationClientTestAsync(AsyncPhoneNumberCommunicationTestCase): def setUp(self): @@ -140,11 +133,7 @@ def setUp(self): @pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON) async def test_list_all_phone_numbers_from_managed_identity(self): endpoint, access_key = parse_connection_str(self.connection_str) - from devtools_testutils import is_live - if not is_live(): - credential = FakeTokenCredential() - else: - credential = DefaultAzureCredential() + credential = create_token_credential() phone_number_client = PhoneNumberAdministrationClient(endpoint, credential) async with phone_number_client: pages = phone_number_client.list_all_phone_numbers()