def test_app_exists(self): source_app = factories.GrantApplication() target_cycle = factories.GrantCycle(status='open') app = factories.GrantApplication( organization=source_app.organization, grant_cycle=target_cycle) res = self.client.post( reverse(views.admin_rollover, kwargs={'app_id': source_app.pk}), data={'cycle': target_cycle.pk} ) self.assertEqual(res.status_code, 200) self.assertFalse(res.context['form'].is_valid()) self.assert_count(GrantApplication.objects.filter(grant_cycle_id=target_cycle.pk), 1)
def test_start_conflicting_apps(self): a = factories.Organization() b = factories.Organization() # create apps for same cycle app_a = factories.GrantApplication(organization=a) app_b = factories.GrantApplication(organization=b, grant_cycle=app_a.grant_cycle) post_data = {'action': 'merge', '_selected_action': [a.pk, b.pk]} res = self.client.post(self.admin_url, post_data, follow=True) self.assertEqual(res.status_code, 200) self.assertTemplateUsed(res, 'admin/change_list.html') self.assert_message(res, r'same grant cycle. Cannot be automatically merged.$', regex=True)
def test_load_revert(self): app = factories.GrantApplication() res = self.client.get(self._get_url(app.pk)) self.assertEqual(200, res.status_code) self.assertContains(res, 'Are you sure you want to revert this application into a draft?')
def test_unknown_cycle(self): source_app = factories.GrantApplication() res = self.client.post( reverse(views.admin_rollover, kwargs={'app_id': source_app.pk}), data={'cycle': 99} ) self.assertEqual(res.status_code, 200) self.assertIn('Select a valid choice', res.context['form'].errors['cycle'][0])
def test_get_narrative_answer(self): app = factories.GrantApplication() answers = models.NarrativeAnswer.objects.filter(grant_application=app) self.assert_count(answers, len(gc.STANDARD_NARRATIVES)) self.assertNotEqual(app.get_narrative_answer('describe_mission'), answers.get(cycle_narrative__narrative_question__name='describe_mission'))
def test_staff(self): self.login_as_admin() app = factories.GrantApplication() res = self.client.get(self._get_url(app.pk)) self.assertEqual(res.status_code, 200) self.assertTemplateUsed(res, 'grants/reading.html') self.assertEqual(2, res.context['perm'])
def setUp(self): super(ApplyValidation, self).setUp() self.login_as_org() # take a valid grant application to use as draft app = factories.GrantApplication() self.draft = DraftGrantApplication.objects.create_from_submitted_app( app) self.draft.organization = self.org self.draft.grant_cycle = factories.GrantCycle(status='open')
def test_already_submitted(self): app = factories.GrantApplication(organization=self.org) res = self.client.get(_get_apply_url(app.grant_cycle.pk)) self.assertEqual(200, res.status_code) self.assertTemplateUsed(res, 'grants/already_applied.html') self.assert_count( DraftGrantApplication.objects.filter(organization=self.org), 0)
def test_cycle_open(self): source_app = factories.GrantApplication() target_cycle = factories.GrantCycle(status='open') res = self.client.post( reverse(views.admin_rollover, kwargs={'app_id': source_app.pk}), data={'cycle': target_cycle.pk} ) self.assertEqual(res.status_code, 302) self.assertRegexpMatches(res.get('Location'), r'/admin/grants/grantapplication/\d/$') self.assert_count(GrantApplication.objects.filter(grant_cycle=target_cycle), 1)
def test_other_org(self): self.login_as_org() app = factories.GrantApplication() yer = factories.YearEndReport(award__projectapp__application=app) res = self.client.get(self._get_url(app.pk)) self.assertEqual(res.status_code, 200) self.assertTemplateUsed(res, 'grants/reading.html') self.assertEqual(0, res.context['perm']) self.assertNotContains(res, 'year end report')
def test_author(self): self.login_as_org() app = factories.GrantApplication(organization=self.org) report = factories.GranteeReport( giving_project_grant__projectapp__application=app) res = self.client.get(self._get_url(app.pk)) self.assertEqual(res.status_code, 200) self.assertTemplateUsed(res, 'grants/reading.html') self.assertEqual(3, res.context['perm']) self.assertContains(res, 'grantee report')
def test_two_year_grant_question(self): self.login_as_org() app = factories.GrantApplication( organization=self.org, grant_cycle__questions__add=[gc.TWO_YEAR_GRANT_QUESTION]) res = self.client.get(self._get_url(app.pk)) self.assertEqual(res.status_code, 200) self.assertTemplateUsed(res, 'grants/reading.html') self.assertEqual(3, res.context['perm']) self.assertContains(res, app.get_narrative_answer('two_year_grant'))
def test_merge_both_have_objs(self): cycles = factories.GrantCycle.create_batch(5) a = factories.Organization() factories.GrantApplication.create_batch(2, organization=a) factories.SponsoredProgramGrant(organization=a) b = factories.Organization() app = factories.GrantApplication(organization=b) # get draft & app IDs that were associated with secondary org sec_apps = list(sec_org.grantapplication_set.values_list('pk', flat=True)) sec_drafts = list(sec_org.draftgrantapplication_set.values_list('pk', flat=True)) sec_papps = ProjectApp.objects.filter(application__organization_id=sec) self.assert_length(sec_apps, 2) self.assert_length(sec_drafts, 2) self.assert_count(sec_papps, 1) sponsored = SponsoredProgramGrant(organization_id=sec, amount=400) sponsored.save() # create app for primary org app = GrantApplication(organization_id=primary, grant_cycle_id=4, founded='1998', budget_last=300, budget_current=600, amount_requested=99) app.save() papp = ProjectApp(application_id=app.pk, giving_project_id=3) papp.save() gpg = GivingProjectGrant(projectapp_id=papp.pk, amount=199, first_report_due='2017-01-03') gpg.save() url = reverse('sjfnw.grants.views.merge_orgs', kwargs={'id_a': sec, 'id_b': primary}) post_data = {'primary': primary} res = self.client.post(url, post_data, follow=True) self.assertEqual(res.status_code, 200) self.assertTemplateUsed(res, 'admin/change_form.html') self.assert_message(res, 'Merge successful. Redirected to new organization page') apps = GrantApplication.objects.filter(organization_id=primary) drafts = DraftGrantApplication.objects.filter(organization_id=primary) papps = ProjectApp.objects.filter(application__organization_id=primary) sponsored = SponsoredProgramGrant.objects.filter(organization_id=primary) self.assert_length(apps, 3) self.assert_length(drafts, 2) self.assert_count(papps, 2) self.assert_count(sponsored, 1)
def test_revert_app(self): app = factories.GrantApplication() answers = (NarrativeAnswer.objects .filter(grant_application=app) .values('cycle_narrative__narrative_question__name', 'text')) a = {} for ans in answers: a[ans['cycle_narrative__narrative_question__name']] = ans['text'] self.client.post(self._get_url(app.pk)) self.assert_count( GrantApplication.objects.filter(organization_id=app.organization_id), 0) draft = DraftGrantApplication.objects.get( organization_id=app.organization_id, grant_cycle_id=app.grant_cycle_id) self.assert_draft_matches_app(draft, app, a)
def test_refs(self): app = factories.GrantApplication() collab_refs = json.loads(app.get_narrative_answer('collaboration_references')) rj_refs = json.loads(app.get_narrative_answer('racial_justice_references')) draft = models.DraftGrantApplication.objects.create_from_submitted_app(app) contents = json.loads(draft.contents) self.assertNotIn('racial_justice_references', contents) self.assertNotIn('collaboration_references', contents) self.assertEqual(contents['racial_justice_references_0'], rj_refs[0]['name']) self.assertEqual(contents['racial_justice_references_1'], rj_refs[0]['org']) self.assertEqual(contents['racial_justice_references_2'], rj_refs[0]['phone']) self.assertEqual(contents['racial_justice_references_3'], rj_refs[0]['email']) self.assertEqual(contents['racial_justice_references_4'], rj_refs[1]['name']) self.assertEqual(contents['racial_justice_references_5'], rj_refs[1]['org']) self.assertEqual(contents['racial_justice_references_6'], rj_refs[1]['phone']) self.assertEqual(contents['racial_justice_references_7'], rj_refs[1]['email']) self.assertEqual(contents['collaboration_references_0'], collab_refs[0]['name']) self.assertEqual(contents['collaboration_references_1'], collab_refs[0]['org']) self.assertEqual(contents['collaboration_references_2'], collab_refs[0]['phone']) self.assertEqual(contents['collaboration_references_3'], collab_refs[0]['email']) self.assertEqual(contents['collaboration_references_4'], collab_refs[1]['name']) self.assertEqual(contents['collaboration_references_5'], collab_refs[1]['org']) self.assertEqual(contents['collaboration_references_6'], collab_refs[1]['phone']) self.assertEqual(contents['collaboration_references_7'], collab_refs[1]['email']) # get fields & files from draft draft_data = json.loads(draft.contents) files_data = model_to_dict(draft, fields=draft.file_fields()) # add automated fields draft_data['organization'] = draft.organization.pk draft_data['grant_cycle'] = draft.grant_cycle.pk app.delete() form = get_form_for_cycle(draft.grant_cycle)(draft.grant_cycle, draft_data, files_data) if not form.is_valid(): logger.error(form.errors) raise Exception('Expected form to be valid')
def test_app_rollover(self): app = factories.GrantApplication(organization=self.org) target_cycle = factories.GrantCycle(status='open') post_data = { 'cycle': target_cycle.pk, 'draft': '', 'application': app.pk } response = self.client.post('/apply/copy', post_data, follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'grants/org_app.html') ids = { 'organization_id': self.org.pk, 'grant_cycle_id': target_cycle.pk } draft = DraftGrantApplication.objects.get(**ids) self.assert_draft_matches_app(draft, app)