def test_one_way_numbers(self):
        user1 = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
        user2 = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
        user3 = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
        self.addCleanup(user1.delete)
        self.addCleanup(user2.delete)
        self.addCleanup(user3.delete)

        self.assertIsNone(user1.memoized_usercase)
        self.assertIsNone(Content.get_two_way_entry_or_phone_number(user1))

        with self.create_user_case(user2) as case:
            self.assertIsNotNone(user2.memoized_usercase)
            self.assertIsNone(Content.get_two_way_entry_or_phone_number(user2))
            self.assertIsNone(Content.get_two_way_entry_or_phone_number(case))

        with self.create_user_case(user3) as case:
            # If the user has no number, the user case's number is used
            update_case(self.domain, case.case_id, case_properties={'contact_phone_number': '12345678'})
            case = CaseAccessors(self.domain).get_case(case.case_id)
            self.assertPhoneEntryCount(1)
            self.assertPhoneEntryCount(0, only_count_two_way=True)
            self.assertIsNotNone(user3.memoized_usercase)
            self.assertEqual(Content.get_two_way_entry_or_phone_number(user3), '12345678')

            # If the user has a number, it is used before the user case's number
            user3.add_phone_number('87654321')
            user3.save()
            self.assertPhoneEntryCount(2)
            self.assertPhoneEntryCount(0, only_count_two_way=True)
            self.assertEqual(Content.get_two_way_entry_or_phone_number(user3), '87654321')

            # Referencing the case directly uses the case's phone number
            self.assertEqual(Content.get_two_way_entry_or_phone_number(case), '12345678')
    def test_user_case_phone_number(self):
        user1 = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
        user2 = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
        user3 = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
        self.addCleanup(user1.delete)
        self.addCleanup(user2.delete)
        self.addCleanup(user3.delete)

        self.assertIsNone(user1.memoized_usercase)
        self.assertIsNone(Content.get_one_way_phone_number(user1))

        with self.create_user_case(user2) as case:
            self.assertIsNotNone(user2.memoized_usercase)
            self.assertIsNone(Content.get_one_way_phone_number(user2))

        with self.create_user_case(user3) as case:
            update_case(self.domain,
                        case.case_id,
                        case_properties={'contact_phone_number': '12345678'})
            self.assertIsNotNone(user3.memoized_usercase)
            self.assertEqual(Content.get_one_way_phone_number(user3),
                             '12345678')

            user3.add_phone_number('87654321')
            self.assertEqual(Content.get_one_way_phone_number(user3),
                             '87654321')
Exemple #3
0
    def test_get_translation_empty_message(self):
        message_dict = {}
        content = Content()
        content.set_context(schedule_instance=Mock(memoized_schedule=Schedule(
            domain=self.domain)))

        self.assertEqual(
            content.get_translation_from_message_dict(
                self.domain, message_dict, self.user.get_language_code()), '')
Exemple #4
0
    def test_get_translation_general_default(self):
        message_dict = {
            '*': 'non-translated message',
        }
        content = Content()
        content.set_context(schedule_instance=Mock(memoized_schedule=Schedule(
            domain=self.domain)))

        self.assertEqual(
            content.get_translation_from_message_dict(
                self.domain_obj, message_dict, self.user.get_language_code()),
            message_dict['*'])
    def test_not_using_phone_entries(self):
        with patch('corehq.apps.sms.tasks.use_phone_entries') as patch1, \
                patch('corehq.messaging.tasks.use_phone_entries') as patch2, \
                patch('corehq.messaging.scheduling.models.abstract.use_phone_entries') as patch3:

            patch1.return_value = patch2.return_value = patch3.return_value = False

            user1 = CommCareUser.create(self.domain,
                                        uuid.uuid4().hex, 'abc', None, None)
            user2 = CommCareUser.create(self.domain,
                                        uuid.uuid4().hex, 'abc', None, None)
            user3 = CommCareUser.create(self.domain,
                                        uuid.uuid4().hex, 'abc', None, None)
            self.addCleanup(user1.delete, deleted_by=None)
            self.addCleanup(user2.delete, deleted_by=None)
            self.addCleanup(user3.delete, deleted_by=None)

            self.assertIsNone(user1.memoized_usercase)
            self.assertIsNone(Content.get_two_way_entry_or_phone_number(user1))

            with self.create_user_case(user2) as case:
                self.assertIsNotNone(user2.memoized_usercase)
                self.assertIsNone(
                    Content.get_two_way_entry_or_phone_number(user2))
                self.assertIsNone(
                    Content.get_two_way_entry_or_phone_number(case))

            with self.create_user_case(user3) as case:
                # If the user has no number, the user case's number is used
                update_case(
                    self.domain,
                    case.case_id,
                    case_properties={'contact_phone_number': '12345678'})
                case = CaseAccessors(self.domain).get_case(case.case_id)
                self.assertPhoneEntryCount(0)
                self.assertIsNotNone(user3.memoized_usercase)
                self.assertEqual(
                    Content.get_two_way_entry_or_phone_number(user3),
                    '12345678')

                # If the user has a number, it is used before the user case's number
                user3.add_phone_number('87654321')
                user3.save()
                self.assertPhoneEntryCount(0)
                self.assertEqual(
                    Content.get_two_way_entry_or_phone_number(user3),
                    '87654321')

                # Referencing the case directly uses the case's phone number
                self.assertEqual(
                    Content.get_two_way_entry_or_phone_number(case),
                    '12345678')
Exemple #6
0
    def test_get_translation_empty_message(self):
        message_dict = {}
        content = Content()
        content.set_context(schedule_instance=Mock(memoized_schedule=Schedule(domain=self.domain)))

        self.assertEqual(
            content.get_translation_from_message_dict(
                self.domain,
                message_dict,
                self.user.get_language_code()
            ),
            ''
        )
Exemple #7
0
    def test_get_translation_schedule_default(self):
        message_dict = {
            '*': 'non-translated message',
            'en': 'english message',
            'hin': 'hindi message',
        }
        content = Content()
        content.set_context(schedule_instance=Mock(memoized_schedule=Schedule(
            domain=self.domain, default_language_code='hin')))

        self.assertEqual(
            content.get_translation_from_message_dict(
                self.domain, message_dict, self.user.get_language_code()),
            message_dict['hin'])
    def _test_two_way_numbers(self, change_context_manager):
        user1 = CommCareUser.create(self.domain,
                                    uuid.uuid4().hex, 'abc', None, None)
        user2 = CommCareUser.create(self.domain,
                                    uuid.uuid4().hex, 'abc', None, None)
        user3 = CommCareUser.create(self.domain,
                                    uuid.uuid4().hex, 'abc', None, None)
        self.addCleanup(user1.delete, deleted_by=None)
        self.addCleanup(user2.delete, deleted_by=None)
        self.addCleanup(user3.delete, deleted_by=None)

        self.assertIsNone(user1.memoized_usercase)
        self.assertIsNone(Content.get_two_way_entry_or_phone_number(user1))

        with change_context_manager:
            with self.create_user_case(user2) as case:
                self.assertIsNotNone(user2.memoized_usercase)
                self.assertIsNone(
                    Content.get_two_way_entry_or_phone_number(user2))
                self.assertIsNone(
                    Content.get_two_way_entry_or_phone_number(case))

        with self.create_user_case(user3) as case:
            # If the user has no number, the user case's number is used
            with change_context_manager:
                update_case(self.domain,
                            case.case_id,
                            case_properties={
                                'contact_phone_number': '12345678',
                                'contact_phone_number_is_verified': '1'
                            })
            case = CaseAccessors(self.domain).get_case(case.case_id)
            self.assertPhoneEntryCount(1)
            self.assertPhoneEntryCount(1, only_count_two_way=True)
            self.assertIsNotNone(user3.memoized_usercase)
            self.assertTwoWayEntry(
                Content.get_two_way_entry_or_phone_number(user3), '12345678')

            # If the user has a number, it is used before the user case's number
            user3.add_phone_number('87654321')
            user3.save()
            PhoneNumber.objects.get(phone_number='87654321').set_two_way()
            self.assertPhoneEntryCount(2)
            self.assertPhoneEntryCount(2, only_count_two_way=True)
            self.assertTwoWayEntry(
                Content.get_two_way_entry_or_phone_number(user3), '87654321')

            # Referencing the case directly uses the case's phone number
            self.assertTwoWayEntry(
                Content.get_two_way_entry_or_phone_number(case), '12345678')
Exemple #9
0
    def test_get_translation_general_default(self):
        message_dict = {
            '*': 'non-translated message',
        }
        content = Content()
        content.set_context(schedule_instance=Mock(memoized_schedule=Schedule(domain=self.domain)))

        self.assertEqual(
            content.get_translation_from_message_dict(
                self.domain,
                message_dict,
                self.user.get_language_code()
            ),
            message_dict['*']
        )
Exemple #10
0
    def test_not_using_phone_entries(self):
        with override_settings(USE_PHONE_ENTRIES=False):
            user1 = CommCareUser.create(self.domain,
                                        uuid.uuid4().hex, 'abc', None, None)
            user2 = CommCareUser.create(self.domain,
                                        uuid.uuid4().hex, 'abc', None, None)
            user3 = CommCareUser.create(self.domain,
                                        uuid.uuid4().hex, 'abc', None, None)
            self.addCleanup(user1.delete, self.domain, deleted_by=None)
            self.addCleanup(user2.delete, self.domain, deleted_by=None)
            self.addCleanup(user3.delete, self.domain, deleted_by=None)

            self.assertIsNone(user1.memoized_usercase)
            self.assertIsNone(Content.get_two_way_entry_or_phone_number(user1))

            with self.create_usercase(user2) as case:
                self.assertIsNotNone(user2.memoized_usercase)
                self.assertIsNone(
                    Content.get_two_way_entry_or_phone_number(user2))
                self.assertIsNone(
                    Content.get_two_way_entry_or_phone_number(case))

            with self.create_usercase(user3) as case:
                # If the user has no number, the user case's number is used
                update_case(
                    self.domain,
                    case.case_id,
                    case_properties={'contact_phone_number': '12345678'})
                case = CommCareCase.objects.get_case(case.case_id, self.domain)
                self.assertPhoneEntryCount(0)
                self.assertIsNotNone(user3.memoized_usercase)
                self.assertEqual(
                    Content.get_two_way_entry_or_phone_number(user3),
                    '12345678')

                # If the user has a number, it is used before the user case's number
                user3.add_phone_number('87654321')
                user3.save()
                self.assertPhoneEntryCount(0)
                self.assertEqual(
                    Content.get_two_way_entry_or_phone_number(user3),
                    '87654321')

                # Referencing the case directly uses the case's phone number
                self.assertEqual(
                    Content.get_two_way_entry_or_phone_number(case),
                    '12345678')
    def test_phone_number_preference(self):
        user = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
        self.addCleanup(user.delete)

        user.add_phone_number('12345')
        user.add_phone_number('23456')
        user.add_phone_number('34567')
        user.save()

        self.assertPhoneEntryCount(3)
        self.assertPhoneEntryCount(0, only_count_two_way=True)
        self.assertEqual(Content.get_two_way_entry_or_phone_number(user), '12345')

        PhoneNumber.objects.get(phone_number='23456').set_two_way()
        self.assertPhoneEntryCount(3)
        self.assertPhoneEntryCount(1, only_count_two_way=True)
        self.assertTwoWayEntry(Content.get_two_way_entry_or_phone_number(user), '23456')
Exemple #12
0
    def test_phone_number_preference(self):
        user = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
        self.addCleanup(user.delete)

        user.add_phone_number('12345')
        user.add_phone_number('23456')
        user.add_phone_number('34567')
        user.save()

        self.assertPhoneEntryCount(3)
        self.assertPhoneEntryCount(0, only_count_two_way=True)
        self.assertEqual(Content.get_two_way_entry_or_phone_number(user), '12345')

        PhoneNumber.objects.get(phone_number='23456').set_two_way()
        self.assertPhoneEntryCount(3)
        self.assertPhoneEntryCount(1, only_count_two_way=True)
        self.assertTwoWayEntry(Content.get_two_way_entry_or_phone_number(user), '23456')
Exemple #13
0
    def test_get_translation_user_preferred(self):
        message_dict = {
            '*': 'non-translated message',
            'en': 'english message',
            'hin': 'hindi message',
            'es': 'spanish message',
        }
        content = Content()
        content.set_context(
            schedule_instance=Mock(memoized_schedule=Schedule(domain=self.domain, default_language_code='hin'))
        )

        self.assertEqual(
            content.get_translation_from_message_dict(
                self.domain,
                message_dict,
                self.user.get_language_code()
            ),
            message_dict['es']
        )
Exemple #14
0
    def test_get_translation_empty_message(self):
        message_dict = {}
        schedule = Schedule(domain=self.domain)

        self.assertEqual(
            Content.get_translation_from_message_dict(
                message_dict,
                schedule,
                self.user.get_language_code()
            ),
            ''
        )
Exemple #15
0
    def test_one_way_numbers(self):
        user1 = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
        user2 = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
        user3 = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
        self.addCleanup(user1.delete)
        self.addCleanup(user2.delete)
        self.addCleanup(user3.delete)

        self.assertIsNone(user1.memoized_usercase)
        self.assertIsNone(Content.get_two_way_entry_or_phone_number(user1))

        with self.create_user_case(user2) as case:
            self.assertIsNotNone(user2.memoized_usercase)
            self.assertIsNone(Content.get_two_way_entry_or_phone_number(user2))
            self.assertIsNone(Content.get_two_way_entry_or_phone_number(case))

        with self.create_user_case(user3) as case:
            # If the user has no number, the user case's number is used
            update_case(self.domain, case.case_id, case_properties={'contact_phone_number': '12345678'})
            case = CaseAccessors(self.domain).get_case(case.case_id)
            self.assertPhoneEntryCount(1)
            self.assertPhoneEntryCount(0, only_count_two_way=True)
            self.assertIsNotNone(user3.memoized_usercase)
            self.assertEqual(Content.get_two_way_entry_or_phone_number(user3), '12345678')

            # If the user has a number, it is used before the user case's number
            user3.add_phone_number('87654321')
            user3.save()
            self.assertPhoneEntryCount(2)
            self.assertPhoneEntryCount(0, only_count_two_way=True)
            self.assertEqual(Content.get_two_way_entry_or_phone_number(user3), '87654321')

            # Referencing the case directly uses the case's phone number
            self.assertEqual(Content.get_two_way_entry_or_phone_number(case), '12345678')
Exemple #16
0
    def test_get_translation_general_default(self):
        message_dict = {
            '*': 'non-translated message',
        }
        schedule = Schedule(domain=self.domain)

        self.assertEqual(
            Content.get_translation_from_message_dict(
                message_dict,
                schedule,
                self.user.get_language_code()
            ),
            message_dict['*']
        )
Exemple #17
0
    def test_get_translation_schedule_default(self):
        message_dict = {
            '*': 'non-translated message',
            'en': 'english message',
            'hin': 'hindi message',
        }
        schedule = Schedule(domain=self.domain, default_language_code='hin')

        self.assertEqual(
            Content.get_translation_from_message_dict(
                message_dict,
                schedule,
                self.user.get_language_code()
            ),
            message_dict['hin']
        )
Exemple #18
0
    def test_ignoring_entries(self):
        with create_case(self.domain, 'person') as case:
            update_case(self.domain, case.case_id,
                case_properties={'contact_phone_number': '12345', 'contact_phone_number_is_verified': '1'})

            self.assertPhoneEntryCount(1)
            self.assertPhoneEntryCount(1, only_count_two_way=True)

            with patch('corehq.apps.sms.tasks.use_phone_entries') as patch1, \
                    patch('corehq.messaging.tasks.use_phone_entries') as patch2, \
                    patch('corehq.messaging.scheduling.models.abstract.use_phone_entries') as patch3:

                patch1.return_value = patch2.return_value = patch3.return_value = False
                update_case(self.domain, case.case_id, case_properties={'contact_phone_number': '23456'})
                case = CaseAccessors(self.domain).get_case(case.case_id)

                self.assertPhoneEntryCount(1)
                self.assertPhoneEntryCount(1, only_count_two_way=True)
                self.assertTwoWayEntry(PhoneNumber.objects.get(owner_id=case.case_id), '12345')
                self.assertEqual(Content.get_two_way_entry_or_phone_number(case), '23456')
Exemple #19
0
    def test_not_using_phone_entries(self):
        with patch('corehq.apps.sms.tasks.use_phone_entries') as patch1, \
                patch('corehq.messaging.tasks.use_phone_entries') as patch2, \
                patch('corehq.messaging.scheduling.models.abstract.use_phone_entries') as patch3:

            patch1.return_value = patch2.return_value = patch3.return_value = False

            user1 = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
            user2 = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
            user3 = CommCareUser.create(self.domain, uuid.uuid4().hex, 'abc')
            self.addCleanup(user1.delete)
            self.addCleanup(user2.delete)
            self.addCleanup(user3.delete)

            self.assertIsNone(user1.memoized_usercase)
            self.assertIsNone(Content.get_two_way_entry_or_phone_number(user1))

            with self.create_user_case(user2) as case:
                self.assertIsNotNone(user2.memoized_usercase)
                self.assertIsNone(Content.get_two_way_entry_or_phone_number(user2))
                self.assertIsNone(Content.get_two_way_entry_or_phone_number(case))

            with self.create_user_case(user3) as case:
                # If the user has no number, the user case's number is used
                update_case(self.domain, case.case_id, case_properties={'contact_phone_number': '12345678'})
                case = CaseAccessors(self.domain).get_case(case.case_id)
                self.assertPhoneEntryCount(0)
                self.assertIsNotNone(user3.memoized_usercase)
                self.assertEqual(Content.get_two_way_entry_or_phone_number(user3), '12345678')

                # If the user has a number, it is used before the user case's number
                user3.add_phone_number('87654321')
                user3.save()
                self.assertPhoneEntryCount(0)
                self.assertEqual(Content.get_two_way_entry_or_phone_number(user3), '87654321')

                # Referencing the case directly uses the case's phone number
                self.assertEqual(Content.get_two_way_entry_or_phone_number(case), '12345678')
Exemple #20
0
    def test_sms_language_fallback(self):
        message_dict = {
            '*': 'non-translated message',
            'en': 'english message',            # project default
            'hin': 'hindi message',             # schedule default
            'es': 'spanish message',            # user's preferred language
        }
        user_lang = self.user.get_language_code()
        content = Content()
        content.set_context(
            schedule_instance=Mock(memoized_schedule=Schedule(domain=self.domain, default_language_code='hin'))
        )

        self.domain_obj.sms_language_fallback = LANGUAGE_FALLBACK_NONE
        self.assertEqual(
            content.get_translation_from_message_dict(self.domain_obj, message_dict, user_lang),
            message_dict['es']
        )
        message_dict.pop('es')
        self.assertEqual(
            content.get_translation_from_message_dict(self.domain_obj, message_dict, user_lang),
            ''
        )

        self.domain_obj.sms_language_fallback = LANGUAGE_FALLBACK_SCHEDULE
        self.assertEqual(
            content.get_translation_from_message_dict(self.domain_obj, message_dict, user_lang),
            message_dict['hin']
        )
        message_dict.pop('hin')
        self.assertEqual(
            content.get_translation_from_message_dict(self.domain_obj, message_dict, user_lang),
            ''
        )

        self.domain_obj.sms_language_fallback = LANGUAGE_FALLBACK_DOMAIN
        self.assertEqual(
            content.get_translation_from_message_dict(self.domain_obj, message_dict, user_lang),
            message_dict['en']
        )
        message_dict.pop('en')
        self.assertEqual(
            content.get_translation_from_message_dict(self.domain_obj, message_dict, user_lang),
            ''
        )

        self.domain_obj.sms_language_fallback = LANGUAGE_FALLBACK_UNTRANSLATED
        self.assertEqual(
            content.get_translation_from_message_dict(self.domain_obj, message_dict, user_lang),
            message_dict['*']
        )

        # Default: same as LANGUAGE_FALLBACK_UNTRANSLATED
        self.domain_obj.sms_language_fallback = None
        self.assertEqual(
            content.get_translation_from_message_dict(self.domain_obj, message_dict, user_lang),
            message_dict['*']
        )