|
| 1 | +""" |
| 2 | +Integration test for Bandwidth's WebRTC Endpoints API |
| 3 | +""" |
| 4 | +import unittest |
| 5 | +import time |
| 6 | + |
| 7 | +from hamcrest import assert_that, has_properties, not_none, instance_of, equal_to |
| 8 | + |
| 9 | +from bandwidth import ApiClient, ApiResponse, Configuration |
| 10 | +from bandwidth.api.endpoints_api import EndpointsApi |
| 11 | +from bandwidth.models.create_web_rtc_connection_request import CreateWebRtcConnectionRequest |
| 12 | +from bandwidth.models.create_endpoint_response import CreateEndpointResponse |
| 13 | +from bandwidth.models.endpoint_response import EndpointResponse |
| 14 | +from bandwidth.models.list_endpoints_response import ListEndpointsResponse |
| 15 | +from bandwidth.models.endpoint_type_enum import EndpointTypeEnum |
| 16 | +from bandwidth.models.endpoint_direction_enum import EndpointDirectionEnum |
| 17 | +from bandwidth.models.endpoint_status_enum import EndpointStatusEnum |
| 18 | +from bandwidth.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException |
| 19 | +from test.utils.env_variables import * |
| 20 | + |
| 21 | + |
| 22 | +class TestEndpointsApi(unittest.TestCase): |
| 23 | + """EndpointsApi integration Test""" |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def setUpClass(cls) -> None: |
| 27 | + configuration = Configuration( |
| 28 | + client_id=BW_CLIENT_ID, |
| 29 | + client_secret=BW_CLIENT_SECRET |
| 30 | + ) |
| 31 | + api_client = ApiClient(configuration) |
| 32 | + cls.endpoints_api_instance = EndpointsApi(api_client) |
| 33 | + |
| 34 | + # Unauthorized API Client |
| 35 | + unauthorized_configuration = Configuration( |
| 36 | + username='bad_username', |
| 37 | + password='bad_password' |
| 38 | + ) |
| 39 | + unauthorized_api_client = ApiClient(unauthorized_configuration) |
| 40 | + cls.unauthorized_api_instance = EndpointsApi(unauthorized_api_client) |
| 41 | + |
| 42 | + # Forbidden API Client |
| 43 | + forbidden_configuration = Configuration( |
| 44 | + username=FORBIDDEN_USERNAME, |
| 45 | + password=FORBIDDEN_PASSWORD |
| 46 | + ) |
| 47 | + forbidden_api_client = ApiClient(forbidden_configuration) |
| 48 | + cls.forbidden_api_instance = EndpointsApi(forbidden_api_client) |
| 49 | + |
| 50 | + cls.account_id = BW_ACCOUNT_ID |
| 51 | + cls.endpoint_id_array = [] |
| 52 | + cls.TEST_SLEEP = 2 |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def tearDownClass(cls): |
| 56 | + """Clean up endpoints created during tests""" |
| 57 | + for endpoint_id in cls.endpoint_id_array: |
| 58 | + try: |
| 59 | + cls.endpoints_api_instance.delete_endpoint(cls.account_id, endpoint_id) |
| 60 | + time.sleep(1) |
| 61 | + except Exception as e: |
| 62 | + print(f"Failed to cleanup endpoint {endpoint_id}: {e}") |
| 63 | + |
| 64 | + def test_create_endpoint(self): |
| 65 | + """Test creating a new WebRTC endpoint with all parameters""" |
| 66 | + time.sleep(self.TEST_SLEEP) |
| 67 | + |
| 68 | + create_request = CreateWebRtcConnectionRequest( |
| 69 | + type=EndpointTypeEnum.WEBRTC, |
| 70 | + direction=EndpointDirectionEnum.BIDIRECTIONAL, |
| 71 | + event_callback_url=BASE_CALLBACK_URL + "/endpoint/callback", |
| 72 | + event_fallback_url=BASE_CALLBACK_URL + "/endpoint/fallback", |
| 73 | + tag="python-sdk-test-endpoint" |
| 74 | + ) |
| 75 | + |
| 76 | + response: ApiResponse = self.endpoints_api_instance.create_endpoint_with_http_info( |
| 77 | + self.account_id, |
| 78 | + create_request |
| 79 | + ) |
| 80 | + |
| 81 | + assert_that(response.status_code, equal_to(201)) |
| 82 | + assert_that(response.data, instance_of(CreateEndpointResponse)) |
| 83 | + assert_that(response.data.data, has_properties( |
| 84 | + 'endpoint_id', instance_of(str), |
| 85 | + 'type', EndpointTypeEnum.WEBRTC, |
| 86 | + 'status', instance_of(EndpointStatusEnum), |
| 87 | + 'token', instance_of(str) |
| 88 | + )) |
| 89 | + |
| 90 | + # Store endpoint ID for cleanup |
| 91 | + self.endpoint_id_array.append(response.data.data.endpoint_id) |
| 92 | + |
| 93 | + def test_create_endpoint_minimal(self): |
| 94 | + """Test creating an endpoint with only required parameters""" |
| 95 | + time.sleep(self.TEST_SLEEP) |
| 96 | + |
| 97 | + create_request = CreateWebRtcConnectionRequest( |
| 98 | + type=EndpointTypeEnum.WEBRTC, |
| 99 | + direction=EndpointDirectionEnum.OUTBOUND |
| 100 | + ) |
| 101 | + |
| 102 | + response: CreateEndpointResponse = self.endpoints_api_instance.create_endpoint( |
| 103 | + self.account_id, |
| 104 | + create_request |
| 105 | + ) |
| 106 | + |
| 107 | + assert_that(response.data, has_properties( |
| 108 | + 'endpoint_id', instance_of(str), |
| 109 | + 'type', EndpointTypeEnum.WEBRTC, |
| 110 | + 'token', not_none() |
| 111 | + )) |
| 112 | + |
| 113 | + # Store endpoint ID for cleanup |
| 114 | + self.endpoint_id_array.append(response.data.endpoint_id) |
| 115 | + |
| 116 | + def test_get_endpoint(self): |
| 117 | + """Test retrieving an endpoint by ID""" |
| 118 | + time.sleep(self.TEST_SLEEP) |
| 119 | + |
| 120 | + # First create an endpoint |
| 121 | + create_request = CreateWebRtcConnectionRequest( |
| 122 | + type=EndpointTypeEnum.WEBRTC, |
| 123 | + direction=EndpointDirectionEnum.INBOUND, |
| 124 | + tag="test-get-endpoint" |
| 125 | + ) |
| 126 | + |
| 127 | + create_response: CreateEndpointResponse = self.endpoints_api_instance.create_endpoint( |
| 128 | + self.account_id, |
| 129 | + create_request |
| 130 | + ) |
| 131 | + endpoint_id = create_response.data.endpoint_id |
| 132 | + self.endpoint_id_array.append(endpoint_id) |
| 133 | + |
| 134 | + time.sleep(self.TEST_SLEEP) |
| 135 | + |
| 136 | + # Now get the endpoint |
| 137 | + response: ApiResponse = self.endpoints_api_instance.get_endpoint_with_http_info( |
| 138 | + self.account_id, |
| 139 | + endpoint_id |
| 140 | + ) |
| 141 | + |
| 142 | + assert_that(response.status_code, equal_to(200)) |
| 143 | + assert_that(response.data, instance_of(EndpointResponse)) |
| 144 | + assert_that(response.data.data, has_properties( |
| 145 | + 'endpoint_id', endpoint_id, |
| 146 | + 'type', EndpointTypeEnum.WEBRTC, |
| 147 | + 'tag', "test-get-endpoint" |
| 148 | + )) |
| 149 | + |
| 150 | + def test_get_endpoint_not_found(self): |
| 151 | + """Test getting a non-existent endpoint returns 404""" |
| 152 | + time.sleep(self.TEST_SLEEP) |
| 153 | + |
| 154 | + with self.assertRaises(ApiException) as context: |
| 155 | + self.endpoints_api_instance.get_endpoint( |
| 156 | + self.account_id, |
| 157 | + "non-existent-endpoint-id" |
| 158 | + ) |
| 159 | + |
| 160 | + assert_that(context.exception.status, equal_to(404)) |
| 161 | + |
| 162 | + def test_list_endpoints(self): |
| 163 | + """Test listing endpoints""" |
| 164 | + time.sleep(self.TEST_SLEEP) |
| 165 | + |
| 166 | + # Create a couple of endpoints first |
| 167 | + for i in range(2): |
| 168 | + create_request = CreateWebRtcConnectionRequest( |
| 169 | + type=EndpointTypeEnum.WEBRTC, |
| 170 | + direction=EndpointDirectionEnum.BIDIRECTIONAL, |
| 171 | + tag=f"test-list-endpoint-{i}" |
| 172 | + ) |
| 173 | + create_response = self.endpoints_api_instance.create_endpoint( |
| 174 | + self.account_id, |
| 175 | + create_request |
| 176 | + ) |
| 177 | + self.endpoint_id_array.append(create_response.data.endpoint_id) |
| 178 | + time.sleep(1) |
| 179 | + |
| 180 | + time.sleep(self.TEST_SLEEP) |
| 181 | + |
| 182 | + # List endpoints |
| 183 | + response: ApiResponse = self.endpoints_api_instance.list_endpoints_with_http_info( |
| 184 | + self.account_id, |
| 185 | + limit=10 |
| 186 | + ) |
| 187 | + |
| 188 | + assert_that(response.status_code, equal_to(200)) |
| 189 | + assert_that(response.data, instance_of(ListEndpointsResponse)) |
| 190 | + assert_that(response.data.data, instance_of(list)) |
| 191 | + |
| 192 | + def test_list_endpoints_with_filter(self): |
| 193 | + """Test listing endpoints with type filter""" |
| 194 | + time.sleep(self.TEST_SLEEP) |
| 195 | + |
| 196 | + response: ListEndpointsResponse = self.endpoints_api_instance.list_endpoints( |
| 197 | + self.account_id, |
| 198 | + type=EndpointTypeEnum.WEBRTC, |
| 199 | + limit=5 |
| 200 | + ) |
| 201 | + |
| 202 | + assert_that(response.data, instance_of(list)) |
| 203 | + # Verify all returned endpoints are of type WEBRTC |
| 204 | + for endpoint in response.data: |
| 205 | + assert_that(endpoint.type, equal_to(EndpointTypeEnum.WEBRTC)) |
| 206 | + |
| 207 | + def test_delete_endpoint(self): |
| 208 | + """Test deleting an endpoint""" |
| 209 | + time.sleep(self.TEST_SLEEP) |
| 210 | + |
| 211 | + # Create an endpoint to delete |
| 212 | + create_request = CreateWebRtcConnectionRequest( |
| 213 | + type=EndpointTypeEnum.WEBRTC, |
| 214 | + direction=EndpointDirectionEnum.BIDIRECTIONAL, |
| 215 | + tag="test-delete-endpoint" |
| 216 | + ) |
| 217 | + |
| 218 | + create_response: CreateEndpointResponse = self.endpoints_api_instance.create_endpoint( |
| 219 | + self.account_id, |
| 220 | + create_request |
| 221 | + ) |
| 222 | + endpoint_id = create_response.data.endpoint_id |
| 223 | + |
| 224 | + time.sleep(self.TEST_SLEEP) |
| 225 | + |
| 226 | + # Delete the endpoint |
| 227 | + response: ApiResponse = self.endpoints_api_instance.delete_endpoint_with_http_info( |
| 228 | + self.account_id, |
| 229 | + endpoint_id |
| 230 | + ) |
| 231 | + |
| 232 | + assert_that(response.status_code, equal_to(204)) |
| 233 | + |
| 234 | + # Verify endpoint is deleted |
| 235 | + time.sleep(self.TEST_SLEEP) |
| 236 | + with self.assertRaises(ApiException) as context: |
| 237 | + self.endpoints_api_instance.get_endpoint(self.account_id, endpoint_id) |
| 238 | + |
| 239 | + assert_that(context.exception.status, equal_to(404)) |
| 240 | + |
| 241 | + def test_delete_endpoint_not_found(self): |
| 242 | + """Test deleting a non-existent endpoint returns 404""" |
| 243 | + time.sleep(self.TEST_SLEEP) |
| 244 | + |
| 245 | + with self.assertRaises(ApiException) as context: |
| 246 | + self.endpoints_api_instance.delete_endpoint( |
| 247 | + self.account_id, |
| 248 | + "non-existent-endpoint-id" |
| 249 | + ) |
| 250 | + |
| 251 | + assert_that(context.exception.status, equal_to(404)) |
| 252 | + |
| 253 | + def test_create_endpoint_unauthorized(self): |
| 254 | + """Test creating an endpoint with invalid credentials returns 401""" |
| 255 | + time.sleep(self.TEST_SLEEP) |
| 256 | + |
| 257 | + create_request = CreateWebRtcConnectionRequest( |
| 258 | + type=EndpointTypeEnum.WEBRTC, |
| 259 | + direction=EndpointDirectionEnum.BIDIRECTIONAL |
| 260 | + ) |
| 261 | + |
| 262 | + with self.assertRaises(ApiException) as context: |
| 263 | + self.unauthorized_api_instance.create_endpoint( |
| 264 | + self.account_id, |
| 265 | + create_request |
| 266 | + ) |
| 267 | + |
| 268 | + assert_that(context.exception.status, equal_to(401)) |
| 269 | + |
| 270 | + def test_create_endpoint_forbidden(self): |
| 271 | + """Test creating an endpoint with forbidden credentials returns 403""" |
| 272 | + time.sleep(self.TEST_SLEEP) |
| 273 | + |
| 274 | + create_request = CreateWebRtcConnectionRequest( |
| 275 | + type=EndpointTypeEnum.WEBRTC, |
| 276 | + direction=EndpointDirectionEnum.BIDIRECTIONAL |
| 277 | + ) |
| 278 | + |
| 279 | + with self.assertRaises(ApiException) as context: |
| 280 | + self.forbidden_api_instance.create_endpoint( |
| 281 | + self.account_id, |
| 282 | + create_request |
| 283 | + ) |
| 284 | + |
| 285 | + assert_that(context.exception.status, equal_to(403)) |
| 286 | + |
| 287 | + |
| 288 | +if __name__ == '__main__': |
| 289 | + unittest.main() |
0 commit comments