From 2528e018897fd112efadd6f6bc98d9d9eca33a0a Mon Sep 17 00:00:00 2001 From: Jiri Burant Date: Tue, 13 Dec 2022 16:12:02 +0100 Subject: [PATCH 1/2] Fixed bug in sip routing. SDK shouldnt send empty patch. --- .../siprouting/_sip_routing_client.py | 7 +- .../aio/_sip_routing_client_async.py | 7 +- ...ngClientE2Etest_set_trunks_empty_list.json | 65 ++++++++++++++ ...entE2EAsynctest_set_trunks_empty_list.json | 90 +++++++++++++++++++ .../test/test_sip_routing_client_e2e.py | 12 ++- .../test/test_sip_routing_client_e2e_async.py | 14 ++- 6 files changed, 187 insertions(+), 8 deletions(-) create mode 100644 sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e.pyTestSipRoutingClientE2Etest_set_trunks_empty_list.json create mode 100644 sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e_async.pyTestSipRoutingClientE2EAsynctest_set_trunks_empty_list.json diff --git a/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/_sip_routing_client.py b/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/_sip_routing_client.py index bccb6ce98c95..30c2fe492511 100644 --- a/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/_sip_routing_client.py +++ b/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/_sip_routing_client.py @@ -206,9 +206,10 @@ def set_trunks( if x.fqdn not in [o.fqdn for o in trunks]: config.trunks[x.fqdn] = None - self._rest_service.patch_sip_configuration( - body=config, **kwargs - ) + if len(config.trunks) > 0: + self._rest_service.patch_sip_configuration( + body=config, **kwargs + ) @distributed_trace def set_routes( diff --git a/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/aio/_sip_routing_client_async.py b/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/aio/_sip_routing_client_async.py index de813269457d..ffc1acd1d731 100644 --- a/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/aio/_sip_routing_client_async.py +++ b/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/aio/_sip_routing_client_async.py @@ -210,9 +210,10 @@ async def set_trunks( if x.fqdn not in [o.fqdn for o in trunks]: config.trunks[x.fqdn] = None - await self._rest_service.patch_sip_configuration( - body=config, **kwargs - ) + if len(config.trunks) > 0: + await self._rest_service.patch_sip_configuration( + body=config, **kwargs + ) @distributed_trace_async async def set_routes( diff --git a/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e.pyTestSipRoutingClientE2Etest_set_trunks_empty_list.json b/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e.pyTestSipRoutingClientE2Etest_set_trunks_empty_list.json new file mode 100644 index 000000000000..d1f95b38ad69 --- /dev/null +++ b/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e.pyTestSipRoutingClientE2Etest_set_trunks_empty_list.json @@ -0,0 +1,65 @@ +{ + "Entries": [ + { + "RequestUri": "https://sanitized.communication.azure.com/sip?api-version=2021-05-01-preview", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Accept-Encoding": "gzip, deflate", + "Connection": "keep-alive", + "User-Agent": "azsdk-python-communication-phonenumbers/1.1.0b3 Python/3.10.0 (Windows-10-10.0.22621-SP0)", + "x-ms-content-sha256": "sanitized", + "x-ms-date": "sanitized", + "x-ms-return-client-request-id": "true" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "api-supported-versions": "2021-05-01-preview, 2022-09-01-preview", + "Content-Type": "application/json; charset=utf-8", + "Date": "sanitized", + "MS-CV": "sanitized", + "Strict-Transport-Security": "max-age=2592000", + "Transfer-Encoding": "chunked", + "X-Azure-Ref": "sanitized", + "X-Cache": "CONFIG_NOCACHE", + "X-Processing-Time": "182ms" + }, + "ResponseBody": { + "trunks": {}, + "routes": [] + } + }, + { + "RequestUri": "https://sanitized.communication.azure.com/sip?api-version=2021-05-01-preview", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Accept-Encoding": "gzip, deflate", + "Connection": "keep-alive", + "User-Agent": "azsdk-python-communication-phonenumbers/1.1.0b3 Python/3.10.0 (Windows-10-10.0.22621-SP0)", + "x-ms-content-sha256": "sanitized", + "x-ms-date": "sanitized", + "x-ms-return-client-request-id": "true" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "api-supported-versions": "2021-05-01-preview, 2022-09-01-preview", + "Content-Type": "application/json; charset=utf-8", + "Date": "sanitized", + "MS-CV": "sanitized", + "Strict-Transport-Security": "max-age=2592000", + "Transfer-Encoding": "chunked", + "X-Azure-Ref": "sanitized", + "X-Cache": "CONFIG_NOCACHE", + "X-Processing-Time": "279ms" + }, + "ResponseBody": { + "trunks": {}, + "routes": [] + } + } + ], + "Variables": {} +} diff --git a/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e_async.pyTestSipRoutingClientE2EAsynctest_set_trunks_empty_list.json b/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e_async.pyTestSipRoutingClientE2EAsynctest_set_trunks_empty_list.json new file mode 100644 index 000000000000..4f1a7fa35774 --- /dev/null +++ b/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e_async.pyTestSipRoutingClientE2EAsynctest_set_trunks_empty_list.json @@ -0,0 +1,90 @@ +{ + "Entries": [ + { + "RequestUri": "https://login.microsoftonline.com/sanitized/oauth2/v2.0/token", + "RequestMethod": "POST", + "RequestHeaders": { + "Accept": "*/*", + "Accept-Encoding": "gzip, deflate", + "Content-Length": "131", + "Content-Type": "application/x-www-form-urlencoded", + "User-Agent": "azsdk-python-identity/1.9.0 Python/3.10.0 (Windows-10-10.0.22621-SP0)" + }, + "RequestBody": "client_id=sanitized\u0026client_secret=sanitized\u0026grant_type=client_credentials\u0026scope=https%3A%2F%2Fcommunication.azure.com%2F%2F.default", + "StatusCode": 200, + "ResponseHeaders": { + "Cache-Control": "no-store, no-cache", + "Content-Length": "90", + "Content-Type": "application/json; charset=utf-8", + "Date": "sanitized", + "Expires": "-1", + "P3P": "sanitized", + "Pragma": "no-cache", + "Set-Cookie": "sanitized", + "Strict-Transport-Security": "max-age=31536000; includeSubDomains", + "X-Content-Type-Options": "nosniff", + "x-ms-ests-server": "sanitized", + "X-XSS-Protection": "0" + }, + "ResponseBody": { + "token_type": "Bearer", + "expires_in": 3599, + "ext_expires_in": 3599, + "access_token": "Sanitized" + } + }, + { + "RequestUri": "https://sanitized.communication.azure.com/sip?api-version=2021-05-01-preview", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Accept-Encoding": "gzip, deflate", + "User-Agent": "azsdk-python-communication-phonenumbers/1.1.0b3 Python/3.10.0 (Windows-10-10.0.22621-SP0)" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "api-supported-versions": "2021-05-01-preview, 2022-09-01-preview", + "Content-Type": "application/json; charset=utf-8", + "Date": "sanitized", + "MS-CV": "sanitized", + "Strict-Transport-Security": "max-age=2592000", + "Transfer-Encoding": "chunked", + "X-Azure-Ref": "sanitized", + "X-Cache": "CONFIG_NOCACHE", + "X-Processing-Time": "1647ms" + }, + "ResponseBody": { + "trunks": {}, + "routes": [] + } + }, + { + "RequestUri": "https://sanitized.communication.azure.com/sip?api-version=2021-05-01-preview", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Accept-Encoding": "gzip, deflate", + "User-Agent": "azsdk-python-communication-phonenumbers/1.1.0b3 Python/3.10.0 (Windows-10-10.0.22621-SP0)" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "api-supported-versions": "2021-05-01-preview, 2022-09-01-preview", + "Content-Type": "application/json; charset=utf-8", + "Date": "sanitized", + "MS-CV": "sanitized", + "Strict-Transport-Security": "max-age=2592000", + "Transfer-Encoding": "chunked", + "X-Azure-Ref": "sanitized", + "X-Cache": "CONFIG_NOCACHE", + "X-Processing-Time": "194ms" + }, + "ResponseBody": { + "trunks": {}, + "routes": [] + } + } + ], + "Variables": {} +} diff --git a/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e.py b/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e.py index f2ea6a947b52..2e7276a774d8 100644 --- a/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e.py +++ b/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e.py @@ -3,7 +3,7 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- - +from azure.core.exceptions import HttpResponseError from phone_numbers_testcase import PhoneNumbersTestCase from _shared.utils import create_token_credential, get_http_logging_policy from sip_routing_helper import get_user_domain, assert_trunks_are_equal, assert_routes_are_equal @@ -79,6 +79,16 @@ def test_set_trunks_from_managed_identity(self, **kwargs): assert result_trunks is not None, "No trunks were returned." assert_trunks_are_equal(result_trunks,[self.additional_trunk]) + @recorded_by_proxy + def test_set_trunks_empty_list(self, **kwargs): + """Verification of bug fix. SDK shouldn't send empty PATCH, otherwise it will receive exception. + This situation occurs, when sending empty trunks list to already empty trunk configuration.""" + try: + self._sip_routing_client.set_trunks([]) + self._sip_routing_client.set_trunks([]) + except HttpResponseError as exception: + assert False, "Trying to set empty trunks list returned Http error: " + str(exception.status_code) + ", message: " + exception.message + @recorded_by_proxy def test_set_routes(self, **kwargs): self._prepare_test() diff --git a/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e_async.py b/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e_async.py index 84cc0c70174a..ed07c2a4a446 100644 --- a/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e_async.py +++ b/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e_async.py @@ -3,8 +3,8 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- - import pytest +from azure.core.exceptions import HttpResponseError from phone_numbers_testcase import PhoneNumbersTestCase from devtools_testutils.aio import recorded_by_proxy_async from _shared.utils import async_create_token_credential, get_http_logging_policy @@ -88,6 +88,18 @@ async def test_set_trunks_from_managed_identity(self): result_trunks = await self._sip_routing_client.get_trunks() assert result_trunks is not None, "No trunks were returned." assert_trunks_are_equal(result_trunks,[self.additional_trunk]), "Trunks are not equal." + + @recorded_by_proxy_async + async def test_set_trunks_empty_list(self): + """Verification of bug fix. SDK shouldn't send empty PATCH, otherwise it will receive exception. + This situation occurs, when sending empty trunks list to already empty trunk configuration.""" + self._sip_routing_client = self._get_sip_client_managed_identity() + async with self._sip_routing_client: + try: + await self._sip_routing_client.set_trunks([]) + await self._sip_routing_client.set_trunks([]) + except HttpResponseError as exception: + assert False, "Trying to set empty trunks list returned Http error: " + str(exception.status_code) + ", message: " + exception.message @recorded_by_proxy_async async def test_set_routes(self): From 27bad736717a46ca2671af55609cba3ca6e5b5ae Mon Sep 17 00:00:00 2001 From: Jiri Burant Date: Tue, 13 Dec 2022 16:34:37 +0100 Subject: [PATCH 2/2] Changing client for new async testcase to be non-managed identity. --- ...entE2EAsynctest_set_trunks_empty_list.json | 49 +++++-------------- .../test/test_sip_routing_client_e2e_async.py | 1 - 2 files changed, 12 insertions(+), 38 deletions(-) diff --git a/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e_async.pyTestSipRoutingClientE2EAsynctest_set_trunks_empty_list.json b/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e_async.pyTestSipRoutingClientE2EAsynctest_set_trunks_empty_list.json index 4f1a7fa35774..2ea199e26dcf 100644 --- a/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e_async.pyTestSipRoutingClientE2EAsynctest_set_trunks_empty_list.json +++ b/sdk/communication/azure-communication-phonenumbers/test/recordings/test_sip_routing_client_e2e_async.pyTestSipRoutingClientE2EAsynctest_set_trunks_empty_list.json @@ -1,45 +1,16 @@ { "Entries": [ - { - "RequestUri": "https://login.microsoftonline.com/sanitized/oauth2/v2.0/token", - "RequestMethod": "POST", - "RequestHeaders": { - "Accept": "*/*", - "Accept-Encoding": "gzip, deflate", - "Content-Length": "131", - "Content-Type": "application/x-www-form-urlencoded", - "User-Agent": "azsdk-python-identity/1.9.0 Python/3.10.0 (Windows-10-10.0.22621-SP0)" - }, - "RequestBody": "client_id=sanitized\u0026client_secret=sanitized\u0026grant_type=client_credentials\u0026scope=https%3A%2F%2Fcommunication.azure.com%2F%2F.default", - "StatusCode": 200, - "ResponseHeaders": { - "Cache-Control": "no-store, no-cache", - "Content-Length": "90", - "Content-Type": "application/json; charset=utf-8", - "Date": "sanitized", - "Expires": "-1", - "P3P": "sanitized", - "Pragma": "no-cache", - "Set-Cookie": "sanitized", - "Strict-Transport-Security": "max-age=31536000; includeSubDomains", - "X-Content-Type-Options": "nosniff", - "x-ms-ests-server": "sanitized", - "X-XSS-Protection": "0" - }, - "ResponseBody": { - "token_type": "Bearer", - "expires_in": 3599, - "ext_expires_in": 3599, - "access_token": "Sanitized" - } - }, { "RequestUri": "https://sanitized.communication.azure.com/sip?api-version=2021-05-01-preview", "RequestMethod": "GET", "RequestHeaders": { "Accept": "application/json", "Accept-Encoding": "gzip, deflate", - "User-Agent": "azsdk-python-communication-phonenumbers/1.1.0b3 Python/3.10.0 (Windows-10-10.0.22621-SP0)" + "Content-Length": "0", + "User-Agent": "azsdk-python-communication-phonenumbers/1.1.0b3 Python/3.10.0 (Windows-10-10.0.22621-SP0)", + "x-ms-content-sha256": "sanitized", + "x-ms-date": "sanitized", + "x-ms-return-client-request-id": "true" }, "RequestBody": null, "StatusCode": 200, @@ -52,7 +23,7 @@ "Transfer-Encoding": "chunked", "X-Azure-Ref": "sanitized", "X-Cache": "CONFIG_NOCACHE", - "X-Processing-Time": "1647ms" + "X-Processing-Time": "270ms" }, "ResponseBody": { "trunks": {}, @@ -65,7 +36,11 @@ "RequestHeaders": { "Accept": "application/json", "Accept-Encoding": "gzip, deflate", - "User-Agent": "azsdk-python-communication-phonenumbers/1.1.0b3 Python/3.10.0 (Windows-10-10.0.22621-SP0)" + "Content-Length": "0", + "User-Agent": "azsdk-python-communication-phonenumbers/1.1.0b3 Python/3.10.0 (Windows-10-10.0.22621-SP0)", + "x-ms-content-sha256": "sanitized", + "x-ms-date": "sanitized", + "x-ms-return-client-request-id": "true" }, "RequestBody": null, "StatusCode": 200, @@ -78,7 +53,7 @@ "Transfer-Encoding": "chunked", "X-Azure-Ref": "sanitized", "X-Cache": "CONFIG_NOCACHE", - "X-Processing-Time": "194ms" + "X-Processing-Time": "76ms" }, "ResponseBody": { "trunks": {}, diff --git a/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e_async.py b/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e_async.py index ed07c2a4a446..5764edf79f90 100644 --- a/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e_async.py +++ b/sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e_async.py @@ -93,7 +93,6 @@ async def test_set_trunks_from_managed_identity(self): async def test_set_trunks_empty_list(self): """Verification of bug fix. SDK shouldn't send empty PATCH, otherwise it will receive exception. This situation occurs, when sending empty trunks list to already empty trunk configuration.""" - self._sip_routing_client = self._get_sip_client_managed_identity() async with self._sip_routing_client: try: await self._sip_routing_client.set_trunks([])