22Integration test for Bandwidth's WebRTC Endpoints API
33"""
44import unittest
5- import time
5+ from datetime import datetime
66
7- from hamcrest import assert_that , has_properties , not_none , instance_of , equal_to
7+ from hamcrest import assert_that , has_properties , instance_of , equal_to , greater_than
88
99from bandwidth import ApiClient , ApiResponse , Configuration
1010from bandwidth .api .endpoints_api import EndpointsApi
1111from bandwidth .models .create_web_rtc_connection_request import CreateWebRtcConnectionRequest
1212from bandwidth .models .create_endpoint_response import CreateEndpointResponse
13+ from bandwidth .models .create_endpoint_response_data import CreateEndpointResponseData
14+ from bandwidth .models .endpoint import Endpoint
1315from bandwidth .models .endpoint_response import EndpointResponse
1416from bandwidth .models .list_endpoints_response import ListEndpointsResponse
17+ from bandwidth .models .endpoints import Endpoints
1518from bandwidth .models .endpoint_type_enum import EndpointTypeEnum
1619from bandwidth .models .endpoint_direction_enum import EndpointDirectionEnum
1720from bandwidth .models .endpoint_status_enum import EndpointStatusEnum
18- from bandwidth .exceptions import ApiException , UnauthorizedException , ForbiddenException , NotFoundException
21+ from bandwidth .models .link import Link
22+ from bandwidth .exceptions import ApiException
1923from test .utils .env_variables import *
2024
2125
2226class TestEndpointsApi (unittest .TestCase ):
23- """EndpointsApi integration Test """
27+ """EndpointsApi integration test """
2428
2529 @classmethod
2630 def setUpClass (cls ) -> None :
@@ -31,28 +35,11 @@ def setUpClass(cls) -> None:
3135 api_client = ApiClient (configuration )
3236 cls .endpoints_api_instance = EndpointsApi (api_client )
3337
34- # Unauthorized API Client
35- cls .unauthorized_api_client = ApiClient ()
36- cls .unauthorized_api_instance = EndpointsApi (cls .unauthorized_api_client )
38+ cls .unauthorized_api_instance = EndpointsApi (ApiClient ())
3739
3840 cls .account_id = BW_ACCOUNT_ID
39- cls .endpoint_id_array = []
40- cls .TEST_SLEEP = 2
41-
42- @classmethod
43- def tearDownClass (cls ):
44- """Clean up endpoints created during tests"""
45- for endpoint_id in cls .endpoint_id_array :
46- try :
47- cls .endpoints_api_instance .delete_endpoint (cls .account_id , endpoint_id )
48- time .sleep (1 )
49- except Exception as e :
50- print (f"Failed to cleanup endpoint { endpoint_id } : { e } " )
51-
52- def test_create_endpoint (self ):
53- """Test creating a new WebRTC endpoint with all parameters"""
54- time .sleep (self .TEST_SLEEP )
5541
42+ def createEndpoint (self ):
5643 create_request = CreateWebRtcConnectionRequest (
5744 type = EndpointTypeEnum .WEBRTC ,
5845 direction = EndpointDirectionEnum .BIDIRECTIONAL ,
@@ -68,181 +55,94 @@ def test_create_endpoint(self):
6855
6956 assert_that (response .status_code , equal_to (201 ))
7057 assert_that (response .data , instance_of (CreateEndpointResponse ))
58+ assert_that (response .data .links , instance_of (list ))
59+ assert_that (len (response .data .links ), greater_than (0 ))
60+ assert_that (response .data .links [0 ], instance_of (Link ))
61+ assert_that (response .data .errors , instance_of (list ))
62+ assert_that (response .data .data , instance_of (CreateEndpointResponseData ))
7163 assert_that (response .data .data , has_properties (
7264 'endpoint_id' , instance_of (str ),
7365 'type' , EndpointTypeEnum .WEBRTC ,
7466 'status' , instance_of (EndpointStatusEnum ),
75- 'token' , instance_of (str )
67+ 'token' , instance_of (str ),
68+ 'creation_timestamp' , instance_of (datetime ),
69+ 'expiration_timestamp' , instance_of (datetime ),
70+ 'tag' , equal_to ("python-sdk-test-endpoint" ),
71+ 'devices' , instance_of (list )
7672 ))
7773
78- # Store endpoint ID for cleanup
79- self .endpoint_id_array .append (response .data .data .endpoint_id )
80-
81- def test_create_endpoint_minimal (self ):
82- """Test creating an endpoint with only required parameters"""
83- time .sleep (self .TEST_SLEEP )
84-
85- create_request = CreateWebRtcConnectionRequest (
86- type = EndpointTypeEnum .WEBRTC ,
87- direction = EndpointDirectionEnum .OUTBOUND
88- )
74+ self .__class__ .endpoint_id = response .data .data .endpoint_id
8975
90- response : CreateEndpointResponse = self .endpoints_api_instance .create_endpoint (
76+ def listEndpoints (self ):
77+ response : ApiResponse = self .endpoints_api_instance .list_endpoints_with_http_info (
9178 self .account_id ,
92- create_request
93- )
94-
95- assert_that (response .data , has_properties (
96- 'endpoint_id' , instance_of (str ),
97- 'type' , EndpointTypeEnum .WEBRTC ,
98- 'token' , not_none ()
99- ))
100-
101- # Store endpoint ID for cleanup
102- self .endpoint_id_array .append (response .data .endpoint_id )
103-
104- def test_get_endpoint (self ):
105- """Test retrieving an endpoint by ID"""
106- time .sleep (self .TEST_SLEEP )
107-
108- # First create an endpoint
109- create_request = CreateWebRtcConnectionRequest (
11079 type = EndpointTypeEnum .WEBRTC ,
111- direction = EndpointDirectionEnum .INBOUND ,
112- tag = "test-get-endpoint"
80+ limit = 10
11381 )
11482
115- create_response : CreateEndpointResponse = self .endpoints_api_instance .create_endpoint (
116- self .account_id ,
117- create_request
118- )
83+ assert_that (response .status_code , equal_to (200 ))
84+ assert_that (response .data , instance_of (ListEndpointsResponse ))
85+ assert_that (response .data .links , instance_of (list ))
86+ assert_that (len (response .data .links ), greater_than (0 ))
87+ assert_that (response .data .links [0 ], instance_of (Link ))
88+ assert_that (response .data .errors , instance_of (list ))
89+ assert_that (response .data .data , instance_of (list ))
90+ assert_that (len (response .data .data ), greater_than (0 ))
11991
120- endpoint_id = create_response . data .endpoint_id
121- self .endpoint_id_array . append ( endpoint_id )
92+ listed_ids = [ ep . endpoint_id for ep in response . data .data ]
93+ assert_that ( self .endpoint_id in listed_ids , equal_to ( True ) )
12294
123- time .sleep (self .TEST_SLEEP )
95+ endpoint = response .data .data [0 ]
96+ assert_that (endpoint , instance_of (Endpoints ))
97+ assert_that (endpoint , has_properties (
98+ 'endpoint_id' , instance_of (str ),
99+ 'type' , instance_of (EndpointTypeEnum ),
100+ 'status' , instance_of (EndpointStatusEnum ),
101+ 'creation_timestamp' , instance_of (datetime ),
102+ 'expiration_timestamp' , instance_of (datetime )
103+ ))
124104
125- # Now get the endpoint
105+ def getEndpoint ( self ):
126106 response : ApiResponse = self .endpoints_api_instance .get_endpoint_with_http_info (
127107 self .account_id ,
128- endpoint_id
108+ self . endpoint_id
129109 )
130110
131111 assert_that (response .status_code , equal_to (200 ))
132112 assert_that (response .data , instance_of (EndpointResponse ))
113+ assert_that (response .data .links , instance_of (list ))
114+ assert_that (len (response .data .links ), greater_than (0 ))
115+ assert_that (response .data .links [0 ], instance_of (Link ))
116+ assert_that (response .data .errors , instance_of (list ))
117+ assert_that (response .data .data , instance_of (Endpoint ))
133118 assert_that (response .data .data , has_properties (
134- 'endpoint_id' , endpoint_id ,
119+ 'endpoint_id' , equal_to ( self . endpoint_id ) ,
135120 'type' , EndpointTypeEnum .WEBRTC ,
136- 'tag' , "test-get-endpoint"
121+ 'status' , instance_of (EndpointStatusEnum ),
122+ 'creation_timestamp' , instance_of (datetime ),
123+ 'expiration_timestamp' , instance_of (datetime ),
124+ 'tag' , equal_to ("python-sdk-test-endpoint" ),
125+ 'devices' , instance_of (list )
137126 ))
138127
139- def test_get_endpoint_not_found (self ):
140- """Test getting a non-existent endpoint returns 404"""
141- time .sleep (self .TEST_SLEEP )
142-
143- with self .assertRaises (ApiException ) as context :
144- self .endpoints_api_instance .get_endpoint (
145- self .account_id ,
146- "non-existent-endpoint-id"
147- )
148-
149- assert_that (context .exception .status , equal_to (404 ))
150-
151- def test_list_endpoints (self ):
152- """Test listing endpoints"""
153- time .sleep (self .TEST_SLEEP )
154-
155- # Create a couple of endpoints first
156- for i in range (2 ):
157- create_request = CreateWebRtcConnectionRequest (
158- type = EndpointTypeEnum .WEBRTC ,
159- direction = EndpointDirectionEnum .BIDIRECTIONAL ,
160- tag = f"test-list-endpoint-{ i } "
161- )
162- create_response = self .endpoints_api_instance .create_endpoint (
163- self .account_id ,
164- create_request
165- )
166- self .endpoint_id_array .append (create_response .data .endpoint_id )
167- time .sleep (1 )
168-
169- time .sleep (self .TEST_SLEEP )
170-
171- # List endpoints
172- response : ApiResponse = self .endpoints_api_instance .list_endpoints_with_http_info (
173- self .account_id ,
174- limit = 10
175- )
176-
177- assert_that (response .status_code , equal_to (200 ))
178- assert_that (response .data , instance_of (ListEndpointsResponse ))
179- assert_that (response .data .data , instance_of (list ))
180-
181- def test_list_endpoints_with_filter (self ):
182- """Test listing endpoints with type filter"""
183- time .sleep (self .TEST_SLEEP )
184-
185- response : ListEndpointsResponse = self .endpoints_api_instance .list_endpoints (
186- self .account_id ,
187- type = EndpointTypeEnum .WEBRTC ,
188- limit = 5
189- )
190-
191- assert_that (response .data , instance_of (list ))
192- # Verify all returned endpoints are of type WEBRTC
193- for endpoint in response .data :
194- assert_that (endpoint .type , equal_to (EndpointTypeEnum .WEBRTC ))
195-
196- def test_delete_endpoint (self ):
197- """Test deleting an endpoint"""
198- time .sleep (self .TEST_SLEEP )
199-
200- # Create an endpoint to delete
201- create_request = CreateWebRtcConnectionRequest (
202- type = EndpointTypeEnum .WEBRTC ,
203- direction = EndpointDirectionEnum .BIDIRECTIONAL ,
204- tag = "test-delete-endpoint"
205- )
206-
207- create_response : CreateEndpointResponse = self .endpoints_api_instance .create_endpoint (
208- self .account_id ,
209- create_request
210- )
211- endpoint_id = create_response .data .endpoint_id
212-
213- time .sleep (self .TEST_SLEEP )
214-
215- # Delete the endpoint
128+ def deleteEndpoint (self ):
216129 response : ApiResponse = self .endpoints_api_instance .delete_endpoint_with_http_info (
217130 self .account_id ,
218- endpoint_id
131+ self . endpoint_id
219132 )
220133
221134 assert_that (response .status_code , equal_to (204 ))
222135
223- # Verify endpoint is deleted
224- time .sleep (self .TEST_SLEEP )
225- with self .assertRaises (ApiException ) as context :
226- self .endpoints_api_instance .get_endpoint (self .account_id , endpoint_id )
227-
228- assert_that (context .exception .status , equal_to (404 ))
136+ def _steps (self ):
137+ call_order = ['createEndpoint' , 'listEndpoints' , 'getEndpoint' , 'deleteEndpoint' ]
138+ for name in call_order :
139+ yield name , getattr (self , name )
229140
230- def test_delete_endpoint_not_found (self ):
231- """Test deleting a non-existent endpoint returns 404"""
232- time .sleep (self .TEST_SLEEP )
233-
234- with self .assertRaises (ApiException ) as context :
235- self .endpoints_api_instance .delete_endpoint (
236- self .account_id ,
237- "non-existent-endpoint-id"
238- )
239-
240- assert_that (context .exception .status , equal_to (404 ))
141+ def test_steps (self ):
142+ for name , step in self ._steps ():
143+ step ()
241144
242145 def test_create_endpoint_unauthorized (self ):
243- """Test creating an endpoint with invalid credentials returns 401"""
244- time .sleep (self .TEST_SLEEP )
245-
246146 create_request = CreateWebRtcConnectionRequest (
247147 type = EndpointTypeEnum .WEBRTC ,
248148 direction = EndpointDirectionEnum .BIDIRECTIONAL
0 commit comments