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_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')
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')
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')
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')
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_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')
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')