示例#1
0
    def test_buy_number_areacode_bw(self, mock_buy_tf_number, mock_buy_number):
        """
           tests that when trying to buy a phone number from
           bandwidth provider, appropriate function is called with
           appropriate parameters.

           based on area code
        """
        dummy_sid = 'dummysid'
        area_code = '919'
        phone_number = '+19193456789'
        dummy_user = '******'
        mock_buy_tf_number.return_value = '+18331234567'
        mock_buy_number.return_value = phone_number

        bought_num = BuyPhoneNumberFromCarrier()(
            settings.SMS_GATEWAY_BANDWIDTH,
            dummy_sid,
            area_code=area_code,
            toll_free=False,
            user=dummy_user)

        mock_buy_number.assert_called_with(phone_number=None,
                                           area_code=area_code,
                                           user_id=dummy_user,
                                           country_code='US')
        self.assertEqual(bought_num.phone_number, phone_number)
        self.assertEqual(bought_num.sid, None)
        self.assertEqual(bought_num.gateway, settings.SMS_GATEWAY_BANDWIDTH)
        mock_buy_tf_number.assert_not_called()
示例#2
0
    def test_buy_number_bw_other_exception(self, mock_twilio_buy,
                                           mock_bw_tf_buy, mock_bw_buy):
        """
            tests other exception than AreaCode unavailable error...
            should not try
        """
        dummy_sid = 'dummysid'
        phone_number = '+12345678999'
        area_code = '919'
        dummy_user = '******'
        mock_bw_buy.side_effect = BWNumberUnavailableError(
            'phone number is not available')  # noqa

        self.assertRaises(BWNumberUnavailableError,
                          BuyPhoneNumberFromCarrier(),
                          settings.SMS_GATEWAY_BANDWIDTH,
                          dummy_sid,
                          area_code=area_code,
                          phone_number=phone_number,
                          user=dummy_user,
                          alt_gateway=True)

        mock_bw_buy.assert_called_with(phone_number=phone_number,
                                       area_code=area_code,
                                       user_id=dummy_user,
                                       country_code='US')

        mock_twilio_buy.assert_not_called()
        mock_bw_tf_buy.assert_not_called()
示例#3
0
    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()
示例#4
0
    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)
示例#5
0
    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()
示例#6
0
    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)
示例#7
0
    def test_buy_number_invalid(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_bw_tf_buy.return_value = '+18331234567'
        mock_bw_buy.return_value = phone_number
        mock_twilio_buy.return_value = phone_number

        self.assertRaises(AreaCodeUnavailableError,
                          BuyPhoneNumberFromCarrier(),
                          'invalid-gw',
                          dummy_sid,
                          area_code=area_code,
                          phone_number=None,
                          toll_free=True,
                          user=dummy_user)

        mock_bw_tf_buy.assert_not_called()
        mock_twilio_buy.assert_not_called()
        mock_bw_buy.assert_not_called()
示例#8
0
    def test_buy_tf_number_bw_failure(self, mock_buy_tf_number,
                                      mock_buy_number, mock_twilio_buy):
        """
           tests that when trying to buy a phone number from
           bandwidth provider, appropriate function is called with
           appropriate parameters.

           tests failure to buy BW toll-free number
        """
        dummy_sid = 'dummysid'
        area_code = '919'
        phone_number = '+19193456789'
        dummy_user = '******'
        mock_buy_tf_number.side_effect = BWTollFreeUnavailableError(
            'Toll Free Phone number is not available')  # noqa

        self.assertRaises(BWTollFreeUnavailableError,
                          BuyPhoneNumberFromCarrier(),
                          settings.SMS_GATEWAY_BANDWIDTH,
                          dummy_sid,
                          area_code=area_code,
                          phone_number=phone_number,
                          toll_free=True,
                          user=dummy_user)

        mock_buy_tf_number.assert_called_with(user_id=dummy_user)
        mock_buy_number.assert_not_called()
        mock_twilio_buy.assert_not_called()
示例#9
0
    def test_buy_tf_number_bw(self, mock_buy_tf_number, mock_buy_number):
        """
           tests that when trying to buy a phone number from
           bandwidth provider, appropriate function is called with
           appropriate parameters.

           buy toll free number
        """
        dummy_sid = 'dummysid'
        phone_number = '+19193456789'
        tf_phone_number = '+18331234567'
        dummy_user = '******'
        mock_buy_tf_number.return_value = tf_phone_number
        mock_buy_number.return_value = phone_number

        bought_num = BuyPhoneNumberFromCarrier()(
            settings.SMS_GATEWAY_BANDWIDTH,
            dummy_sid,
            toll_free=True,
            user=dummy_user)

        mock_buy_tf_number.assert_called_with(user_id=dummy_user)
        self.assertEqual(bought_num.phone_number, tf_phone_number)
        self.assertEqual(bought_num.sid, None)
        self.assertEqual(bought_num.gateway, settings.SMS_GATEWAY_BANDWIDTH)
        mock_buy_number.assert_not_called()
示例#10
0
    def test_buy_number_twilio_failure(self, mock_twilio_buy):
        """
           tests that when trying to buy a phone number from
           twilio provider, appropriate function is called with
           appropriate parameters.

           tests failure of buying a number
        """
        dummy_sid = 'dummysid'
        phone_number = '+19193456789'
        mock_twilio_buy.return_value = None
        self.assertRaises(AreaCodeUnavailableError,
                          BuyPhoneNumberFromCarrier(),
                          settings.SMS_GATEWAY_TWILIO,
                          dummy_sid,
                          phone_number=phone_number)
        mock_twilio_buy.assert_called_with(twilioClient=settings.TWILIO_CLIENT,
                                           appSid=dummy_sid,
                                           areaCode=None,
                                           countryCode='US',
                                           phoneNumber=phone_number)
示例#11
0
    def test_buy_number_twilio(self, mock_twilio_buy):
        """
           tests that when trying to buy a phone number from
           twilio provider, appropriate function is called with
           appropriate parameters.

           buy actual phone number
        """
        dummy_sid = 'dummysid'
        phone_number = '+19193456789'
        mock_twilio_buy.return_value = SHBoughtNumberObject(
            phone_number, dummy_sid, 'twilio')  # noqa
        bought_num = BuyPhoneNumberFromCarrier()(settings.SMS_GATEWAY_TWILIO,
                                                 dummy_sid,
                                                 phone_number=phone_number)
        mock_twilio_buy.assert_called_with(twilioClient=settings.TWILIO_CLIENT,
                                           appSid=dummy_sid,
                                           areaCode=None,
                                           countryCode='US',
                                           phoneNumber=phone_number)
        self.assertEqual(bought_num.phone_number, phone_number)
        self.assertEqual(bought_num.sid, dummy_sid)
        self.assertEqual(bought_num.gateway, settings.SMS_GATEWAY_TWILIO)
示例#12
0
    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)