def test_it_triggers_if_rapidpro_and_campaign_are_configured( self, settings, tasks_trigger): settings.RAPIDPRO_FOLLOWUP_CAMPAIGN_RH = "Boop Group,date_of_boop" settings.RAPIDPRO_API_TOKEN = "blorp" trigger_followup_campaign_async("Boop Jones", "5551234567", "RH", "en") tasks_trigger.delay.assert_called_once_with("Boop Jones", "5551234567", "RH", "en")
def perform_mutate(cls, form, info): request = info.context form_data = RhFormInfo.get_dict_from_request(request) if form_data is None: cls.log(info, "User has not completed the rental history form, aborting mutation.") return cls.make_error("You haven't completed all the previous steps yet.") rhr = models.RentalHistoryRequest(**form_data) rhr.set_user(request.user) rhr.full_clean() rhr.save() slack.sendmsg_async(get_slack_notify_text(rhr), is_safe=True) first_name: str = form_data["first_name"] last_name: str = form_data["last_name"] email_dhcr.send_email_to_dhcr( first_name, last_name, form_data["address"], BOROUGH_CHOICES.get_label(form_data["borough"]), form_data["zipcode"], form_data["apartment_number"] ) trigger_followup_campaign_async( f"{first_name} {last_name}", form_data["phone_number"], "RH" ) RhFormInfo.clear_from_request(request) return cls.mutation_success()
def test_it_triggers_if_rapidpro_and_campaign_are_configured( self, settings, tasks_trigger): settings.RAPIDPRO_FOLLOWUP_CAMPAIGN_RH = 'Boop Group,date_of_boop' settings.RAPIDPRO_API_TOKEN = 'blorp' trigger_followup_campaign_async('Boop Jones', '5551234567', 'RH') tasks_trigger.delay.assert_called_once_with('Boop Jones', '5551234567', 'RH')
def test_task_works(self, settings, monkeypatch): settings.RAPIDPRO_FOLLOWUP_CAMPAIGN_RH = 'Boop Group,date_of_boop' settings.RAPIDPRO_API_TOKEN = 'blorp' dsfc = MagicMock() campaign = MagicMock() dsfc.get_campaign.return_value = campaign monkeypatch.setattr(tasks, 'DjangoSettingsFollowupCampaigns', dsfc) trigger_followup_campaign_async('Boop Jones', '5551234567', 'RH') dsfc.get_campaign.assert_called_once_with('RH') campaign.add_contact.assert_called_once() assert campaign.add_contact.call_args.args[1:] == ('Boop Jones', '5551234567')
def test_task_works(self, settings, monkeypatch): settings.RAPIDPRO_FOLLOWUP_CAMPAIGN_RH = "Boop Group,date_of_boop" settings.RAPIDPRO_API_TOKEN = "blorp" dsfc = MagicMock() campaign = MagicMock() dsfc.get_campaign.return_value = campaign monkeypatch.setattr(tasks, "DjangoSettingsFollowupCampaigns", dsfc) trigger_followup_campaign_async("Boop Jones", "5551234567", "RH", "en") dsfc.get_campaign.assert_called_once_with("RH") campaign.add_contact.assert_called_once() assert campaign.add_contact.call_args.args[1:] == ("Boop Jones", "5551234567") assert campaign.add_contact.call_args.kwargs == {"locale": "en"}
def perform_mutate(cls, form, info): request = info.context scf = get_scaffolding(request) if not scaffolding_has_rental_history_request_info(scf): cls.log( info, "User has not completed the rental history form, aborting mutation." ) return cls.make_error( "You haven't completed all the previous steps yet.") rhr = models.RentalHistoryRequest( first_name=scf.first_name, last_name=scf.last_name, apartment_number=scf.apt_number, phone_number=scf.phone_number, address=scf.street, address_verified=scf.address_verified, borough=scf.borough, zipcode=scf.zip_code, ) rhr.set_user(request.user) rhr.full_clean() rhr.save() slack.sendmsg_async(get_slack_notify_text(rhr), is_safe=True) email = react_render_email( SITE_CHOICES.JUSTFIX, project.locales.DEFAULT, "rh/email-to-dhcr.txt", session=request.session, ) email_dhcr.send_email_to_dhcr(email.subject, email.body) trigger_followup_campaign_async( f"{scf.first_name} {scf.last_name}", scf.phone_number, "RH", locale=translation.get_language_from_request(request, check_path=True), ) # Note that we used to purge the scaffolding information here, but lots # of users go on to create an account after this, and we don't want them # to have to re-enter all their information, so we'll keep it around. return cls.mutation_success()
def trigger_followup_campaign_async(self, campaign_name: str) -> None: from rapidpro import followup_campaigns as fc fc.ensure_followup_campaign_exists(campaign_name) if self.can_we_sms: logging.info(f"Triggering rapidpro campaign '{campaign_name}' on user " f"{self.username}.") fc.trigger_followup_campaign_async( self.full_name, self.phone_number, campaign_name ) else: logging.info( f"Not triggering rapidpro campaign '{campaign_name}' on user " f"{self.username} because they opted out." )
def trigger_followup_campaign_async(self, campaign_name: str) -> None: from rapidpro import followup_campaigns as fc fc.ensure_followup_campaign_exists(campaign_name) if self.can_we_sms: logging.info( f"Triggering rapidpro campaign '{campaign_name}' on user " f"{self.username}.") fc.trigger_followup_campaign_async( self. full_preferred_name, # Use the user's preferred name when we text them. self.phone_number, campaign_name, locale=self.locale, ) else: logging.info( f"Not triggering rapidpro campaign '{campaign_name}' on user " f"{self.username} because they opted out.")
def test_task_retries_on_api_errors(self, settings, monkeypatch): settings.RAPIDPRO_FOLLOWUP_CAMPAIGN_RH = "Boop Group,date_of_boop" settings.RAPIDPRO_API_TOKEN = "blorp" dsfc = MagicMock() campaign = MagicMock() dsfc.get_campaign.return_value = campaign campaign.add_contact.side_effect = TembaHttpError("blah") retry = MagicMock() def fake_retry(exc): assert isinstance(exc, TembaHttpError) raise Retry() retry.side_effect = fake_retry monkeypatch.setattr(tasks.trigger_followup_campaign, "retry", retry) monkeypatch.setattr(tasks, "DjangoSettingsFollowupCampaigns", dsfc) with pytest.raises(Retry): trigger_followup_campaign_async("Boop Jones", "5551234567", "RH", "en") dsfc.get_campaign.assert_called_once_with("RH") campaign.add_contact.assert_called_once()
def test_it_does_nothing_if_campaign_is_unconfigured( self, settings, tasks_trigger): settings.RAPIDPRO_API_TOKEN = "blorp" trigger_followup_campaign_async("Boop", "5551234567", "RH", "en") tasks_trigger.assert_not_called()
def test_it_does_nothing_if_rapidpro_is_unconfigured( self, settings, tasks_trigger): settings.RAPIDPRO_FOLLOWUP_CAMPAIGN_RH = "Boop Group,date_of_boop" trigger_followup_campaign_async("Boop", "5551234567", "RH", "en") tasks_trigger.assert_not_called()
def test_it_does_nothing_if_campaign_is_unconfigured( self, settings, tasks_trigger): settings.RAPIDPRO_API_TOKEN = 'blorp' trigger_followup_campaign_async('Boop', '5551234567', 'RH') tasks_trigger.assert_not_called()