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')
def test_request_lifecycle_no_email(self, mock_request): """Test a request going through the full cycle as if we had to physically mail it """ # pylint: disable=too-many-statements # pylint: disable=protected-access mock_squarelet(mock_request) mail.outbox = [] user = UserFactory(membership__organization__number_requests=1) agency = AgencyFactory(email=None, fax=None) cal = agency.jurisdiction.get_calendar() with freeze_time("2010-02-01"): nose.tools.eq_(len(mail.outbox), 0) ## create and submit request composer = FOIAComposerFactory( user=user, organization=user.profile.organization, title="Test with no email", agencies=[agency], ) composer.submit() foia = FOIARequest.objects.get(composer=composer) comm = foia.communications.last() self.run_commit_hooks() # check that a snail mail task was created nose.tools.ok_( SnailMailTask.objects.filter(communication=comm, category="n").exists()) ## two days pass, then the admin mails in the request with freeze_time("2010-02-03"): foia.status = "processed" foia.update_dates() foia.save() # make sure dates were set correctly nose.tools.eq_(foia.composer.datetime_submitted, datetime(2010, 2, 1, tzinfo=pytz.utc)) nose.tools.eq_( foia.date_due, cal.business_days_from(date(2010, 2, 1), agency.jurisdiction.days), ) nose.tools.eq_( foia.date_followup, max( foia.date_due, foia.communications.last().datetime.date() + timedelta(foia._followup_days()), ), ) nose.tools.ok_(foia.days_until_due is None) # no more mail should have been sent nose.tools.eq_(len(mail.outbox), 0) old_date_due = foia.date_due ## after 5 days agency replies with a fix needed with freeze_time("2010-02-08"): comm = FOIACommunication.objects.create( foia=foia, from_user=agency.get_user(), to_user=user, datetime=timezone.now(), response=True, communication="Test communication", ) foia.status = "fix" foia.save() foia.update(comm.anchor()) # make sure dates were set correctly nose.tools.eq_(foia.composer.datetime_submitted, datetime(2010, 2, 1, tzinfo=pytz.utc)) nose.tools.ok_(foia.date_due is None) nose.tools.ok_(foia.date_followup is None) nose.tools.eq_( foia.days_until_due, cal.business_days_between(date(2010, 2, 8), old_date_due), ) old_days_until_due = foia.days_until_due ## after 10 days the user submits the fix and the admin submits it right away with freeze_time("2010-02-18"): comm = FOIACommunication.objects.create( foia=foia, from_user=user, to_user=agency.get_user(), datetime=timezone.now(), response=False, communication="Test communication", ) foia.status = "submitted" foia.save() foia.submit() self.run_commit_hooks() # check that another snail mail task is created nose.tools.ok_( SnailMailTask.objects.filter(communication=comm, category="u").exists()) foia.status = "processed" foia.update_dates() foia.save() # make sure dates were set correctly nose.tools.eq_(foia.composer.datetime_submitted, datetime(2010, 2, 1, tzinfo=pytz.utc)) nose.tools.eq_( foia.date_due, cal.business_days_from(date.today(), old_days_until_due)) nose.tools.eq_( foia.date_followup, max( foia.date_due, foia.communications.last().datetime.date() + timedelta(foia._followup_days()), ), ) nose.tools.ok_(foia.days_until_due is None) old_date_due = foia.date_due ## after 4 days agency replies with the documents with freeze_time("2010-02-22"): comm = FOIACommunication.objects.create( foia=foia, from_user=agency.get_user(), to_user=user, datetime=timezone.now(), response=True, communication="Test communication", ) foia.status = "done" foia.save() foia.update(comm.anchor()) # make sure dates were set correctly nose.tools.eq_(foia.composer.datetime_submitted, datetime(2010, 2, 1, tzinfo=pytz.utc)) nose.tools.eq_(foia.date_due, old_date_due) nose.tools.ok_(foia.date_followup is None) nose.tools.ok_(foia.days_until_due is None)
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')