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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
|
import os
import pytest
import uuid
from devtools_testutils import recorded_by_proxy, add_general_regex_sanitizer
from _shared.utils import create_token_credential, get_header_policy, get_http_logging_policy
from azure.communication.phonenumbers import PhoneNumbersClient
from azure.communication.phonenumbers import (
PhoneNumberAssignmentType,
PhoneNumberCapabilities,
PhoneNumberCapabilityType,
PhoneNumberType,
ReservationStatus,
PhoneNumberAvailabilityStatus
)
from azure.communication.phonenumbers._generated.models import PhoneNumberOperationStatus
from azure.communication.phonenumbers._shared.utils import parse_connection_str
from phone_numbers_testcase import PhoneNumbersTestCase
# Used to sanitize the reservation ID in the test recordings
STATIC_RESERVATION_ID = "6227aeb8-8086-4824-9586-05cafe96f37b"
SKIP_PURCHASE_PHONE_NUMBER_TESTS = True
PURCHASE_PHONE_NUMBER_TEST_SKIP_REASON = "Phone numbers shouldn't be purchased in live tests"
SKIP_INT_PHONE_NUMBER_TESTS = os.getenv("COMMUNICATION_SKIP_INT_PHONENUMBERS_TEST", "false") == "true"
INT_PHONE_NUMBER_TEST_SKIP_REASON = (
"Phone numbers setting SMS capability does not support in INT. Skip these tests in INT."
)
SKIP_UPDATE_CAPABILITIES_TESTS = os.getenv("COMMUNICATION_SKIP_CAPABILITIES_LIVE_TEST", "false") == "true"
SKIP_UPDATE_CAPABILITIES_TESTS_REASON = "Phone number capabilities are skipped."
def _get_test_phone_number():
if SKIP_UPDATE_CAPABILITIES_TESTS:
return os.environ["AZURE_PHONE_NUMBER"]
test_agent = os.environ["AZURE_TEST_AGENT"]
return os.environ["AZURE_PHONE_NUMBER_" + test_agent]
def is_client_error_status_code(
status_code, # type: int
):
return status_code >= 400 and status_code < 500
class TestPhoneNumbersClient(PhoneNumbersTestCase):
def setup_method(self):
super(TestPhoneNumbersClient, self).setUp(use_dynamic_resource=False)
if self.is_playback():
self.phone_number = "sanitized"
self.country_code = "US"
# In playback mode, all reservation IDs are sanitized to the same value
self.reservation_id = STATIC_RESERVATION_ID
self.purchased_reservation_id = STATIC_RESERVATION_ID
else:
self.phone_number = _get_test_phone_number()
self.country_code = os.getenv("AZURE_COMMUNICATION_SERVICE_COUNTRY_CODE", "US")
# In live mode, generate unique reservation IDs for each test run
# Tests that create and delete reservations will use the same ID
self.reservation_id = str(uuid.uuid4())
# The purchase reservation test will use a different ID,
# since purchased reservations are immutable and cannot be modified after purchase
self.purchased_reservation_id = str(uuid.uuid4())
self.phone_number_client = PhoneNumbersClient.from_connection_string(
self.connection_str, http_logging_policy=get_http_logging_policy(), headers_policy=get_header_policy()
)
def _get_managed_identity_phone_number_client(self):
endpoint, *_ = parse_connection_str(self.connection_str)
credential = create_token_credential()
return PhoneNumbersClient(
endpoint, credential, http_logging_policy=get_http_logging_policy(), headers_policy=get_header_policy()
)
@recorded_by_proxy
def test_list_purchased_phone_numbers_from_managed_identity(self, **kwargs):
phone_number_client = self._get_managed_identity_phone_number_client()
phone_numbers = phone_number_client.list_purchased_phone_numbers()
assert phone_numbers.next()
@recorded_by_proxy
def test_list_purchased_phone_numbers(self, **kwargs):
client = self.phone_number_client
phone_numbers = client.list_purchased_phone_numbers()
assert phone_numbers.next()
@recorded_by_proxy
def test_get_purchased_phone_number_from_managed_identity(self, **kwargs):
phone_number_client = self._get_managed_identity_phone_number_client()
phone_number = phone_number_client.get_purchased_phone_number(self.phone_number)
assert phone_number.phone_number == self.phone_number
@recorded_by_proxy
def test_get_purchased_phone_number(self, **kwargs):
phone_number = self.phone_number_client.get_purchased_phone_number(self.phone_number)
assert phone_number.phone_number == self.phone_number
@pytest.mark.skipif(SKIP_INT_PHONE_NUMBER_TESTS, reason=INT_PHONE_NUMBER_TEST_SKIP_REASON)
@recorded_by_proxy
def test_search_available_phone_numbers_from_managed_identity(self, **kwargs):
phone_number_client = self._get_managed_identity_phone_number_client()
capabilities = PhoneNumberCapabilities(
calling=PhoneNumberCapabilityType.INBOUND, sms=PhoneNumberCapabilityType.INBOUND_OUTBOUND
)
poller = phone_number_client.begin_search_available_phone_numbers(
self.country_code,
PhoneNumberType.TOLL_FREE,
PhoneNumberAssignmentType.APPLICATION,
capabilities,
polling=True,
)
assert poller.result()
@pytest.mark.skipif(SKIP_INT_PHONE_NUMBER_TESTS, reason=INT_PHONE_NUMBER_TEST_SKIP_REASON)
@recorded_by_proxy
def test_search_available_phone_numbers(self, **kwargs):
capabilities = PhoneNumberCapabilities(
calling=PhoneNumberCapabilityType.INBOUND, sms=PhoneNumberCapabilityType.INBOUND_OUTBOUND
)
poller = self.phone_number_client.begin_search_available_phone_numbers(
self.country_code,
PhoneNumberType.TOLL_FREE,
PhoneNumberAssignmentType.APPLICATION,
capabilities,
polling=True,
)
assert poller.result()
@pytest.mark.skipif(SKIP_INT_PHONE_NUMBER_TESTS, reason=INT_PHONE_NUMBER_TEST_SKIP_REASON)
@pytest.mark.skipif(SKIP_UPDATE_CAPABILITIES_TESTS, reason=SKIP_UPDATE_CAPABILITIES_TESTS_REASON)
@recorded_by_proxy
def test_update_phone_number_capabilities_from_managed_identity(self, **kwargs):
phone_number_client = self._get_managed_identity_phone_number_client()
current_phone_number = phone_number_client.get_purchased_phone_number(self.phone_number)
calling_capabilities = (
PhoneNumberCapabilityType.INBOUND
if current_phone_number.capabilities.calling == PhoneNumberCapabilityType.OUTBOUND
else PhoneNumberCapabilityType.OUTBOUND
)
sms_capabilities = (
PhoneNumberCapabilityType.INBOUND_OUTBOUND
if current_phone_number.capabilities.sms == PhoneNumberCapabilityType.OUTBOUND
else PhoneNumberCapabilityType.OUTBOUND
)
poller = phone_number_client.begin_update_phone_number_capabilities(
self.phone_number, sms_capabilities, calling_capabilities, polling=True
)
assert poller.result()
assert poller.status() == PhoneNumberOperationStatus.SUCCEEDED.value
@pytest.mark.skipif(SKIP_INT_PHONE_NUMBER_TESTS, reason=INT_PHONE_NUMBER_TEST_SKIP_REASON)
@pytest.mark.skipif(SKIP_UPDATE_CAPABILITIES_TESTS, reason=SKIP_UPDATE_CAPABILITIES_TESTS_REASON)
@recorded_by_proxy
def test_update_phone_number_capabilities(self, **kwargs):
current_phone_number = self.phone_number_client.get_purchased_phone_number(self.phone_number)
calling_capabilities = (
PhoneNumberCapabilityType.INBOUND
if current_phone_number.capabilities.calling == PhoneNumberCapabilityType.OUTBOUND
else PhoneNumberCapabilityType.OUTBOUND
)
sms_capabilities = (
PhoneNumberCapabilityType.INBOUND_OUTBOUND
if current_phone_number.capabilities.sms == PhoneNumberCapabilityType.OUTBOUND
else PhoneNumberCapabilityType.OUTBOUND
)
poller = self.phone_number_client.begin_update_phone_number_capabilities(
self.phone_number, sms_capabilities, calling_capabilities, polling=True
)
assert poller.result()
assert poller.status() == PhoneNumberOperationStatus.SUCCEEDED.value
@pytest.mark.skipif(SKIP_PURCHASE_PHONE_NUMBER_TESTS, reason=PURCHASE_PHONE_NUMBER_TEST_SKIP_REASON)
@recorded_by_proxy
def test_purchase_phone_number_from_managed_identity(self, **kwargs):
phone_number_client = self._get_managed_identity_phone_number_client()
capabilities = PhoneNumberCapabilities(
calling=PhoneNumberCapabilityType.INBOUND, sms=PhoneNumberCapabilityType.INBOUND_OUTBOUND
)
search_poller = phone_number_client.begin_search_available_phone_numbers(
self.country_code,
PhoneNumberType.TOLL_FREE,
PhoneNumberAssignmentType.APPLICATION,
capabilities,
polling=True,
)
phone_number_to_buy = search_poller.result()
purchase_poller = phone_number_client.begin_purchase_phone_numbers(phone_number_to_buy.search_id, polling=True)
purchase_poller.result()
assert purchase_poller.status() == PhoneNumberOperationStatus.SUCCEEDED.value
release_poller = phone_number_client.begin_release_phone_number(phone_number_to_buy.phone_numbers[0])
release_poller.result()
assert release_poller.status() == PhoneNumberOperationStatus.SUCCEEDED.value
@pytest.mark.skipif(SKIP_PURCHASE_PHONE_NUMBER_TESTS, reason=PURCHASE_PHONE_NUMBER_TEST_SKIP_REASON)
@recorded_by_proxy
def test_purchase_phone_numbers(self, **kwargs):
capabilities = PhoneNumberCapabilities(
calling=PhoneNumberCapabilityType.INBOUND, sms=PhoneNumberCapabilityType.INBOUND_OUTBOUND
)
search_poller = self.phone_number_client.begin_search_available_phone_numbers(
self.country_code,
PhoneNumberType.TOLL_FREE,
PhoneNumberAssignmentType.APPLICATION,
capabilities,
polling=True,
)
phone_number_to_buy = search_poller.result()
purchase_poller = self.phone_number_client.begin_purchase_phone_numbers(
phone_number_to_buy.search_id, polling=True
)
purchase_poller.result()
assert purchase_poller.status() == PhoneNumberOperationStatus.SUCCEEDED.value
release_poller = self.phone_number_client.begin_release_phone_number(phone_number_to_buy.phone_numbers[0])
release_poller.result()
assert release_poller.status() == PhoneNumberOperationStatus.SUCCEEDED.value
@recorded_by_proxy
def test_purchase_phone_numbers_without_agreement_to_not_resell(self,):
capabilities = PhoneNumberCapabilities(
calling=PhoneNumberCapabilityType.OUTBOUND, sms=PhoneNumberCapabilityType.NONE
)
# France doesn't allow reselling of phone numbers, so purchases without agreement to not resell should fail
search_poller = self.phone_number_client.begin_search_available_phone_numbers(
"FR",
PhoneNumberType.TOLL_FREE,
PhoneNumberAssignmentType.APPLICATION,
capabilities,
polling=True,
)
phone_number_to_buy = search_poller.result()
with pytest.raises(Exception) as ex:
self.phone_number_client.begin_purchase_phone_numbers(
phone_number_to_buy.search_id, agree_to_not_resell=False, polling=True
)
assert is_client_error_status_code(ex.value.status_code) is True
assert ex.value.message is not None
@recorded_by_proxy
def test_get_purchased_phone_number_with_invalid_phone_number(self, **kwargs):
if self.is_playback():
phone_number = "sanitized"
else:
phone_number = "+14255550123"
with pytest.raises(Exception) as ex:
self.phone_number_client.get_purchased_phone_number(phone_number)
assert (
is_client_error_status_code(ex.value.status_code) is True
), "Status code {ex.value.status_code} does not indicate a client error" # type: ignore
assert ex.value.message is not None # type: ignore
@recorded_by_proxy
def test_search_available_phone_numbers_with_invalid_country_code(self, **kwargs):
capabilities = PhoneNumberCapabilities(
calling=PhoneNumberCapabilityType.INBOUND, sms=PhoneNumberCapabilityType.INBOUND_OUTBOUND
)
with pytest.raises(Exception) as ex:
self.phone_number_client.begin_search_available_phone_numbers(
"XX", PhoneNumberType.TOLL_FREE, PhoneNumberAssignmentType.APPLICATION, capabilities, polling=True
)
@recorded_by_proxy
def test_update_phone_number_capabilities_with_unauthorized_number(self, **kwargs):
if self.is_playback():
phone_number = "sanitized"
else:
phone_number = "+14255555111"
with pytest.raises(Exception) as ex:
self.phone_number_client.begin_update_phone_number_capabilities(
phone_number,
PhoneNumberCapabilityType.INBOUND_OUTBOUND,
PhoneNumberCapabilityType.INBOUND,
polling=True,
)
assert (
is_client_error_status_code(ex.value.status_code) is True
), "Status code {ex.value.status_code} does not indicate a client error" # type: ignore
assert ex.value.message is not None # type: ignore
@recorded_by_proxy
def test_update_phone_number_capabilities_with_invalid_number(self, **kwargs):
if self.is_playback():
phone_number = "invalid_phone_number"
else:
phone_number = "invalid_phone_number"
with pytest.raises(Exception) as ex:
self.phone_number_client.begin_update_phone_number_capabilities(
phone_number,
PhoneNumberCapabilityType.INBOUND_OUTBOUND,
PhoneNumberCapabilityType.INBOUND,
polling=True,
)
assert (
is_client_error_status_code(ex.value.status_code) is True
), "Status code {ex.value.status_code} does not indicate a client error" # type: ignore
assert ex.value.message is not None # type: ignore
@recorded_by_proxy
def test_update_phone_number_capabilities_with_empty_number(self, **kwargs):
if self.is_playback():
phone_number = ""
else:
phone_number = ""
with pytest.raises(ValueError) as ex:
self.phone_number_client.begin_update_phone_number_capabilities(
phone_number,
PhoneNumberCapabilityType.INBOUND_OUTBOUND,
PhoneNumberCapabilityType.INBOUND,
polling=True,
)
@recorded_by_proxy
def test_list_toll_free_area_codes_from_managed_identity(self):
phone_number_client = self._get_managed_identity_phone_number_client()
area_codes = phone_number_client.list_available_area_codes(
"US", PhoneNumberType.TOLL_FREE, assignment_type=PhoneNumberAssignmentType.APPLICATION
)
expected_area_codes = {"888", "877", "866", "855", "844", "800", "833", "88"}
for area_code in area_codes:
assert area_code.area_code in expected_area_codes
assert area_codes is not None
@recorded_by_proxy
def test_list_toll_free_area_codes(self):
area_codes = self.phone_number_client.list_available_area_codes(
"US", PhoneNumberType.TOLL_FREE, assignment_type=PhoneNumberAssignmentType.APPLICATION
)
expected_area_codes = {"888", "877", "866", "855", "844", "800", "833", "88"}
for area_code in area_codes:
assert area_code.area_code in expected_area_codes
assert area_codes is not None
@recorded_by_proxy
def test_list_geographic_area_codes_from_managed_identity(self):
phone_number_client = self._get_managed_identity_phone_number_client()
first_locality = phone_number_client.list_available_localities("US").next()
area_codes = self.phone_number_client.list_available_area_codes(
"US",
PhoneNumberType.GEOGRAPHIC,
assignment_type=PhoneNumberAssignmentType.PERSON,
locality=first_locality.localized_name,
administrative_division=first_locality.administrative_division.abbreviated_name,
)
assert area_codes.next()
@recorded_by_proxy
def test_list_geographic_area_codes(self):
first_locality = self.phone_number_client.list_available_localities("US").next()
area_codes = self.phone_number_client.list_available_area_codes(
"US",
PhoneNumberType.GEOGRAPHIC,
assignment_type=PhoneNumberAssignmentType.PERSON,
locality=first_locality.localized_name,
administrative_division=first_locality.administrative_division.abbreviated_name,
)
assert area_codes.next()
@recorded_by_proxy
def test_list_mobile_area_codes_from_managed_identity(self):
phone_number_client = self._get_managed_identity_phone_number_client()
first_locality = phone_number_client.list_available_localities("IE", phone_number_type=PhoneNumberType.MOBILE).next()
area_codes = self.phone_number_client.list_available_area_codes(
"IE",
PhoneNumberType.MOBILE,
assignment_type=PhoneNumberAssignmentType.APPLICATION,
locality=first_locality.localized_name,
)
assert area_codes.next()
@recorded_by_proxy
def test_list_mobile_area_codes(self):
first_locality = self.phone_number_client.list_available_localities("IE", phone_number_type=PhoneNumberType.MOBILE).next()
area_codes = self.phone_number_client.list_available_area_codes(
"IE",
PhoneNumberType.MOBILE,
assignment_type=PhoneNumberAssignmentType.APPLICATION,
locality=first_locality.localized_name,
)
assert area_codes.next()
@recorded_by_proxy
def test_list_countries_from_managed_identity(self):
phone_number_client = self._get_managed_identity_phone_number_client()
countries = phone_number_client.list_available_countries()
assert countries.next()
@recorded_by_proxy
def test_list_countries(self):
countries = self.phone_number_client.list_available_countries()
assert countries.next()
@recorded_by_proxy
def test_list_localities_from_managed_identity(self):
phone_number_client = self._get_managed_identity_phone_number_client()
localities = phone_number_client.list_available_localities("US")
assert localities.next()
@recorded_by_proxy
def test_list_localities(self):
localities = self.phone_number_client.list_available_localities("US")
assert localities.next()
@recorded_by_proxy
def test_list_localities_with_ad_from_managed_identity(self):
phone_number_client = self._get_managed_identity_phone_number_client()
first_locality = phone_number_client.list_available_localities("US")
localities = phone_number_client.list_available_localities(
"US", administrative_division=first_locality.next().administrative_division.abbreviated_name
)
assert localities.next()
@recorded_by_proxy
def test_list_localities_with_ad(self):
first_locality = self.phone_number_client.list_available_localities("US")
localities = self.phone_number_client.list_available_localities(
"US", administrative_division=first_locality.next().administrative_division.abbreviated_name
)
assert localities.next()
@recorded_by_proxy
def test_list_localities_with_number_type(self):
localities = self.phone_number_client.list_available_localities("IE", phone_number_type=PhoneNumberType.MOBILE)
assert localities.next()
@recorded_by_proxy
def test_list_offerings_from_managed_identity(self):
phone_number_client = self._get_managed_identity_phone_number_client()
offerings = phone_number_client.list_available_offerings("US")
assert offerings.next()
@recorded_by_proxy
def test_list_offerings(self):
offerings = self.phone_number_client.list_available_offerings("US")
assert offerings.next()
@recorded_by_proxy
def test_search_operator_information_with_too_many_phone_numbers(self):
if self.is_playback():
phone_numbers = ["sanitized", "sanitized"]
else:
phone_numbers = [self.phone_number, self.phone_number]
with pytest.raises(Exception) as ex:
self.phone_number_client.search_operator_information(phone_numbers)
assert (
is_client_error_status_code(ex.value.status_code) is True
), "Status code {ex.value.status_code} does not indicate a client error" # type: ignore
assert ex.value.message is not None # type: ignore
@recorded_by_proxy
def test_search_operator_information_with_list(self):
if self.is_playback():
phone_number = "sanitized"
else:
phone_number = self.phone_number
results = self.phone_number_client.search_operator_information([phone_number])
assert len(results.values) == 1
assert results.values[0].phone_number == self.phone_number
@recorded_by_proxy
def test_search_operator_information_with_single_string(self):
if self.is_playback():
phone_number = "sanitized"
else:
phone_number = self.phone_number
results = self.phone_number_client.search_operator_information(phone_number)
assert len(results.values) == 1
assert results.values[0].phone_number == self.phone_number
@recorded_by_proxy
def test_list_phone_numbers_reservations(self):
reservations = self.phone_number_client.list_reservations()
assert reservations.next()
@recorded_by_proxy
def test_browse_available_numbers(self):
result = self.phone_number_client.browse_available_phone_numbers(
country_code=self.country_code,
phone_number_type=PhoneNumberType.TOLL_FREE
)
available_phone_numbers = result.phone_numbers
assert available_phone_numbers
assert len(available_phone_numbers) > 0
# This test does a lot of stuff because pytest doesn't have an easy built-in way to execute tests in a specific order.
@recorded_by_proxy
def test_phone_numbers_reservation_management(self):
add_general_regex_sanitizer(
regex=r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}",
value=STATIC_RESERVATION_ID,
function_scoped=True, # This ensures the sanitizer is only applied to this test
)
reservation_id = self.reservation_id
# Test that a reservation can be created without any phone numbers
created_reservation = self.phone_number_client.create_or_update_reservation(reservation_id=reservation_id)
assert created_reservation.id == reservation_id
assert created_reservation.status == ReservationStatus.ACTIVE
assert len(created_reservation.phone_numbers) == 0
# Test that we can add phone numbers to the reservation
browse_result = self.phone_number_client.browse_available_phone_numbers(
country_code=self.country_code,
phone_number_type=PhoneNumberType.TOLL_FREE
)
phone_number_to_reserve = browse_result.phone_numbers[0]
updated_reservation = self.phone_number_client.create_or_update_reservation(
reservation_id=reservation_id, numbers_to_add=[phone_number_to_reserve]
)
assert updated_reservation.id == reservation_id
assert updated_reservation.status == ReservationStatus.ACTIVE
assert updated_reservation.expires_at > created_reservation.expires_at
assert phone_number_to_reserve.id in updated_reservation.phone_numbers
assert updated_reservation.phone_numbers[phone_number_to_reserve.id].status == PhoneNumberAvailabilityStatus.RESERVED
# Test that we can get the reservation by ID
retrieved_reservation = self.phone_number_client.get_reservation(reservation_id)
assert retrieved_reservation.id == updated_reservation.id
assert retrieved_reservation.status == updated_reservation.status
assert retrieved_reservation.expires_at == updated_reservation.expires_at
assert len(retrieved_reservation.phone_numbers) == len(updated_reservation.phone_numbers)
# Test that we can remove numbers from the reservation
reservation_after_remove = self.phone_number_client.create_or_update_reservation(
reservation_id=reservation_id, numbers_to_remove=[phone_number_to_reserve.id])
assert reservation_after_remove.id == updated_reservation.id
assert reservation_after_remove.status == updated_reservation.status
assert reservation_after_remove.expires_at > updated_reservation.expires_at
assert phone_number_to_reserve.id not in reservation_after_remove.phone_numbers
# Test that we can delete the reservation
self.phone_number_client.delete_reservation(reservation_id)
with pytest.raises(Exception) as ex:
self.phone_number_client.get_reservation(reservation_id)
assert ex.value.status_code == 404
@recorded_by_proxy
def test_purchase_reservation_without_agreement_to_not_resell(self):
add_general_regex_sanitizer(
regex=r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}",
value=STATIC_RESERVATION_ID,
function_scoped=True, # This ensures the sanitizer is only applied to this test
)
reservation_id = self.reservation_id
# France doesn't allow reselling of phone numbers, so purchases without agreement to not resell should fail
browse_result = self.phone_number_client.browse_available_phone_numbers(
country_code="FR",
phone_number_type=PhoneNumberType.TOLL_FREE
)
# The phone number can be reserved, but not purchased without agreement to not resell
phone_number = browse_result.phone_numbers[0]
created_reservation = self.phone_number_client.create_or_update_reservation(
reservation_id=reservation_id, numbers_to_add=[phone_number])
assert created_reservation.phone_numbers[phone_number.id].status == PhoneNumberAvailabilityStatus.RESERVED
assert created_reservation.status == ReservationStatus.ACTIVE
# Purchase should fail without agreement to not resell
with pytest.raises(Exception) as ex:
self.phone_number_client.begin_purchase_reservation(
reservation_id, agree_to_not_resell=False, polling=True
)
assert is_client_error_status_code(ex.value.status_code) is True
assert ex.value.message is not None
# Clean up the reservation
self.phone_number_client.delete_reservation(reservation_id)
@pytest.mark.skipif(SKIP_PURCHASE_PHONE_NUMBER_TESTS, reason=PURCHASE_PHONE_NUMBER_TEST_SKIP_REASON)
@recorded_by_proxy
def test_purchase_reservation(self):
add_general_regex_sanitizer(
regex=r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}",
value=STATIC_RESERVATION_ID,
function_scoped=True, # This ensures the sanitizer is only applied to this test
)
reservation_id = self.purchased_reservation_id
# Test that we can purchase a reservation
browse_result = self.phone_number_client.browse_available_phone_numbers(
country_code=self.country_code,
phone_number_type=PhoneNumberType.TOLL_FREE
)
phone_number = browse_result.phone_numbers[0]
created_reservation = self.phone_number_client.create_or_update_reservation(
reservation_id=reservation_id, numbers_to_add=[phone_number])
assert created_reservation.phone_numbers[phone_number.id].status == PhoneNumberAvailabilityStatus.RESERVED
assert created_reservation.status == ReservationStatus.ACTIVE
# Purchase the reservation
poller = self.phone_number_client.begin_purchase_reservation(reservation_id, polling=True)
poller.result()
assert poller.status() == PhoneNumberOperationStatus.SUCCEEDED.value
# Verify that the number has been purchased
purchased_phone_number = self.phone_number_client.get_purchased_phone_number(phone_number.id)
assert purchased_phone_number is not None
# Release the purchased phone number
release_poller = self.phone_number_client.begin_release_phone_number(phone_number.id, polling=True)
release_poller.result()
assert release_poller.status() == PhoneNumberOperationStatus.SUCCEEDED.value
|