def test_set_projects(self): """Posting a group of projects to an article should set that article's projects.""" project1 = ProjectFactory() project2 = ProjectFactory() staff = UserFactory(is_staff=True) project_form = ProjectManagerForm( { 'projects': [project1.pk, project2.pk] }, user=staff, ) ok_( project_form.is_valid(), 'We want to be sure we are posting valid data.' ) data = {'action': 'projects'} data.update(project_form.data) response = self.post_helper(data, staff) self.article.refresh_from_db() project1.refresh_from_db() project2.refresh_from_db() ok_(response.status_code, 200) ok_( self.article in project1.articles.all(), 'The article should be added to the project.' ) ok_( self.article in project2.articles.all(), 'The article should be added to teh project.' )
def test_add_projects(self): """Posting a collection of projects to a request should add it to those projects.""" project = ProjectFactory() project.contributors.add(self.foia.user) form = ProjectManagerForm({"projects": [project.pk]}, user=self.foia.user) ok_(form.is_valid()) data = {"action": "projects"} data.update(form.data) http_post_response(self.url, self.view, data, self.foia.user, **self.kwargs) project.refresh_from_db() ok_(self.foia in project.requests.all())
class TestProjectCrowdfundView(TestCase): """Tests the creation of a crowdfund for a project.""" def setUp(self): self.project = ProjectFactory(private=False, approved=True) self.url = reverse('project-crowdfund', kwargs={ 'slug': self.project.slug, 'pk': self.project.pk }) self.view = views.ProjectCrowdfundView.as_view() self.request_factory = RequestFactory() def test_post(self): """Posting data for a crowdfund should create it.""" user = UserFactory(is_staff=True) name = 'Project Crowdfund' description = 'A crowdfund' payment_required = 100 payment_capped = True date_due = date.today() + timedelta(20) data = { 'name': name, 'description': description, 'payment_required': payment_required, 'payment_capped': payment_capped, 'date_due': date_due } request = self.request_factory.post(self.url, data) request.user = user request = mock_middleware(request) response = self.view(request, slug=self.project.slug, pk=self.project.pk) self.project.refresh_from_db() eq_(self.project.crowdfunds.count(), 1, 'A crowdfund should be created and added to the project.') crowdfund = self.project.crowdfunds.first() eq_(crowdfund.name, name) eq_(crowdfund.description, description) expected_payment_required = Decimal(payment_required + payment_required * .15) / 100 eq_( crowdfund.payment_required, expected_payment_required, 'Expected payment of %(expected).2f, actually %(actual).2f' % { 'expected': expected_payment_required, 'actual': crowdfund.payment_required }) eq_(crowdfund.payment_capped, payment_capped) eq_(crowdfund.date_due, date_due) eq_(response.status_code, 302)
class TestProjectCrowdfundView(TestCase): """Tests the creation of a crowdfund for a project.""" def setUp(self): self.project = ProjectFactory(private=False, approved=True) self.url = reverse( "project-crowdfund", kwargs={"slug": self.project.slug, "pk": self.project.pk}, ) self.view = views.ProjectCrowdfundView.as_view() self.request_factory = RequestFactory() def test_get(self): """Users should be able to GET the ProjectCrowdfundView.""" user = UserFactory(is_staff=True) response = http_get_response( self.url, self.view, user, slug=self.project.slug, pk=self.project.pk ) eq_(response.status_code, 200) def test_post(self): """Posting data for a crowdfund should create it.""" user = UserFactory(is_staff=True) name = "Project Crowdfund" description = "A crowdfund" payment_required = 100 payment_capped = True date_due = date.today() + timedelta(20) data = { "name": name, "description": description, "payment_required": payment_required, "payment_capped": payment_capped, "date_due": date_due, } request = self.request_factory.post(self.url, data) request.user = user request = mock_middleware(request) response = self.view(request, slug=self.project.slug, pk=self.project.pk) self.project.refresh_from_db() eq_( self.project.crowdfunds.count(), 1, "A crowdfund should be created and added to the project.", ) crowdfund = self.project.crowdfunds.first() eq_(crowdfund.name, name) eq_(crowdfund.description, description) expected_payment_required = ( Decimal(payment_required + payment_required * 0.15) / 100 ) eq_( crowdfund.payment_required, expected_payment_required, "Expected payment of %(expected).2f, actually %(actual).2f" % { "expected": expected_payment_required, "actual": crowdfund.payment_required, }, ) eq_(crowdfund.payment_capped, payment_capped) eq_(crowdfund.date_due, date_due) eq_(response.status_code, 302)
class TestProjectEditView(TestCase): """Contributors and staff may edit a project.""" def setUp(self): # We will start with a project that's already been made. # We will give that project a single contributor. self.contributor = UserFactory() self.project = ProjectFactory() self.project.contributors.add(self.contributor) self.kwargs = {"slug": self.project.slug, "pk": self.project.pk} self.url = reverse("project-edit", kwargs=self.kwargs) self.view = views.ProjectEditView.as_view() def test_staff(self): """Staff users should be able to edit projects.""" staff_user = UserFactory(is_staff=True) response = http_get_response(self.url, self.view, staff_user, **self.kwargs) eq_(response.status_code, 200) def test_contributor(self): """Contributors should be able to edit projects.""" response = http_get_response( self.url, self.view, self.contributor, **self.kwargs ) eq_(response.status_code, 200) @raises(Http404) def test_basic(self): """Basic users should not be able to edit projects.""" user = UserFactory() http_get_response(self.url, self.view, user, **self.kwargs) def test_anonymous(self): """Logged out users cannot edit projects.""" response = http_get_response(self.url, self.view, AnonymousUser()) redirect_url = reverse("acct-login") + "?next=" + self.url eq_(response.status_code, 302, "The user should be redirected.") eq_( response.url, redirect_url, "The user should be redirected to the login page.", ) def test_edit_description(self): """ The description should be editable. When sending data, the 'edit' keyword should be set to 'description'. """ desc = "Lorem ipsum" data = {"title": self.project.title, "description": desc, "tags": ""} form = forms.ProjectUpdateForm(data, instance=self.project) ok_(form.is_valid(), "The form should validate. %s" % form.errors) http_post_response(self.url, self.view, data, self.contributor, **self.kwargs) self.project.refresh_from_db() eq_(self.project.description, desc, "The description should be updated.") @mock.patch("muckrock.message.tasks.notify_project_contributor.delay") def test_add_contributors(self, mock_notify): """When adding contributors, each new contributor should get an email notification.""" new_contributor = UserFactory() data = { "title": self.project.title, "contributors": [self.contributor.pk, new_contributor.pk], "tags": "", } form = forms.ProjectUpdateForm(data, instance=self.project) ok_(form.is_valid(), "The form should validate. %s" % form.errors) http_post_response(self.url, self.view, data, self.contributor, **self.kwargs) self.project.refresh_from_db() ok_(self.project.has_contributor(new_contributor)) ok_(self.project.has_contributor(self.contributor)) mock_notify.assert_called_once_with( new_contributor.pk, self.project.pk, self.contributor.pk )