def test_merged_one_sided(self): a = factories.Organization() b = factories.Organization() factories.GrantApplication.create_batch(2, organization=b) factories.DraftGrantApplication.create_batch(2, organization=b) url = reverse(views.merge_orgs, kwargs={'id_a': a.pk, 'id_b': b.pk}) post_data = {'primary': a.pk} 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') # secondary org & user should have been deleted self.assert_count(Organization.objects.filter(pk=b.pk), 0) self.assert_count(User.objects.filter(username=b.user.username), 0) log = (GrantApplicationLog.objects.filter(organization=a) .order_by('-date') .first()) self.assertIsNotNone(log) self.assertRegexpMatches(log.notes, r'^Merged') self.assert_count(GrantApplication.objects.filter(organization_id=a.pk), 2) self.assert_count(DraftGrantApplication.objects.filter(organization_id=a.pk), 2)
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_start_triple_org(self): a = factories.Organization() b = factories.Organization() c = factories.Organization() post_data = { 'action': 'merge', '_selected_action': [a.pk, b.pk, c.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, 'Merge can only be done on two organizations. You selected 3.')
def test_wrong_org(self): other_org = factories.Organization() draft = factories.DraftGrantApplication(organization=other_org) response = self.client.delete(self.url) self.assertEqual(response.status_code, 400) self.assertEqual(response.content, 'User does not have permission to delete this draft')
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_start_valid_one_empty(self): a = factories.Organization() b = factories.OrganizationWithProfile() 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/grants/merge_orgs.html') self.assertContains(res, a.name) self.assertContains(res, b.name)
def test_repeat_org_name(self): """ Verify that registration fails if org with same org name and some user is already in DB. """ existing_org = factories.Organization() registration = { 'email': '*****@*****.**', 'password': '******', 'passwordtwo': 'one', 'organization': existing_org.name } self.assert_count(User.objects.filter(username=registration['email']), 0) res = self.client.post(self.url, registration, follow=True) self.assertTemplateUsed(res, self.template_error) self.assertFormError(res, 'register', None, FORM_ERRORS['org_registered']) self.assert_count(User.objects.filter(username=registration['email']), 0)
def test_repeat_org_email(self): """ Email matches an existing org (name doesn't) """ existing_org = factories.Organization() registration = { 'email': existing_org.user.username, 'password': '******', 'passwordtwo': 'one', 'organization': 'Brand New' } self.assert_count(Organization.objects.filter(name=registration['organization']), 0) res = self.client.post(self.url, registration, follow=True) self.assert_count(Organization.objects.filter(name=registration['organization']), 0) self.assertTemplateUsed(res, self.template_error) self.assertFormError(res, 'register', None, FORM_ERRORS['email_registered'])
def login_as_org(self, with_profile=False): self.org = factories.OrganizationWithProfile( ) if with_profile else factories.Organization() self.login_strict(self.org.user.username, 'password')
def test_action_available(self): factories.Organization() # need at least 1 org to exist for actions to appear res = self.client.get(self.admin_url, follow=True) self.assertContains(res, '<option value="merge"')