File: test_sip_routing_client_e2e_async.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (201 lines) | stat: -rw-r--r-- 11,013 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# 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
from sip_routing_helper import get_user_domain, assert_trunks_are_equal, assert_routes_are_equal

from azure.communication.phonenumbers.siprouting.aio import SipRoutingClient
from azure.communication.phonenumbers.siprouting._generated.models import SipTrunkRoute
from azure.communication.phonenumbers.siprouting._models import SipTrunk
from azure.communication.phonenumbers._shared.utils import parse_connection_str

@pytest.mark.asyncio
class TestSipRoutingClientE2EAsync(PhoneNumbersTestCase):
    user_domain = get_user_domain()
    
    first_trunk = SipTrunk(fqdn="sbs1." + user_domain, sip_signaling_port=1122)
    second_trunk = SipTrunk(fqdn="sbs2." + user_domain, sip_signaling_port=1123)
    additional_trunk = SipTrunk(fqdn="sbs3." + user_domain, sip_signaling_port=2222)
    first_route = SipTrunkRoute(name="First rule", description="Handle numbers starting with '+123'", number_pattern="\\+123[0-9]+", trunks=["sbs1." + user_domain])
            
    def setup_method(self):
        super(TestSipRoutingClientE2EAsync, self).setUp(use_dynamic_resource=True)
        self._sip_routing_client = SipRoutingClient.from_connection_string(
            self.connection_str, http_logging_policy=get_http_logging_policy()
        )

    async def _prepare_test(self):
        await self._sip_routing_client.set_routes([])
        await self._sip_routing_client.set_trunks([self.first_trunk, self.second_trunk])

    @recorded_by_proxy_async
    async def test_get_trunks(self):
        await self._prepare_test()
        async with self._sip_routing_client:
            trunks = await self._sip_routing_client.list_trunks()
        assert trunks is not None, "No trunks were returned."
        assert_trunks_are_equal(trunks,[self.first_trunk, self.second_trunk]), "Trunks are not equal."
    
    @recorded_by_proxy_async
    async def test_get_trunks_from_managed_identity(self):
        await self._prepare_test()
        self._sip_routing_client = self._get_sip_client_managed_identity()
        async with self._sip_routing_client:
            trunks = await self._sip_routing_client.list_trunks()
        assert trunks is not None, "No trunks were returned."
        assert_trunks_are_equal(trunks,[self.first_trunk, self.second_trunk]), "Trunks are not equal."

    @recorded_by_proxy_async
    async def test_get_routes(self):
        await self._prepare_test()
        async with self._sip_routing_client:
            await self._sip_routing_client.set_routes([self.first_route])
            routes = await self._sip_routing_client.list_routes()
        assert routes is not None, "No routes were returned."
        assert_routes_are_equal(routes,[self.first_route]), "Routes are not equal."

    @recorded_by_proxy_async
    async def test_get_routes_from_managed_identity(self):
        await self._prepare_test()
        self._sip_routing_client = self._get_sip_client_managed_identity()
        async with self._sip_routing_client:
            await self._sip_routing_client.set_routes([self.first_route])
            routes = await self._sip_routing_client.list_routes()
        assert routes is not None, "No routes were returned."
        assert_routes_are_equal(routes,[self.first_route]), "Routes are not equal."

    @recorded_by_proxy_async
    async def test_set_trunks(self):
        await self._prepare_test()
        async with self._sip_routing_client:
            await self._sip_routing_client.set_trunks([self.additional_trunk])
            result_trunks = await self._sip_routing_client.list_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_from_managed_identity(self):
        await self._prepare_test()
        self._sip_routing_client = self._get_sip_client_managed_identity()
        async with self._sip_routing_client:
            await self._sip_routing_client.set_trunks([self.additional_trunk])
            result_trunks = await self._sip_routing_client.list_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."""
        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):
        await self._prepare_test()
        new_routes = [SipTrunkRoute(name="Alternative rule", description="Handle numbers starting with '+999'", number_pattern="\\+999[0-9]+", trunks=[self.second_trunk.fqdn])]
        async with self._sip_routing_client:
            await self._sip_routing_client.set_routes([self.first_route])
            await self._sip_routing_client.set_routes(new_routes)
            result_routes = await self._sip_routing_client.list_routes()
        assert result_routes is not None, "No routes were returned."
        assert_routes_are_equal(result_routes,new_routes), "Routes are not equal."

    @recorded_by_proxy_async
    async def test_set_routes_from_managed_identity(self):
        await self._prepare_test()
        new_routes = [SipTrunkRoute(name="Alternative rule", description="Handle numbers starting with '+999'", number_pattern="\\+999[0-9]+", trunks=[self.second_trunk.fqdn])]
        self._sip_routing_client = self._get_sip_client_managed_identity()
        async with self._sip_routing_client:
            await self._sip_routing_client.set_routes([self.first_route])
            await self._sip_routing_client.set_routes(new_routes)
            result_routes = await self._sip_routing_client.list_routes()
        assert result_routes is not None, "No routes were returned."
        assert_routes_are_equal(result_routes,new_routes), "Routes are not equal."

    @recorded_by_proxy_async
    async def test_delete_trunk(self):
        await self._prepare_test()
        trunk_to_delete = self.second_trunk.fqdn
        async with self._sip_routing_client:
            await self._sip_routing_client.delete_trunk(trunk_to_delete)
            new_trunks = await self._sip_routing_client.list_trunks()
        assert_trunks_are_equal(new_trunks,[self.first_trunk]), "Trunk was not deleted."

    @recorded_by_proxy_async
    async def test_delete_trunk_from_managed_identity(self):
        await self._prepare_test()
        trunk_to_delete = self.second_trunk.fqdn
        self._sip_routing_client = self._get_sip_client_managed_identity()
        async with self._sip_routing_client:
            await self._sip_routing_client.delete_trunk(trunk_to_delete)
            new_trunks = await self._sip_routing_client.list_trunks()
        assert_trunks_are_equal(new_trunks,[self.first_trunk]), "Trunk was not deleted."

    @recorded_by_proxy_async
    async def test_add_trunk(self):
        await self._prepare_test()
        async with self._sip_routing_client:
            await self._sip_routing_client.set_trunk(self.additional_trunk)
            new_trunks = await self._sip_routing_client.list_trunks()
        assert_trunks_are_equal(new_trunks,[self.first_trunk,self.second_trunk,self.additional_trunk])
    
    @recorded_by_proxy_async
    async def test_add_trunk_from_managed_identity(self):
        await self._prepare_test()
        self._sip_routing_client = self._get_sip_client_managed_identity()
        async with self._sip_routing_client:
            await self._sip_routing_client.set_trunk(self.additional_trunk)
            new_trunks = await self._sip_routing_client.list_trunks()
        assert_trunks_are_equal(new_trunks,[self.first_trunk,self.second_trunk,self.additional_trunk])

    @recorded_by_proxy_async
    async def test_get_trunk(self):
        await self._prepare_test()
        async with self._sip_routing_client:
            trunk = await self._sip_routing_client.get_trunk(self.first_trunk.fqdn)
        assert trunk is not None, "No trunk was returned."
        assert_trunks_are_equal([trunk],[self.first_trunk]), "Returned trunk does not match the required trunk."

    @recorded_by_proxy_async
    async def test_get_trunk_from_managed_identity(self):
        await self._prepare_test()
        self._sip_routing_client = self._get_sip_client_managed_identity()
        async with self._sip_routing_client:
            trunk = await self._sip_routing_client.get_trunk(self.first_trunk.fqdn)
        assert trunk is not None, "No trunk was returned."
        assert_trunks_are_equal([trunk],[self.first_trunk]), "Returned trunk does not match the required trunk."
        
    @recorded_by_proxy_async
    async def test_set_trunk(self):
        await self._prepare_test()
        modified_trunk = SipTrunk(fqdn=self.second_trunk.fqdn,sip_signaling_port=7777)
        async with self._sip_routing_client:
            await self._sip_routing_client.set_trunk(modified_trunk)
            new_trunks = await self._sip_routing_client.list_trunks()
        assert_trunks_are_equal(new_trunks,[self.first_trunk,modified_trunk])
    
    @recorded_by_proxy_async
    async def test_set_trunk_from_managed_identity(self):
        await self._prepare_test()
        modified_trunk = SipTrunk(fqdn=self.second_trunk.fqdn,sip_signaling_port=7777)
        self._sip_routing_client = self._get_sip_client_managed_identity()
        async with self._sip_routing_client:
            await self._sip_routing_client.set_trunk(modified_trunk)
            new_trunks = await self._sip_routing_client.list_trunks()
        assert_trunks_are_equal(new_trunks,[self.first_trunk,modified_trunk])

    def _get_sip_client_managed_identity(self):
        endpoint, *_ = parse_connection_str(self.connection_str)
        credential = async_create_token_credential()
        return SipRoutingClient(endpoint, credential, http_logging_policy=get_http_logging_policy())