def find_number_in_area_code(self, area_code, quantity=1, country_code='US'): """Find a number within an area code.""" if country_code not in ('US', 'CA'): logging.info('Only numbers in US/CA are supported, requested ' 'country: {}'.format(country_code)) if quantity < 1: raise ValueError( 'Quantity can not be < 1 - passed: {}'.format(quantity)) try: numbers = self.account_client.search_available_local_numbers( area_code=area_code, quantity=quantity) except BandwidthAccountAPIException as e: logging.info('Failed to search for phone number in given area ' 'code - error: {}'.format(e)) raise AreaCodeUnavailableError( SHBandwidthClient.NUMBER_UNAVAILABLE_MSG) else: if not numbers: raise AreaCodeUnavailableError( SHBandwidthClient.NUMBER_UNAVAILABLE_MSG) return self._cleanup_and_return_numbers(numbers, quantity)
def test_buy_number_bw_then_twilio_no_number(self, mock_twilio_buy, mock_bw_tf_buy, mock_bw_buy): """ tests bandwidth and twilio gateways in that order when buying a number should raise exception """ dummy_sid = 'dummysid' area_code = '919' dummy_user = '******' mock_bw_buy.side_effect = AreaCodeUnavailableError( 'Area Code is not available') # noqa mock_twilio_buy.side_effect = AreaCodeUnavailableError( 'Area Code is not available') # noqa self.assertRaises(AreaCodeUnavailableError, BuyPhoneNumberFromCarrier(), settings.SMS_GATEWAY_BANDWIDTH, dummy_sid, area_code=area_code, user=dummy_user, alt_gateway=True) mock_bw_buy.assert_called_with(phone_number=None, area_code=area_code, user_id=dummy_user, country_code='US') mock_twilio_buy.assert_called_with(twilioClient=settings.TWILIO_CLIENT, appSid=dummy_sid, areaCode=area_code, countryCode='US', phoneNumber=None)
def test_buy_number_bw_failure_no_alternate(self, mock_twilio_buy, mock_bw_tf_buy, mock_bw_buy): """ tests bandwidth first which fails and then tries alternate gateway to get a number """ dummy_sid = 'dummysid' area_code = '919' phone_number = '+19193456789' dummy_user = '******' mock_twilio_buy.return_value = SHBoughtNumberObject( phone_number, dummy_sid, '') # noqa mock_bw_buy.side_effect = AreaCodeUnavailableError( 'Area Code is not available') # noqa self.assertRaises(AreaCodeUnavailableError, BuyPhoneNumberFromCarrier(), settings.SMS_GATEWAY_BANDWIDTH, dummy_sid, area_code=area_code, user=dummy_user) mock_bw_buy.assert_called_with(phone_number=None, area_code=area_code, user_id=dummy_user, country_code='US') mock_twilio_buy.assert_not_called() mock_bw_tf_buy.assert_not_called()
def test_buy_number_twilio_failure_no_alternate(self, mock_twilio_buy, mock_bw_tf_buy, mock_bw_buy): """ tests invalid gateway when buying a number should return an error. """ dummy_sid = 'dummysid' area_code = '919' phone_number = '+19193456789' dummy_user = '******' mock_twilio_buy.side_effect = AreaCodeUnavailableError( 'Area Code is not available') # noqa mock_bw_buy.return_value = phone_number self.assertRaises(AreaCodeUnavailableError, BuyPhoneNumberFromCarrier(), settings.SMS_GATEWAY_TWILIO, dummy_sid, area_code=area_code, user=dummy_user) mock_twilio_buy.assert_called_with(twilioClient=settings.TWILIO_CLIENT, appSid=dummy_sid, areaCode=area_code, countryCode='US', phoneNumber=None) mock_bw_buy.assert_not_called() mock_bw_tf_buy.assert_not_called()
def test_buy_number_twilio_then_bw(self, mock_twilio_buy, mock_bw_tf_buy, mock_bw_buy): """ tests invalid gateway when buying a number should return an error. """ dummy_sid = 'dummysid' area_code = '919' phone_number = '+19193456789' dummy_user = '******' mock_twilio_buy.side_effect = AreaCodeUnavailableError( 'Area Code is not available') # noqa mock_bw_buy.return_value = phone_number bought_num = BuyPhoneNumberFromCarrier()(settings.SMS_GATEWAY_TWILIO, dummy_sid, area_code=area_code, user=dummy_user, alt_gateway=True) mock_twilio_buy.assert_called_with(twilioClient=settings.TWILIO_CLIENT, appSid=dummy_sid, areaCode=area_code, countryCode='US', phoneNumber=None) mock_bw_buy.assert_called_with(phone_number=None, area_code=area_code, user_id=dummy_user, country_code='US') mock_bw_tf_buy.assert_not_called() self.assertEqual(bought_num.phone_number, phone_number) self.assertEqual(bought_num.sid, None) self.assertEqual(bought_num.gateway, settings.SMS_GATEWAY_BANDWIDTH)
def test_avail_number_invalid_areacode_exception(self, mock_twilio, mock_bw, mock_bw_tf): """ tests that when trying to query available phone numbers from invalid provider """ area_code = '919' mock_bw.side_effect = AreaCodeUnavailableError( 'area code not available') # noqa self.assertEqual( FindPhoneNumberInAreaCode()(settings.SMS_GATEWAY_BANDWIDTH, area_code=area_code), []) mock_twilio.assert_not_called() mock_bw_tf.assert_not_called()
def test_buy_number_bw_then_twilio(self, mock_twilio_buy, mock_bw_tf_buy, mock_bw_buy): """ tests bandwidth first which fails and then tries alternate gateway to get a number """ dummy_sid = 'dummysid' area_code = '919' phone_number = '+19193456789' dummy_user = '******' mock_twilio_buy.return_value = SHBoughtNumberObject( phone_number, dummy_sid, '') # noqa mock_bw_buy.side_effect = AreaCodeUnavailableError( 'Area Code is not available') # noqa bought_num = BuyPhoneNumberFromCarrier()( settings.SMS_GATEWAY_BANDWIDTH, dummy_sid, area_code=area_code, user=dummy_user, alt_gateway=True) mock_bw_buy.assert_called_with(phone_number=None, area_code=area_code, user_id=dummy_user, country_code='US') mock_twilio_buy.assert_called_with(twilioClient=settings.TWILIO_CLIENT, appSid=dummy_sid, areaCode=area_code, countryCode='US', phoneNumber=None) mock_bw_tf_buy.assert_not_called() self.assertEqual(bought_num.phone_number, phone_number) self.assertEqual(bought_num.sid, dummy_sid)
def buy_phone_number(self, phone_number=None, area_code=None, user_id=None, site_id=None, country_code='US'): """ buy a phone number 'phone_number' from bandwidth : :param: phone_number - if specified, number to be bought :param: area_code - if specific phone number is not the ask :param: country_code = 'US' only supported :param: user_id this phone number if allocated to - for BW dashboard : returns: phone number bought, None if invalid parameters : or Exception if there is one. """ if country_code not in ('US', 'CA'): logging.info('Only numbers in US or CA are supported, requested ' 'country: {}'.format(country_code)) site_id = site_id if site_id else settings.BW_SITE_ID if phone_number: if validatePhoneNumber(phone_number, False) is False: raise ValueError("Invalid phone number passed- unable to buy") # a specific number ought to be ordered logging.info( 'buy_phone_number(): buying requested number: {}.'.format( phone_number, site_id)) try: newNumber = self.account_client.order_phone_number( number=self._parse_number_to_bw_format(phone_number), name='SendHub Customer: {}'.format(user_id), quantity=1, siteid=site_id) except BandwidthOrderPendingException as order_id: logging.warn('Order {} is pending for phone number: {}, ' 'user: {}, looks like bandwidth service is ' 'slow. Error out for now and nightly cleanup ' 'task will release the number.'.format( order_id, phone_number, user_id)) raise BWNumberUnavailableError( 'Pending Number Order: ' + SHBandwidthClient.NUMBER_UNAVAILABLE_MSG) except BandwidthAccountAPIException as e: # If we didn't get the number, throw an error err_resp = u'We could not get number {} from our carrier. ' \ u'Carrier Message: {}.'.format(phone_number, e) logging.error(err_resp) raise BWNumberUnavailableError(err_resp) # we bought the number successfully return self._cleanup_and_return_numbers(newNumber, quantity=1) else: if area_code is None: return False try: ordered_number = self.account_client.search_and_order_local_numbers( # noqa area_code=area_code, quantity=1, name='SendHub Customer: {}'.format(user_id), siteid=site_id) except BandwidthOrderPendingException as order_id: logging.warn('Order {} is pending for a number in ' 'area code: {}, user_id: {}, qty: 1, ' 'looks like bandwidth service is slow. ' 'Error out for now and nightly cleanup task ' 'will release the number.'.format( order_id, area_code, user_id)) raise AreaCodeUnavailableError( 'Pending Area Code Order: ' + SHBandwidthClient.NUMBER_UNAVAILABLE_MSG) except BandwidthAccountAPIException as e: # If we didn't get the number, throw an error logging.error(u'buy_phone_number(): could not get number. ' u'Throwing an error - {}.'.format(e)) raise AreaCodeUnavailableError( SHBandwidthClient.NUMBER_UNAVAILABLE_MSG) return self._cleanup_and_return_numbers(ordered_number, quantity=1)