def test_pending(self): """Projects that are pending review should reject access to the Publish view.""" pending_project = ProjectFactory(private=False, approved=False) pending_project.contributors.add(self.contributor) response = http_get_response( self.url, self.view, self.contributor, **{ 'slug': pending_project.slug, 'pk': pending_project.pk }) eq_(response.status_code, 302) eq_(response.url, pending_project.get_absolute_url(), 'The user should be redirected to the project.')
class TestProjectPublishView(TestCase): """Tests publishing a project.""" def setUp(self): # We will start with a project that's already been made. self.project = ProjectFactory(private=True, approved=False) self.contributor = UserFactory() self.project.contributors.add(self.contributor) self.kwargs = {"slug": self.project.slug, "pk": self.project.pk} self.url = reverse("project-publish", kwargs=self.kwargs) self.view = views.ProjectPublishView.as_view() def test_staff(self): """Staff users should be able to publish 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 delete 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 delete projects.""" user = UserFactory() http_get_response(self.url, self.view, user, **self.kwargs) def test_anonymous(self): """Anonymous users cannot delete projects.""" response = http_get_response( self.url, self.view, AnonymousUser(), **self.kwargs ) 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 reidrected to the login screen.", ) def test_pending(self): """Projects that are pending review should reject access to the Publish view.""" pending_project = ProjectFactory(private=False, approved=False) pending_project.contributors.add(self.contributor) response = http_get_response( self.url, self.view, self.contributor, **{"slug": pending_project.slug, "pk": pending_project.pk} ) eq_(response.status_code, 302) eq_( response.url, pending_project.get_absolute_url(), "The user should be redirected to the project.", ) @mock.patch("muckrock.project.models.Project.publish") def test_post(self, mock_publish): """Posting a valid ProjectPublishForm should publish the project.""" notes = "Testing project publishing" form = forms.ProjectPublishForm({"notes": notes}) ok_(form.is_valid(), "The form should validate.") response = http_post_response( self.url, self.view, form.data, self.contributor, **self.kwargs ) eq_(response.status_code, 302, "The user should be redirected.") eq_( response.url, self.project.get_absolute_url(), "The user should be redirected back to the project page.", ) mock_publish.assert_called_with(notes)