Exemplo n.º 1
0
    def test_backend(self):
        backend1 = SQLTestSMSBackend.objects.create(
            hq_api_id=SQLTestSMSBackend.get_api_id(),
            is_global=False,
            domain=self.domain,
            name='BACKEND1'
        )

        backend2 = SQLTestSMSBackend.objects.create(
            hq_api_id=SQLTestSMSBackend.get_api_id(),
            is_global=False,
            domain=self.domain,
            name='BACKEND2'
        )

        SQLMobileBackendMapping.set_default_domain_backend(self.domain, backend1)

        number = PhoneNumber(domain=self.domain, phone_number='+999123')
        self.assertEqual(number.backend, backend1)

        number.backend_id = backend2.name
        self.assertEqual(number.backend, backend2)

        number.backend_id = '  '
        self.assertEqual(number.backend, backend1)
Exemplo n.º 2
0
    def test_domain_is_authorized(self):
        backend1 = SQLTestSMSBackend.objects.create(
            name='BACKEND1',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        backend2 = SQLTestSMSBackend.objects.create(
            name='BACKEND2',
            domain='auth-test-1',
            is_global=False,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        self.assertTrue(backend1.domain_is_authorized('auth-test-1'))
        self.assertTrue(backend1.domain_is_authorized('auth-test-2'))
        self.assertTrue(backend1.domain_is_authorized('auth-test-3'))

        self.assertTrue(backend2.domain_is_authorized('auth-test-1'))
        self.assertFalse(backend2.domain_is_authorized('auth-test-2'))
        self.assertFalse(backend2.domain_is_authorized('auth-test-3'))

        backend2.set_shared_domains(['auth-test-2'])
        self.assertTrue(backend2.domain_is_authorized('auth-test-1'))
        self.assertTrue(backend2.domain_is_authorized('auth-test-2'))
        self.assertFalse(backend2.domain_is_authorized('auth-test-3'))

        backend1.delete()
        backend2.delete()
Exemplo n.º 3
0
    def test_set_default_domain_backend(self):
        backend1 = SQLTestSMSBackend.objects.create(
            name='BACKEND1',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        backend2 = SQLTestSMSBackend.objects.create(
            name='BACKEND2',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        domain = 'domain-default-backend-test'
        self.assertNoDomainDefaultBackend(domain)

        SQLMobileBackendMapping.set_default_domain_backend(domain, backend1)
        self.assertDomainDefaultBackend(domain, backend1)

        SQLMobileBackendMapping.set_default_domain_backend(domain, backend2)
        self.assertDomainDefaultBackend(domain, backend2)

        SQLMobileBackendMapping.unset_default_domain_backend(domain)
        self.assertNoDomainDefaultBackend(domain)

        backend1.delete()
        backend2.delete()
Exemplo n.º 4
0
    def setUpClass(cls):
        super(TestEnterpriseSMSBillablesReport, cls).setUpClass()
        cls.domain = 'test-domain'
        cls.domain_obj = create_domain(cls.domain)
        cls.user = WebUser.create(
            cls.domain,
            "test-user",
            "123",
            None,
            None,
        )

        cls.account = generator.billing_account(cls.user,
                                                "*****@*****.**")
        cls.subscription_length = 15  # months
        subscription_start_date = datetime(2021, 1, 1)
        cls.subscription = generator.generate_domain_subscription(
            cls.account,
            cls.domain_obj,
            date_start=subscription_start_date,
            date_end=None)
        cls.subscription.is_active = True
        cls.subscription.save()

        cls.backend = SQLTestSMSBackend(
            name="TEST",
            is_global=True,
            domain=cls.domain,
            hq_api_id=SQLTestSMSBackend.get_api_id())
        cls.backend.save()
Exemplo n.º 5
0
    def test_backend(self):
        backend1 = SQLTestSMSBackend.objects.create(
            hq_api_id=SQLTestSMSBackend.get_api_id(),
            is_global=False,
            domain=self.domain,
            name='BACKEND1'
        )

        backend2 = SQLTestSMSBackend.objects.create(
            hq_api_id=SQLTestSMSBackend.get_api_id(),
            is_global=False,
            domain=self.domain,
            name='BACKEND2'
        )

        SQLMobileBackendMapping.set_default_domain_backend(self.domain, backend1)

        number = PhoneNumber(domain=self.domain, phone_number='+999123')
        self.assertEqual(number.backend, backend1)

        number.backend_id = backend2.name
        self.assertEqual(number.backend, backend2)

        number.backend_id = '  '
        self.assertEqual(number.backend, backend1)
def bootstrap_test_gateway(apps):
    default_currency, _ = (apps.get_model('accounting', 'Currency')
                           if apps else Currency).objects.get_or_create(
                               code=settings.DEFAULT_CURRENCY)
    sms_gateway_fee_class = apps.get_model(
        'smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee
    sms_gateway_fee_criteria_class = apps.get_model(
        'smsbillables',
        'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria

    SmsGatewayFee.create_new(
        SQLTestSMSBackend.get_api_id(),
        INCOMING,
        Decimal('0.0'),
        currency=default_currency,
        fee_class=sms_gateway_fee_class,
        criteria_class=sms_gateway_fee_criteria_class,
    )

    SmsGatewayFee.create_new(
        SQLTestSMSBackend.get_api_id(),
        OUTGOING,
        Decimal('0.0'),
        currency=default_currency,
        fee_class=sms_gateway_fee_class,
        criteria_class=sms_gateway_fee_criteria_class,
    )

    log_smsbillables_info("Updated Test gateway fees.")
Exemplo n.º 7
0
    def test_get_backend_api_id(self):
        backend = SQLTestSMSBackend.objects.create(
            name='BACKEND',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        self.assertEquals(
            SQLMobileBackend.get_backend_api_id(backend.pk),
            SQLTestSMSBackend.get_api_id()
        )

        self.assertEquals(
            SQLMobileBackend.get_backend_api_id(backend.couch_id, is_couch_id=True),
            SQLTestSMSBackend.get_api_id()
        )

        backend.soft_delete()
        with self.assertRaises(SQLMobileBackend.DoesNotExist):
            SQLMobileBackend.get_backend_api_id(backend.pk)

        with self.assertRaises(SQLMobileBackend.DoesNotExist):
            SQLMobileBackend.get_backend_api_id(backend.couch_id, is_couch_id=True)

        backend.delete()
Exemplo n.º 8
0
    def setUpClass(cls):
        super(TestGatewayChargeWithNoAPISupport, cls).setUpClass()
        cls.domain = 'sms_test_domain'

        cls.backend = SQLTestSMSBackend(
            name="TEST BACKEND",
            is_global=True,
            domain=cls.domain,
            hq_api_id=SQLTestSMSBackend.get_api_id())
        cls.backend.save()
Exemplo n.º 9
0
    def setUpClass(cls):
        super(TestBillableCreation, cls).setUpClass()
        cls.domain = 'sms_test_domain'

        cls.backend = SQLTestSMSBackend(
            name="TEST",
            is_global=True,
            domain=cls.domain,
            hq_api_id=SQLTestSMSBackend.get_api_id())
        cls.backend.save()
Exemplo n.º 10
0
    def setUpTestData(cls):
        cls.domain_name = uuid.uuid4().hex

        cls.backend = SQLTestSMSBackend.objects.create(
            name='BACKEND',
            domain=cls.domain_name,
            is_global=False,
            hq_api_id=SQLTestSMSBackend.get_api_id())

        cls.number1 = PhoneNumber.objects.create(
            domain=cls.domain_name,
            owner_doc_type='CommCareCase',
            owner_id='fake-owner-id1',
            phone_number='01112223333',
            backend_id='BACKEND',
            ivr_backend_id=None,
            verified=True,
            is_two_way=True,
            pending_verification=False,
            contact_last_modified=datetime.utcnow())

        cls.number2 = PhoneNumber.objects.create(
            domain=cls.domain_name,
            owner_doc_type='CommCareCase',
            owner_id='fake-owner-id2',
            phone_number='01112223333',
            backend_id='BACKEND',
            ivr_backend_id=None,
            verified=True,
            is_two_way=True,
            pending_verification=False,
            contact_last_modified=datetime.utcnow())
Exemplo n.º 11
0
    def setUpClass(cls):
        super().setUpClass()
        cls.domain = Domain(name=uuid.uuid4().hex)
        cls.domain.save()

        cls.number = PhoneNumber(domain=cls.domain.name,
                                 owner_doc_type='CommCareCase',
                                 owner_id='fake-owner-id1',
                                 phone_number='01112223333',
                                 backend_id=None,
                                 ivr_backend_id=None,
                                 verified=True,
                                 is_two_way=True,
                                 pending_verification=False,
                                 contact_last_modified=datetime.utcnow())
        cls.number.save()

        cls.session = SQLXFormsSession.create_session_object(
            cls.domain.name,
            Mock(get_id=cls.number.owner_id),
            cls.number.phone_number,
            Mock(get_id='app_id'),
            Mock(xmlns='xmlns'),
            expire_after=24 * 60,
        )
        cls.session.save()

        cls.backend = SQLTestSMSBackend.objects.create(
            name='BACKEND',
            domain=cls.domain,
            is_global=False,
            hq_api_id=SQLTestSMSBackend.get_api_id())
Exemplo n.º 12
0
    def create(cls, message_log, multipart_count=1):
        phone_number = clean_phone_number(message_log.phone_number)
        direction = message_log.direction
        domain = message_log.domain
        log_id = message_log.couch_id

        billable = cls(
            log_id=log_id,
            phone_number=phone_number,
            direction=direction,
            date_sent=message_log.date,
            domain=domain,
        )

        gateway_charge_info = cls._get_gateway_fee(
            message_log.backend_api, message_log.backend_id, phone_number, direction, log_id,
            message_log.backend_message_id, domain
        )
        billable.gateway_fee = gateway_charge_info.gateway_fee
        billable.gateway_fee_conversion_rate = gateway_charge_info.conversion_rate
        billable.direct_gateway_fee = gateway_charge_info.direct_gateway_fee
        billable.multipart_count = cls._get_multipart_count(
            message_log.backend_api, message_log.backend_id, message_log.backend_message_id, multipart_count
        )
        billable.usage_fee = cls._get_usage_fee(domain, direction)

        if message_log.backend_api == SQLTestSMSBackend.get_api_id():
            billable.is_valid = False

        billable.save()
        return billable
Exemplo n.º 13
0
    def create(cls, message_log, multipart_count=1):
        phone_number = clean_phone_number(message_log.phone_number)
        direction = message_log.direction
        domain = message_log.domain
        log_id = message_log.couch_id

        billable = cls(
            log_id=log_id,
            phone_number=phone_number,
            direction=direction,
            date_sent=message_log.date,
            domain=domain,
        )

        gateway_charge_info = cls._get_gateway_fee(
            message_log.backend_api, message_log.backend_id, phone_number, direction, log_id,
            message_log.backend_message_id
        )
        billable.gateway_fee = gateway_charge_info.gateway_fee
        billable.gateway_fee_conversion_rate = gateway_charge_info.conversion_rate
        billable.direct_gateway_fee = gateway_charge_info.direct_gateway_fee
        billable.multipart_count = cls._get_multipart_count(
            message_log.backend_api, message_log.backend_id, message_log.backend_message_id, multipart_count
        )
        billable.usage_fee = cls._get_usage_fee(domain, direction)

        if message_log.backend_api == SQLTestSMSBackend.get_api_id():
            billable.is_valid = False

        billable.save()
        return billable
Exemplo n.º 14
0
    def test_load(self):
        backend = SQLTestSMSBackend.objects.create(
            name='BACKEND',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        self.assertBackendsEqual(
            SQLMobileBackend.load(backend.pk),
            backend
        )

        self.assertBackendsEqual(
            SQLMobileBackend.load(backend.pk, api_id=SQLTestSMSBackend.get_api_id()),
            backend
        )

        self.assertBackendsEqual(
            SQLMobileBackend.load(backend.couch_id, is_couch_id=True),
            backend
        )

        self.assertBackendsEqual(
            SQLMobileBackend.load(
                backend.couch_id,
                api_id=SQLTestSMSBackend.get_api_id(),
                is_couch_id=True
            ),
            backend
        )

        backend.soft_delete()

        with self.assertRaises(SQLMobileBackend.DoesNotExist):
            SQLMobileBackend.load(backend.pk, api_id=SQLTestSMSBackend.get_api_id())

        with self.assertRaises(SQLMobileBackend.DoesNotExist):
            SQLMobileBackend.load(
                backend.couch_id,
                api_id=SQLTestSMSBackend.get_api_id(),
                is_couch_id=True
            )

        with self.assertRaises(BadSMSConfigException):
            SQLMobileBackend.load(backend.pk, api_id='this-api-id-does-not-exist')

        backend.delete()
Exemplo n.º 15
0
    def create(cls, message_log, api_response=None):
        phone_number = clean_phone_number(message_log.phone_number)
        direction = message_log.direction

        billable = cls(
            log_id=message_log._id,
            phone_number=phone_number,
            direction=direction,
            date_sent=message_log.date,
            domain=message_log.domain,
        )

        # Fetch gateway_fee
        backend_api_id = message_log.backend_api
        backend_instance = message_log.backend_id

        country_code, national_number = get_country_code_and_national_number(phone_number)

        if backend_instance is None or _sms_backend_is_global(backend_instance):
            billable.gateway_fee = SmsGatewayFee.get_by_criteria(
                backend_api_id,
                direction,
                backend_instance=backend_instance,
                country_code=country_code,
                national_number=national_number,
            )
            if billable.gateway_fee is not None:
                conversion_rate = billable.gateway_fee.currency.rate_to_default
                if conversion_rate != 0:
                    billable.gateway_fee_conversion_rate = conversion_rate
                else:
                    smsbillables_logging.error(
                        "Gateway fee conversion rate for currency %s is 0", billable.gateway_fee.currency.code
                    )
            else:
                smsbillables_logging.error("No matching gateway fee criteria for SMSLog %s" % message_log._id)

        # Fetch usage_fee todo
        domain = message_log.domain
        billable.usage_fee = SmsUsageFee.get_by_criteria(direction, domain=domain)

        if billable.usage_fee is None:
            smsbillables_logging.error("Did not find usage fee for direction %s and domain %s" % (direction, domain))

        if api_response is not None:
            billable.api_response = api_response

        if backend_api_id == SQLTestSMSBackend.get_api_id():
            billable.is_valid = False

        billable.save()

        return billable
Exemplo n.º 16
0
def setup_default_sms_test_backend():
    backend = SQLTestSMSBackend.objects.create(
        name='MOBILE_BACKEND_TEST',
        is_global=True,
        hq_api_id=SQLTestSMSBackend.get_api_id())

    backend_mapping = SQLMobileBackendMapping.objects.create(
        is_global=True,
        backend_type=SQLMobileBackend.SMS,
        prefix='*',
        backend=backend,
    )

    return (backend, backend_mapping)
Exemplo n.º 17
0
def setup_default_sms_test_backend():
    backend = SQLTestSMSBackend.objects.create(
        name='MOBILE_BACKEND_TEST',
        is_global=True,
        hq_api_id=SQLTestSMSBackend.get_api_id()
    )

    backend_mapping = SQLMobileBackendMapping.objects.create(
        is_global=True,
        backend_type=SQLMobileBackend.SMS,
        prefix='*',
        backend=backend,
    )

    return (backend, backend_mapping)
Exemplo n.º 18
0
def bootstrap_test_gateway(apps):
    default_currency, _ = (apps.get_model('accounting', 'Currency') if apps else Currency).objects.get_or_create(code=settings.DEFAULT_CURRENCY)
    sms_gateway_fee_class = apps.get_model('smsbillables', 'SmsGatewayFee') if apps else SmsGatewayFee
    sms_gateway_fee_criteria_class = apps.get_model('smsbillables', 'SmsGatewayFeeCriteria') if apps else SmsGatewayFeeCriteria

    SmsGatewayFee.create_new(
        SQLTestSMSBackend.get_api_id(),
        INCOMING,
        Decimal('0.0'),
        currency=default_currency,
        fee_class=sms_gateway_fee_class,
        criteria_class=sms_gateway_fee_criteria_class,
    )

    SmsGatewayFee.create_new(
        SQLTestSMSBackend.get_api_id(),
        OUTGOING,
        Decimal('0.0'),
        currency=default_currency,
        fee_class=sms_gateway_fee_class,
        criteria_class=sms_gateway_fee_criteria_class,
    )

    logger.info("Updated Test gateway fees.")
Exemplo n.º 19
0
    def test_domain_is_shared(self):
        backend = SQLTestSMSBackend.objects.create(
            name='BACKEND',
            domain='shared-test-1',
            is_global=False,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        self.assertFalse(backend.domain_is_shared('shared-test-2'))

        backend.set_shared_domains(['shared-test-2'])
        self.assertTrue(backend.domain_is_shared('shared-test-2'))

        backend.soft_delete()
        self.assertFalse(backend.domain_is_shared('shared-test-2'))

        backend.delete()
Exemplo n.º 20
0
    def setUp(self):
        super(RegistrationTestCase, self).setUp()

        self.domain = 'sms-reg-test-domain'
        self.domain_obj = Domain(name=self.domain)
        self.domain_obj.save()

        self.create_account_and_subscription(self.domain)
        self.domain_obj = Domain.get(self.domain_obj.get_id)

        self.backend = SQLTestSMSBackend.objects.create(
            name='BACKEND',
            is_global=False,
            domain=self.domain,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )

        SQLMobileBackendMapping.set_default_domain_backend(self.domain, self.backend)
Exemplo n.º 21
0
    def setUp(self):
        super(RegistrationTestCase, self).setUp()

        self.domain = 'sms-reg-test-domain'
        self.domain_obj = Domain(name=self.domain)
        self.domain_obj.save()

        self.create_account_and_subscription(self.domain)
        self.domain_obj = Domain.get(self.domain_obj.get_id)

        self.backend = SQLTestSMSBackend.objects.create(
            name='BACKEND',
            is_global=False,
            domain=self.domain,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )

        SQLMobileBackendMapping.set_default_domain_backend(self.domain, self.backend)

        self.app_id = 'app_id'
Exemplo n.º 22
0
    def setUpClass(cls):
        super(OptTestCase, cls).setUpClass()
        cls.domain = 'opt-test'
        cls.domain_obj = Domain(name=cls.domain)
        cls.domain_obj.sms_case_registration_enabled = True
        cls.domain_obj.save()

        cls.setup_subscription(cls.domain, SoftwarePlanEdition.ADVANCED)
        cls.backend, cls.backend_mapping = setup_default_sms_test_backend()
        cls.custom_backend = SQLTestSMSBackend.objects.create(
            name='MOBILE_BACKEND_CUSTOM_TEST',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
            opt_in_keywords=['RESTART'],
            opt_out_keywords=['RESTOP'])
        cls.custom_backend_mapping = SQLMobileBackendMapping.objects.create(
            is_global=True,
            backend_type=SQLMobileBackend.SMS,
            prefix='1',
            backend=cls.custom_backend,
        )
Exemplo n.º 23
0
    def test_get_prefix_to_backend_map(self):
        backend1 = SQLTestSMSBackend.objects.create(
            name='BACKEND1',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        backend2 = SQLTestSMSBackend.objects.create(
            name='BACKEND2',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        backend3 = SQLTestSMSBackend.objects.create(
            name='BACKEND3',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        backend4 = SQLTestSMSBackend.objects.create(
            name='BACKEND4',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        backend5 = SQLTestSMSBackend.objects.create(
            name='BACKEND5',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        backend6 = SQLTestSMSBackend.objects.create(
            name='BACKEND6',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        backend_mapping1 = SQLMobileBackendMapping.objects.create(
            is_global=True,
            backend_type=SQLMobileBackend.SMS,
            prefix='*',
            backend=backend1
        )

        backend_mapping2 = SQLMobileBackendMapping.objects.create(
            is_global=True,
            backend_type=SQLMobileBackend.SMS,
            prefix='27',
            backend=backend2
        )

        backend_mapping3 = SQLMobileBackendMapping.objects.create(
            is_global=True,
            backend_type=SQLMobileBackend.SMS,
            prefix='1',
            backend=backend3
        )

        backend_mapping4 = SQLMobileBackendMapping.objects.create(
            is_global=False,
            domain='prefix-backend-map-test',
            backend_type=SQLMobileBackend.SMS,
            prefix='*',
            backend=backend4
        )

        backend_mapping5 = SQLMobileBackendMapping.objects.create(
            is_global=False,
            domain='prefix-backend-map-test',
            backend_type=SQLMobileBackend.SMS,
            prefix='256',
            backend=backend5
        )

        backend_mapping6 = SQLMobileBackendMapping.objects.create(
            is_global=False,
            domain='prefix-backend-map-test',
            backend_type=SQLMobileBackend.SMS,
            prefix='25670',
            backend=backend6
        )

        global_backend_map = SQLMobileBackendMapping.get_prefix_to_backend_map(SQLMobileBackend.SMS)
        self.assertEqual(global_backend_map.catchall_backend_id, backend1.pk)
        self.assertEqual(global_backend_map.backend_map_dict, {
            '27': backend2.pk,
            '1': backend3.pk,
        })

        domain_backend_map = SQLMobileBackendMapping.get_prefix_to_backend_map(
            SQLMobileBackend.SMS,
            domain='prefix-backend-map-test'
        )
        self.assertEqual(domain_backend_map.catchall_backend_id, backend4.pk)
        self.assertEqual(domain_backend_map.backend_map_dict, {
            '256': backend5.pk,
            '25670': backend6.pk,
        })

        backend_mapping1.delete()
        backend_mapping2.delete()
        backend_mapping3.delete()
        backend_mapping4.delete()
        backend_mapping5.delete()
        backend_mapping6.delete()
        backend1.delete()
        backend2.delete()
        backend3.delete()
        backend4.delete()
        backend5.delete()
        backend6.delete()
Exemplo n.º 24
0
class AllBackendTest(BaseSMSTest):
    def setUp(self):
        super(AllBackendTest, self).setUp()

        self.domain_obj = Domain(name='all-backend-test')
        self.domain_obj.save()
        self.create_account_and_subscription(self.domain_obj.name)
        self.domain_obj = Domain.get(self.domain_obj._id)

        self.test_phone_number = '99912345'
        self.contact1 = CommCareCase(domain=self.domain_obj.name)
        self.contact1.set_case_property('contact_phone_number', self.test_phone_number)
        self.contact1.set_case_property('contact_phone_number_is_verified', '1')
        self.contact1.save()
        self.contact1 = CommConnectCase.wrap(self.contact1.to_json())

        # For use with megamobile only
        self.contact2 = CommCareCase(domain=self.domain_obj.name)
        self.contact2.set_case_property('contact_phone_number', '63%s' % self.test_phone_number)
        self.contact2.set_case_property('contact_phone_number_is_verified', '1')
        self.contact2.save()
        self.contact2 = CommConnectCase.wrap(self.contact2.to_json())

        self.unicel_backend = SQLUnicelBackend(
            name='UNICEL',
            is_global=True,
            hq_api_id=SQLUnicelBackend.get_api_id()
        )
        self.unicel_backend.save()

        self.mach_backend = SQLMachBackend(
            name='MACH',
            is_global=True,
            hq_api_id=SQLMachBackend.get_api_id()
        )
        self.mach_backend.save()

        self.tropo_backend = SQLTropoBackend(
            name='TROPO',
            is_global=True,
            hq_api_id=SQLTropoBackend.get_api_id()
        )
        self.tropo_backend.save()

        self.http_backend = SQLHttpBackend(
            name='HTTP',
            is_global=True,
            hq_api_id=SQLHttpBackend.get_api_id()
        )
        self.http_backend.save()

        self.telerivet_backend = SQLTelerivetBackend(
            name='TELERIVET',
            is_global=True,
            hq_api_id=SQLTelerivetBackend.get_api_id()
        )
        self.telerivet_backend.set_extra_fields(
            **dict(
                webhook_secret='telerivet-webhook-secret'
            )
        )
        self.telerivet_backend.save()

        self.test_backend = SQLTestSMSBackend(
            name='TEST',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )
        self.test_backend.save()

        self.grapevine_backend = SQLGrapevineBackend(
            name='GRAPEVINE',
            is_global=True,
            hq_api_id=SQLGrapevineBackend.get_api_id()
        )
        self.grapevine_backend.save()

        self.twilio_backend = SQLTwilioBackend(
            name='TWILIO',
            is_global=True,
            hq_api_id=SQLTwilioBackend.get_api_id()
        )
        self.twilio_backend.save()

        self.megamobile_backend = SQLMegamobileBackend(
            name='MEGAMOBILE',
            is_global=True,
            hq_api_id=SQLMegamobileBackend.get_api_id()
        )
        self.megamobile_backend.save()

        self.smsgh_backend = SQLSMSGHBackend(
            name='SMSGH',
            is_global=True,
            hq_api_id=SQLSMSGHBackend.get_api_id()
        )
        self.smsgh_backend.save()

        self.apposit_backend = SQLAppositBackend(
            name='APPOSIT',
            is_global=True,
            hq_api_id=SQLAppositBackend.get_api_id()
        )
        self.apposit_backend.save()

        self.sislog_backend = SQLSislogBackend(
            name='SISLOG',
            is_global=True,
            hq_api_id=SQLSislogBackend.get_api_id()
        )
        self.sislog_backend.save()

        self.yo_backend = SQLYoBackend(
            name='YO',
            is_global=True,
            hq_api_id=SQLYoBackend.get_api_id()
        )
        self.yo_backend.save()

    def _test_outbound_backend(self, backend, msg_text, mock_send):
        SQLMobileBackendMapping.set_default_domain_backend(self.domain_obj.name, backend)

        send_sms(self.domain_obj.name, None, self.test_phone_number, msg_text)
        sms = SMS.objects.get(
            domain=self.domain_obj.name,
            direction='O',
            text=msg_text
        )

        self.assertTrue(mock_send.called)
        msg_arg = mock_send.call_args[0][0]
        self.assertEqual(msg_arg.date, sms.date)
        self.assertEqual(sms.backend_api, backend.hq_api_id)
        self.assertEqual(sms.backend_id, backend.couch_id)

    def _verify_inbound_request(self, backend_api_id, msg_text, backend_couch_id=None):
        sms = SMS.objects.get(
            domain=self.domain_obj.name,
            direction='I',
            text=msg_text
        )
        self.assertEqual(sms.backend_api, backend_api_id)
        if backend_couch_id:
            self.assertEqual(sms.backend_id, backend_couch_id)

    def _simulate_inbound_request_with_payload(self, url,
            content_type, payload):
        response = Client().post(url, payload, content_type=content_type)
        self.assertEqual(response.status_code, 200)

    def _simulate_inbound_request(self, url, phone_param,
            msg_param, msg_text, post=False, additional_params=None,
            expected_response_code=200):
        fcn = Client().post if post else Client().get

        payload = {
            phone_param: self.test_phone_number,
            msg_param: msg_text,
        }

        if additional_params:
            payload.update(additional_params)

        response = fcn(url, payload)
        self.assertEqual(response.status_code, expected_response_code)

    @patch('corehq.messaging.smsbackends.unicel.models.SQLUnicelBackend.send')
    @patch('corehq.messaging.smsbackends.mach.models.SQLMachBackend.send')
    @patch('corehq.messaging.smsbackends.tropo.models.SQLTropoBackend.send')
    @patch('corehq.messaging.smsbackends.http.models.SQLHttpBackend.send')
    @patch('corehq.messaging.smsbackends.telerivet.models.SQLTelerivetBackend.send')
    @patch('corehq.messaging.smsbackends.test.models.SQLTestSMSBackend.send')
    @patch('corehq.messaging.smsbackends.grapevine.models.SQLGrapevineBackend.send')
    @patch('corehq.messaging.smsbackends.twilio.models.SQLTwilioBackend.send')
    @patch('corehq.messaging.smsbackends.megamobile.models.SQLMegamobileBackend.send')
    @patch('corehq.messaging.smsbackends.smsgh.models.SQLSMSGHBackend.send')
    @patch('corehq.messaging.smsbackends.apposit.models.SQLAppositBackend.send')
    @patch('corehq.messaging.smsbackends.sislog.models.SQLSislogBackend.send')
    @patch('corehq.messaging.smsbackends.yo.models.SQLYoBackend.send')
    def test_outbound_sms(
            self,
            yo_send,
            sislog_send,
            apposit_send,
            smsgh_send,
            megamobile_send,
            twilio_send,
            grapevine_send,
            test_send,
            telerivet_send,
            http_send,
            tropo_send,
            mach_send,
            unicel_send):
        self._test_outbound_backend(self.unicel_backend, 'unicel test', unicel_send)
        self._test_outbound_backend(self.mach_backend, 'mach test', mach_send)
        self._test_outbound_backend(self.tropo_backend, 'tropo test', tropo_send)
        self._test_outbound_backend(self.http_backend, 'http test', http_send)
        self._test_outbound_backend(self.telerivet_backend, 'telerivet test', telerivet_send)
        self._test_outbound_backend(self.test_backend, 'test test', test_send)
        self._test_outbound_backend(self.grapevine_backend, 'grapevine test', grapevine_send)
        self._test_outbound_backend(self.twilio_backend, 'twilio test', twilio_send)
        self._test_outbound_backend(self.megamobile_backend, 'megamobile test', megamobile_send)
        self._test_outbound_backend(self.smsgh_backend, 'smsgh test', smsgh_send)
        self._test_outbound_backend(self.apposit_backend, 'apposit test', apposit_send)
        self._test_outbound_backend(self.sislog_backend, 'sislog test', sislog_send)
        self._test_outbound_backend(self.yo_backend, 'yo test', yo_send)

    def test_unicel_inbound_sms(self):
        self._simulate_inbound_request('/unicel/in/', phone_param=InboundParams.SENDER,
            msg_param=InboundParams.MESSAGE, msg_text='unicel test')

        self._verify_inbound_request(self.unicel_backend.get_api_id(), 'unicel test')

    def test_tropo_inbound_sms(self):
        tropo_data = {'session': {'from': {'id': self.test_phone_number}, 'initialText': 'tropo test'}}
        self._simulate_inbound_request_with_payload('/tropo/sms/',
            content_type='text/json', payload=json.dumps(tropo_data))

        self._verify_inbound_request(self.tropo_backend.get_api_id(), 'tropo test')

    def test_telerivet_inbound_sms(self):
        additional_params = {
            'event': 'incoming_message',
            'message_type': 'sms',
            'secret': self.telerivet_backend.config.webhook_secret
        }
        self._simulate_inbound_request('/telerivet/in/', phone_param='from_number_e164',
            msg_param='content', msg_text='telerivet test', post=True,
            additional_params=additional_params)

        self._verify_inbound_request(self.telerivet_backend.get_api_id(), 'telerivet test')

    @override_settings(SIMPLE_API_KEYS={'grapevine-test': 'grapevine-api-key'})
    def test_grapevine_inbound_sms(self):
        xml = """
        <gviSms>
            <smsDateTime>2015-10-12T12:00:00</smsDateTime>
            <cellNumber>99912345</cellNumber>
            <content>grapevine test</content>
        </gviSms>
        """
        payload = urlencode({'XML': xml})
        self._simulate_inbound_request_with_payload(
            '/gvi/api/sms/?apiuser=grapevine-test&apikey=grapevine-api-key',
            content_type='application/x-www-form-urlencoded', payload=payload)

        self._verify_inbound_request(self.grapevine_backend.get_api_id(), 'grapevine test')

    def test_twilio_inbound_sms(self):
        url = '/twilio/sms/%s' % self.twilio_backend.inbound_api_key
        self._simulate_inbound_request(url, phone_param='From',
            msg_param='Body', msg_text='twilio test', post=True)

        self._verify_inbound_request(self.twilio_backend.get_api_id(), 'twilio test',
            backend_couch_id=self.twilio_backend.couch_id)

    def test_twilio_401_response(self):
        start_count = SMS.objects.count()

        self._simulate_inbound_request('/twilio/sms/xxxxx', phone_param='From',
            msg_param='Body', msg_text='twilio test', post=True,
            expected_response_code=401)

        end_count = SMS.objects.count()

        self.assertEqual(start_count, end_count)

    def test_megamobile_inbound_sms(self):
        self._simulate_inbound_request('/megamobile/sms/', phone_param='cel',
            msg_param='msg', msg_text='megamobile test')

        self._verify_inbound_request(self.megamobile_backend.get_api_id(), 'megamobile test')

    def test_sislog_inbound_sms(self):
        self._simulate_inbound_request('/sislog/in/', phone_param='sender',
            msg_param='msgdata', msg_text='sislog test')

        self._verify_inbound_request(self.sislog_backend.get_api_id(), 'sislog test')

    def test_yo_inbound_sms(self):
        self._simulate_inbound_request('/yo/sms/', phone_param='sender',
            msg_param='message', msg_text='yo test')

        self._verify_inbound_request(self.yo_backend.get_api_id(), 'yo test')

    def test_smsgh_inbound_sms(self):
        user = ApiUser.create('smsgh-api-key', 'smsgh-api-key', permissions=[PERMISSION_POST_SMS])
        user.save()

        self._simulate_inbound_request('/smsgh/sms/smsgh-api-key/', phone_param='snr',
            msg_param='msg', msg_text='smsgh test')

        self._verify_inbound_request('SMSGH', 'smsgh test')

        user.delete()

    def test_apposit_inbound_sms(self):
        user = ApiUser.create('apposit-api-key', 'apposit-api-key', permissions=[PERMISSION_POST_SMS])
        user.save()

        self._simulate_inbound_request(
            '/apposit/in/apposit-api-key/',
            phone_param='fromAddress',
            msg_param='content',
            msg_text='apposit test',
            post=True,
            additional_params={'channel': 'SMS'}
        )
        self._verify_inbound_request('APPOSIT', 'apposit test')

        user.delete()

    def tearDown(self):
        delete_domain_phone_numbers(self.domain_obj.name)
        self.contact1.delete()
        self.contact2.delete()
        self.domain_obj.delete()
        self.unicel_backend.delete()
        self.mach_backend.delete()
        self.tropo_backend.delete()
        self.http_backend.delete()
        self.telerivet_backend.delete()
        self.test_backend.delete()
        self.grapevine_backend.delete()
        self.twilio_backend.delete()
        self.megamobile_backend.delete()
        self.smsgh_backend.delete()
        self.apposit_backend.delete()
        self.sislog_backend.delete()
        self.yo_backend.delete()
        super(AllBackendTest, self).tearDown()
Exemplo n.º 25
0
    def setUp(self):
        super(AllBackendTest, self).setUp()

        self.domain_obj = Domain(name='all-backend-test')
        self.domain_obj.save()
        self.create_account_and_subscription(self.domain_obj.name)
        self.domain_obj = Domain.get(self.domain_obj._id)

        self.test_phone_number = '99912345'
        self.contact1 = CommCareCase(domain=self.domain_obj.name)
        self.contact1.set_case_property('contact_phone_number', self.test_phone_number)
        self.contact1.set_case_property('contact_phone_number_is_verified', '1')
        self.contact1.save()
        self.contact1 = CommConnectCase.wrap(self.contact1.to_json())

        # For use with megamobile only
        self.contact2 = CommCareCase(domain=self.domain_obj.name)
        self.contact2.set_case_property('contact_phone_number', '63%s' % self.test_phone_number)
        self.contact2.set_case_property('contact_phone_number_is_verified', '1')
        self.contact2.save()
        self.contact2 = CommConnectCase.wrap(self.contact2.to_json())

        self.unicel_backend = SQLUnicelBackend(
            name='UNICEL',
            is_global=True,
            hq_api_id=SQLUnicelBackend.get_api_id()
        )
        self.unicel_backend.save()

        self.mach_backend = SQLMachBackend(
            name='MACH',
            is_global=True,
            hq_api_id=SQLMachBackend.get_api_id()
        )
        self.mach_backend.save()

        self.tropo_backend = SQLTropoBackend(
            name='TROPO',
            is_global=True,
            hq_api_id=SQLTropoBackend.get_api_id()
        )
        self.tropo_backend.save()

        self.http_backend = SQLHttpBackend(
            name='HTTP',
            is_global=True,
            hq_api_id=SQLHttpBackend.get_api_id()
        )
        self.http_backend.save()

        self.telerivet_backend = SQLTelerivetBackend(
            name='TELERIVET',
            is_global=True,
            hq_api_id=SQLTelerivetBackend.get_api_id()
        )
        self.telerivet_backend.set_extra_fields(
            **dict(
                webhook_secret='telerivet-webhook-secret'
            )
        )
        self.telerivet_backend.save()

        self.test_backend = SQLTestSMSBackend(
            name='TEST',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )
        self.test_backend.save()

        self.grapevine_backend = SQLGrapevineBackend(
            name='GRAPEVINE',
            is_global=True,
            hq_api_id=SQLGrapevineBackend.get_api_id()
        )
        self.grapevine_backend.save()

        self.twilio_backend = SQLTwilioBackend(
            name='TWILIO',
            is_global=True,
            hq_api_id=SQLTwilioBackend.get_api_id()
        )
        self.twilio_backend.save()

        self.megamobile_backend = SQLMegamobileBackend(
            name='MEGAMOBILE',
            is_global=True,
            hq_api_id=SQLMegamobileBackend.get_api_id()
        )
        self.megamobile_backend.save()

        self.smsgh_backend = SQLSMSGHBackend(
            name='SMSGH',
            is_global=True,
            hq_api_id=SQLSMSGHBackend.get_api_id()
        )
        self.smsgh_backend.save()

        self.apposit_backend = SQLAppositBackend(
            name='APPOSIT',
            is_global=True,
            hq_api_id=SQLAppositBackend.get_api_id()
        )
        self.apposit_backend.save()

        self.sislog_backend = SQLSislogBackend(
            name='SISLOG',
            is_global=True,
            hq_api_id=SQLSislogBackend.get_api_id()
        )
        self.sislog_backend.save()

        self.yo_backend = SQLYoBackend(
            name='YO',
            is_global=True,
            hq_api_id=SQLYoBackend.get_api_id()
        )
        self.yo_backend.save()
Exemplo n.º 26
0
    def setUp(self):
        super(OutgoingFrameworkTestCase, self).setUp()

        self.domain = "test-domain"
        self.domain2 = "test-domain2"

        self.domain_obj = Domain(name=self.domain)
        self.domain_obj.save()

        self.create_account_and_subscription(self.domain_obj.name)
        self.domain_obj = Domain.get(self.domain_obj._id)

        self.backend1 = SQLTestSMSBackend.objects.create(
            name='BACKEND1',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )

        self.backend2 = SQLTestSMSBackend.objects.create(
            name='BACKEND2',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )

        self.backend3 = SQLTestSMSBackend.objects.create(
            name='BACKEND3',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )

        self.backend4 = SQLTestSMSBackend.objects.create(
            name='BACKEND4',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )

        self.backend5 = SQLTestSMSBackend.objects.create(
            name='BACKEND5',
            domain=self.domain,
            is_global=False,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )

        self.backend6 = SQLTestSMSBackend.objects.create(
            name='BACKEND6',
            domain=self.domain2,
            is_global=False,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )
        self.backend6.set_shared_domains([self.domain])

        self.backend7 = SQLTestSMSBackend.objects.create(
            name='BACKEND7',
            domain=self.domain2,
            is_global=False,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )

        self.backend8 = SQLTestSMSBackend.objects.create(
            name='BACKEND',
            domain=self.domain,
            is_global=False,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )

        self.backend9 = SQLTestSMSBackend.objects.create(
            name='BACKEND',
            domain=self.domain2,
            is_global=False,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )
        self.backend9.set_shared_domains([self.domain])

        self.backend10 = SQLTestSMSBackend.objects.create(
            name='BACKEND',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id()
        )

        self.backend_mapping1 = SQLMobileBackendMapping.objects.create(
            is_global=True,
            backend_type=SQLMobileBackend.SMS,
            prefix='*',
            backend=self.backend1
        )

        self.backend_mapping2 = SQLMobileBackendMapping.objects.create(
            is_global=True,
            backend_type=SQLMobileBackend.SMS,
            prefix='1',
            backend=self.backend2
        )

        self.backend_mapping3 = SQLMobileBackendMapping.objects.create(
            is_global=True,
            backend_type=SQLMobileBackend.SMS,
            prefix='91',
            backend=self.backend3
        )

        self.backend_mapping4 = SQLMobileBackendMapping.objects.create(
            is_global=True,
            backend_type=SQLMobileBackend.SMS,
            prefix='265',
            backend=self.backend4
        )

        self.backend_mapping5 = SQLMobileBackendMapping.objects.create(
            is_global=True,
            backend_type=SQLMobileBackend.SMS,
            prefix='256',
            backend=self.backend5
        )

        self.backend_mapping6 = SQLMobileBackendMapping.objects.create(
            is_global=True,
            backend_type=SQLMobileBackend.SMS,
            prefix='25670',
            backend=self.backend6
        )

        self.backend_mapping7 = SQLMobileBackendMapping.objects.create(
            is_global=True,
            backend_type=SQLMobileBackend.SMS,
            prefix='25675',
            backend=self.backend7
        )

        self.case = CommCareCase(domain=self.domain)
        self.case.set_case_property('contact_phone_number', '15551234567')
        self.case.set_case_property('contact_phone_number_is_verified', '1')
        self.case.save()

        self.contact = CommConnectCase.wrap(self.case.to_json())
    def setUpTestData(cls):
        cls.domain1_name = uuid.uuid4().hex
        cls.domain2_name = uuid.uuid4().hex

        cls.global_backend = SQLTestSMSBackend.objects.create(
            hq_api_id=SQLTestSMSBackend.get_api_id(),
            is_global=True,
            name='GLOBAL')

        cls.domain1_backend = SQLTestSMSBackend.objects.create(
            hq_api_id=SQLTestSMSBackend.get_api_id(),
            is_global=False,
            domain=cls.domain1_name,
            name='BACKEND1')

        MobileBackendInvitation.objects.create(domain=cls.domain2_name,
                                               backend=cls.domain1_backend,
                                               accepted=True)

        cls.domain2_backend = SQLTestSMSBackend.objects.create(
            hq_api_id=SQLTestSMSBackend.get_api_id(),
            is_global=False,
            domain=cls.domain2_name,
            name='BACKEND2')

        cls.phone_number = '01112223333'
        cls.number1_domain1 = PhoneNumber.objects.create(
            domain=cls.domain1_name,
            owner_doc_type='CommCareCase',
            owner_id='fake-owner-1',
            phone_number=cls.phone_number,
            verified=True,
            is_two_way=True,
            pending_verification=False)
        cls.number2_domain1 = PhoneNumber.objects.create(
            domain=cls.domain1_name,
            owner_doc_type='CommCareCase',
            owner_id='fake-owner-2',
            phone_number=cls.phone_number,
            verified=True,
            is_two_way=False,
            pending_verification=False)
        cls.number3_domain2 = PhoneNumber.objects.create(
            domain=cls.domain2_name,
            owner_doc_type='CommCareCase',
            owner_id='fake-owner-3',
            phone_number=cls.phone_number,
            verified=True,
            is_two_way=False,
            pending_verification=False)
        cls.number4_domain2 = PhoneNumber.objects.create(
            domain=cls.domain2_name,
            owner_doc_type='CommCareCase',
            owner_id='fake-owner-4',
            phone_number=cls.phone_number,
            verified=True,
            is_two_way=False,
            pending_verification=False)

        for number in [
                cls.number1_domain1, cls.number2_domain1, cls.number3_domain2
        ]:
            number._clear_caches()

        SQLMobileBackend.load.clear(SQLMobileBackend,
                                    cls.global_backend.couch_id,
                                    is_couch_id=True)
        SQLMobileBackend.load.clear(SQLMobileBackend,
                                    cls.domain1_backend.couch_id,
                                    is_couch_id=True)
        SQLMobileBackend.load.clear(SQLMobileBackend,
                                    cls.domain2_backend.couch_id,
                                    is_couch_id=True)
Exemplo n.º 28
0
    def test_load_by_name(self):
        backend1 = SQLTestSMSBackend.objects.create(
            name='BACKEND_BY_NAME_TEST',
            is_global=False,
            domain='backend-by-name-test-1',
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        backend2 = SQLTestSMSBackend.objects.create(
            name='BACKEND_BY_NAME_TEST',
            is_global=False,
            domain='backend-by-name-test-2',
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )
        backend2.set_shared_domains(['backend-by-name-test-1'])

        backend3 = SQLTestSMSBackend.objects.create(
            name='BACKEND_BY_NAME_TEST',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        self.assertBackendsEqual(
            SQLMobileBackend.load_by_name(
                SQLMobileBackend.SMS,
                'backend-by-name-test-1',
                'BACKEND_BY_NAME_TEST'
            ),
            backend1
        )

        self.assertBackendsEqual(
            SQLMobileBackend.load_by_name(
                SQLMobileBackend.SMS,
                'backend-by-name-test-3',
                'BACKEND_BY_NAME_TEST'
            ),
            backend3
        )

        backend1.soft_delete()
        self.assertBackendsEqual(
            SQLMobileBackend.load_by_name(
                SQLMobileBackend.SMS,
                'backend-by-name-test-1',
                'BACKEND_BY_NAME_TEST'
            ),
            backend2
        )

        backend2.set_shared_domains([])
        self.assertBackendsEqual(
            SQLMobileBackend.load_by_name(
                SQLMobileBackend.SMS,
                'backend-by-name-test-1',
                'BACKEND_BY_NAME_TEST'
            ),
            backend3
        )

        self.assertBackendsEqual(
            SQLMobileBackend.load_by_name(
                SQLMobileBackend.SMS,
                'backend-by-name-test-2',
                'BACKEND_BY_NAME_TEST'
            ),
            backend2
        )

        backend2.soft_delete()
        self.assertBackendsEqual(
            SQLMobileBackend.load_by_name(
                SQLMobileBackend.SMS,
                'backend-by-name-test-2',
                'BACKEND_BY_NAME_TEST'
            ),
            backend3
        )

        backend3.soft_delete()
        with self.assertRaises(BadSMSConfigException):
            SQLMobileBackend.load_by_name(
                SQLMobileBackend.SMS,
                'backend-by-name-test-1',
                'BACKEND_BY_NAME_TEST'
            )

        backend1.delete()
        backend2.delete()
        backend3.delete()
Exemplo n.º 29
0
    def test_load_default_by_phone_and_domain(self):
        backend1 = SQLTestSMSBackend.objects.create(
            name='BACKEND1',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        backend2 = SQLTestSMSBackend.objects.create(
            name='BACKEND2',
            is_global=True,
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        backend3 = SQLTestSMSBackend.objects.create(
            name='BACKEND3',
            is_global=False,
            domain='load-default-test',
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        backend4 = SQLTestSMSBackend.objects.create(
            name='BACKEND4',
            is_global=False,
            domain='load-default-test',
            hq_api_id=SQLTestSMSBackend.get_api_id(),
        )

        SQLMobileBackendMapping.objects.create(
            is_global=True,
            backend_type=SQLMobileBackend.SMS,
            prefix='*',
            backend=backend1
        )

        SQLMobileBackendMapping.objects.create(
            is_global=True,
            backend_type=SQLMobileBackend.SMS,
            prefix='27',
            backend=backend2
        )

        SQLMobileBackendMapping.objects.create(
            is_global=False,
            domain='load-default-test',
            backend_type=SQLMobileBackend.SMS,
            prefix='*',
            backend=backend3
        )

        SQLMobileBackendMapping.objects.create(
            is_global=False,
            domain='load-default-test',
            backend_type=SQLMobileBackend.SMS,
            prefix='27',
            backend=backend4
        )

        # Test global prefix map
        self.assertBackendsEqual(
            SQLMobileBackend.load_default_by_phone_and_domain(
                SQLMobileBackend.SMS,
                '2700000000',
                domain='load-default-test-2'
            ),
            backend2
        )

        # Test domain-level prefix map
        self.assertBackendsEqual(
            SQLMobileBackend.load_default_by_phone_and_domain(
                SQLMobileBackend.SMS,
                '2700000000',
                domain='load-default-test'
            ),
            backend4
        )

        # Test domain catch-all
        backend4.soft_delete()
        self.assertBackendsEqual(
            SQLMobileBackend.load_default_by_phone_and_domain(
                SQLMobileBackend.SMS,
                '2700000000',
                domain='load-default-test'
            ),
            backend3
        )

        # Test global prefix map
        backend3.soft_delete()
        self.assertBackendsEqual(
            SQLMobileBackend.load_default_by_phone_and_domain(
                SQLMobileBackend.SMS,
                '2700000000',
                domain='load-default-test'
            ),
            backend2
        )

        # Test global catch-all
        backend2.soft_delete()
        self.assertBackendsEqual(
            SQLMobileBackend.load_default_by_phone_and_domain(
                SQLMobileBackend.SMS,
                '2700000000',
                domain='load-default-test'
            ),
            backend1
        )

        # Test raising exception if nothing found
        backend1.soft_delete()
        with self.assertRaises(BadSMSConfigException):
            SQLMobileBackend.load_default_by_phone_and_domain(
                SQLMobileBackend.SMS,
                '2700000000',
                domain='load-default-test'
            )

        backend1.delete()
        backend2.delete()
        backend3.delete()
        backend4.delete()