def test_last_submitting_user_recipient(self): with create_test_case(self.domain, 'person', 'Joe', user_id=self.mobile_user.get_id) as case: instance = CaseTimedScheduleInstance( domain=self.domain, case_id=case.case_id, recipient_type=CaseScheduleInstanceMixin. RECIPIENT_TYPE_LAST_SUBMITTING_USER) self.assertTrue(isinstance(instance.recipient, CommCareUser)) self.assertEqual(instance.recipient.get_id, self.mobile_user.get_id) with create_test_case(self.domain, 'person', 'Joe', user_id=self.web_user.get_id) as case: instance = CaseTimedScheduleInstance( domain=self.domain, case_id=case.case_id, recipient_type=CaseScheduleInstanceMixin. RECIPIENT_TYPE_LAST_SUBMITTING_USER) self.assertTrue(isinstance(instance.recipient, WebUser)) self.assertEqual(instance.recipient.get_id, self.web_user.get_id) with create_test_case(self.domain, 'person', 'Joe', user_id='system') as case: instance = CaseTimedScheduleInstance( domain=self.domain, case_id=case.case_id, recipient_type=CaseScheduleInstanceMixin. RECIPIENT_TYPE_LAST_SUBMITTING_USER) self.assertIsNone(instance.recipient)
def test_phone_number_already_in_use(self): self.assertEqual(PhoneNumber.count_by_domain(self.domain), 0) with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case1, \ create_test_case(self.domain, 'participant', 'test2', drop_signals=False) as case2: case1 = self.set_case_property(case1, 'contact_phone_number', '99987658765') case1 = self.set_case_property(case1, 'contact_phone_number_is_verified', '1') case2 = self.set_case_property(case2, 'contact_phone_number', '99987698769') case2 = self.set_case_property(case2, 'contact_phone_number_is_verified', '1') self.assertIsNotNone(self.get_case_phone_number(case1)) self.assertIsNotNone(self.get_case_phone_number(case2)) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2) case2 = self.set_case_property(case2, 'contact_phone_number', '99987658765') self.assertIsNotNone(self.get_case_phone_number(case1)) self.assertIsNotNone(self.get_case_phone_number(case2)) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2) self.assertPhoneNumberDetails(case1, '99987658765', None, None, True, False, True) self.assertPhoneNumberDetails(case2, '99987658765', None, None, False, False, False)
def test_host_case_owner_location(self): with create_test_case(self.domain, 'test-extension-case', 'name') as extension_case: with create_test_case(self.domain, 'test-host-case', 'name') as host_case: update_case(self.domain, host_case.case_id, case_properties={ 'owner_id': self.city_location.location_id }) set_parent_case(self.domain, extension_case, host_case, relationship='extension') # Test the recipient is returned correctly instance = CaseTimedScheduleInstance( domain=self.domain, case_id=extension_case.case_id, recipient_type=CaseScheduleInstanceMixin. RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION', ) self.assertIsInstance(instance.recipient, SQLLocation) self.assertEqual(instance.recipient.location_id, self.city_location.location_id) # Test location that does not exist update_case(self.domain, host_case.case_id, case_properties={'owner_id': 'does-not-exist'}) instance = CaseTimedScheduleInstance( domain=self.domain, case_id=extension_case.case_id, recipient_type=CaseScheduleInstanceMixin. RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION', ) self.assertIsNone(instance.recipient) # Test on a case that is not an extension case instance = CaseTimedScheduleInstance( domain=self.domain, case_id=host_case.case_id, recipient_type=CaseScheduleInstanceMixin. RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION', ) self.assertIsNone(instance.recipient) # Test with case id that doesn't exist instance = CaseTimedScheduleInstance( domain=self.domain, case_id='does-not-exist', recipient_type=CaseScheduleInstanceMixin. RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION', ) self.assertIsNone(instance.recipient)
def test_host_case_owner_location_parent(self): with create_test_case(self.domain, 'test-extension-case', 'name') as extension_case: with create_test_case(self.domain, 'test-host-case', 'name') as host_case: update_case(self.domain, host_case.case_id, case_properties={'owner_id': self.city_location.location_id}) set_parent_case(self.domain, extension_case, host_case, relationship='extension') # Test the recipient is returned correctly instance = CaseTimedScheduleInstance( domain=self.domain, case_id=extension_case.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION_PARENT', ) self.assertIsInstance(instance.recipient, SQLLocation) self.assertEqual(instance.recipient.location_id, self.state_location.location_id) # Test no parent location update_case(self.domain, host_case.case_id, case_properties={'owner_id': self.country_location.location_id}) instance = CaseTimedScheduleInstance( domain=self.domain, case_id=extension_case.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION_PARENT', ) self.assertIsNone(instance.recipient) # Test location that does not exist update_case(self.domain, host_case.case_id, case_properties={'owner_id': 'does-not-exist'}) instance = CaseTimedScheduleInstance( domain=self.domain, case_id=extension_case.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION_PARENT', ) self.assertIsNone(instance.recipient) # Test on a case that is not an extension case instance = CaseTimedScheduleInstance( domain=self.domain, case_id=host_case.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION_PARENT', ) self.assertIsNone(instance.recipient) # Test with case id that doesn't exist instance = CaseTimedScheduleInstance( domain=self.domain, case_id='does-not-exist', recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION_PARENT', ) self.assertIsNone(instance.recipient)
def test_parent_case_property_criteria(self): reminder = (CaseReminderHandler .create(self.domain, 'test') .set_case_criteria_start_condition('participant', 'parent/status', MATCH_EXACT, 'green') .set_case_criteria_start_date() .set_case_recipient() .set_sms_content_type('en') .set_daily_schedule(fire_time=time(12, 0), message={'en': 'Hello {case.name}, your test result was normal.'}) .set_stop_condition(max_iteration_count=REPEAT_SCHEDULE_INDEFINITELY) .set_advanced_options()) reminder.active = False reminder.save() self.assertEqual(self.get_reminders(), []) with create_test_case(self.domain, 'participant', 'bob', drop_signals=False) as child_case, \ create_test_case(self.domain, 'parent-case', 'jim', drop_signals=False) as parent_case, \ patch('corehq.apps.reminders.models.CaseReminderHandler.get_now') as now_mock: process_handlers_for_case_changed(self.domain, child_case.case_id, [reminder._id]) self.assertEqual(self.get_reminders(), []) set_parent_case(self.domain, child_case, parent_case) process_handlers_for_case_changed(self.domain, child_case.case_id, [reminder._id]) self.assertEqual(self.get_reminders(), []) now_mock.return_value = datetime(2016, 1, 1, 10, 0) update_case(self.domain, child_case.case_id, case_properties={'status': 'green'}) self.assertEqual(self.get_reminders(), []) update_case(self.domain, child_case.case_id, case_properties={'status': ''}) self.assertEqual(self.get_reminders(), []) update_case(self.domain, parent_case.case_id, case_properties={'status': 'green'}) process_handlers_for_case_changed(self.domain, child_case.case_id, [reminder._id]) reminder_instance = self.assertOneReminder() self.assertEqual(reminder_instance.domain, self.domain) self.assertEqual(reminder_instance.handler_id, reminder.get_id) self.assertFalse(reminder_instance.active) self.assertEqual(reminder_instance.start_date, date(2016, 1, 1)) self.assertIsNone(reminder_instance.start_condition_datetime) self.assertEqual(reminder_instance.next_fire, datetime(2016, 1, 1, 12, 0)) self.assertEqual(reminder_instance.case_id, child_case.case_id) self.assertEqual(reminder_instance.schedule_iteration_num, 1) self.assertEqual(reminder_instance.current_event_sequence_num, 0) self.assertEqual(reminder_instance.callback_try_count, 0) update_case(self.domain, parent_case.case_id, case_properties={'status': 'red'}) process_handlers_for_case_changed(self.domain, child_case.case_id, [reminder._id]) self.assertEqual(self.get_reminders(), [])
def test_modified_by_template_params(self): with self.create_parent_case(modified_by=self.mobile_user, owner=self.location) as case: expected_result = {'case': case.to_json()} expected_result['case'][ 'owner'] = self.get_expected_template_params_for_location() expected_result['case'][ 'last_modified_by'] = self.get_expected_template_params_for_mobile( ) self.assertEqual(get_message_template_params(case), expected_result) with self.create_parent_case(modified_by=self.web_user, owner=self.location) as case: expected_result = {'case': case.to_json()} expected_result['case'][ 'owner'] = self.get_expected_template_params_for_location() expected_result['case'][ 'last_modified_by'] = self.get_expected_template_params_for_web( ) self.assertEqual(get_message_template_params(case), expected_result) with create_test_case(self.domain, 'person', 'Joe', owner_id=self.location.location_id, user_id='system') as case: expected_result = {'case': case.to_json()} expected_result['case'][ 'owner'] = self.get_expected_template_params_for_location() expected_result['case'][ 'last_modified_by'] = _get_system_user_template_info() self.assertEqual(get_message_template_params(case), expected_result)
def test_phone_number_already_in_use(self): with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case1, \ create_test_case(self.domain, 'participant', 'test2', drop_signals=False) as case2: case1 = self.set_case_property(case1, 'contact_phone_number', '99987658765') case1 = self.set_case_property(case1, 'contact_phone_number_is_verified', '1') case2 = self.set_case_property(case2, 'contact_phone_number', '99987698769') case2 = self.set_case_property(case2, 'contact_phone_number_is_verified', '1') self.assertIsNotNone(self.get_case_verified_number(case1)) self.assertIsNotNone(self.get_case_verified_number(case2)) case2 = self.set_case_property(case2, 'contact_phone_number', '99987658765') self.assertIsNotNone(self.get_case_verified_number(case1)) self.assertIsNone(self.get_case_verified_number(case2))
def test_case_phone_number_updates(self): with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case: self.assertIsNone(self.get_case_verified_number(case)) case = self.set_case_property(case, 'contact_phone_number', '99987658765') self.assertIsNone(self.get_case_verified_number(case)) case = self.set_case_property(case, 'contact_phone_number_is_verified', '1') self.assertPhoneNumberDetails(case, '99987658765', None, None) pk = self.get_case_verified_number(case).pk case = self.set_case_property(case, 'contact_phone_number', '99987698769') self.assertPhoneNumberDetails(case, '99987698769', None, None, pk=pk) case = self.set_case_property(case, 'contact_backend_id', 'sms-backend') self.assertPhoneNumberDetails(case, '99987698769', 'sms-backend', None, pk=pk) case = self.set_case_property(case, 'contact_ivr_backend_id', 'ivr-backend') self.assertPhoneNumberDetails(case, '99987698769', 'sms-backend', 'ivr-backend', pk=pk) # If nothing changes, the phone entry should not be saved with patch('corehq.apps.sms.models.PhoneNumber.save') as mock_save: case = self.set_case_property(case, 'abc', 'def') mock_save.assert_not_called() # If phone entry is ahead of the case in terms of contact_last_modified, no update should happen v = self.get_case_verified_number(case) v.contact_last_modified += timedelta(days=1) v.save() with patch('corehq.apps.sms.models.PhoneNumber.save') as mock_save: case = self.set_case_property(case, 'contact_phone_number', '99912341234') mock_save.assert_not_called()
def test_case_owner(self): with create_test_case(self.domain, 'participant', 'test') as case: number = PhoneNumber(owner_doc_type='CommCareCase', owner_id=case.case_id) owner = number.owner self.assertTrue(is_commcarecase(owner)) self.assertEqual(owner.case_id, case.case_id)
def test_correct_message(self): self._setup_fixture_type() with create_test_case(self.domain_name, self.case_type, 'test-case', {'risk_profile': 'risk1'}) as case: self.assertEqual( ucla_message_bank_content(self._reminder(), self._handler(), case), 'message1')
def test_passing_case_without_risk_profile_new(self): self._setup_fixture_type() with create_test_case(self.domain_name, self.case_type, 'test-case') as case: with self.assertRaises(AssertionError) as e: self._get_current_event_messages(self._schedule_instance(case)) self.assertIn('does not include risk_profile', str(e.exception))
def test_correct_message_new(self): self._setup_fixture_type() with create_test_case(self.domain_name, self.case_type, 'test-case', {'risk_profile': 'risk1'}) as case: self.assertEqual( self._get_current_event_messages(self._schedule_instance(case)), ['message1'] )
def test_case_type_mismatch(self): reminder = (CaseReminderHandler .create(self.domain, 'test') .set_case_criteria_start_condition('participant', 'status', MATCH_EXACT, 'green') .set_case_criteria_start_date() .set_case_recipient() .set_sms_content_type('en') .set_daily_schedule(fire_time=time(12, 0), message={'en': 'Hello {case.name}, your test result was normal.'}) .set_stop_condition(max_iteration_count=REPEAT_SCHEDULE_INDEFINITELY) .set_advanced_options()) reminder.save() self.assertEqual(self.get_reminders(), []) with create_test_case(self.domain, 'ex-participant', 'bob', drop_signals=False) as case, \ patch('corehq.apps.reminders.models.CaseReminderHandler.get_now') as now_mock: self.assertEqual(self.get_reminders(), []) now_mock.return_value = datetime(2016, 1, 1, 10, 0) update_case(self.domain, case.case_id, case_properties={'status': 'green'}) self.assertEqual(self.get_reminders(), []) reminder.case_changed(case) self.assertEqual(self.get_reminders(), [])
def test_recipient_case_owner_location_parent(self): with create_test_case(self.domain, 'test-case', 'test-name', owner_id=self.user.get_id) as case: self.assertEqual(case.owner_id, self.user.get_id) def instance(case_id=''): # recipient is memoized return CaseTimedScheduleInstance( domain=self.domain, case_id=case_id or case.case_id, recipient_type='CustomRecipient', recipient_id='CASE_OWNER_LOCATION_PARENT') # Test the recipient is returned correctly self.assertTrue(isinstance(instance().recipient, SQLLocation)) self.assertEqual(instance().recipient.pk, self.parent_location.pk) # Test when the user's location has no parent location self.user.set_location(self.parent_location) self.assertIsNone(instance().recipient) # Remove child location self.user.unset_location() self.assertIsNone(instance().recipient) # Remove case self.assertIsNone(instance(case_id='does-not-exist').recipient)
def test_case_time_zone(self): reminder = (CaseReminderHandler .create(self.domain, 'test') .set_case_criteria_start_condition('participant', 'status', MATCH_EXACT, 'green') .set_case_criteria_start_date() .set_case_recipient() .set_sms_content_type('en') .set_daily_schedule(fire_time=time(12, 0), message={'en': 'Hello {case.name}, your test result was normal.'}) .set_stop_condition(max_iteration_count=REPEAT_SCHEDULE_INDEFINITELY) .set_advanced_options()) reminder.save() self.assertEqual(self.get_reminders(), []) with create_test_case(self.domain, 'participant', 'bob', drop_signals=False) as case, \ patch('corehq.apps.reminders.models.CaseReminderHandler.get_now') as now_mock: self.assertEqual(self.get_reminders(), []) now_mock.return_value = datetime(2016, 1, 1, 10, 0) update_case(self.domain, case.case_id, case_properties={'status': 'green', 'time_zone': 'America/New_York'}) reminder_instance = self.assertOneReminder() self.assertEqual(reminder_instance.domain, self.domain) self.assertEqual(reminder_instance.handler_id, reminder.get_id) self.assertTrue(reminder_instance.active) self.assertEqual(reminder_instance.start_date, date(2016, 1, 1)) self.assertIsNone(reminder_instance.start_condition_datetime) self.assertEqual(reminder_instance.next_fire, datetime(2016, 1, 1, 17, 0)) self.assertEqual(reminder_instance.case_id, case.case_id) self.assertEqual(reminder_instance.schedule_iteration_num, 1) self.assertEqual(reminder_instance.current_event_sequence_num, 0) self.assertEqual(reminder_instance.callback_try_count, 0)
def test_no_relevant_message_invalid_risk(self): self._setup_fixture_type() with create_test_case(self.domain_name, self.case_type, 'test-case', {'risk_profile': 'risk2'}) as case: self.assertRaises(AssertionError, general_health_message_bank_content, self._reminder(), self._handler(), case)
def test_passing_case_without_risk_profile(self): self._setup_fixture_type() with create_test_case(self.domain_name, self.case_type, 'test-case') as case: self.assertRaises(AssertionError, general_health_message_bank_content, self._reminder(), self._handler(), case)
def test_no_relevant_message_invalid_seq_num_new(self): self._setup_fixture_type() with create_test_case(self.domain_name, self.case_type, 'test-case', {'risk_profile': 'risk1'}) as case: with self.assertRaises(AssertionError) as e: self._get_current_event_messages(self._schedule_instance(case, iteration_num=2)) self.assertIn('No message for case', str(e.exception))
def test_multiple_entries(self): extra_number = PhoneNumber.objects.create(domain=self.domain, owner_doc_type='X', owner_id='X', phone_number='999124', verified=False, pending_verification=False, is_two_way=False) with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case: case = self.set_case_property(case, 'contact_phone_number', '999124') case = self.set_case_property(case, 'contact_phone_number_is_verified', '1') case.create_phone_entry('999125') self.assertEqual(PhoneNumber.objects.count(), 3) sync_case_phone_number(case) self.assertEqual(PhoneNumber.objects.count(), 2) number1 = PhoneNumber.objects.get(pk=extra_number.pk) self.assertEqual(number1.owner_id, 'X') number2 = PhoneNumber.objects.get(owner_id=case.case_id) self.assertTrue(number2.verified) self.assertTrue(number2.is_two_way) self.assertFalse(number2.pending_verification)
def test_case_soft_delete(self): extra_number = PhoneNumber.objects.create(domain=self.domain, owner_doc_type='X', owner_id='X', phone_number='999123', verified=True, pending_verification=False, is_two_way=True) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 1) with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case: case = self.set_case_property(case, 'contact_phone_number', '99987658765') case = self.set_case_property(case, 'contact_phone_number_is_verified', '1') self.assertIsNotNone(self.get_case_phone_number(case)) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2) tag_cases_as_deleted_and_remove_indices(self.domain, [case.case_id], '123', datetime.utcnow()) self.assertIsNone(self.get_case_phone_number(case)) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 1)
def test_is_contact_active_for_case(self): with create_test_case(self.domain, 'contact', 'test-case') as case: self.assertTrue( is_contact_active(self.domain, 'CommCareCase', case.case_id)) update_case(self.domain, case.case_id, close=True) self.assertFalse( is_contact_active(self.domain, 'CommCareCase', case.case_id))
def test_no_relevant_message_invalid_seq_num(self): self._setup_fixture_type() with create_test_case(self.domain_name, self.case_type, 'test-case', {'risk_profile': 'risk1'}) as case: self.assertRaises(AssertionError, ucla_message_bank_content, self._reminder(iteration_num=2), self._handler(), case)
def test_invalid_phone_format(self): extra_number = PhoneNumber.objects.create(domain=self.domain, owner_doc_type='X', owner_id='X', phone_number='999123', verified=True, pending_verification=False, is_two_way=True) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 1) with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case: case = self.set_case_property(case, 'contact_phone_number', '99987658765') case = self.set_case_property(case, 'contact_phone_number_is_verified', '1') self.assertIsNotNone(self.get_case_phone_number(case)) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2) case = self.set_case_property(case, 'contact_phone_number', 'xyz') self.assertIsNone(self.get_case_phone_number(case)) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 1)
def test_modified_by_template_params(self): r = MessagingTemplateRenderer() with self.create_parent_case(modified_by=self.mobile_user) as case: r.set_context_param('case', CaseMessagingTemplateParam(case)) self.assertEqual(r.render("Name: {case.last_modified_by.name}"), "Name: mobile1") self.assertEqual(r.render("First Name: {case.last_modified_by.first_name}"), "First Name: Mobile") self.assertEqual(r.render("Last Name: {case.last_modified_by.last_name}"), "Last Name: User") self.assertEqual(r.render("Phone: {case.last_modified_by.phone_number}"), "Phone: 999123") self.assertEqual(r.render("Unknown: {case.last_modified_by.unknown}"), "Unknown: (?)") self.assertEqual(r.render("Unknown: {case.last_modified_by.unknown.unknown}"), "Unknown: (?)") with self.create_parent_case(modified_by=self.web_user) as case: r.set_context_param('case', CaseMessagingTemplateParam(case)) self.assertEqual(r.render("Name: {case.last_modified_by.name}"), "Name: web1@templating-test") self.assertEqual(r.render("First Name: {case.last_modified_by.first_name}"), "First Name: Web") self.assertEqual(r.render("Last Name: {case.last_modified_by.last_name}"), "Last Name: User") self.assertEqual(r.render("Phone: {case.last_modified_by.phone_number}"), "Phone: 999456") self.assertEqual(r.render("Unknown: {case.last_modified_by.unknown}"), "Unknown: (?)") self.assertEqual(r.render("Unknown: {case.last_modified_by.unknown.unknown}"), "Unknown: (?)") with create_test_case(self.domain, 'person', 'Joe', user_id='system') as case: r.set_context_param('case', CaseMessagingTemplateParam(case)) self.assertEqual(r.render("Name: {case.last_modified_by.name}"), "Name: System") self.assertEqual(r.render("First Name: {case.last_modified_by.first_name}"), "First Name: System") self.assertEqual(r.render("Last Name: {case.last_modified_by.last_name}"), "Last Name: ") self.assertEqual(r.render("Phone: {case.last_modified_by.phone_number}"), "Phone: ") self.assertEqual(r.render("Unknown: {case.last_modified_by.unknown}"), "Unknown: (?)") self.assertEqual(r.render("Unknown: {case.last_modified_by.unknown.unknown}"), "Unknown: (?)")
def test_multiple_relevant_messages(self): self._setup_fixture_type() self._setup_data_item('risk1', '1', 'message2') with create_test_case(self.domain_name, self.case_type, 'test-case', {'risk_profile': 'risk1'}) as case: self.assertRaises(AssertionError, ucla_message_bank_content, self._reminder(), self._handler(), case)
def test_recipient_case_owner_location_parent(self): with create_test_case(self.domain, 'test-case', 'test-name', owner_id=self.user.get_id) as case: self.assertEqual(case.owner_id, self.user.get_id) def instance(case_id=''): # recipient is memoized return CaseTimedScheduleInstance( domain=self.domain, case_id=case_id or case.case_id, recipient_type='CustomRecipient', recipient_id='CASE_OWNER_LOCATION_PARENT' ) # Test the recipient is returned correctly self.assertTrue(isinstance(instance().recipient, SQLLocation)) self.assertEqual(instance().recipient.pk, self.parent_location.pk) # Test when the user's location has no parent location self.user.set_location(self.parent_location) self.assertIsNone(instance().recipient) # Remove child location self.user.unset_location() self.assertIsNone(instance().recipient) # Remove case self.assertIsNone(instance(case_id='does-not-exist').recipient)
def test_user_recipient(self): reminder = (CaseReminderHandler .create(self.domain, 'test') .set_case_criteria_start_condition('participant', 'status', MATCH_EXACT, 'green') .set_case_criteria_start_date() .set_last_submitting_user_recipient() .set_sms_content_type('en') .set_daily_schedule(fire_time=time(12, 0), message={'en': "{case.name}'s test result was normal."}) .set_stop_condition(max_iteration_count=REPEAT_SCHEDULE_INDEFINITELY) .set_advanced_options()) reminder.save() self.assertEqual(self.get_reminders(), []) with create_test_case(self.domain, 'participant', 'bob', drop_signals=False) as case, \ patch('corehq.apps.reminders.models.CaseReminderHandler.get_now') as now_mock: self.assertEqual(self.get_reminders(), []) now_mock.return_value = datetime(2016, 1, 1, 10, 0) # There should still be no reminder instance since this is submitted by the system user update_case(self.domain, case.case_id, case_properties={'status': 'green'}) self.assertEqual(self.get_reminders(), []) # Update the user id on the case CaseFactory(self.domain).create_or_update_case( CaseStructure( case_id=case.case_id, attrs={'user_id': self.user.get_id} ) ) reminder_instance = self.assertOneReminder()
def create_case(self): case_properties = { 'contact_phone_number': self.phone_number, 'contact_phone_number_is_verified': '1', } return create_test_case(self.domain, 'contact', 'test', case_properties=case_properties, drop_signals=False)
def test_case_property_reminder_time(self): reminder = (CaseReminderHandler .create(self.domain, 'test') .set_case_criteria_start_condition('participant', 'status', MATCH_EXACT, 'green') .set_case_criteria_start_date() .set_case_recipient() .set_sms_content_type('en') .set_daily_schedule(fire_time='preferred_time', fire_time_type=FIRE_TIME_CASE_PROPERTY, message={'en': 'Hello {case.name}, your test result was normal.'}) .set_stop_condition(max_iteration_count=REPEAT_SCHEDULE_INDEFINITELY) .set_advanced_options()) reminder.save() self.assertEqual(self.get_reminders(), []) with create_test_case(self.domain, 'participant', 'bob', drop_signals=False) as case, \ patch('corehq.apps.reminders.models.CaseReminderHandler.get_now') as now_mock: self.assertEqual(self.get_reminders(), []) now_mock.return_value = datetime(2016, 1, 1, 10, 0) update_case(self.domain, case.case_id, case_properties={'status': 'green', 'preferred_time': '14:00:00'}) reminder_instance = self.assertOneReminder() self.assertEqual(reminder_instance.domain, self.domain) self.assertEqual(reminder_instance.handler_id, reminder.get_id) self.assertTrue(reminder_instance.active) self.assertEqual(reminder_instance.start_date, date(2016, 1, 1)) self.assertIsNone(reminder_instance.start_condition_datetime) self.assertEqual(reminder_instance.next_fire, datetime(2016, 1, 1, 14, 0)) self.assertEqual(reminder_instance.case_id, case.case_id) self.assertEqual(reminder_instance.schedule_iteration_num, 1) self.assertEqual(reminder_instance.current_event_sequence_num, 0) self.assertEqual(reminder_instance.callback_try_count, 0)
def test_multiple_entries(self): extra_number = PhoneNumber.objects.create( domain=self.domain, owner_doc_type='X', owner_id='X', phone_number='999124', verified=False, pending_verification=False, is_two_way=False ) with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case: case = self.set_case_property(case, 'contact_phone_number', '999124') case = self.set_case_property(case, 'contact_phone_number_is_verified', '1') case.create_phone_entry('999125') self.assertEqual(PhoneNumber.objects.count(), 3) sync_case_phone_number(case) self.assertEqual(PhoneNumber.objects.count(), 2) number1 = PhoneNumber.objects.get(pk=extra_number.pk) self.assertEqual(number1.owner_id, 'X') number2 = PhoneNumber.objects.get(owner_id=case.case_id) self.assertTrue(number2.verified) self.assertTrue(number2.is_two_way) self.assertFalse(number2.pending_verification)
def test_case_soft_delete(self): with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case: case = self.set_case_property(case, 'contact_phone_number', '99987658765') case = self.set_case_property(case, 'contact_phone_number_is_verified', '1') self.assertIsNotNone(self.get_case_verified_number(case)) tag_cases_as_deleted_and_remove_indices(self.domain, [case.case_id], '123', datetime.utcnow()) self.assertIsNone(self.get_case_verified_number(case))
def test_invalid_phone_format(self): with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case: case = self.set_case_property(case, 'contact_phone_number', '99987658765') case = self.set_case_property(case, 'contact_phone_number_is_verified', '1') self.assertIsNotNone(self.get_case_verified_number(case)) case = self.set_case_property(case, 'contact_phone_number', 'xyz') self.assertIsNone(self.get_case_verified_number(case))
def test_get_contact_for_case(self): with create_test_case(self.domain, "contact", "test-case") as case: contact = get_contact(self.domain, case.case_id) self.assertEqual(contact.case_id, case.case_id) self.assertTrue(is_commcarecase(contact)) with self.assertRaises(ContactNotFoundException): get_contact(self.domain + "x", case.case_id)
def test_get_contact_for_case(self): with create_test_case(self.domain, 'contact', 'test-case') as case: contact = get_contact(self.domain, case.case_id) self.assertEqual(contact.case_id, case.case_id) self.assertTrue(is_commcarecase(contact)) with self.assertRaises(ContactNotFoundException): get_contact(self.domain + 'x', case.case_id)
def test_multiple_relevant_messages_new(self): self._setup_fixture_type() self._setup_data_item('risk1', '1', 'message2') with create_test_case(self.domain_name, self.case_type, 'test-case', {'risk_profile': 'risk1'}) as case: with self.assertRaises(AssertionError) as e: self._get_current_event_messages(self._schedule_instance(case)) self.assertIn('Multiple messages for case', str(e.exception))
def test_close_case(self): with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case: case = self.set_case_property(case, 'contact_phone_number', '99987658765') case = self.set_case_property(case, 'contact_phone_number_is_verified', '1') self.assertIsNotNone(self.get_case_verified_number(case)) update_case(self.domain, case.case_id, close=True) self.assertIsNone(self.get_case_verified_number(case))
def test_last_submitting_user_recipient(self): with create_test_case(self.domain, 'person', 'Joe', user_id=self.mobile_user.get_id) as case: instance = CaseTimedScheduleInstance(domain=self.domain, case_id=case.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_LAST_SUBMITTING_USER) self.assertTrue(isinstance(instance.recipient, CommCareUser)) self.assertEqual(instance.recipient.get_id, self.mobile_user.get_id) with create_test_case(self.domain, 'person', 'Joe', user_id=self.web_user.get_id) as case: instance = CaseTimedScheduleInstance(domain=self.domain, case_id=case.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_LAST_SUBMITTING_USER) self.assertTrue(isinstance(instance.recipient, WebUser)) self.assertEqual(instance.recipient.get_id, self.web_user.get_id) with create_test_case(self.domain, 'person', 'Joe', user_id='system') as case: instance = CaseTimedScheduleInstance(domain=self.domain, case_id=case.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_LAST_SUBMITTING_USER) self.assertIsNone(instance.recipient)
def test_calculate_start_date_with_today_option(self): now = datetime.utcnow() with create_test_case(self.domain, 'contact', 'test-case') as case: reminder = CaseReminderHandler( domain=self.domain, use_today_if_start_date_is_blank=True) self.assertEqual( reminder.get_case_criteria_reminder_start_date_info(case, now), (now, True, True)) reminder.start_date = 'start_date_case_property' self.assertEqual( reminder.get_case_criteria_reminder_start_date_info(case, now), (now, True, True)) update_case(self.domain, case.case_id, {'start_date_case_property': ''}) case = CaseAccessors(self.domain).get_case(case.case_id) self.assertEqual( reminder.get_case_criteria_reminder_start_date_info(case, now), (now, True, True)) update_case(self.domain, case.case_id, {'start_date_case_property': ' '}) case = CaseAccessors(self.domain).get_case(case.case_id) self.assertEqual( reminder.get_case_criteria_reminder_start_date_info(case, now), (now, True, True)) update_case(self.domain, case.case_id, {'start_date_case_property': 'abcdefg'}) case = CaseAccessors(self.domain).get_case(case.case_id) self.assertEqual( reminder.get_case_criteria_reminder_start_date_info(case, now), (now, True, True)) update_case(self.domain, case.case_id, {'start_date_case_property': '2016-01-32'}) case = CaseAccessors(self.domain).get_case(case.case_id) self.assertEqual( reminder.get_case_criteria_reminder_start_date_info(case, now), (now, True, True)) update_case(self.domain, case.case_id, {'start_date_case_property': '2016-01-10'}) case = CaseAccessors(self.domain).get_case(case.case_id) self.assertEqual( reminder.get_case_criteria_reminder_start_date_info(case, now), (datetime(2016, 1, 10), True, False)) update_case(self.domain, case.case_id, {'start_date_case_property': '2016-01-12T00:00:00Z'}) case = CaseAccessors(self.domain).get_case(case.case_id) self.assertEqual( reminder.get_case_criteria_reminder_start_date_info(case, now), (datetime(2016, 1, 12), True, False))
def create_parent_case(self, owner=None, modified_by=None): owner = owner or self.mobile_user modified_by = modified_by or self.mobile_user return create_test_case(self.domain, 'parent', 'P001', case_properties={'parent_prop1': 'abc'}, owner_id=owner.get_id, user_id=modified_by.get_id)
def create_case_contact(self, phone_number): return create_test_case(self.domain, 'participant', 'test', case_properties={ 'contact_phone_number': phone_number, 'contact_phone_number_is_verified': '1', }, drop_signals=False)
def create_child_case(self, owner=None, modified_by=None): owner = owner or self.mobile_user modified_by = modified_by or self.mobile_user return create_test_case(self.domain, 'child', 'P002', case_properties={'child_prop1': 'def'}, owner_id=owner.get_id, user_id=modified_by.get_id)
def test_not_passing_case_new(self): self._setup_fixture_type() with create_test_case(self.domain_name, self.case_type, 'test-case', {'risk_profile': 'risk1'}) as case: schedule_instance = self._schedule_instance(case) schedule_instance.recipient_type = ScheduleInstance.RECIPIENT_TYPE_WEB_USER schedule_instance.recipient_id = self.user.get_id with self.assertRaises(AssertionError) as e: self._get_current_event_messages(schedule_instance) self.assertIn('must be a case', str(e.exception))
def test_case_property_start_date_with_start_day_of_week(self): reminder = (CaseReminderHandler .create(self.domain, 'test') .set_case_criteria_start_condition('participant', 'status', MATCH_EXACT, 'green') .set_case_criteria_start_date(start_date='start_date', start_day_of_week=DAY_MON) .set_case_recipient() .set_sms_content_type('en') .set_daily_schedule(fire_time=time(12, 0), message={'en': 'Hello {case.name}, your test result was normal.'}) .set_stop_condition(max_iteration_count=30) .set_advanced_options(use_today_if_start_date_is_blank=False)) reminder.save() self.assertEqual(self.get_reminders(), []) with create_test_case(self.domain, 'participant', 'bob', drop_signals=False) as case, \ patch('corehq.apps.reminders.models.CaseReminderHandler.get_now') as now_mock: self.assertEqual(self.get_reminders(), []) now_mock.return_value = datetime(2016, 1, 1, 10, 0) update_case(self.domain, case.case_id, case_properties={'status': 'green', 'start_date': '2016-01-08'}) reminder_instance = self.assertOneReminder() self.assertEqual(reminder_instance.domain, self.domain) self.assertEqual(reminder_instance.case_id, case.case_id) self.assertEqual(reminder_instance.handler_id, reminder.get_id) self.assertIsNone(reminder_instance.user_id) self.assertEqual(reminder_instance.next_fire, datetime(2016, 1, 11, 12, 0)) self.assertTrue(reminder_instance.active) self.assertEqual(reminder_instance.start_date, date(2016, 1, 8)) self.assertEqual(reminder_instance.schedule_iteration_num, 1) self.assertEqual(reminder_instance.current_event_sequence_num, 0) self.assertEqual(reminder_instance.callback_try_count, 0) self.assertEqual(reminder_instance.start_condition_datetime, datetime(2016, 1, 8)) self.assertEqual(reminder_instance.callback_try_count, 0) # update start day of week and the schedule should be recalculated prev_definition = reminder reminder.set_case_criteria_start_date(start_date='start_date', start_day_of_week=DAY_TUE) reminder.save(schedule_changed=True, prev_definition=prev_definition) reminder_instance = self.assertOneReminder() self.assertEqual(reminder_instance.domain, self.domain) self.assertEqual(reminder_instance.case_id, case.case_id) self.assertEqual(reminder_instance.handler_id, reminder.get_id) self.assertIsNone(reminder_instance.user_id) self.assertEqual(reminder_instance.next_fire, datetime(2016, 1, 12, 12, 0)) self.assertTrue(reminder_instance.active) self.assertEqual(reminder_instance.start_date, date(2016, 1, 8)) self.assertEqual(reminder_instance.schedule_iteration_num, 1) self.assertEqual(reminder_instance.current_event_sequence_num, 0) self.assertEqual(reminder_instance.callback_try_count, 0) self.assertEqual(reminder_instance.start_condition_datetime, datetime(2016, 1, 8)) self.assertEqual(reminder_instance.callback_try_count, 0)
def create_case_contact(self, phone_number): return create_test_case( self.domain, 'participant', 'test', case_properties={ 'contact_phone_number': phone_number, 'contact_phone_number_is_verified': '1', }, drop_signals=False )
def create_parent_case(self, owner=None, modified_by=None): owner = owner or self.mobile_user modified_by = modified_by or self.mobile_user return create_test_case( self.domain, "parent", "P001", case_properties={"parent_prop1": "abc"}, owner_id=owner.get_id, user_id=modified_by.get_id, )
def create_child_case(self, owner=None, modified_by=None): owner = owner or self.mobile_user modified_by = modified_by or self.mobile_user return create_test_case( self.domain, "child", "P002", case_properties={"child_prop1": "def"}, owner_id=owner.get_id, user_id=modified_by.get_id, )
def test_message_bank_doesnt_have_correct_properties(self): data_type = FixtureDataType(domain=self.domain_name, tag=self.fixture_name, fields=[], item_attributes=[]) data_type.save() self.addCleanup(data_type.delete) with create_test_case(self.domain_name, self.case_type, 'test-case') as case: self.assertRaises(AssertionError, ucla_message_bank_content, self._reminder(), self._handler(), case)
def test_user_recipient(self): reminder = (CaseReminderHandler .create(self.domain, 'test') .set_case_criteria_start_condition('participant', 'status', MATCH_EXACT, 'green') .set_case_criteria_start_date() .set_last_submitting_user_recipient() .set_sms_content_type('en') .set_daily_schedule(fire_time=time(12, 0), message={'en': "{case.name}'s test result was normal."}) .set_stop_condition(max_iteration_count=REPEAT_SCHEDULE_INDEFINITELY) .set_advanced_options()) reminder.save() self.assertEqual(self.get_reminders(), []) with create_test_case(self.domain, 'participant', 'bob', drop_signals=False) as case, \ patch('corehq.apps.reminders.models.CaseReminderHandler.get_now') as now_mock: self.assertEqual(self.get_reminders(), []) now_mock.return_value = datetime(2016, 1, 1, 10, 0) # There should still be no reminder instance since this is submitted by the system user update_case(self.domain, case.case_id, case_properties={'status': 'green'}) self.assertEqual(self.get_reminders(), []) # Update the user id on the case CaseFactory(self.domain).create_or_update_case( CaseStructure( case_id=case.case_id, attrs={'user_id': self.user.get_id} ) ) reminder_instance = self.assertOneReminder() self.assertEqual(reminder_instance.domain, self.domain) self.assertEqual(reminder_instance.handler_id, reminder.get_id) self.assertTrue(reminder_instance.active) self.assertEqual(reminder_instance.start_date, date(2016, 1, 1)) self.assertIsNone(reminder_instance.start_condition_datetime) self.assertEqual(reminder_instance.next_fire, datetime(2016, 1, 1, 12, 0)) self.assertEqual(reminder_instance.case_id, case.case_id) self.assertEqual(reminder_instance.schedule_iteration_num, 1) self.assertEqual(reminder_instance.current_event_sequence_num, 0) self.assertEqual(reminder_instance.user_id, self.user.get_id) self.assertEqual(reminder_instance.callback_try_count, 0) # Set a user_id that does not exist CaseFactory(self.domain).create_or_update_case( CaseStructure( case_id=case.case_id, attrs={'user_id': 'this-user-id-does-not-exist'} ) ) self.assertEqual(self.get_reminders(), [])
def test_message_bank_doesnt_have_correct_properties(self): data_type = FixtureDataType( domain=self.domain_name, tag=self.fixture_name, fields=[], item_attributes=[] ) data_type.save() self.addCleanup(data_type.delete) with create_test_case(self.domain_name, self.case_type, 'test-case') as case: self.assertRaises( AssertionError, general_health_message_bank_content, self._reminder(), self._handler(), case)
def test_recipient_case_owner_location_parent(self): parent_location = SQLLocation.objects.create( domain=self.domain, name='parent test', site_code='parent', location_type=self.parent_location_type) child_location = SQLLocation.objects.create( domain=self.domain, name='child test', site_code='child', location_type=self.child_location_type, parent=parent_location) self.user.set_location(child_location.couch_location) with create_test_case(self.domain, 'test-case', 'test-name', owner_id=self.user.get_id) as case: self.assertEqual(case.owner_id, self.user.get_id) handler = CaseReminderHandler( domain=self.domain, recipient=RECIPIENT_CASE_OWNER_LOCATION_PARENT) reminder = CaseReminder(domain=self.domain, case_id=case.case_id) # Test the recipient is returned correctly with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler): self.assertEqual(reminder.recipient, [parent_location]) # Remove parent location parent_location.delete() child_location.parent = None child_location.save() with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler): self.assertIsNone(reminder.recipient) # Remove child location self.user.unset_location() child_location.delete() with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler): self.assertIsNone(reminder.recipient) # Remove case reminder.case_id = None with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler): self.assertIsNone(reminder.recipient)
def test_message_bank_doesnt_have_correct_properties_new(self): data_type = FixtureDataType( domain=self.domain_name, tag=self.fixture_name, fields=[], item_attributes=[] ) data_type.save() self.addCleanup(data_type.delete) with create_test_case(self.domain_name, self.case_type, 'test-case') as case: with self.assertRaises(AssertionError) as e: self._get_current_event_messages(self._schedule_instance(case)) self.assertIn('must have', str(e.exception))
def test_case_phone_number_updates(self): extra_number = PhoneNumber.objects.create( domain=self.domain, owner_doc_type='X', owner_id='X', phone_number='999123', verified=True, pending_verification=False, is_two_way=True ) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 1) with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case: self.assertIsNone(self.get_case_phone_number(case)) case = self.set_case_property(case, 'contact_phone_number', '99987658765') self.assertPhoneNumberDetails(case, '99987658765', None, None, False, False, False) pk = self.get_case_phone_number(case).pk self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2) case = self.set_case_property(case, 'contact_phone_number_is_verified', '1') self.assertPhoneNumberDetails(case, '99987658765', None, None, True, False, True, pk=pk) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2) case = self.set_case_property(case, 'contact_phone_number', '99987698769') self.assertPhoneNumberDetails(case, '99987698769', None, None, True, False, True) pk = self.get_case_phone_number(case).pk self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2) case = self.set_case_property(case, 'contact_backend_id', 'sms-backend') self.assertPhoneNumberDetails(case, '99987698769', 'sms-backend', None, True, False, True, pk=pk) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2) case = self.set_case_property(case, 'contact_ivr_backend_id', 'ivr-backend') self.assertPhoneNumberDetails(case, '99987698769', 'sms-backend', 'ivr-backend', True, False, True, pk=pk) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2) # If phone entry is ahead of the case in terms of contact_last_modified, no update should happen v = self.get_case_phone_number(case) v.contact_last_modified += timedelta(days=1) v.save() with patch('corehq.apps.sms.models.PhoneNumber.save') as mock_save: case = self.set_case_property(case, 'contact_phone_number', '99912341234') self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2) mock_save.assert_not_called() self.assertEqual(PhoneNumber.get_two_way_number('999123'), extra_number)
def test_recipient_case_owner_location_parent(self): parent_location = SQLLocation.objects.create( domain=self.domain, name='parent test', site_code='parent', location_type=self.parent_location_type ) child_location = SQLLocation.objects.create( domain=self.domain, name='child test', site_code='child', location_type=self.child_location_type, parent=parent_location ) self.user.set_location(child_location.couch_location) with create_test_case(self.domain, 'test-case', 'test-name', owner_id=self.user.get_id) as case: self.assertEqual(case.owner_id, self.user.get_id) handler = CaseReminderHandler(domain=self.domain, recipient=RECIPIENT_CASE_OWNER_LOCATION_PARENT) reminder = CaseReminder(domain=self.domain, case_id=case.case_id) # Test the recipient is returned correctly with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler): self.assertEqual(reminder.recipient, [parent_location]) # Remove parent location parent_location.delete() child_location.parent = None child_location.save() with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler): self.assertIsNone(reminder.recipient) # Remove child location self.user.unset_location() child_location.delete() with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler): self.assertIsNone(reminder.recipient) # Remove case reminder.case_id = None with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler): self.assertIsNone(reminder.recipient)
def test_get_contact(self): with create_test_case(self.domain, 'contact', 'test-case') as case: user = CommCareUser.create(self.domain, 'test-user', '123') self.addCleanup(user.delete) contact = get_contact(self.domain, case.case_id) self.assertEqual(contact.case_id, case.case_id) self.assertTrue(is_commcarecase(contact)) contact = get_contact(self.domain, user.get_id) self.assertEqual(contact.get_id, user.get_id) self.assertTrue(isinstance(contact, CommCareUser)) with self.assertRaises(ContactNotFoundException): get_contact(self.domain, 'this-id-should-not-be-found') with self.assertRaises(ContactNotFoundException): get_contact(self.domain + 'x', case.case_id) with self.assertRaises(ContactNotFoundException): get_contact(self.domain + 'x', user.get_id)
def test_invalid_phone_format(self): extra_number = PhoneNumber.objects.create( domain=self.domain, owner_doc_type='X', owner_id='X', phone_number='999123', verified=True, pending_verification=False, is_two_way=True ) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 1) with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case: case = self.set_case_property(case, 'contact_phone_number', '99987658765') case = self.set_case_property(case, 'contact_phone_number_is_verified', '1') self.assertIsNotNone(self.get_case_phone_number(case)) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2) case = self.set_case_property(case, 'contact_phone_number', 'xyz') self.assertIsNone(self.get_case_phone_number(case)) self.assertEqual(PhoneNumber.count_by_domain(self.domain), 1) self.assertEqual(PhoneNumber.get_two_way_number('999123'), extra_number)