Beispiel #1
0
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
        )
Beispiel #2
0
class TestProject(TestCase):
    """Projects are a mixture of general and specific information on a broad subject."""

    def setUp(self):
        self.project = ProjectFactory(title=test_title)

    def test_project_attributes(self):
        """
        All projects need at least a title.
        Projects should be private by default.
        Projects should be unapproved by default.
        Projects should have a statement describing their purpose
        and an image or illustration to accompany them.
        """
        ok_(self.project)
        eq_(self.project.title, test_title)
        ok_(self.project.private)
        ok_(not self.project.approved)
        self.project.summary = test_summary
        self.project.description = test_description
        self.project.image = test_image
        self.project.save()
        ok_(self.project)

    def test_project_unicode(self):
        """Projects should default to printing their title."""
        eq_(self.project.__unicode__(), test_title)

    def test_contributors(self):
        """
        A project should keep a list of contributors,
        but a list of contributors should not be required.
        """
        user1 = UserFactory()
        user2 = UserFactory()
        self.project.contributors.add(user1, user2)
        ok_(
            user1 in self.project.contributors.all()
            and user2 in self.project.contributors.all()
        )
        self.project.contributors.clear()
        eq_(len(self.project.contributors.all()), 0)

    def test_articles(self):
        """Projects should keep a list of relevant articles."""
        article1 = ArticleFactory()
        article2 = ArticleFactory()
        self.project.articles.add(article1, article2)
        ok_(article1 in self.project.articles.all())
        ok_(article2 in self.project.articles.all())
        self.project.articles.clear()
        eq_(len(self.project.articles.all()), 0)

    def test_requests(self):
        """Projects should keep a list of relevant FOIA requests."""
        request1 = FOIARequestFactory()
        request2 = FOIARequestFactory()
        self.project.requests.add(request1, request2)
        ok_(request1 in self.project.requests.all())
        ok_(request2 in self.project.requests.all())
        self.project.articles.clear()
        eq_(len(self.project.articles.all()), 0)

    def test_make_public(self):
        """Projects can be made public, but they shouldn't be approved."""
        self.project.make_public()
        ok_(not self.project.private)
        ok_(not self.project.approved)

    def test_has_contributors(self):
        """Projects should test to see if a given user is a contributor."""
        user1 = UserFactory()
        user2 = UserFactory()
        self.project.contributors.add(user1)
        ok_(self.project.has_contributor(user1))
        ok_(not self.project.has_contributor(user2))

    def test_editable_by(self):
        """Projects should test to see if a given user can edit a request."""
        user1 = UserFactory()
        user2 = UserFactory()
        self.project.contributors.add(user1)
        ok_(self.project.editable_by(user1))
        ok_(not self.project.editable_by(user2))

    def test_publish(self):
        """Publishing a project should make it public and submit it for approval."""
        explanation = 'Test'
        task = self.project.publish(explanation)
        eq_(self.project.private, False, 'The project should be made public.')
        eq_(
            self.project.approved, False,
            'The project should be waiting approval.'
        )
        ok_(
            isinstance(task, ProjectReviewTask),
            'A ProjectReviewTask should be created.\n\tTask: %s' % type(task)
        )

    def test_suggest_requests(self):
        """
        Projects should recommend requests to be added to them.
        They should recommend requests that intersect both the
        project's set of contributors and the project's set of tags.
        But projects should not recommend requests that they already contain.
        """
        # set up data
        tags = u'a'
        user = UserFactory()
        self.project.contributors.add(user)
        self.project.tags.add(tags)
        test_request = FOIARequestFactory(composer__user=user)
        test_request.tags.add(tags)
        # since they have the same user and tags, the project should suggest the request
        ok_(test_request in self.project.suggest_requests())
        # add the request to the project, then try again. it should not be suggested
        self.project.requests.add(test_request)
        ok_(test_request not in self.project.suggest_requests())

    def test_suggest_articles(self):
        """
        Projects should recommend articles to be added to them.
        They should recommend articles that intersect both the
        project's set of contributors and the project's set of tags.
        But projects should not recommend articles that they already contain.
        """
        # set up data
        tags = u'a'
        user = UserFactory()
        self.project.contributors.add(user)
        self.project.tags.add(tags)
        test_article = ArticleFactory()
        test_article.authors.add(user)
        test_article.tags.add(tags)
        # since they have the same user and tags, the project should suggest the article.
        ok_(test_article in self.project.suggest_articles())
        # add the article to the project, then try again. it should not be suggested
        self.project.articles.add(test_article)
        ok_(test_article not in self.project.suggest_articles())