Exemplo n.º 1
0
class TestProcessMessageStatus(TestCase):
    def setUp(self):
        super(TestProcessMessageStatus, self).setUp()
        self.backend = SQLTelerivetBackend(
            name='TELERIVET',
            is_global=True,
            hq_api_id=SQLTelerivetBackend.get_api_id())
        self.backend.set_extra_fields(
            webhook_secret='The chamber of webhook secrets')
        self.backend.save()

        self.sms = SMS(
            text='I am testy',
            custom_metadata={'case_id': '123'},
        )
        self.sms.save()

    def tearDown(self):
        self.sms.delete()
        super(TestProcessMessageStatus, self).tearDown()

    def test_status_delivered(self):
        process_message_status(self.sms, DELIVERED)

        sms = SMS.objects.get(couch_id=self.sms.couch_id)
        self.assertTrue('gateway_delivered' in sms.custom_metadata.keys())

    def test_error_status(self):
        message_id = self.sms.couch_id
        process_message_status(self.sms,
                               FAILED,
                               error_message='Somthing went wrong')

        sms = SMS.objects.get(couch_id=message_id)
        self.assertTrue('gateway_delivered' not in sms.custom_metadata.keys())
Exemplo n.º 2
0
 def _get_fake_sms(self, text):
     msg = SMS(domain=self.domain,
               phone_number='+16175555454',
               direction=OUTGOING,
               date=datetime.utcnow(),
               backend_id=self.mobile_backend.couch_id,
               text=text)
     msg.save()
     return msg
Exemplo n.º 3
0
 def _get_fake_sms(self, text):
     msg = SMS(
         domain=self.domain,
         phone_number='+16175555454',
         direction=OUTGOING,
         date=datetime.utcnow(),
         backend_id=self.mobile_backend.couch_id,
         text=text
     )
     msg.save()
     return msg
Exemplo n.º 4
0
 def create_fake_sms(self, backend_api_id, backend_couch_id, text, date):
     msg = SMS(domain=self.domain,
               phone_number='+12223334444',
               direction=OUTGOING,
               date=date,
               backend_api=backend_api_id,
               backend_id=backend_couch_id,
               backend_message_id=uuid.uuid4().hex,
               text=text)
     msg.save()
     return msg
Exemplo n.º 5
0
    def setUp(self):
        self.backend = SQLTelerivetBackend(
            name='TELERIVET',
            is_global=True,
            hq_api_id=SQLTelerivetBackend.get_api_id())
        self.backend.set_extra_fields(
            webhook_secret='The chamber of webhook secrets')
        self.backend.save()

        self.sms = SMS(phone_number='27845698785', text="Fame is fickle")
        self.sms.save()
Exemplo n.º 6
0
def get_fake_sms(domain, backend_api_id, backend_couch_id, text):
    msg = SMS(domain=domain,
              phone_number='+12223334444',
              direction=OUTGOING,
              date=datetime.utcnow(),
              backend_api=backend_api_id,
              backend_id=backend_couch_id,
              backend_message_id=uuid.uuid4().hex,
              text=text)
    msg.save()
    return msg
 def setUp(self):
     self.domain = Domain(name='mockdomain')
     self.domain.save()
     SMS.by_domain(self.domain.name).delete()
     self.user = '******'
     self.password = '******'
     self.number = 5555551234
     self.couch_user = WebUser.create(self.domain.name, self.user, self.password)
     self.couch_user.add_phone_number(self.number)
     self.couch_user.save()
     self.message_ascii = 'It Works'
     self.message_utf_hex = '0939093F0928094D092609400020091509300924093E00200939094800200907093800200938092E092F00200915093E092E002009390948003F'
 def setUp(self):
     self.domain = Domain(name='mockdomain')
     self.domain.save()
     SMS.by_domain(self.domain.name).delete()
     self.user = '******'
     self.password = '******'
     self.number = 5555551234
     self.couch_user = WebUser.create(self.domain.name, self.user, self.password)
     self.couch_user.add_phone_number(self.number)
     self.couch_user.save()
     self.message_ascii = 'It Works'
     self.message_utf_hex = '0939093F0928094D092609400020091509300924093E00200939094800200907093800200938092E092F00200915093E092E002009390948003F'
Exemplo n.º 9
0
    def main_context(self):
        contacts = CommCareUser.by_domain(self.domain, reduce=True)
        web_users = WebUser.by_domain(self.domain)
        web_users_admins = web_users_read_only = 0
        facilities = SQLLocation.objects.filter(domain=self.domain, location_type__name__iexact='FACILITY')
        admin_role_list = UserRole.by_domain_and_name(self.domain, 'Administrator')
        if admin_role_list:
            admin_role = admin_role_list[0]
        else:
            admin_role = None
        for web_user in web_users:
            dm = web_user.get_domain_membership(self.domain)
            if admin_role and dm.role_id == admin_role.get_id:
                web_users_admins += 1
            else:
                web_users_read_only += 1

        main_context = super(GlobalStats, self).main_context
        entities_reported_stock = SQLLocation.objects.filter(
            domain=self.domain,
            location_type__administrative=False
        ).count()

        context = {
            'root_name': self.root_name,
            'country': SQLLocation.objects.filter(domain=self.domain,
                                                  location_type__name__iexact=self.root_name).count(),
            'region': SQLLocation.objects.filter(domain=self.domain, location_type__name__iexact='region').count(),
            'district': SQLLocation.objects.filter(
                domain=self.domain,
                location_type__name__iexact='district'
            ).count(),
            'entities_reported_stock': entities_reported_stock,
            'facilities': len(facilities),
            'contacts': contacts[0]['value'] if contacts else 0,
            'web_users': len(web_users),
            'web_users_admins': web_users_admins,
            'web_users_read_only': web_users_read_only,
            'products': SQLProduct.objects.filter(domain=self.domain, is_archived=False).count(),
            'product_stocks': StockState.objects.filter(sql_product__domain=self.domain).count(),
            'stock_transactions': StockTransaction.objects.filter(report__domain=self.domain).count(),
            'inbound_messages': SMS.count_by_domain(self.domain, direction=INCOMING),
            'outbound_messages': SMS.count_by_domain(self.domain, direction=OUTGOING),
        }

        if self.show_supply_point_types:
            counts = SQLLocation.objects.values('location_type__name').filter(domain=self.domain).annotate(
                Count('location_type')
            ).order_by('location_type__name')
            context['location_types'] = counts
        main_context.update(context)
        return main_context
Exemplo n.º 10
0
    def main_context(self):
        contacts = CommCareUser.by_domain(self.domain, reduce=True)
        web_users = WebUser.by_domain(self.domain)
        web_users_admins = web_users_read_only = 0
        facilities = SQLLocation.objects.filter(domain=self.domain, location_type__name__iexact='FACILITY')
        admin_role_list = UserRole.by_domain_and_name(self.domain, 'Administrator')
        if admin_role_list:
            admin_role = admin_role_list[0]
        else:
            admin_role = None
        for web_user in web_users:
            dm = web_user.get_domain_membership(self.domain)
            if admin_role and dm.role_id == admin_role.get_id:
                web_users_admins += 1
            else:
                web_users_read_only += 1

        main_context = super(GlobalStats, self).main_context
        entities_reported_stock = SQLLocation.objects.filter(
            domain=self.domain,
            location_type__administrative=False
        ).count()

        context = {
            'root_name': self.root_name,
            'country': SQLLocation.objects.filter(domain=self.domain,
                                                  location_type__name__iexact=self.root_name).count(),
            'region': SQLLocation.objects.filter(domain=self.domain, location_type__name__iexact='region').count(),
            'district': SQLLocation.objects.filter(
                domain=self.domain,
                location_type__name__iexact='district'
            ).count(),
            'entities_reported_stock': entities_reported_stock,
            'facilities': len(facilities),
            'contacts': contacts[0]['value'] if contacts else 0,
            'web_users': len(web_users),
            'web_users_admins': web_users_admins,
            'web_users_read_only': web_users_read_only,
            'products': SQLProduct.objects.filter(domain=self.domain, is_archived=False).count(),
            'product_stocks': StockState.objects.filter(sql_product__domain=self.domain).count(),
            'stock_transactions': StockTransaction.objects.filter(report__domain=self.domain).count(),
            'inbound_messages': SMS.count_by_domain(self.domain, direction=INCOMING),
            'outbound_messages': SMS.count_by_domain(self.domain, direction=OUTGOING),
        }

        if self.show_supply_point_types:
            counts = SQLLocation.objects.values('location_type__name').filter(domain=self.domain).annotate(
                Count('location_type')
            ).order_by('location_type__name')
            context['location_types'] = counts
        main_context.update(context)
        return main_context
Exemplo n.º 11
0
    def tearDown(self):
        SmsBillable.objects.all().delete()
        SmsGatewayFee.objects.all().delete()
        SmsGatewayFeeCriteria.objects.all().delete()
        SmsUsageFee.objects.all().delete()
        SmsUsageFeeCriteria.objects.all().delete()

        self.currency_usd.delete()
        self.other_currency.delete()
        SMS.by_domain(generator.TEST_DOMAIN).delete()

        for api_id, backend_id in self.backend_ids.iteritems():
            SQLMobileBackend.load(backend_id, is_couch_id=True).delete()
Exemplo n.º 12
0
    def testSMSSync(self):
        prev_couch_count = self.getSMSLogCount()
        prev_sql_count = self.getSMSCount()

        # Test Create
        sms = SMS()
        self.setRandomSMSValues(sms)
        sms.save()

        sleep(1)
        self.assertEqual(self.getSMSLogCount(), prev_couch_count + 1)
        self.assertEqual(self.getSMSCount(), prev_sql_count + 1)

        smslog = FRISMSLog.get(sms.couch_id)
        self.checkFieldValues(smslog, sms, SMS._migration_get_fields())
        self.assertTrue(FRISMSLog.get_db().get_rev(
            smslog._id).startswith('2-'))

        # Test Update
        self.setRandomSMSValues(sms)
        sms.save()

        sleep(1)
        self.assertEqual(self.getSMSLogCount(), prev_couch_count + 1)
        self.assertEqual(self.getSMSCount(), prev_sql_count + 1)
        smslog = FRISMSLog.get(sms.couch_id)
        self.checkFieldValues(smslog, sms, SMS._migration_get_fields())
        self.assertTrue(FRISMSLog.get_db().get_rev(
            smslog._id).startswith('3-'))
Exemplo n.º 13
0
    def setUp(self):
        super(TestProcessMessageStatus, self).setUp()
        self.backend = SQLTelerivetBackend(
            name='TELERIVET',
            is_global=True,
            hq_api_id=SQLTelerivetBackend.get_api_id())
        self.backend.set_extra_fields(
            webhook_secret='The chamber of webhook secrets')
        self.backend.save()

        self.sms = SMS(
            text='I am testy',
            custom_metadata={'case_id': '123'},
        )
        self.sms.save()
Exemplo n.º 14
0
    def add_sms_count_info(self, result, days):
        end_date = self.domain_now.date()
        start_date = end_date - timedelta(days=days - 1)
        counts = SMS.get_counts_by_date(self.domain, start_date, end_date, self.timezone.zone)

        inbound_counts = {}
        outbound_counts = {}

        for row in counts:
            if row.direction == INCOMING:
                inbound_counts[row.date] = row.sms_count
            elif row.direction == OUTGOING:
                outbound_counts[row.date] = row.sms_count

        inbound_values = []
        outbound_values = []

        for i in range(days):
            dt = start_date + timedelta(days=i)
            inbound_values.append({
                'x': dt.strftime('%Y-%m-%d'),
                'y': inbound_counts.get(dt, 0),
            })
            outbound_values.append({
                'x': dt.strftime('%Y-%m-%d'),
                'y': outbound_counts.get(dt, 0),
            })

        result['sms_count_data'] = [
            {'key': _("Incoming"), 'values': inbound_values},
            {'key': _("Outgoing"), 'values': outbound_values},
        ]
 def handle(self, domains, **options):
     for domain in domains:
         print("*** Processing Domain %s ***" % domain)
         user_cache = {}
         for msg in SMS.by_domain(domain):
             if msg.couch_recipient:
                 if msg.couch_recipient_doc_type != "CommCareCase":
                     user = None
                     if msg.couch_recipient in user_cache:
                         user = user_cache[msg.couch_recipient]
                     else:
                         try:
                             user = CouchUser.get_by_user_id(
                                 msg.couch_recipient)
                         except Exception:
                             user = None
                         if user is None:
                             print("Could not find user %s" %
                                   msg.couch_recipient)
                     user_cache[msg.couch_recipient] = user
                     if user and msg.couch_recipient_doc_type != user.doc_type:
                         msg.couch_recipient_doc_type = user.doc_type
                         msg.save()
             else:
                 if msg.couch_recipient_doc_type is not None or msg.couch_recipient is not None:
                     msg.couch_recipient = None
                     msg.couch_recipient_doc_type = None
                     msg.save()
Exemplo n.º 16
0
def handle_domain_specific_delays(msg, domain_object, utcnow):
    """
    Checks whether or not we need to hold off on sending an outbound message
    due to any restrictions set on the domain, and delays processing of the
    message if necessary.

    Returns True if a delay was made, False if not.
    """
    domain_now = ServerTime(utcnow).user_time(domain_object.get_default_timezone()).done()

    if len(domain_object.restricted_sms_times) > 0:
        if not time_within_windows(domain_now, domain_object.restricted_sms_times):
            delay_processing(msg, settings.SMS_QUEUE_DOMAIN_RESTRICTED_RETRY_INTERVAL)
            return True

    if msg.chat_user_id is None and len(domain_object.sms_conversation_times) > 0:
        if time_within_windows(domain_now, domain_object.sms_conversation_times):
            sms_conversation_length = domain_object.sms_conversation_length
            conversation_start_timestamp = utcnow - timedelta(minutes=sms_conversation_length)
            if SMS.inbound_entry_exists(
                msg.couch_recipient_doc_type,
                msg.couch_recipient,
                conversation_start_timestamp,
                to_timestamp=utcnow
            ):
                delay_processing(msg, 1)
                return True

    return False
    def test_get_inbound_phone_entry__no_backend_id(self):
        sms = SMS(phone_number=self.phone_number)
        phone_number, has_domain_two_way_scope = get_inbound_phone_entry_from_sms(
            sms)

        self.assertEqual(phone_number.owner_id, "fake-owner-1")
        self.assertFalse(has_domain_two_way_scope)
Exemplo n.º 18
0
 def get_participant_messages(self, case):
     return SMS.by_recipient(
         'CommCareCase',
         case.get_id
     ).filter(
         direction=OUTGOING
     )
Exemplo n.º 19
0
def handle_domain_specific_delays(msg, domain_object, utcnow):
    """
    Checks whether or not we need to hold off on sending an outbound message
    due to any restrictions set on the domain, and delays processing of the
    message if necessary.

    Returns True if a delay was made, False if not.
    """
    domain_now = ServerTime(utcnow).user_time(
        domain_object.get_default_timezone()).done()

    if len(domain_object.restricted_sms_times) > 0:
        if not time_within_windows(domain_now,
                                   domain_object.restricted_sms_times):
            delay_processing(
                msg, settings.SMS_QUEUE_DOMAIN_RESTRICTED_RETRY_INTERVAL)
            return True

    if msg.chat_user_id is None and len(
            domain_object.sms_conversation_times) > 0:
        if time_within_windows(domain_now,
                               domain_object.sms_conversation_times):
            sms_conversation_length = domain_object.sms_conversation_length
            conversation_start_timestamp = utcnow - timedelta(
                minutes=sms_conversation_length)
            if SMS.inbound_entry_exists(msg.couch_recipient_doc_type,
                                        msg.couch_recipient,
                                        conversation_start_timestamp,
                                        to_timestamp=utcnow):
                delay_processing(msg, 1)
                return True

    return False
 def _get_inbound_phone_entry__backend_domain_2(self):
     """The utilities main objective is to ensure consistency between the various
     test methods that are testing `get_inbound_phone_entry` under different conditions
     """
     sms = SMS(phone_number=self.phone_number,
               backend_id=self.domain2_backend.couch_id)
     return get_inbound_phone_entry_from_sms(sms)
Exemplo n.º 21
0
    def get_first_survey_response(self, case, dt):
        timestamp_start = datetime.combine(dt, time(20, 45))
        timestamp_start = UserTime(
            timestamp_start, self.timezone).server_time().done()

        timestamp_end = datetime.combine(dt + timedelta(days=1), time(11, 45))
        timestamp_end = UserTime(
            timestamp_end, self.timezone).server_time().done()
        if timestamp_end > datetime.utcnow():
            return RESPONSE_NOT_APPLICABLE

        survey_responses = SMS.by_recipient(
            'CommCareCase',
            case.get_id
        ).filter(
            direction=INCOMING,
            xforms_session_couch_id__isnull=False,
            date__gte=timestamp_start,
            date__lte=timestamp_end
        ).order_by('date')[:1]

        if survey_responses:
            return survey_responses[0]

        return NO_RESPONSE
Exemplo n.º 22
0
    def add_sms_count_info(self, result, days):
        end_date = self.domain_now.date()
        start_date = end_date - timedelta(days=days - 1)
        counts = SMS.get_counts_by_date(self.domain, start_date, end_date, self.timezone.zone)

        inbound_counts = {}
        outbound_counts = {}

        for row in counts:
            if row.direction == INCOMING:
                inbound_counts[row.date] = row.sms_count
            elif row.direction == OUTGOING:
                outbound_counts[row.date] = row.sms_count

        inbound_values = []
        outbound_values = []

        for i in range(days):
            dt = start_date + timedelta(days=i)
            inbound_values.append({
                'x': dt.strftime('%Y-%m-%d'),
                'y': inbound_counts.get(dt, 0),
            })
            outbound_values.append({
                'x': dt.strftime('%Y-%m-%d'),
                'y': outbound_counts.get(dt, 0),
            })

        result['sms_count_data'] = [
            {'key': _("Incoming"), 'values': inbound_values},
            {'key': _("Outgoing"), 'values': outbound_values},
        ]
    def rows(self):
        data = SMS.by_domain(self.domain,
                             start_date=self.datespan.startdate_utc,
                             end_date=self.datespan.enddate_utc).filter(
                                 workflow__iexact=WORKFLOW_DEFAULT).exclude(
                                     direction=OUTGOING,
                                     processed=False,
                                 ).order_by('date')
        result = []

        direction_map = {
            INCOMING: _("Incoming"),
            OUTGOING: _("Outgoing"),
        }
        reporting_locations_id = self.get_location_filter()

        contact_cache = {}
        for message in data:
            if reporting_locations_id and message.location_id not in reporting_locations_id:
                continue

            doc_info = self.get_recipient_info(message, contact_cache)
            phone_number = message.phone_number
            timestamp = ServerTime(message.date).user_time(
                self.timezone).done()
            result.append([
                self._fmt_timestamp(timestamp),
                self._fmt_contact_link(message, doc_info),
                _fmt(phone_number),
                _fmt(direction_map.get(message.direction, "-")),
                _fmt(message.text)
            ])

        return result
 def handle(self, domains, **options):
     for domain in domains:
         print("*** Processing Domain %s ***" % domain)
         user_cache = {}
         for msg in SMS.by_domain(domain):
             if msg.couch_recipient:
                 if msg.couch_recipient_doc_type != "CommCareCase":
                     user = None
                     if msg.couch_recipient in user_cache:
                         user = user_cache[msg.couch_recipient]
                     else:
                         try:
                             user = CouchUser.get_by_user_id(msg.couch_recipient)
                         except Exception:
                             user = None
                         if user is None:
                             print("Could not find user %s" % msg.couch_recipient)
                     user_cache[msg.couch_recipient] = user
                     if user and msg.couch_recipient_doc_type != user.doc_type:
                         msg.couch_recipient_doc_type = user.doc_type
                         msg.save()
             else:
                 if msg.couch_recipient_doc_type is not None or msg.couch_recipient is not None:
                     msg.couch_recipient = None
                     msg.couch_recipient_doc_type = None
                     msg.save()
    def handle(self, *args, **options):
        if len(args) == 0:
            raise CommandError("Usage: python manage.py fix_smslog_recipient_doc_type <domain1 domain2 ...>")

        for domain in args:
            print "*** Processing Domain %s ***" % domain
            user_cache = {}
            for msg in SMS.by_domain(domain):
                if msg.couch_recipient:
                    if msg.couch_recipient_doc_type != "CommCareCase":
                        user = None
                        if msg.couch_recipient in user_cache:
                            user = user_cache[msg.couch_recipient]
                        else:
                            try:
                                user = CouchUser.get_by_user_id(msg.couch_recipient)
                            except Exception:
                                user = None
                            if user is None:
                                print "Could not find user %s" % msg.couch_recipient
                        user_cache[msg.couch_recipient] = user
                        if user and msg.couch_recipient_doc_type != user.doc_type:
                            msg.couch_recipient_doc_type = user.doc_type
                            msg.save()
                else:
                    if msg.couch_recipient_doc_type is not None or msg.couch_recipient is not None:
                        msg.couch_recipient = None
                        msg.couch_recipient_doc_type = None
                        msg.save()
    def handle(self, *args, **options):
        if len(args) == 0:
            raise CommandError(
                "Usage: python manage.py fix_smslog_recipient_doc_type <domain1 domain2 ...>"
            )

        for domain in args:
            print "*** Processing Domain %s ***" % domain
            user_cache = {}
            for msg in SMS.by_domain(domain):
                if msg.couch_recipient:
                    if msg.couch_recipient_doc_type != "CommCareCase":
                        user = None
                        if msg.couch_recipient in user_cache:
                            user = user_cache[msg.couch_recipient]
                        else:
                            try:
                                user = CouchUser.get_by_user_id(
                                    msg.couch_recipient)
                            except Exception:
                                user = None
                            if user is None:
                                print "Could not find user %s" % msg.couch_recipient
                        user_cache[msg.couch_recipient] = user
                        if user and msg.couch_recipient_doc_type != user.doc_type:
                            msg.couch_recipient_doc_type = user.doc_type
                            msg.save()
                else:
                    if msg.couch_recipient_doc_type is not None or msg.couch_recipient is not None:
                        msg.couch_recipient = None
                        msg.couch_recipient_doc_type = None
                        msg.save()
Exemplo n.º 27
0
    def tearDown(self):
        SmsBillable.objects.all().delete()
        SmsGatewayFee.objects.all().delete()
        SmsGatewayFeeCriteria.objects.all().delete()
        SmsUsageFee.objects.all().delete()
        SmsUsageFeeCriteria.objects.all().delete()

        self.currency_usd.delete()
        self.other_currency.delete()
        SMS.by_domain(generator.TEST_DOMAIN).delete()

        for api_id, backend_id in self.backend_ids.iteritems():
            SQLMobileBackend.load(backend_id, is_couch_id=True).delete()

        FakeTwilioMessageFactory.backend_message_id_to_price = {}

        super(TestGatewayFee, self).tearDown()
Exemplo n.º 28
0
    def tearDown(self):
        SmsBillable.objects.all().delete()
        SmsGatewayFee.objects.all().delete()
        SmsGatewayFeeCriteria.objects.all().delete()
        SmsUsageFee.objects.all().delete()
        SmsUsageFeeCriteria.objects.all().delete()

        self.currency_usd.delete()
        self.other_currency.delete()
        SMS.by_domain(generator.TEST_DOMAIN).delete()

        for api_id, backend_id in six.iteritems(self.backend_ids):
            SQLMobileBackend.load(backend_id, is_couch_id=True).delete()

        FakeTwilioMessageFactory.backend_message_id_to_price = {}

        super(TestGatewayFee, self).tearDown()
Exemplo n.º 29
0
    def testSMSSync(self):
        self.deleteAllLogs()
        self.assertEqual(self.getSMSLogCount(), 0)
        self.assertEqual(self.getSMSCount(), 0)

        # Test Create
        sms = SMS()
        self.setRandomSMSValues(sms)
        sms.save()

        sleep(1)
        self.assertEqual(self.getSMSLogCount(), 1)
        self.assertEqual(self.getSMSCount(), 1)

        smslog = FRISMSLog.get(sms.couch_id)
        self.checkFieldValues(smslog, sms, SMS._migration_get_fields())
        self.assertTrue(FRISMSLog.get_db().get_rev(smslog._id).startswith('2-'))

        # Test Update
        self.setRandomSMSValues(sms)
        sms.save()

        sleep(1)
        self.assertEqual(self.getSMSLogCount(), 1)
        self.assertEqual(self.getSMSCount(), 1)
        smslog = FRISMSLog.get(sms.couch_id)
        self.checkFieldValues(smslog, sms, SMS._migration_get_fields())
        self.assertTrue(FRISMSLog.get_db().get_rev(smslog._id).startswith('3-'))
Exemplo n.º 30
0
    def rows(self):
        data = SMS.by_domain(
            self.domain,
            start_date=self.datespan.startdate_utc,
            end_date=self.datespan.enddate_utc
        ).exclude(
            direction=OUTGOING,
            processed=False
        ).order_by('date')

        if self.show_only_survey_traffic():
            data = data.filter(
                xforms_session_couch_id__isnull=False
            )

        result = []
        direction_map = {
            INCOMING: _("Incoming"),
            OUTGOING: _("Outgoing"),
        }
        message_bank_messages = get_message_bank(self.domain, for_comparing=True)

        FormProcessorInterface(self.domain).casedb_cache(
            domain=self.domain, strip_history=False, deleted_ok=True
        )
        user_cache = UserCache()

        for message in data:
            # Add metadata from the message bank if it has not been added already
            if (message.direction == OUTGOING) and (not message.fri_message_bank_lookup_completed):
                add_metadata(message, message_bank_messages)

            if message.couch_recipient_doc_type == "CommCareCase":
                recipient = case_cache.get(message.couch_recipient)
            else:
                recipient = user_cache.get(message.couch_recipient)

            if message.chat_user_id:
                sender = user_cache.get(message.chat_user_id)
            else:
                sender = None

            study_arm = None
            if message.couch_recipient_doc_type == "CommCareCase":
                study_arm = case_cache.get(message.couch_recipient).get_case_property("study_arm")

            timestamp = ServerTime(message.date).user_time(self.timezone).done()
            result.append([
                self._fmt(self._participant_id(recipient)),
                self._fmt(study_arm or "-"),
                self._fmt(self._originator(message, recipient, sender)),
                self._fmt_timestamp(timestamp),
                self._fmt(message.text),
                self._fmt(message.fri_id or "-"),
                self._fmt(direction_map.get(message.direction,"-")),
            ])
        return result
Exemplo n.º 31
0
    def rows(self):
        data = SMS.by_domain(self.domain,
                             start_date=self.datespan.startdate_utc,
                             end_date=self.datespan.enddate_utc).exclude(
                                 direction=OUTGOING,
                                 processed=False).order_by('date')

        if self.show_only_survey_traffic():
            data = data.filter(xforms_session_couch_id__isnull=False)

        result = []
        direction_map = {
            INCOMING: _("Incoming"),
            OUTGOING: _("Outgoing"),
        }
        message_bank_messages = get_message_bank(self.domain,
                                                 for_comparing=True)

        FormProcessorInterface(self.domain).casedb_cache(domain=self.domain,
                                                         strip_history=False,
                                                         deleted_ok=True)
        user_cache = UserCache()

        for message in data:
            # Add metadata from the message bank if it has not been added already
            if (message.direction == OUTGOING) and (
                    not message.fri_message_bank_lookup_completed):
                add_metadata(message, message_bank_messages)

            if message.couch_recipient_doc_type == "CommCareCase":
                recipient = case_cache.get(message.couch_recipient)
            else:
                recipient = user_cache.get(message.couch_recipient)

            if message.chat_user_id:
                sender = user_cache.get(message.chat_user_id)
            else:
                sender = None

            study_arm = None
            if message.couch_recipient_doc_type == "CommCareCase":
                study_arm = case_cache.get(
                    message.couch_recipient).get_case_property("study_arm")

            timestamp = ServerTime(message.date).user_time(
                self.timezone).done()
            result.append([
                self._fmt(self._participant_id(recipient)),
                self._fmt(study_arm or "-"),
                self._fmt(self._originator(message, recipient, sender)),
                self._fmt_timestamp(timestamp),
                self._fmt(message.text),
                self._fmt(message.fri_id or "-"),
                self._fmt(direction_map.get(message.direction, "-")),
            ])
        return result
Exemplo n.º 32
0
 def _test_unicel_send(self, text, expected_msg):
     message = SMS(text=text, phone_number='+15555555555')
     with patch('corehq.messaging.smsbackends.unicel.models.urlopen') as patch_urlopen:
         self.backend.send(message)
     self.assertEqual(len(patch_urlopen.call_args_list), 1)
     called_args = patch_urlopen.call_args_list[0]
     url, = called_args[0]
     parsed_url = urlparse(url)
     url_params = parse_qs(parsed_url.query)
     self.assertEqual(url_params['msg'], [expected_msg])
    def test_get_inbound_phone_entry__global_backend(self):
        sms = SMS(phone_number=self.phone_number,
                  backend_id=self.global_backend.couch_id)
        phone_number, has_domain_two_way_scope = get_inbound_phone_entry_from_sms(
            sms)

        self.assertEqual(phone_number.owner_id, "fake-owner-1")
        self.assertFalse(has_domain_two_way_scope)
        self.assertTrue(
            self.global_backend.domain_is_authorized(phone_number.domain))
Exemplo n.º 34
0
def send_sample_sms(request, domain):
    request_token = request.POST.get('request_token')
    if not TelerivetSetupView.get_cached_webhook_secret(request_token):
        return {
            'success': False,
            'unexpected_error': TelerivetSetupView.unexpected_error,
        }

    outgoing_sms_form = TelerivetOutgoingSMSForm({
        'api_key':
        request.POST.get('api_key'),
        'project_id':
        request.POST.get('project_id'),
        'phone_id':
        request.POST.get('phone_id'),
    })

    test_sms_form = TelerivetPhoneNumberForm({
        'test_phone_number':
        request.POST.get('test_phone_number'),
    })

    # Be sure to call .is_valid() on both
    outgoing_sms_form_valid = outgoing_sms_form.is_valid()
    test_sms_form_valid = test_sms_form.is_valid()
    if not outgoing_sms_form_valid or not test_sms_form_valid:
        return json_response({
            'success':
            False,
            'api_key_error':
            TelerivetSetupView.get_error_message(outgoing_sms_form, 'api_key'),
            'project_id_error':
            TelerivetSetupView.get_error_message(outgoing_sms_form,
                                                 'project_id'),
            'phone_id_error':
            TelerivetSetupView.get_error_message(outgoing_sms_form,
                                                 'phone_id'),
            'test_phone_number_error':
            TelerivetSetupView.get_error_message(test_sms_form,
                                                 'test_phone_number'),
        })

    tmp_backend = SQLTelerivetBackend()
    tmp_backend.set_extra_fields(
        api_key=outgoing_sms_form.cleaned_data.get('api_key'),
        project_id=outgoing_sms_form.cleaned_data.get('project_id'),
        phone_id=outgoing_sms_form.cleaned_data.get('phone_id'),
    )
    sms = SMS(phone_number=clean_phone_number(
        test_sms_form.cleaned_data.get('test_phone_number')),
              text="This is a test SMS from CommCareHQ.")
    tmp_backend.send(sms)
    return json_response({
        'success': True,
    })
Exemplo n.º 35
0
def arbitrary_messages_by_backend_and_direction(backend_ids,
                                                phone_number=None,
                                                domain=None,
                                                directions=DIRECTIONS):
    phone_number = phone_number or TEST_NUMBER
    domain = domain or TEST_DOMAIN
    messages = []
    for api_id, instance_id in backend_ids.items():
        for direction in directions:
            sms_log = SMS(
                direction=direction,
                phone_number=phone_number,
                domain=domain,
                backend_api=api_id,
                backend_id=instance_id,
                text=arbitrary_message(),
                date=datetime.datetime.utcnow()
            )
            sms_log.save()
            messages.append(sms_log)
    return messages
Exemplo n.º 36
0
def arbitrary_messages_by_backend_and_direction(backend_ids,
                                                phone_number=None,
                                                domain=None,
                                                directions=None):
    phone_number = phone_number or TEST_NUMBER
    domain = domain or TEST_DOMAIN
    directions = directions or DIRECTIONS
    messages = []
    for api_id, instance_id in backend_ids.items():
        for direction in directions:
            sms_log = SMS(direction=direction,
                          phone_number=phone_number,
                          domain=domain,
                          backend_api=api_id,
                          backend_id=instance_id,
                          backend_message_id=uuid.uuid4().hex,
                          text=arbitrary_message(),
                          date=datetime.datetime.utcnow())
            sms_log.save()
            messages.append(sms_log)
    return messages
Exemplo n.º 37
0
 def _get_sms(self):
     msg = SMS(
         phone_number=self.number1.phone_number,
         direction=INCOMING,
         date=datetime.utcnow(),
         text="test message",
         domain_scope=None,
         backend_api=None,
         backend_id=None,
         backend_message_id=None,
         raw_text=None,
     )
     return msg
Exemplo n.º 38
0
 def rows(self):
     data = (SMS.by_domain(self.config['domain'])
             .filter(location_id=self.config['location_id'])
             .order_by('date'))
     messages = []
     for message in data:
         timestamp = ServerTime(message.date).user_time(self.config['timezone']).done()
         messages.append([
             _fmt_timestamp(timestamp),
             _fmt(message.direction),
             _fmt(message.text),
         ])
     return messages
Exemplo n.º 39
0
    def _create_message(self,
                        domain='test_domain',
                        phone_number='2511234567',
                        direction=OUTGOING,
                        date=None,
                        text='Hello World'):
        if not date:
            date = datetime(2021, 5, 15)

        return SMS(domain=domain,
                   phone_number=phone_number,
                   direction=direction,
                   date=date,
                   text=text)
Exemplo n.º 40
0
 def rows(self):
     data = (SMS.by_domain(self.config['domain']).filter(
         location_id=self.config['location_id']).exclude(
             processed=False, direction=OUTGOING).order_by('-date'))
     messages = []
     for message in data:
         recipient = message.recipient
         timestamp = ServerTime(message.date).user_time(
             self.config['timezone']).done()
         messages.append([
             _fmt_timestamp(timestamp),
             recipient.full_name,
             message.phone_number,
             _fmt(message.direction),
             _fmt(message.text),
         ])
     return messages
Exemplo n.º 41
0
 def rows(self):
     data = (SMS.by_domain(self.config['domain'])
             .filter(location_id=self.config['location_id'])
             .exclude(processed=False, direction=OUTGOING)
             .order_by('-date'))
     messages = []
     for message in data:
         recipient = message.recipient
         timestamp = ServerTime(message.date).user_time(self.config['timezone']).done()
         messages.append([
             _fmt_timestamp(timestamp),
             recipient.full_name,
             message.phone_number,
             _fmt(message.direction),
             _fmt(message.text),
         ])
     return messages
Exemplo n.º 42
0
    def test_incoming_sms_linked_form_session(self):
        event, expected_subevent = self._create_message_event_and_subevent()
        msg = SMS(
            phone_number=self.number.phone_number,
            direction=INCOMING,
            date=datetime.utcnow(),
            text="test message",
            domain_scope=None,
            backend_api=None,
            backend_id=None,
            backend_message_id=None,
            raw_text=None,
        )
        self.addCleanup(msg.delete)

        form_session_handler(self.number, msg.text, msg)

        self.assertEqual(expected_subevent, msg.messaging_subevent)
Exemplo n.º 43
0
    def test_correctly_handles_ssrf(self, mock_save, mock_metrics_counter):
        # SSRF needs to:
        # - set the error on the message and save, so the message isn't re-queued
        # - generate a metric
        # - raise a PossibleSSRFAttempt exception

        message = SMS(domain='test-domain')

        # raise exception
        with self.assertRaises(PossibleSSRFAttempt):
            verify_sms_url('https://malicious.address', message, self.backend)

        # setting error message and saving
        self.assertTrue(message.error)
        self.assertEqual(message.system_error_message, 'FAULTY_GATEWAY_CONFIGURATION')
        mock_save.assert_called()

        # generating the metric
        mock_metrics_counter.assert_called_with('commcare.sms.ssrf_attempt',
            tags={'domain': 'test-domain', 'src': 'SQLHttpBackend', 'reason': 'is_loopback'})
Exemplo n.º 44
0
def remove_from_queue(queued_sms):
    with transaction.atomic():
        sms = SMS()
        for field in sms._meta.fields:
            if field.name != 'id':
                setattr(sms, field.name, getattr(queued_sms, field.name))
        queued_sms.delete()
        sms.save()

    sms.publish_change()

    if sms.direction == OUTGOING and sms.processed and not sms.error:
        create_billable_for_sms(sms)
    elif sms.direction == INCOMING and sms.domain and domain_has_privilege(sms.domain, privileges.INBOUND_SMS):
        create_billable_for_sms(sms)
Exemplo n.º 45
0
    def handle(self, start_datetime, end_datetime, **options):
        num_sms = 0

        for domain in Domain.get_all():
            result = SMS.by_domain(
                domain.name,
                start_date=start_datetime,
                end_date=end_datetime,
            )

            for sms_log in result:
                if options.get('create', False):
                    SmsBillable.create(sms_log)
                    print('Created billable for SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date))
                else:
                    print('Found SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date))
                num_sms += 1

        print('Number of SMSs in datetime range: %d' % num_sms)
Exemplo n.º 46
0
    def handle(self, start_datetime, end_datetime, **options):
        num_sms = 0

        for domain in Domain.get_all():
            result = SMS.by_domain(
                domain.name,
                start_date=start_datetime,
                end_date=end_datetime,
            )

            for sms_log in result:
                if options.get('create', False):
                    SmsBillable.create(sms_log)
                    print('Created billable for SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date))
                else:
                    print('Found SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date))
                num_sms += 1

        print('Number of SMSs in datetime range: %d' % num_sms)
Exemplo n.º 47
0
    def handle(self, *args, **options):
        num_sms = 0

        start_datetime = datetime.datetime(*str_to_int_tuple(args[0:6]))
        end_datetime = datetime.datetime(*str_to_int_tuple(args[6:12]))

        for domain in Domain.get_all():
            result = SMS.by_domain(domain.name,
                                   start_date=start_datetime,
                                   end_date=end_datetime)

            for sms_log in result:
                if options.get('create', False):
                    SmsBillable.create(sms_log)
                    print 'Created billable for SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date)
                else:
                    print 'Found SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date)
                num_sms += 1

        print 'Number of SMSs in datetime range: %d' % num_sms
Exemplo n.º 48
0
    def handle(self, *args, **options):
        num_sms = 0

        start_datetime = datetime.datetime(*str_to_int_tuple(args[0:6]))
        end_datetime = datetime.datetime(*str_to_int_tuple(args[6:12]))

        for domain in Domain.get_all():
            result = SMS.by_domain(
                domain.name,
                start_date=start_datetime,
                end_date=end_datetime
            )

            for sms_log in result:
                if options.get('create', False):
                    SmsBillable.create(sms_log)
                    print 'Created billable for SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date)
                else:
                    print 'Found SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date)
                num_sms += 1

        print 'Number of SMSs in datetime range: %d' % num_sms
Exemplo n.º 49
0
    def rows(self):
        data = SMS.by_domain(
            self.domain,
            start_date=self.datespan.startdate_utc,
            end_date=self.datespan.enddate_utc
        ).filter(
            workflow__iexact=WORKFLOW_DEFAULT
        ).exclude(
            direction=OUTGOING,
            processed=False,
        ).order_by('date')
        result = []

        direction_map = {
            INCOMING: _("Incoming"),
            OUTGOING: _("Outgoing"),
        }
        reporting_locations_id = self.get_location_filter()

        contact_cache = {}
        for message in data:
            if reporting_locations_id and message.location_id not in reporting_locations_id:
                continue

            doc_info = self.get_recipient_info(message, contact_cache)
            phone_number = message.phone_number
            timestamp = ServerTime(message.date).user_time(self.timezone).done()
            result.append([
                self._fmt_timestamp(timestamp),
                self._fmt_contact_link(message, doc_info),
                _fmt(phone_number),
                _fmt(direction_map.get(message.direction, "-")),
                _fmt(message.text)
            ])

        return result
Exemplo n.º 50
0
 def get_last_outbound_sms(self, contact):
     return SMS.get_last_log_for_recipient(
         contact.doc_type,
         contact.get_id,
         direction=OUTGOING
     )
Exemplo n.º 51
0
 def _migration_get_fields(cls):
     return SMS._migration_get_fields()
Exemplo n.º 52
0
 def setUp(self):
     SMS.by_domain(self.domain.name).delete()