Beispiel #1
0
class TestFOIARequestAppeal(RunCommitHooksMixin, TestCase):
    """A request should be able to send an appeal to the agency that receives them."""
    def setUp(self):
        self.appeal_agency = AppealAgencyFactory()
        self.agency = AgencyFactory(status='approved',
                                    appeal_agency=self.appeal_agency)
        self.foia = FOIARequestFactory(agency=self.agency, status='rejected')

    def test_appeal(self):
        """Sending an appeal to the agency should require the message for the appeal,
        which is then turned into a communication to the correct agency. In this case,
        the correct agency is the same one that received the message."""
        ok_(self.foia.has_perm(self.foia.user, 'appeal'),
            'The request should be appealable.')
        ok_(self.agency and self.agency.status == 'approved',
            'The agency should be approved.')
        ok_(self.appeal_agency.get_emails('appeal'),
            'The appeal agency should accept email.')
        # Create the appeal message and submit it
        appeal_message = 'Lorem ipsum'
        appeal_comm = self.foia.appeal(appeal_message, self.foia.user)
        # Check that everything happened like we expected
        self.foia.refresh_from_db()
        appeal_comm.refresh_from_db()
        self.run_commit_hooks()
        eq_(self.foia.email, self.appeal_agency.get_emails('appeal').first())
        eq_(self.foia.status, 'appealing')
        eq_(appeal_comm.communication, appeal_message)
        eq_(appeal_comm.from_user, self.foia.user)
        eq_(appeal_comm.to_user, self.agency.get_user())
        ok_(appeal_comm.emails.exists())

    def test_mailed_appeal(self):
        """Sending an appeal to an agency via mail should set the request to 'submitted',
        create a snail mail task with the 'a' category, and set the appeal communication
        delivery method to 'mail'."""
        # Make the appeal agency unreceptive to emails
        self.appeal_agency.emails.clear()
        # Create the appeal message and submit it
        appeal_message = 'Lorem ipsum'
        appeal_comm = self.foia.appeal(appeal_message, self.foia.user)
        # Check that everything happened like we expected
        self.foia.refresh_from_db()
        appeal_comm.refresh_from_db()
        self.run_commit_hooks()
        eq_(
            self.foia.status, 'submitted',
            'The status of the request should be updated. Actually: %s' %
            self.foia.status)
        eq_(
            appeal_comm.communication, appeal_message,
            'The appeal message parameter should be used as the body of the communication.'
        )
        eq_(appeal_comm.from_user, self.foia.user,
            'The appeal should be addressed from the request owner.')
        eq_(appeal_comm.to_user, self.agency.get_user(),
            'The appeal should be addressed to the agency.')
        task = SnailMailTask.objects.get(communication=appeal_comm)
        ok_(task, 'A snail mail task should be created.')
        eq_(task.category, 'a')
class TestFOIARequestAppeal(TestCase):
    """A request should be able to send an appeal to the agency that receives them."""
    def setUp(self):
        self.appeal_agency = AppealAgencyFactory()
        self.agency = AgencyFactory(status='approved',
                                    appeal_agency=self.appeal_agency)
        self.foia = FOIARequestFactory(agency=self.agency, status='rejected')

    def run_commit_hooks(self):
        """
        Fake transaction commit to run delayed on_commit functions
        https://medium.com/gitux/speed-up-django-transaction-hooks-tests-6de4a558ef96
        """
        for db_name in reversed(self._databases_names()):
            with mock.patch(
                    'django.db.backends.base.base.BaseDatabaseWrapper.'
                    'validate_no_atomic_block', lambda a: False):
                transaction.get_connection(
                    using=db_name, ).run_and_clear_commit_hooks()

    def test_appeal(self):
        """Sending an appeal to the agency should require the message for the appeal,
        which is then turned into a communication to the correct agency. In this case,
        the correct agency is the same one that received the message."""
        ok_(self.foia.has_perm(self.foia.user, 'appeal'),
            'The request should be appealable.')
        ok_(self.agency and self.agency.status == 'approved',
            'The agency should be approved.')
        ok_(self.appeal_agency.get_emails('appeal'),
            'The appeal agency should accept email.')
        # Create the appeal message and submit it
        appeal_message = 'Lorem ipsum'
        appeal_comm = self.foia.appeal(appeal_message, self.foia.user)
        # Check that everything happened like we expected
        self.foia.refresh_from_db()
        appeal_comm.refresh_from_db()
        self.run_commit_hooks()
        eq_(self.foia.email, self.appeal_agency.get_emails('appeal').first())
        eq_(self.foia.status, 'appealing')
        eq_(appeal_comm.communication, appeal_message)
        eq_(appeal_comm.from_user, self.foia.user)
        eq_(appeal_comm.to_user, self.agency.get_user())
        ok_(appeal_comm.emails.exists())

    def test_mailed_appeal(self):
        """Sending an appeal to an agency via mail should set the request to 'submitted',
        create a snail mail task with the 'a' category, and set the appeal communication
        delivery method to 'mail'."""
        # Make the appeal agency unreceptive to emails
        self.appeal_agency.emails.clear()
        # Create the appeal message and submit it
        appeal_message = 'Lorem ipsum'
        appeal_comm = self.foia.appeal(appeal_message, self.foia.user)
        # Check that everything happened like we expected
        self.foia.refresh_from_db()
        appeal_comm.refresh_from_db()
        eq_(
            self.foia.status, 'submitted',
            'The status of the request should be updated. Actually: %s' %
            self.foia.status)
        eq_(
            appeal_comm.communication, appeal_message,
            'The appeal message parameter should be used as the body of the communication.'
        )
        eq_(appeal_comm.from_user, self.foia.user,
            'The appeal should be addressed from the request owner.')
        eq_(appeal_comm.to_user, self.agency.get_user(),
            'The appeal should be addressed to the agency.')
        task = SnailMailTask.objects.get(communication=appeal_comm)
        ok_(task, 'A snail mail task should be created.')
        eq_(task.category, 'a')