Ejemplo n.º 1
0
 def setUp(self):
     self.foia1 = FOIARequestFactory()
     self.foia2 = FOIARequestFactory()
     self.comm = FOIACommunicationFactory(foia=self.foia1)
     self.file = FOIAFileFactory(comm=self.comm)
     eq_(self.comm.files.count(), 1)
     self.user = UserFactory()
Ejemplo n.º 2
0
    def test_soft_delete(self):
        """Test the soft delete method"""
        foia = FOIARequestFactory(status="processed")
        FOIAFileFactory.create_batch(size=3, comm__foia=foia)
        user = UserFactory(is_superuser=True)

        nose.tools.eq_(foia.get_files().count(), 3)
        nose.tools.eq_(
            RawEmail.objects.filter(email__communication__foia=foia).count(),
            3)

        foia.soft_delete(user, "final message", "note")
        foia.refresh_from_db()
        self.run_commit_hooks()

        # final communication we send out is not cleared
        for comm in list(foia.communications.all())[:-1]:
            nose.tools.eq_(comm.communication, "")
        nose.tools.eq_(foia.get_files().count(), 0)
        # one raw email left on the final outgoing message
        nose.tools.eq_(
            RawEmail.objects.filter(email__communication__foia=foia).count(),
            1)
        nose.tools.eq_(foia.last_request().communication, "final message")
        nose.tools.eq_(foia.notes.first().note, "note")

        nose.tools.ok_(foia.deleted)
        nose.tools.ok_(foia.embargo)
        nose.tools.ok_(foia.permanent_embargo)
        nose.tools.eq_(foia.status, "abandoned")
Ejemplo n.º 3
0
 def test_total_pages(self):
     """
     Jurisdictions should report the pages returned across their requests.
     State jurisdictions should include pages from their local jurisdictions.
     """
     page_count = 10
     local_comm = FOIACommunicationFactory(foia__agency__jurisdiction=self.local)
     state_comm = FOIACommunicationFactory(foia__agency__jurisdiction=self.state)
     local_comm.files.add(FOIAFileFactory(pages=page_count))
     state_comm.files.add(FOIAFileFactory(pages=page_count))
     eq_(self.local.total_pages(), page_count)
     eq_(self.state.total_pages(), 2 * page_count)
Ejemplo n.º 4
0
 def setUp(self):
     self.foia = FOIARequestFactory()
     self.comm = FOIACommunicationFactory(
         foia=self.foia,
         email__from_email=EmailAddress.objects.
         fetch(u'Test Email <*****@*****.**>'),
     )
     self.file = FOIAFileFactory(comm=self.comm)
     eq_(self.comm.files.count(), 1)
Ejemplo n.º 5
0
 def setUp(self):
     self.file = FOIAFileFactory()
     self.foia = self.file.comm.foia
     self.kwargs = {
         'idx': self.foia.pk,
         'slug': self.foia.slug,
         'jidx': self.foia.jurisdiction.pk,
         'jurisdiction': self.foia.jurisdiction.slug
     }
     self.url = reverse('foia-files', kwargs=self.kwargs)
     self.view = FOIAFileListView.as_view()
Ejemplo n.º 6
0
class TestCommunicationMove(test.TestCase):
    """Tests the move method"""

    def setUp(self):
        self.foia1 = FOIARequestFactory()
        self.foia2 = FOIARequestFactory()
        self.comm = FOIACommunicationFactory(foia=self.foia1)
        self.file = FOIAFileFactory(comm=self.comm)
        eq_(self.comm.files.count(), 1)
        self.user = UserFactory()

    @patch('muckrock.foia.tasks.upload_document_cloud.apply_async')
    def test_move_single_comm(self, mock_upload):
        """Should change the request associated with the communication."""
        moved_comms = self.comm.move([self.foia2.pk], self.user)
        eq_(len(moved_comms), 1, 'Move function should only return one item')
        moved_comm = moved_comms[0]
        eq_(
            moved_comm, self.comm,
            'Communication returned should be the same as the one acted on.'
        )
        eq_(
            moved_comm.foia.id, self.foia2.id,
            'Should change the FOIA associated with the communication.'
        )
        moved_files = moved_comm.files.all()
        moved_file = moved_files[0]
        logging.debug(
            'File foia: %d; Expected: %d', moved_file.comm.foia.id,
            self.foia2.id
        )
        eq_(
            moved_file.comm, self.comm,
            'Should not have changed the communication associated with the file.'
        )
        # a move log should be generated
        ok_(
            CommunicationMoveLog.objects.filter(
                communication=moved_comm,
                foia=self.foia1,
                user=self.user,
            ).exists()
        )
        mock_upload.assert_called()

    @patch('muckrock.foia.tasks.upload_document_cloud.apply_async')
    def test_move_multi_comms(self, mock_upload):
        """Should move the comm to the first request, then clone it to the rest."""
        comm_count = FOIACommunication.objects.count()
        comms = self.comm.move([self.foia1.id, self.foia2.id], self.user)
        # + 1 communications created
        self.comm.refresh_from_db()
        eq_(
            self.comm.foia.id, self.foia1.id,
            'The communication should be moved to the first listed request.'
        )
        eq_(
            FOIACommunication.objects.count(), comm_count + 1,
            'A clone should be made for each additional request in the list.'
        )
        eq_(
            len(comms), 2,
            'Two communications should be returned, since two were operated on.'
        )
        eq_(
            self.comm.pk, comms[0].pk,
            'The first communication in the list should be this one.'
        )
        ok_(
            comms[1].pk is not self.comm.pk,
            'The second communication should be a new one, since it was cloned.'
        )
        # each comm should have a move log generated for it
        for comm in comms:
            ok_(
                CommunicationMoveLog.objects.filter(
                    communication=comm,
                    foia=self.foia1,
                    user=self.user,
                ).exists()
            )
        mock_upload.assert_called()

    @raises(ValueError)
    def test_move_invalid_foia(self):
        """Should raise an error if trying to call move on invalid request pks."""
        original_request = self.comm.foia.id
        self.comm.move('abc', self.user)
        self.comm.refresh_from_db()
        eq_(
            self.comm.foia.id, original_request,
            'If something goes wrong, the move should not complete.'
        )

    @raises(ValueError)
    def test_move_empty_list(self):
        """Should raise an error if trying to call move on an empty list."""
        original_request = self.comm.foia.id
        self.comm.move([], self.user)
        self.comm.refresh_from_db()
        eq_(
            self.comm.foia.id, original_request,
            'If something goes wrong, the move should not complete.'
        )

    def test_move_missing_ffile(self):
        """
        The move operation should not crash when FOIAFile has a null ffile field.
        """
        self.file.ffile = None
        self.file.save()
        ok_(not self.comm.files.all()[0].ffile)
        self.comm.move([self.foia2.pk], self.user)
Ejemplo n.º 7
0
 def setUp(self):
     self.comm = FOIACommunicationFactory()
     self.file = FOIAFileFactory(comm=self.comm)
     ok_(self.file in self.comm.files.all())
     self.user = UserFactory()
Ejemplo n.º 8
0
class TestCommunicationClone(test.TestCase):
    """Tests the clone method"""

    def setUp(self):
        self.comm = FOIACommunicationFactory()
        self.file = FOIAFileFactory(comm=self.comm)
        ok_(self.file in self.comm.files.all())
        self.user = UserFactory()

    @patch('muckrock.foia.tasks.upload_document_cloud.apply_async')
    def test_clone_single(self, mock_upload):
        """Should duplicate the communication to the request."""
        other_foia = FOIARequestFactory()
        comm_count = FOIACommunication.objects.count()
        comm_pk = self.comm.pk
        clone_comm = self.comm.clone([other_foia], self.user)
        # + 1 communications
        eq_(
            FOIACommunication.objects.count(), comm_count + 1,
            'Should clone the request once.'
        )
        eq_(
            self.comm.pk, comm_pk,
            'The identity of the communication that calls clone should not change.'
        )
        # a move log should be generated for cloned request
        ok_(
            CommunicationMoveLog.objects.filter(
                communication=clone_comm[0],
                foia=self.comm.foia,
                user=self.user,
            ).exists()
        )
        # a move log should not be generated for the original request
        nose.tools.assert_false(
            CommunicationMoveLog.objects.filter(communication=self.comm,
                                                ).exists()
        )
        mock_upload.assert_called()

    @patch('muckrock.foia.tasks.upload_document_cloud.apply_async')
    def test_clone_multi(self, mock_upload):
        """Should duplicate the communication to each request in the list."""
        first_foia = FOIARequestFactory()
        second_foia = FOIARequestFactory()
        third_foia = FOIARequestFactory()
        comm_count = FOIACommunication.objects.count()
        clones = self.comm.clone(
            [first_foia, second_foia, third_foia],
            self.user,
        )
        # + 3 communications
        eq_(
            FOIACommunication.objects.count(), comm_count + 3,
            'Should clone the request twice.'
        )
        ok_(
            clones[0].pk is not clones[1].pk is not clones[2].pk,
            'The returned list should contain unique communcation objects.'
        )
        # a move log should be generated for each cloned request
        for clone in clones:
            ok_(
                CommunicationMoveLog.objects.filter(
                    communication=clone,
                    foia=self.comm.foia,
                    user=self.user,
                ).exists()
            )
        mock_upload.assert_called()

    @patch('muckrock.foia.tasks.upload_document_cloud.apply_async')
    def test_clone_files(self, mock_upload):
        """Should duplicate all the files for each communication."""
        first_foia = FOIARequestFactory()
        second_foia = FOIARequestFactory()
        third_foia = FOIARequestFactory()
        file_count = self.comm.files.count()
        clones = self.comm.clone(
            [first_foia, second_foia, third_foia],
            self.user,
        )
        for each_clone in clones:
            eq_(
                each_clone.files.count(), file_count,
                'Each clone should have its own set of files.'
            )
        mock_upload.assert_called()

    @raises(ValueError)
    def test_clone_empty_list(self):
        """Should throw a value error if given an empty list"""
        self.comm.clone([], self.user)

    @raises(ValueError)
    def test_clone_bad_pk(self):
        """Should throw an error if bad foia PK given"""
        self.comm.clone('abc', self.user)

    def test_clone_missing_ffile(self):
        """
        The clone operation should not crash when FOIAFile has a null ffile field.
        """
        self.file.ffile = None
        self.file.save()
        ok_(not self.comm.files.all()[0].ffile)
        other_foia = FOIARequestFactory()
        self.comm.clone([other_foia], self.user)