def test_get_permitted_submissions_when_staff(self): orgs = auth_models.Organization.objects.all() for org in orgs: SubmissionsService.create_for_organizations([org], answers={}) subs = set(models.FormSubmission.objects.all()) mock_user = Mock(is_staff=True) result = SubmissionsService.get_permitted_submissions(mock_user) self.assertEqual(set(result), subs)
def test_build_bundled_pdf_with_one_pdf(self, logger, get_parser, slack): # set up associated data sf_pubdef = auth_models.Organization.objects.get( slug=constants.Organizations.SF_PUBDEF) sub = SubmissionsService.create_for_organizations([sf_pubdef], answers={}) fillable = mock.fillable_pdf(organization=sf_pubdef) data = b'content' filled = models.FilledPDF.create_with_pdf_bytes(pdf_bytes=data, submission=sub, original_pdf=fillable) bundle = BundlesService.create_bundle_from_submissions( organization=sf_pubdef, submissions=[sub], skip_pdf=True) # set up mocks should_have_a_pdf = Mock(return_value=True) get_individual_filled_pdfs = Mock(return_value=[filled]) bundle.should_have_a_pdf = should_have_a_pdf bundle.get_individual_filled_pdfs = get_individual_filled_pdfs # run method BundlesService.build_bundled_pdf_if_necessary(bundle) # check results get_parser.assert_not_called() logger.assert_not_called() slack.assert_not_called() get_individual_filled_pdfs.assert_called_once_with() self.assertEqual(bundle.bundled_pdf.read(), data)
def test_build_bundled_pdf_with_one_pdf(self, logger, get_parser, slack): # set up associated data sf_pubdef = auth_models.Organization.objects.get( slug=constants.Organizations.SF_PUBDEF) sub = SubmissionsService.create_for_organizations( [sf_pubdef], answers={}) fillable = mock.fillable_pdf(organization=sf_pubdef) data = b'content' filled = models.FilledPDF.create_with_pdf_bytes( pdf_bytes=data, submission=sub, original_pdf=fillable) bundle = BundlesService.create_bundle_from_submissions( organization=sf_pubdef, submissions=[sub], skip_pdf=True) # set up mocks should_have_a_pdf = Mock(return_value=True) get_individual_filled_pdfs = Mock( return_value=[filled]) bundle.should_have_a_pdf = should_have_a_pdf bundle.get_individual_filled_pdfs = get_individual_filled_pdfs # run method BundlesService.build_bundled_pdf_if_necessary(bundle) # check results get_parser.assert_not_called() logger.assert_not_called() slack.assert_not_called() get_individual_filled_pdfs.assert_called_once_with() self.assertEqual(bundle.bundled_pdf.read(), data)
def test_get_absolute_url(self): org = auth_models.Organization.objects.get( slug=constants.Organizations.SF_PUBDEF) sub = SubmissionsService.create_for_organizations([org], answers={}) expected_url = "/application/{}/pdf/".format(sub.id) filled = models.FilledPDF(submission=sub) self.assertEqual(filled.get_absolute_url(), expected_url)
def test_should_have_a_pdf_negative(self): cc_pubdef = auth_models.Organization.objects.get( slug=constants.Organizations.COCO_PUBDEF) sub = SubmissionsService.create_for_organizations( [cc_pubdef], answers={}) bundle = BundlesService.create_bundle_from_submissions( organization=cc_pubdef, submissions=[sub], skip_pdf=True) self.assertFalse(bundle.should_have_a_pdf())
def test_should_have_a_pdf_positive(self): sf_pubdef = auth_models.Organization.objects.get(slug='sf_pubdef') mock.fillable_pdf(organization=sf_pubdef) sub = SubmissionsService.create_for_organizations( [sf_pubdef], answers={}) bundle = BundlesService.create_bundle_from_submissions( organization=sf_pubdef, submissions=[sub], skip_pdf=True) self.assertTrue(bundle.should_have_a_pdf())
def test_get_counties(self): organizations = auth_models.Organization.objects.all() # a submission for all organizations submission = SubmissionsService.create_for_organizations( organizations, answers={}) # it should be desitned for all counties counties = models.County.objects.order_by('slug').all() counties_from_sub = submission.get_counties().order_by('slug').all() self.assertListEqual(list(counties), list(counties_from_sub))
def test_get_counties(self): organizations = auth_models.Organization.objects.all() # a submission for all organizations submission = SubmissionsService.create_for_organizations(organizations, answers={}) # it should be desitned for all counties counties = models.County.objects.order_by('slug').all() counties_from_sub = submission.get_counties().order_by('slug').all() self.assertListEqual(list(counties), list(counties_from_sub))
def test_get_permitted_submissions_when_not_permitted(self): cc_pubdef = auth_models.Organization.objects.get(slug='cc_pubdef') sf_pubdef = auth_models.Organization.objects.get(slug='sf_pubdef') submission = SubmissionsService.create_for_organizations([cc_pubdef], answers={}) mock_user = Mock(is_staff=False, **{'profile.organization': sf_pubdef}) result = SubmissionsService.get_permitted_submissions( mock_user, [submission.id]) self.assertListEqual(list(result), [])
def test_build_bundled_pdf_with_no_filled_pdfs(self): cc_pubdef = auth_models.Organization.objects.get(slug='cc_pubdef') sub = SubmissionsService.create_for_organizations([cc_pubdef], answers={}) bundle = BundlesService.create_bundle_from_submissions( organization=cc_pubdef, submissions=[sub], skip_pdf=True) get_pdfs_mock = Mock() bundle.get_individual_filled_pdfs = get_pdfs_mock BundlesService.build_bundled_pdf_if_necessary(bundle) get_pdfs_mock.assert_not_called()
def test_get_permitted_submissions_when_not_permitted(self): cc_pubdef = auth_models.Organization.objects.get( slug='cc_pubdef') sf_pubdef = auth_models.Organization.objects.get( slug='sf_pubdef') submission = SubmissionsService.create_for_organizations( [cc_pubdef], answers={}) mock_user = Mock(is_staff=False, **{'profile.organization': sf_pubdef}) result = SubmissionsService.get_permitted_submissions( mock_user, [submission.id]) self.assertListEqual(list(result), [])
def test_build_bundled_pdf_with_no_filled_pdfs(self): cc_pubdef = auth_models.Organization.objects.get( slug=constants.Organizations.COCO_PUBDEF) sub = SubmissionsService.create_for_organizations( [cc_pubdef], answers={}) bundle = BundlesService.create_bundle_from_submissions( organization=cc_pubdef, submissions=[sub], skip_pdf=True) get_pdfs_mock = Mock() bundle.get_individual_filled_pdfs = get_pdfs_mock BundlesService.build_bundled_pdf_if_necessary(bundle) get_pdfs_mock.assert_not_called()
def test_save_binary_data_to_pdf(self): org = auth_models.Organization.objects.get( slug='sf_pubdef') sub = SubmissionsService.create_for_organizations([org], answers={}) data = b'content' fob = SimpleUploadedFile( content=data, name="content.pdf", content_type="application/pdf") filled = models.FilledPDF(submission=sub, pdf=fob) filled.save() self.assertEqual(filled.pdf.read(), data) self.assertIn("content", filled.pdf.name) self.assertIn(".pdf", filled.pdf.name)
def test_calls_pdfparser_correctly(self): sf_pubdef = auth_models.Organization.objects.get(slug='sf_pubdef') fillable = mock.fillable_pdf(organization=sf_pubdef) subs = [ SubmissionsService.create_for_organizations( [sf_pubdef], answers=mock.fake.cleaned_sf_county_form_answers()) for i in range(2)] pdfs = [fillable.fill_for_submission(sub) for sub in subs] parser = models.get_parser() result = parser.join_pdfs(filled.pdf for filled in pdfs) self.assertTrue(len(result) > 30000)
def test_can_log_referral_between_orgs(self): from_org = auth_models.Organization.objects.get(slug='a_pubdef') to_org = auth_models.Organization.objects.get(slug='ebclc') from_org_user = from_org.profiles.first().user answers = mock.fake.alameda_pubdef_answers() submission = SubmissionsService.create_for_organizations( organizations=[from_org], answers=answers) log = models.ApplicationLogEntry.log_referred_from_one_org_to_another( submission.id, to_organization_id=to_org.id, user=from_org_user ) self.assertEqual(log.from_org(), from_org) self.assertEqual(log.user, from_org_user) self.assertEqual(log.to_org(), to_org)
def test_get_individual_filled_pdfs(self): sf_pubdef = auth_models.Organization.objects.get(slug='sf_pubdef') fillable = mock.fillable_pdf(organization=sf_pubdef) subs = [ SubmissionsService.create_for_organizations( [sf_pubdef], answers={}) for i in range(2)] expected_pdfs = [ models.FilledPDF(original_pdf=fillable, submission=sub) for sub in subs] for pdf in expected_pdfs: pdf.save() bundle = BundlesService.create_bundle_from_submissions( organization=sf_pubdef, submissions=subs, skip_pdf=True) query = bundle.get_individual_filled_pdfs().order_by('pk') pdfs = list(query) self.assertListEqual(pdfs, expected_pdfs)
def test_build_bundled_pdf_if_has_pdfs(self, logger, get_parser, slack): sf_pubdef = auth_models.Organization.objects.get( slug=constants.Organizations.SF_PUBDEF) subs = [ SubmissionsService.create_for_organizations( [sf_pubdef], answers={}) for i in range(2)] should_have_a_pdf = Mock(return_value=True) get_individual_filled_pdfs = Mock( return_value=[Mock(pdf=b'pdf') for sub in subs]) get_parser.return_value.join_pdfs.return_value = b'pdf' bundle = BundlesService.create_bundle_from_submissions( organization=sf_pubdef, submissions=subs, skip_pdf=True) bundle.should_have_a_pdf = should_have_a_pdf bundle.get_individual_filled_pdfs = get_individual_filled_pdfs BundlesService.build_bundled_pdf_if_necessary(bundle) logger.assert_not_called() slack.assert_not_called() get_individual_filled_pdfs.assert_called_once_with()
def test_build_bundled_pdf_if_has_pdfs(self, logger, get_parser, slack): sf_pubdef = auth_models.Organization.objects.get(slug='sf_pubdef') subs = [ SubmissionsService.create_for_organizations([sf_pubdef], answers={}) for i in range(2) ] should_have_a_pdf = Mock(return_value=True) get_individual_filled_pdfs = Mock( return_value=[Mock(pdf=b'pdf') for sub in subs]) get_parser.return_value.join_pdfs.return_value = b'pdf' bundle = BundlesService.create_bundle_from_submissions( organization=sf_pubdef, submissions=subs, skip_pdf=True) bundle.should_have_a_pdf = should_have_a_pdf bundle.get_individual_filled_pdfs = get_individual_filled_pdfs BundlesService.build_bundled_pdf_if_necessary(bundle) logger.assert_not_called() slack.assert_not_called() get_individual_filled_pdfs.assert_called_once_with()