Exemplo 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()
Exemplo n.º 2
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)
Exemplo n.º 3
0
 def test_attach_file_with_content(self):
     """Test attaching a file with n memory content"""
     comm = FOIACommunicationFactory()
     comm.attach_file(content='More contents', name='doc.pdf')
     eq_(comm.files.count(), 1)
     foia_file = comm.files.first()
     eq_(foia_file.title, 'doc')
     eq_(foia_file.ffile.file.name, 'doc.pdf')
     eq_(foia_file.ffile.read(), 'More contents')
Exemplo n.º 4
0
    def test_open(self):
        """Test an open webhook"""
        # pylint: disable=too-many-locals

        recipient = "*****@*****.**"
        comm = FOIACommunicationFactory(
            foia__email=EmailAddress.objects.fetch(recipient)
        )
        email = comm.emails.first()
        event = "opened"
        city = "Boston"
        region = "MA"
        country = "US"
        client_type = "browser"
        client_name = "Chrome"
        client_os = "Linux"
        device_type = "desktop"
        user_agent = (
            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 "
            "(KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31"
        )
        ip_address = "50.56.129.169"
        data = {
            "event": event,
            "email_id": email.pk,
            "recipient": recipient,
            "city": city,
            "region": region,
            "country": country,
            "client-type": client_type,
            "client-name": client_name,
            "client-os": client_os,
            "device-type": device_type,
            "user-agent": user_agent,
            "ip": ip_address,
        }
        self.sign(data)
        request = self.factory.post(reverse("mailgun-opened"), data)
        opened(request)  # pylint: disable=no-value-for-parameter
        comm.refresh_from_db()

        nose.tools.ok_(
            EmailOpen.objects.filter(
                email=email,
                datetime=datetime(2017, 1, 2, 17, tzinfo=pytz.utc),
                recipient=EmailAddress.objects.fetch(recipient),
                city=city,
                region=region,
                country=country,
                client_type=client_type,
                client_name=client_name,
                client_os=client_os,
                device_type=device_type,
                user_agent=user_agent[:255],
                ip_address=ip_address,
            ).exists()
        )
Exemplo n.º 5
0
    def test_open(self):
        """Test an open webhook"""
        # pylint: disable=too-many-locals

        recipient = '*****@*****.**'
        comm = FOIACommunicationFactory(
            foia__email=EmailAddress.objects.fetch(recipient),
        )
        email = comm.emails.first()
        event = 'opened'
        city = 'Boston'
        region = 'MA'
        country = 'US'
        client_type = 'browser'
        client_name = 'Chrome'
        client_os = 'Linux'
        device_type = 'desktop'
        user_agent = (
            'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 '
            '(KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31'
        )
        ip_address = '50.56.129.169'
        data = {
            'event': event,
            'email_id': email.pk,
            'recipient': recipient,
            'city': city,
            'region': region,
            'country': country,
            'client-type': client_type,
            'client-name': client_name,
            'client-os': client_os,
            'device-type': device_type,
            'user-agent': user_agent,
            'ip': ip_address,
        }
        self.sign(data)
        request = self.factory.post(reverse('mailgun-opened'), data)
        opened(request)  # pylint: disable=no-value-for-parameter
        comm.refresh_from_db()

        nose.tools.ok_(
            EmailOpen.objects.filter(
                email=email,
                datetime=datetime(2017, 1, 2, 17, tzinfo=pytz.utc),
                recipient=EmailAddress.objects.fetch(recipient),
                city=city,
                region=region,
                country=country,
                client_type=client_type,
                client_name=client_name,
                client_os=client_os,
                device_type=device_type,
                user_agent=user_agent[:255],
                ip_address=ip_address,
            ).exists()
        )
Exemplo n.º 6
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)
Exemplo n.º 7
0
 def test_attach_file_with_file(self):
     """Test attaching a file with an actual file"""
     comm = FOIACommunicationFactory()
     file_ = open('tmp.txt', 'w')
     file_.write('The file contents')
     file_.close()
     file_ = open('tmp.txt', 'r')
     comm.attach_file(file_=file_)
     file_.close()
     os.remove('tmp.txt')
     eq_(comm.files.count(), 1)
     foia_file = comm.files.first()
     eq_(foia_file.title, 'tmp')
     eq_(foia_file.ffile.file.name, 'tmp.txt')
     eq_(foia_file.ffile.read(), 'The file contents')
Exemplo n.º 8
0
    def test_delivered(self):
        """Test a delivered webhook"""

        comm = FOIACommunicationFactory()
        email = comm.emails.first()
        data = {"event": "delivered", "email_id": email.pk}
        self.sign(data)
        request = self.factory.post(reverse("mailgun-delivered"), data)
        delivered(request)  # pylint: disable=no-value-for-parameter
        comm.refresh_from_db()

        nose.tools.eq_(
            comm.emails.first().confirmed_datetime,
            datetime(2017, 1, 2, 17, tzinfo=pytz.utc),
        )
Exemplo n.º 9
0
    def test_foia_followup(self):
        """Make sure the follow up date is set correctly"""
        # pylint: disable=protected-access
        foia = FOIARequestFactory(
            composer__datetime_submitted=timezone.now(),
            status="processed",
            agency__jurisdiction__level="s",
            agency__jurisdiction__law__days=10,
        )
        FOIACommunicationFactory(foia=foia, response=True)
        foia.followup()
        self.run_commit_hooks()
        nose.tools.assert_in("I can expect", mail.outbox[-1].body)
        nose.tools.eq_(
            foia.date_followup,
            foia.communications.last().datetime.date() +
            timedelta(foia._followup_days()),
        )

        nose.tools.eq_(foia._followup_days(), 15)

        num_days = 365
        foia.date_estimate = date.today() + timedelta(num_days)
        foia.followup()
        self.run_commit_hooks()
        nose.tools.assert_in("I am still", mail.outbox[-1].body)
        nose.tools.eq_(foia._followup_days(), num_days)

        foia.date_estimate = date.today()
        foia.followup()
        self.run_commit_hooks()
        nose.tools.assert_in("check on the status", mail.outbox[-1].body)
        nose.tools.eq_(foia._followup_days(), 15)
Exemplo n.º 10
0
 def test_post_status(self):
     """A user updating the status of their request should update the status,
     open a status change task, and close any open response tasks"""
     nose.tools.assert_not_equal(self.foia.status, 'done')
     eq_(
         len(
             StatusChangeTask.objects.filter(
                 foia=self.foia,
                 user=self.foia.user,
                 resolved=False,
             )
         ), 0
     )
     communication = FOIACommunicationFactory(foia=self.foia)
     response_task = ResponseTaskFactory(
         communication=communication,
         resolved=False,
     )
     data = {'action': 'status', 'status': 'done'}
     http_post_response(
         self.url, self.view, data, self.foia.user, **self.kwargs
     )
     self.foia.refresh_from_db()
     eq_(self.foia.status, 'done')
     eq_(
         len(
             StatusChangeTask.objects.filter(
                 foia=self.foia,
                 user=self.foia.user,
                 resolved=False,
             )
         ), 1
     )
     response_task.refresh_from_db()
     ok_(response_task.resolved)
Exemplo n.º 11
0
 def test_confirm_open(self):
     """Test receiving a confirmation message"""
     comm = FOIACommunicationFactory(
         subject="Your first record request 17-1 has been opened.",
         communication=
         " -- Write ABOVE THIS LINE to post a message that will be sent "
         "to staff. --\n\n"
         "Your first Evanston record request (request number 17-764) "
         "has been submitted. It is currently unpublished and is not "
         "available for the general public to view.\n\n"
         "As the requester, you can always see the status of your "
         "request by signing into the Evanston Public Records portal "
         "here. \n",
         foia__status="ack",
     )
     self.portal.receive_msg(comm)
     comm = FOIACommunication.objects.get(pk=comm.pk)
     eq_(comm.foia.status, "processed")
     eq_(comm.foia.current_tracking_id(), "17-1")
     eq_(
         comm.communication,
         "Your first Evanston record request (request number 17-764) "
         "has been submitted. It is currently unpublished and is not "
         "available for the general public to view.\n\n",
     )
     assert_false(comm.hidden)
     eq_(comm.portals.count(), 1)
Exemplo n.º 12
0
 def test_send_msg(self):
     """Sending a message should create a portal task"""
     comm = FOIACommunicationFactory()
     self.portal.send_msg(comm)
     ok_(
         PortalTask.objects.filter(category="n",
                                   communication=comm).exists())
Exemplo n.º 13
0
 def setUp(self):
     """Set up for tests"""
     self.comm = FOIACommunicationFactory(
         foia__composer__user__profile__acct_type='pro')
     self.request_factory = RequestFactory()
     self.url = reverse('foia-raw', kwargs={'idx': self.comm.id})
     self.view = raw
Exemplo n.º 14
0
 def test_pdf_emoji(self):
     """Strip emojis to prevent PDF generation from crashing"""
     comm = FOIACommunicationFactory(
         communication=u'Thank you\U0001f60a\n\n')
     pdf = SnailMailPDF(comm, 'n')
     pdf.generate()
     pdf.output(dest='S')
Exemplo n.º 15
0
 def test_another_appeal(self):
     """The appeal was unsuccessful if a subsequent communication has an Appeal as well."""
     subsequent_communication = FOIACommunicationFactory(
         foia=self.appeal.communication.foia, status="done"
     )
     factories.AppealFactory(communication=subsequent_communication)
     eq_(self.appeal.is_successful(), False)
Exemplo n.º 16
0
 def setUp(self):
     """Set up for tests"""
     self.comm = FOIACommunicationFactory(
         foia__composer__user=ProfessionalUserFactory())
     self.request_factory = RequestFactory()
     self.url = reverse('foia-raw', kwargs={'idx': self.comm.id})
     self.view = raw
Exemplo n.º 17
0
 def test_receive_msg(self):
     """Receiving a message should create a portal task"""
     comm = FOIACommunicationFactory()
     self.portal.receive_msg(comm)
     ok_(
         PortalTask.objects.filter(category="i",
                                   communication=comm).exists())
Exemplo n.º 18
0
 def test_pdf_emoji(self):
     """Strip emojis to prevent PDF generation from crashing"""
     comm = FOIACommunicationFactory(
         communication="Thank you\U0001f60a\n\n")
     pdf = SnailMailPDF(comm, "n", switch=False)
     pdf.generate()
     pdf.output(dest="S").encode("latin-1")
Exemplo n.º 19
0
 def _test_approve(self, mock_submit):
     submitted_foia = FOIARequestFactory(agency=self.agency,
                                         status='submitted')
     FOIACommunicationFactory(foia=submitted_foia)
     self.task.approve()
     eq_(self.task.agency.status, 'approved')
     eq_(mock_submit.call_count, 1)
Exemplo n.º 20
0
 def setUp(self):
     user = UserFactory()
     agency = AgencyFactory(status="pending")
     self.foia = FOIARequestFactory(composer__user=user, agency=agency)
     self.comm = FOIACommunicationFactory(foia=self.foia, response=True)
     # tasks that incorporate FOIAs are:
     # ResponseTask, SnailMailTask, FlaggedTask,
     # StatusChangeTask, NewAgencyTask
     response_task = ResponseTask.objects.create(communication=self.comm)
     snail_mail_task = SnailMailTask.objects.create(category="a",
                                                    communication=self.comm)
     flagged_task = FlaggedTask.objects.create(user=user,
                                               text="Halp",
                                               foia=self.foia)
     status_change_task = StatusChangeTask.objects.create(user=user,
                                                          old_status="ack",
                                                          foia=self.foia)
     new_agency_task = NewAgencyTask.objects.create(user=user,
                                                    agency=agency)
     self.tasks = [
         response_task,
         snail_mail_task,
         flagged_task,
         status_change_task,
         new_agency_task,
     ]
Exemplo n.º 21
0
 def setUp(self):
     self.comm = FOIACommunicationFactory(
         email__from_email__email='*****@*****.**', )
     self.task = OrphanTask.objects.create(reason='ib',
                                           communication=self.comm,
                                           address='Whatever Who Cares')
     self.user = UserFactory()
Exemplo n.º 22
0
 def test_confirm_open(self):
     """Test receiving a confirmation message"""
     comm = FOIACommunicationFactory(subject="eFOIA Request Received",
                                     foia__status="ack")
     self.portal.receive_msg(comm)
     eq_(comm.foia.status, "processed")
     eq_(comm.portals.count(), 1)
Exemplo n.º 23
0
    def test_foia_followup(self):
        """Make sure the follow up date is set correctly"""
        # pylint: disable=protected-access
        foia = FOIARequestFactory(
            composer__datetime_submitted=timezone.now(),
            status='processed',
            agency__jurisdiction__level='s',
            agency__jurisdiction__law__days=10,
        )
        FOIACommunicationFactory(
            foia=foia,
            response=True,
        )
        foia.followup()
        nose.tools.assert_in('I can expect', mail.outbox[-1].body)
        nose.tools.eq_(
            foia.date_followup,
            date.today() + timedelta(foia._followup_days())
        )

        nose.tools.eq_(foia._followup_days(), 15)

        num_days = 365
        foia.date_estimate = date.today() + timedelta(num_days)
        foia.followup()
        nose.tools.assert_in('I am still', mail.outbox[-1].body)
        nose.tools.eq_(foia._followup_days(), num_days)

        foia.date_estimate = date.today()
        foia.followup()
        nose.tools.assert_in('check on the status', mail.outbox[-1].body)
        nose.tools.eq_(foia._followup_days(), 15)
Exemplo n.º 24
0
 def test_attach_file_with_file(self):
     """Test attaching a file with an actual file"""
     try:
         comm = FOIACommunicationFactory()
         with open("tmp.txt", "w") as file_:
             file_.write("The file contents")
         with open("tmp.txt", "r") as file_:
             comm.attach_file(file_=file_)
         eq_(comm.files.count(), 1)
         foia_file = comm.files.first()
         eq_(foia_file.title, "tmp")
         eq_(foia_file.ffile.file.name, "tmp.txt")
         eq_(foia_file.ffile.read(), "The file contents")
     finally:
         try:
             os.remove("tmp.txt")
         except OSError:
             pass
Exemplo n.º 25
0
 def test_classifier(self):
     """Classifier should populate the fields on the response task"""
     comm = FOIACommunicationFactory(
         communication="Here are your responsive documents")
     task = ResponseTaskFactory(communication=comm)
     classify_status.apply(args=(task.pk, ), throw=True)
     task.refresh_from_db()
     nose.tools.ok_(task.predicted_status)
     nose.tools.ok_(task.status_probability)
Exemplo n.º 26
0
 def test_lob_prepare(self):
     """Generate a LobPDF"""
     communication = FOIACommunicationFactory()
     pdf = LobPDF(communication, "n", False, 0)
     prepared_pdf, page_count, files, mail = pdf.prepare()
     ok_(prepared_pdf)
     eq_(page_count, 1)
     eq_(files, [])
     ok_(isinstance(mail, MailCommunication))
Exemplo n.º 27
0
    def test_bounce(self):
        """Test a bounce webhook"""

        recipient = '*****@*****.**'
        comm = FOIACommunicationFactory(
            foia__email=EmailAddress.objects.fetch(recipient),
            foia__agency__fax=None,
        )
        email = comm.emails.first()
        event = 'bounced'
        code = 550
        error = (
            "5.1.1 The email account that you tried to reach "
            "does not exist. Please try 5.1.1 double-checking "
            "the recipient's email address for typos or 5.1.1 "
            "unnecessary spaces. Learn more at 5.1.1 "
            "http://support.example.com/mail/bin/answer.py"
        )
        data = {
            'event': event,
            'email_id': email.pk,
            'code': code,
            'error': error,
            'recipient': recipient,
        }
        self.sign(data)
        request = self.factory.post(reverse('mailgun-bounces'), data)
        bounces(request)  # pylint: disable=no-value-for-parameter
        comm.refresh_from_db()

        nose.tools.ok_(
            EmailError.objects.filter(
                email=email,
                datetime=datetime(2017, 1, 2, 17, tzinfo=pytz.utc),
                recipient=EmailAddress.objects.fetch(recipient),
                code=code,
                error=error,
                event=event,
            ).exists()
        )
Exemplo n.º 28
0
class TestCommunication(test.TestCase):
    """Tests communication methods"""

    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)

    def test_primary_contact(self):
        """Makes the primary email of the FOIA to the email the communication was sent from."""
        self.comm.make_sender_primary_contact()
        self.foia.refresh_from_db()
        eq_(self.foia.email, self.comm.emails.first().from_email)

    def test_attach_file_with_file(self):
        """Test attaching a file with an actual file"""
        comm = FOIACommunicationFactory()
        file_ = open('tmp.txt', 'w')
        file_.write('The file contents')
        file_.close()
        file_ = open('tmp.txt', 'r')
        comm.attach_file(file_=file_)
        file_.close()
        os.remove('tmp.txt')
        eq_(comm.files.count(), 1)
        foia_file = comm.files.first()
        eq_(foia_file.title, 'tmp')
        eq_(foia_file.ffile.file.name, 'tmp.txt')
        eq_(foia_file.ffile.read(), 'The file contents')

    def test_attach_file_with_content(self):
        """Test attaching a file with n memory content"""
        comm = FOIACommunicationFactory()
        comm.attach_file(content='More contents', name='doc.pdf')
        eq_(comm.files.count(), 1)
        foia_file = comm.files.first()
        eq_(foia_file.title, 'doc')
        eq_(foia_file.ffile.file.name, 'doc.pdf')
        eq_(foia_file.ffile.read(), 'More contents')

    @raises(ValueError)
    def test_orphan_error(self):
        """Orphans should raise an error"""
        self.comm.foia = None
        self.comm.make_sender_primary_contact()

    @raises(ValueError)
    def test_bad_sender_error(self):
        """Comms with bad sender email should raise an error"""
        email = self.comm.emails.first()
        email.from_email = None
        email.save()
        self.comm.make_sender_primary_contact()
Exemplo n.º 29
0
 def test_due_date(self):
     """Test receiving a due date reply"""
     comm = FOIACommunicationFactory(
         subject='[Due Date Changed]',
         communication='The due date for record request #18-209 has been '
         'changed to: March 16, 2018\nView Request #18-209',
         foia__status='processed',
     )
     self.portal.receive_msg(comm)
     eq_(comm.foia.status, 'processed')
     eq_(
         comm.communication, 'The due date for record request #18-209 has '
         'been changed to: March 16, 2018')
     assert_false(comm.hidden)
     eq_(comm.foia.date_estimate, date(2018, 3, 16))
Exemplo n.º 30
0
 def test_text_reply(self):
     """Test receiving a normal reply"""
     comm = FOIACommunicationFactory(
         subject="[External Message Added]",
         communication=
         "A message was sent to you regarding record request #17-1:\n"
         "This is the reply\n"
         "View Request",
         foia__status="processed",
     )
     self.portal.receive_msg(comm)
     eq_(comm.foia.status, "processed")
     eq_(comm.communication, "\nThis is the reply\n")
     assert_false(comm.hidden)
     eq_(comm.portals.count(), 1)
     eq_(comm.responsetask_set.count(), 1)