def test_sf_application_redirects_if_missing_recommended_fields( self, slack, send_confirmation): self.be_anonymous() applicant = models.Applicant() applicant.save() sanfrancisco = constants.Counties.SAN_FRANCISCO answers = mock.fake.sf_county_form_answers() answers['ssn'] = '' self.set_session( form_in_progress=dict(counties=[sanfrancisco]), applicant_id=applicant.id ) response = self.client.fill_form( reverse('intake-county_application'), follow=True, **answers ) self.assertEqual( response.wsgi_request.path, reverse('intake-confirm')) form = response.context_data['form'] self.assertTrue(form.warnings) self.assertFalse(form.errors) self.assertIn('ssn', form.warnings) slack.assert_not_called() send_confirmation.assert_not_called() submitted_event_count = applicant.events.filter( name=models.ApplicationEvent.APPLICATION_SUBMITTED).count() self.assertEqual(0, submitted_event_count)
def test_expected_success(self): applicant = models.Applicant() applicant.save() for i in range(2): factories.FormSubmissionFactory(applicant=applicant) results = ApplicantServices.get_applicants_with_multiple_submissions() self.assertListEqual(list(results), [applicant])
def test_can_log_event_with_data(self): applicant = models.Applicant() applicant.save() event_name = "im_being_tested" event_data = {"foo": "bar"} event = applicant.log_event(event_name, event_data) self.assertEqual(event.data, event_data)
def test_case_when_all_have_usable_contact_info(self): orgs = [Organization.objects.get(slug=Organizations.ALAMEDA_PUBDEF)] subs = [] for i in range(4): applicant = models.Applicant() applicant.save() subs.append( mock.FormSubmissionFactory.create( applicant=applicant, organizations=orgs, answers=self.full_answers(), )) FollowupsService.send_followup_notifications(subs) self.assertEqual( FollowupsService.get_submissions_due_for_follow_ups().count(), 0) self.assertEqual( len(self.notifications.email_followup.send.mock_calls), 4) self.assertEqual(len(self.notifications.sms_followup.send.mock_calls), 0) self.assertEqual( len(self.notifications.slack_notification_sent.send.mock_calls), 4) followup_events = models.ApplicationEvent.objects.filter( name=models.ApplicationEvent.FOLLOWUP_SENT) self.assertEqual(followup_events.count(), 4) followed_up_app_ids = set( followup_events.values_list('applicant_id', flat=True)) for sub in subs: self.assertIn(sub.applicant_id, followed_up_app_ids)
def create_new_applicant(request): if not getattr(request, 'applicant', None): applicant = models.Applicant( visitor_id=request.visitor.id) applicant.save() request.session['applicant_id'] = applicant.id request.applicant = applicant return request.applicant
def test_there_can_be_only_one_applicant_per_visitor(self): # aka highlander test applicant = factories.ApplicantFactory() visitor = applicant.visitor second_applicant = models.Applicant(visitor_id=visitor.id) with self.assertRaises(IntegrityError): second_applicant.save() self.assertEqual(visitor.applicant, applicant)
def get_or_create_applicant_id(self, visitor_id=None): applicant_id = self.get_applicant_id() if not applicant_id: applicant = models.Applicant(visitor_id=visitor_id) applicant.save() applicant_id = applicant.id self.request.session['applicant_id'] = applicant.id self.applicant_id = applicant_id return applicant_id
def test_existing_application_has_org_data(self): self.be_anonymous() app = models.Applicant() app.save() sub = models.FormSubmission.objects.all().first() sub.applicant_id = app.id sub.save() self.set_session(applicant_id=app.id) response = self.client.get(reverse('intake-thanks')) for org in sub.organizations.all(): self.assertContains(response, html_utils.escape(org.name))
def test_can_log_event(self): applicant = models.Applicant() applicant.save() event_name = "im_being_tested" event = applicant.log_event(event_name) self.assertTrue(event.id) self.assertEqual(event.applicant, applicant) self.assertEqual(event.name, event_name) self.assertEqual(event.data, {}) all_events = list(applicant.events.all()) self.assertIn(event, all_events)
def test_filters_out_subs_with_previous_followups(self): # given old submissions, some with old followups no_followup = mock.FormSubmissionFactory.create( date_received=get_old_date()) applicant = models.Applicant() applicant.save() sub_w_followup = mock.FormSubmissionFactory.create( date_received=get_old_date(), applicant=applicant) models.ApplicationEvent.log_followup_sent( applicant.id, contact_info=sub_w_followup.answers['email'], message_content="hey how are things going?") # if we grab subs that need followups results = FollowupsService.get_submissions_due_for_follow_ups() results_set = set(results) # we should only have ones that have not received followups self.assertIn(no_followup, results_set) self.assertNotIn(sub_w_followup, results_set)
def make_full_submission(self, orgs, **answer_overrides): applicant = models.Applicant() applicant.save() answers = mock.fake.all_county_answers( first_name="Hubert", contact_preferences=[ 'prefers_email', 'prefers_sms', 'prefers_voicemail', 'prefers_snailmail' ], email='*****@*****.**', phone_number='5554442222', ) answers.update(answer_overrides) return mock.FormSubmissionFactory.create( date_received=get_old_date(), anonymous_name="Cerulean Beetle", organizations=orgs, answers=answers, applicant=applicant)
def test_post_approve_letter(self, slack, send_confirmation): self.be_anonymous() applicant = models.Applicant() applicant.save() alameda = constants.Counties.ALAMEDA mock_letter = mock.fake.declaration_letter_answers() mock_answers = mock.fake.alameda_pubdef_answers() counties = {'counties': [alameda]} session_data = {} for other_data in [counties, mock_answers, mock_letter]: session_data.update(other_data) self.set_session( form_in_progress=session_data, applicant_id=applicant.id) response = self.client.fill_form( reverse('intake-review_letter'), submit_action="approve_letter") self.assertRedirects(response, reverse('intake-thanks')) applicant_id = self.client.session.get('applicant_id') self.assertTrue(applicant_id) submissions = list(models.FormSubmission.objects.filter( applicant_id=applicant_id)) self.assertEqual(len(submissions), 1) submission = submissions[0] county_slugs = [county.slug for county in submission.get_counties()] self.assertListEqual(county_slugs, [alameda]) self.assertIn(self.a_pubdef, submission.organizations.all()) self.assertEqual(submission.organizations.count(), 1) self.assertEqual(submission.organizations.first().county.slug, alameda) filled_pdf_count = models.FilledPDF.objects.count() self.assertEqual(filled_pdf_count, 0) self.be_apubdef_user() resp = self.client.get(reverse("intake-app_index")) url = reverse( "intake-app_detail", kwargs={'submission_id': submission.id}) self.assertContains(resp, url) self.assertTrue(slack.called) self.assertTrue(send_confirmation.called)
def handle(self, *args, **options): subs = models.FormSubmission.objects.filter( applicant_id__isnull=True) backfilled = 0 for sub in subs: if not sub.applicant_id: applicant = models.Applicant() applicant.save() sub.applicant = applicant sub.save() event = models.ApplicationEvent( applicant_id=applicant.id, name=models.ApplicationEvent.APPLICATION_SUBMITTED, time=sub.date_received, data={}) event.save() backfilled += 1 self.stdout.write( self.style.SUCCESS( "Backfilled applicants on {} submissions".format(backfilled)) )
def test_can_start_at_particular_id_to_create_time_interval(self): # assume we have 4 old subs, 1 new sub old_subs = sorted([ mock.FormSubmissionFactory.create(date_received=get_old_date()) for i in range(4) ], key=lambda s: s.date_received) new_sub = mock.FormSubmissionFactory.create( date_received=get_newer_date()) # but we only want ones after the second oldest sub second_oldest_id = old_subs[1].id # and within the old subs, we still don't want ones that already # received followups applicant = models.Applicant() applicant.save() followed_up_sub = old_subs[2] followed_up_sub.applicant = applicant followed_up_sub.save() models.ApplicationEvent.log_followup_sent( applicant.id, contact_info=followed_up_sub.answers['email'], message_content="hey how are things going?") # when we get submissions due for follow ups, results = list( FollowupsService.get_submissions_due_for_follow_ups( after_id=second_oldest_id)) # we should only receive two: self.assertEqual(len(results), 2) # the second oldest self.assertIn(old_subs[1], results) # and not-as-old one that did not have a follow up self.assertIn(old_subs[3], results) # we should not receive # the oldest sub self.assertNotIn(old_subs[0], results) # the one with the follow up self.assertNotIn(followed_up_sub, results) # or the new sub self.assertNotIn(new_sub, results)
def build_seed_submissions(): create_seed_users() from user_accounts.models import Organization from formation.forms import county_form_selector cc_pubdef = Organization.objects.get( slug=constants.Organizations.COCO_PUBDEF) a_pubdef = Organization.objects.get( slug=constants.Organizations.ALAMEDA_PUBDEF) ebclc = Organization.objects.get( slug=constants.Organizations.EBCLC) sf_pubdef = Organization.objects.get( slug=constants.Organizations.SF_PUBDEF) monterey_pubdef = Organization.objects.get( slug=constants.Organizations.MONTEREY_PUBDEF) solano_pubdef = Organization.objects.get( slug=constants.Organizations.SOLANO_PUBDEF) san_diego_pubdef = Organization.objects.get( slug=constants.Organizations.SAN_DIEGO_PUBDEF) san_joaquin_pubdef = Organization.objects.get( slug=constants.Organizations.SAN_JOAQUIN_PUBDEF) santa_clara_pubdef = Organization.objects.get( slug=constants.Organizations.SANTA_CLARA_PUBDEF) fresno_pubdef = Organization.objects.get( slug=constants.Organizations.FRESNO_PUBDEF) receiving_orgs = [ cc_pubdef, a_pubdef, ebclc, sf_pubdef, monterey_pubdef, solano_pubdef, san_diego_pubdef, san_joaquin_pubdef, santa_clara_pubdef, fresno_pubdef] answer_pairs = { sf_pubdef.slug: fake.sf_county_form_answers, cc_pubdef.slug: fake.contra_costa_county_form_answers, ebclc.slug: fake.ebclc_answers, a_pubdef.slug: fake.alameda_pubdef_answers, monterey_pubdef.slug: fake.monterey_pubdef_answers, solano_pubdef.slug: fake.solano_pubdef_answers, san_diego_pubdef.slug: fake.san_diego_pubdef_answers, san_joaquin_pubdef.slug: fake.san_joaquin_pubdef_answers, santa_clara_pubdef.slug: fake.santa_clara_pubdef_answers, fresno_pubdef.slug: fake.fresno_pubdef_answers, } form_pairs = { org.slug: county_form_selector.get_combined_form_class( counties=[org.county.slug]) for org in receiving_orgs } # make 2 submissions to each org applicants = [] subs = [] for org in receiving_orgs: for i in range(2): raw_answers = answer_pairs[org.slug]() Form = form_pairs[org.slug] form = Form(raw_answers, validate=True) applicant = models.Applicant() applicant.save() applicants.append(applicant) sub = models.FormSubmission( applicant=applicant, answers=form.cleaned_data ) if org in (a_pubdef, santa_clara_pubdef, monterey_pubdef): letter = fake.declaration_letter_answers() sub.answers.update(letter) sub.save() application = models.Application( organization=org, form_submission=sub) application.save() StatusUpdateFactory.create( application=application, author=org.profiles.first().user) subs.append(sub) # make 1 submission to multiple orgs target_orgs = [ a_pubdef, cc_pubdef, sf_pubdef, monterey_pubdef, solano_pubdef, san_diego_pubdef, san_joaquin_pubdef, santa_clara_pubdef, fresno_pubdef] answers = fake.all_county_answers() Form = county_form_selector.get_combined_form_class( counties=[org.county.slug for org in target_orgs]) form = Form(answers, validate=True) applicant = models.Applicant() applicant.save() applicants.append(applicant) multi_org_sub = models.FormSubmission( applicant=applicant, answers=form.cleaned_data) multi_org_sub.answers.update(fake.declaration_letter_answers()) multi_org_sub.save() applications = [ models.Application(organization=org, form_submission=multi_org_sub) for org in target_orgs ] models.Application.objects.bulk_create(applications) subs.append(multi_org_sub) # fake the date received for each sub for sub in subs: sub.date_received = local(fake.date_time_between('-2w', 'now')) sub.save() # make a bundle for each org for org in receiving_orgs: org_subs = [] for sub in subs: has_app = sub.applications.filter(organization=org).exists() if has_app and sub != multi_org_sub: org_subs.append(sub) bundle = BundlesService.create_bundle_from_submissions( organization=org, submissions=org_subs, skip_pdf=True) # save bundle filename = 'mock_1_bundle_to_' + org.slug + ".json" dump_as_json([bundle], fixture_path(filename)) filename = 'mock_{}_submissions_to_{}.json'.format( len(org_subs), org.slug) serialize_subs(org_subs, fixture_path(filename)) serialize_subs( [multi_org_sub], fixture_path('mock_1_submission_to_multiple_orgs.json')) events = [] for applicant in applicants: events.extend( make_mock_submission_event_sequence(applicant)) dump_as_json(events, fixture_path('mock_application_events.json'))
def make_applicant(): applicant = models.Applicant() applicant.save() return applicant
def test_cant_create_with_nothing(self): applicant = models.Applicant() with self.assertRaises(IntegrityError): applicant.save()
def test_getuuid_returns_same_uuid_as_corresponding_visitor(self): visitor = factories.VisitorFactory() applicant = models.Applicant(visitor_id=visitor.id) applicant.save() self.assertEqual(applicant.get_uuid(), visitor.get_uuid())
def test_can_create_with_visitor_id(self): visitor = factories.VisitorFactory() applicant = models.Applicant(visitor_id=visitor.id) applicant.save() self.assertTrue(applicant.id)
def test_can_create_with_nothing(self): applicant = models.Applicant() applicant.save() self.assertTrue(applicant.id)
def test_no_error_if_applicant_with_no_sub(self): app = models.Applicant() app.save() self.set_session(applicant_id=app.id) response = self.client.get(reverse('intake-rap_sheet')) self.assertEqual(response.status_code, 200)