示例#1
0
 def setUp(self):
     password = "******"
     self.user = UserFactory(is_staff=True, password=password)
     self.url = reverse("projectreview-task-list")
     self.view = ProjectReviewTaskList.as_view()
     self.task = ProjectReviewTaskFactory()
     self.request_factory = RequestFactory()
     self.client.login(username=self.user.username, password=password)
class ProjectReviewTaskTests(TestCase):
    """
    The ProjectReviewTask provides us a way to moderate community projects.
    When it is created, it should notify Slack.
    When it is approved, it should mark its project approved.
    When it is rejected, it should mark its project private.
    It should allow us a way to communicate with the users of this project.
    """
    def setUp(self):
        self.task = ProjectReviewTaskFactory()
        contributor = UserFactory()
        self.task.project.contributors.add(contributor)

    def test_get_aboslute_url(self):
        _url = reverse('projectreview-task', kwargs={'pk': self.task.pk})
        eq_(self.task.get_absolute_url(), _url)

    @mock.patch('muckrock.message.email.TemplateEmail.send')
    def test_reply(self, mock_feedback_send):
        self.task.reply('Lorem ipsum')
        mock_feedback_send.assert_called_with(fail_silently=False)

    @mock.patch('muckrock.message.email.TemplateEmail.send')
    def test_approve(self, mock_notification_send):
        """Approving the task should mark it approved and notify the contributors."""
        self.task.approve('Lorem ipsum')
        ok_(self.task.project.approved, 'The project should be approved')
        mock_notification_send.assert_called_with(fail_silently=False)

    @mock.patch('muckrock.message.email.TemplateEmail.send')
    def test_reject(self, mock_notification_send):
        """Rejecting the task should mark it private and notify the contributors."""
        self.task.reject('Lorem ipsum')
        ok_(self.task.project.private, 'The project should be made private.')
        mock_notification_send.assert_called_with(fail_silently=False)
示例#3
0
 def setUp(self):
     self.task = ProjectReviewTaskFactory()
     contributor = UserFactory()
     self.task.project.contributors.add(contributor)
示例#4
0
class ProjectReviewTaskViewTests(TestCase):
    """Tests FlaggedTask POST handlers"""

    def setUp(self):
        password = "******"
        self.user = UserFactory(is_staff=True, password=password)
        self.url = reverse("projectreview-task-list")
        self.view = ProjectReviewTaskList.as_view()
        self.task = ProjectReviewTaskFactory()
        self.request_factory = RequestFactory()
        self.client.login(username=self.user.username, password=password)

    def test_get_single(self, mock_reply):
        """Should be able to view a single task"""
        # pylint: disable=unused-argument
        _url = reverse("projectreview-task", kwargs={"pk": self.task.pk})
        request = self.request_factory.get(_url)
        request.user = self.user
        request = mock_middleware(request)
        response = self.view(request)
        eq_(response.status_code, 200)

    def post_request(self, data):
        """Helper to post data and get a response"""
        request = self.request_factory.post(self.url, data)
        request.user = self.user
        request = mock_middleware(request)
        response = self.view(request)
        return response

    def test_post_reply(self, mock_reply):
        """Staff should be able to reply to the user who raised the flag"""
        test_text = "Lorem ipsum"
        form = ProjectReviewTaskForm({"reply": test_text})
        ok_(form.is_valid())
        post_data = form.cleaned_data
        post_data.update({"action": "reply", "task": self.task.pk})
        self.post_request(post_data)
        self.task.refresh_from_db()
        ok_(
            not self.task.resolved,
            "The task should not automatically resolve when replying.",
        )
        mock_reply.assert_called_with(test_text)

    def test_post_reply_approve(self, mock_reply):
        """The task should optionally resolve when replying"""
        test_text = "Lorem ipsum"
        form = ProjectReviewTaskForm({"reply": test_text})
        ok_(form.is_valid())
        post_data = form.cleaned_data
        post_data.update({"action": "approve", "task": self.task.pk})
        self.post_request(post_data)
        self.task.refresh_from_db()
        self.task.project.refresh_from_db()
        ok_(self.task.project.approved, "The project should be approved.")
        ok_(self.task.resolved, "The task should be resolved.")
        mock_reply.assert_called_with(test_text, "approved")

    def test_post_reply_reject(self, mock_reply):
        """The task should optionally resolve when replying"""
        test_text = "Lorem ipsum"
        form = ProjectReviewTaskForm({"reply": test_text})
        ok_(form.is_valid())
        post_data = form.cleaned_data
        post_data.update({"action": "reject", "task": self.task.pk})
        self.post_request(post_data)
        self.task.refresh_from_db()
        self.task.project.refresh_from_db()
        ok_(self.task.project.private, "The project should be made private.")
        ok_(self.task.resolved, "The task should be resolved.")
        mock_reply.assert_called_with(test_text, "rejected")

    @tag("slow")
    def _test_n_plus_one_query(self, mock_reply):
        """Should make the same number of SQL queries regardless of amount of data"""
        # pylint: disable=unused-argument
        n_plus_one_query(self.client, self.url, ProjectReviewTaskFactory)
示例#5
0
 def setUp(self):
     self.user = UserFactory(is_staff=True)
     self.url = reverse('projectreview-task-list')
     self.view = ProjectReviewTaskList.as_view()
     self.task = ProjectReviewTaskFactory()
     self.request_factory = RequestFactory()
示例#6
0
class ProjectReviewTaskViewTests(TestCase):
    """Tests FlaggedTask POST handlers"""
    def setUp(self):
        self.user = UserFactory(is_staff=True)
        self.url = reverse('projectreview-task-list')
        self.view = ProjectReviewTaskList.as_view()
        self.task = ProjectReviewTaskFactory()
        self.request_factory = RequestFactory()

    def test_get_single(self, mock_reply):
        """Should be able to view a single task"""
        # pylint: disable=unused-argument
        _url = reverse('projectreview-task', kwargs={'pk': self.task.pk})
        request = self.request_factory.get(_url)
        request.user = self.user
        request = mock_middleware(request)
        response = self.view(request)
        eq_(response.status_code, 200)

    def post_request(self, data):
        """Helper to post data and get a response"""
        request = self.request_factory.post(self.url, data)
        request.user = self.user
        request = mock_middleware(request)
        response = self.view(request)
        return response

    def test_post_reply(self, mock_reply):
        """Staff should be able to reply to the user who raised the flag"""
        test_text = 'Lorem ipsum'
        form = ProjectReviewTaskForm({'reply': test_text})
        ok_(form.is_valid())
        post_data = form.cleaned_data
        post_data.update({'action': 'reply', 'task': self.task.pk})
        self.post_request(post_data)
        self.task.refresh_from_db()
        ok_(not self.task.resolved,
            'The task should not automatically resolve when replying.')
        mock_reply.assert_called_with(test_text)

    def test_post_reply_approve(self, mock_reply):
        """The task should optionally resolve when replying"""
        test_text = 'Lorem ipsum'
        form = ProjectReviewTaskForm({'reply': test_text})
        ok_(form.is_valid())
        post_data = form.cleaned_data
        post_data.update({'action': 'approve', 'task': self.task.pk})
        self.post_request(post_data)
        self.task.refresh_from_db()
        self.task.project.refresh_from_db()
        ok_(self.task.project.approved, 'The project should be approved.')
        ok_(self.task.resolved, 'The task should be resolved.')
        mock_reply.assert_called_with(test_text, 'approved')

    def test_post_reply_reject(self, mock_reply):
        """The task should optionally resolve when replying"""
        test_text = 'Lorem ipsum'
        form = ProjectReviewTaskForm({'reply': test_text})
        ok_(form.is_valid())
        post_data = form.cleaned_data
        post_data.update({'action': 'reject', 'task': self.task.pk})
        self.post_request(post_data)
        self.task.refresh_from_db()
        self.task.project.refresh_from_db()
        ok_(self.task.project.private, 'The project should be made private.')
        ok_(self.task.resolved, 'The task should be resolved.')
        mock_reply.assert_called_with(test_text, 'rejected')