def test_normal(self): """Test a normal succesful response""" foia = FOIARequestFactory(status='ack') from_name = 'Smith, Bob' from_email = '*****@*****.**' from_ = '"%s" <%s>' % (from_name, from_email) to_ = '%s, "Doe, John" <*****@*****.**>' % foia.get_request_email() subject = 'Test subject' text = 'Test normal.' signature = '-Charlie Jones' self.mailgun_route(from_, to_, subject, text, signature) foia.refresh_from_db() last_comm = foia.communications.last() nose.tools.eq_(last_comm.communication, '%s\n%s' % (text, signature)) nose.tools.eq_(last_comm.subject, subject) nose.tools.eq_(last_comm.from_user, foia.agency.get_user()) nose.tools.eq_(last_comm.to_user, foia.user) nose.tools.eq_(last_comm.response, True) nose.tools.eq_(last_comm.full_html, False) nose.tools.ok_(last_comm.get_raw_email()) nose.tools.eq_(last_comm.responsetask_set.count(), 1) nose.tools.eq_(foia.email, EmailAddress.objects.fetch(from_email)) nose.tools.eq_( set(foia.cc_emails.all()), set(EmailAddress.objects.fetch_many('*****@*****.**')), ) nose.tools.eq_(foia.status, 'processed')
def test_bad_verify(self): """Test an improperly signed message""" foia = FOIARequestFactory(block_incoming=True) to_ = foia.get_request_email() response = self.mailgun_route(to_=to_, sign=False) nose.tools.eq_(response.status_code, 403)
def test_block_incoming(self): """Test receiving a message from an unauthorized sender""" foia = FOIARequestFactory(block_incoming=True) to_ = foia.get_request_email() text = "Test block incoming." signature = "-Too Late" self.mailgun_route(to_=to_, text=text, signature=signature) communication = FOIACommunication.objects.get(likely_foia=foia) nose.tools.eq_(communication.communication, "%s\n%s" % (text, signature)) nose.tools.ok_( OrphanTask.objects.filter( communication=communication, reason="ib", address=foia.get_request_email().split("@")[0], ).exists() )
def test_bad_sender(self): """Test receiving a message from an unauthorized sender""" foia = FOIARequestFactory() from_ = "*****@*****.**" to_ = foia.get_request_email() text = "Test bad sender." signature = "-Spammer" self.mailgun_route(from_, to_, text=text, signature=signature) communication = FOIACommunication.objects.get(likely_foia=foia) nose.tools.eq_(communication.communication, "%s\n%s" % (text, signature)) nose.tools.ok_( OrphanTask.objects.filter( communication=communication, reason="bs", address=foia.get_request_email().split("@")[0], ).exists() )
def test_bad_strip(self): """Test an improperly stripped message""" foia = FOIARequestFactory() to_ = foia.get_request_email() text = "" body = "Here is the full body." self.mailgun_route(to_=to_, text=text, body=body) last_comm = foia.communications.last() nose.tools.eq_(last_comm.communication, body)
def test_attachments(self): """Test a message with an attachment""" try: foia = FOIARequestFactory() to_ = foia.get_request_email() attachments = [StringIO("Good file"), StringIO("Ignore File")] attachments[0].name = "data.pdf" attachments[1].name = "ignore.p7s" self.mailgun_route(to_=to_, attachments=attachments) foia.refresh_from_db() file_path = date.today().strftime("foia_files/%Y/%m/%d/data.pdf") nose.tools.eq_(foia.get_files().count(), 1) nose.tools.eq_(foia.get_files().first().ffile.name, file_path) finally: foia.communications.first().files.first().delete() file_path = os.path.join(settings.SITE_ROOT, "static/media/", file_path) if os.path.exists(file_path): os.remove(file_path)
def test_deleted(self): """Test a message to a deleted request""" foia = FOIARequestFactory(status="abandoned", deleted=True) from_name = "Smith, Bob" from_email = "*****@*****.**" from_ = '"%s" <%s>' % (from_name, from_email) to_ = foia.get_request_email() subject = "Test subject" text = "Test normal." signature = "-Charlie Jones" self.mailgun_route(from_, to_, subject, text, signature) foia.refresh_from_db() # no communication should be created, and an autoreply sould be mailed out nose.tools.eq_(foia.communications.count(), 0) nose.tools.eq_( mail.outbox[0].body, render_to_string("text/foia/deleted_autoreply.txt") ) nose.tools.eq_(mail.outbox[0].to, [from_])