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_ = ('%[email protected], "Doe, John" <*****@*****.**>' % foia.get_mail_id()) 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_who, from_name) nose.tools.eq_(last_comm.priv_from_who, from_) nose.tools.eq_(last_comm.to_who, foia.user.get_full_name()) nose.tools.eq_(last_comm.priv_to_who, to_) nose.tools.eq_(last_comm.response, True) nose.tools.eq_(last_comm.full_html, False) nose.tools.eq_(last_comm.delivered, 'email') nose.tools.ok_(last_comm.rawemail) nose.tools.eq_(last_comm.responsetask_set.count(), 1) nose.tools.eq_(foia.email, from_email) nose.tools.eq_(foia.other_emails, '*****@*****.**') nose.tools.eq_(foia.status, 'processed')
def setUp(self): agency = AgencyFactory() self.owner = UserFactory() self.follower = UserFactory() self.request = FOIARequestFactory(user=self.owner, agency=agency) follow(self.follower, self.request) self.action = new_action(agency, 'completed', target=self.request)
def test_bad_verify(self): """Test an improperly signed message""" foia = FOIARequestFactory(block_incoming=True) to_ = '*****@*****.**' % foia.get_mail_id() response = self.mailgun_route(to_=to_, sign=False) nose.tools.eq_(response.status_code, 403)
def test_foia_submit_views(self): """Test submitting a FOIA request""" foia = FOIARequestFactory( status='started', user=User.objects.get(username='******'), ) FOIACommunicationFactory(foia=foia) self.client.login(username='******', password='******') foia_data = { 'title': foia.title, 'request': 'updated request', 'submit': 'Submit', 'agency': foia.agency.pk, 'combo-name': foia.agency.name, } kwargs = { 'jurisdiction': foia.jurisdiction.slug, 'jidx': foia.jurisdiction.pk, 'idx': foia.pk, 'slug': foia.slug, } draft = reverse('foia-draft', kwargs=kwargs) detail = reverse('foia-detail', kwargs=kwargs) post_allowed(self.client, draft, foia_data, detail) foia.refresh_from_db() nose.tools.ok_(foia.first_request().startswith('updated request')) nose.tools.eq_(foia.status, 'ack')
def setUp(self): self.foia = FOIARequestFactory(status='payment') self.url = reverse('foia-crowdfund', args=(self.foia.jurisdiction.slug, self.foia.jurisdiction.id, self.foia.slug, self.foia.id)) self.request_factory = RequestFactory() self.view = crowdfund_request
def test_requests(self): """Projects should keep a list of relevant FOIA requests.""" request1 = FOIARequestFactory() request2 = FOIARequestFactory() self.project.requests.add(request1, request2) ok_(request1 in self.project.requests.all()) ok_(request2 in self.project.requests.all()) self.project.articles.clear() eq_(len(self.project.articles.all()), 0)
def test_fee_rate(self): """ Jurisdictions should report the rate at which requests have fees. State jurisdictions should include fee rates of local jurisdictions. """ FOIARequestFactory(jurisdiction=self.state, status='ack', price=0) FOIARequestFactory(jurisdiction=self.local, status='ack', price=1.00) eq_(self.state.fee_rate(), 50.0) eq_(self.local.fee_rate(), 100.0)
def test_access_key(self): """Editors should be able to generate a secure access key to view an embargoed request.""" embargoed_foia = FOIARequestFactory(embargo=True) access_key = embargoed_foia.generate_access_key() assert_true(access_key == embargoed_foia.access_key, 'The key in the URL should match the key saved to the request.') embargoed_foia.generate_access_key() assert_false(access_key == embargoed_foia.access_key, 'After regenerating the link, the key should no longer match.')
def setUp(self): agency = AgencyFactory(appeal_agency=AppealAgencyFactory()) self.foia = FOIARequestFactory(agency=agency) self.view = Detail.as_view() self.url = self.foia.get_absolute_url() self.kwargs = { 'jurisdiction': self.foia.jurisdiction.slug, 'jidx': self.foia.jurisdiction.id, 'slug': self.foia.slug, 'idx': self.foia.id }
def setUp(self): self.factory = RequestFactory() self.foia = FOIARequestFactory() self.creator = self.foia.user self.editor = UserFactory() self.viewer = UserFactory() self.staff = UserFactory(is_staff=True) self.normie = UserFactory() self.foia.add_editor(self.editor) self.foia.add_viewer(self.viewer) self.foia.save()
def test_bad_strip(self): """Test an improperly stripped message""" foia = FOIARequestFactory() to_ = '*****@*****.**' % foia.get_mail_id() 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_idential_different_requests(self): """An identical action on a different request should not mark anything as read.""" other_request = FOIARequestFactory(user=self.owner, agency=self.request.agency) other_action = new_action(self.request.agency, 'completed', target=other_request) unread_count = self.owner.notifications.get_unread().count() self.request.notify(self.action) eq_(self.owner.notifications.get_unread().count(), unread_count + 1, 'The user should have one unread notification.') other_request.notify(other_action) eq_(self.owner.notifications.get_unread().count(), unread_count + 2, 'The user should have two unread notifications.')
def test_success_rate(self): """ Jurisdictions should report their success rate: completed/filed. State jurisdictions should include success rates of local jurisdictions. """ today = date.today() FOIARequestFactory(jurisdiction=self.state, status='done', date_done=today) FOIARequestFactory(jurisdiction=self.local, status='ack') eq_(self.state.success_rate(), 50.0) eq_(self.local.success_rate(), 0.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_foia = FOIARequestFactory(jurisdiction=self.local) state_foia = FOIARequestFactory(jurisdiction=self.state) local_foia.files.add(FOIAFileFactory(pages=page_count)) state_foia.files.add(FOIAFileFactory(pages=page_count)) eq_(self.local.total_pages(), page_count) eq_(self.state.total_pages(), 2 * page_count)
def test_promote_viewer(self): """Editors should be able to promote viewers to editors.""" embargoed_foia = FOIARequestFactory(embargo=True) viewer = UserFactory() embargoed_foia.add_viewer(viewer) nose.tools.assert_true(embargoed_foia.has_perm(viewer, 'view')) nose.tools.assert_false(embargoed_foia.has_perm(viewer, 'change')) embargoed_foia.promote_viewer(viewer) nose.tools.assert_true(embargoed_foia.has_perm(viewer, 'change'))
def test_demote_editor(self): """Editors should be able to demote editors to viewers.""" embargoed_foia = FOIARequestFactory(embargo=True) editor = UserFactory() embargoed_foia.add_editor(editor) assert_true(embargoed_foia.viewable_by(editor)) assert_true(embargoed_foia.editable_by(editor)) embargoed_foia.demote_editor(editor) assert_false(embargoed_foia.editable_by(editor))
def test_demote_editor(self): """Editors should be able to demote editors to viewers.""" embargoed_foia = FOIARequestFactory(embargo=True) editor = UserFactory() embargoed_foia.add_editor(editor) nose.tools.assert_true(embargoed_foia.has_perm(editor, 'view')) nose.tools.assert_true(embargoed_foia.has_perm(editor, 'change')) embargoed_foia.demote_editor(editor) nose.tools.assert_false(embargoed_foia.has_perm(editor, 'change'))
def test_promote_viewer(self): """Editors should be able to promote viewers to editors.""" embargoed_foia = FOIARequestFactory(embargo=True) viewer = UserFactory() embargoed_foia.add_viewer(viewer) assert_true(embargoed_foia.viewable_by(viewer)) assert_false(embargoed_foia.editable_by(viewer)) embargoed_foia.promote_viewer(viewer) assert_true(embargoed_foia.editable_by(viewer))
def test_allowed_email(self): """Test allowed email function""" foia = FOIARequestFactory( email='*****@*****.**', other_emails='*****@*****.**', agency__email='*****@*****.**', agency__other_emails='*****@*****.**', ) WhitelistDomain.objects.create(domain='whitehat.edu') allowed_emails = [ '*****@*****.**', # same domain '*****@*****.**', # case insensitive '*****@*****.**', # other email '*****@*****.**', # agency email '*****@*****.**', # any government tld '*****@*****.**', # any government tld '*****@*****.**', # white listed domain ] not_allowed_emails = [ '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', ] for email in allowed_emails: nose.tools.ok_(_allowed_email(email, foia)) for email in not_allowed_emails: nose.tools.assert_false(_allowed_email(email, foia)) # non foia test - any agency email nose.tools.ok_(_allowed_email('*****@*****.**'))
def test_session_success(self): """Test a successful post to the session view""" foia = FOIARequestFactory() attachments = OutboundAttachmentFactory.create_batch( 3, foia=foia, user=foia.user, sent=False, ) OutboundAttachmentFactory.create_batch( 3, foia=foia, user=foia.user, sent=True, ) OutboundAttachmentFactory.create_batch(3) request_factory = RequestFactory() request = request_factory.get( reverse('fine-uploader-session'), {'foia_id': foia.pk}, ) request.user = foia.user response = views.session(request) eq_(response.status_code, 200) attm_data = json.loads(response.content) attm_data.sort(key=lambda f: f['uuid']) attachments.sort(key=lambda f: f.pk) for attm_datum, attm in zip(attm_data, attachments): eq_(attm_datum['name'], attm.name()) eq_(attm_datum['uuid'], attm.pk) eq_(attm_datum['size'], attm.ffile.size) eq_(attm_datum['s3Key'], attm.ffile.name)
def test_average_response_time(self): """ Jurisdictions should report their average response time. State jurisdictions should include avg. response time of their local jurisdictions. """ today = date.today() state_duration = 12 local_duration = 6 FOIARequestFactory(jurisdiction=self.state, date_done=today, date_submitted=today - timedelta(state_duration)) FOIARequestFactory(jurisdiction=self.local, date_done=today, date_submitted=today - timedelta(local_duration)) eq_(self.state.average_response_time(), (local_duration + state_duration) / 2) eq_(self.local.average_response_time(), local_duration)
def test_block_incoming(self): """Test receiving a message from an unauthorized sender""" foia = FOIARequestFactory(block_incoming=True) to_ = '*****@*****.**' % foia.get_mail_id() 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_mail_id(), ).exists())
def setUp(self): self.foia = FOIARequestFactory() self.editor = UserFactory() self.viewer = UserFactory() self.foia.add_editor(self.editor) self.foia.add_viewer(self.viewer) self.data = { 'action': 'add_note', 'note': u'Lorem ipsum dolor su ament.' } self.url = self.foia.get_absolute_url() self.view = Detail.as_view() self.kwargs = { 'jurisdiction': self.foia.jurisdiction.slug, 'jidx': self.foia.jurisdiction.id, 'slug': self.foia.slug, 'idx': self.foia.id }
def test_bad_sender(self): """Test receiving a message from an unauthorized sender""" foia = FOIARequestFactory() from_ = '*****@*****.**' to_ = '*****@*****.**' % foia.get_mail_id() 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_mail_id(), ).exists())
def setUp(self): self.endpoint = '/exemption/submit/' self.factory = APIRequestFactory() self.view = ExemptionViewSet.as_view({'post': 'submit'}) self.user = UserFactory() self.foia = FOIARequestFactory(user=self.user) self.data = { 'foia': self.foia.id, 'language': 'Lorem ipsum', }
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 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() 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')
class TestRequestPayment(TestCase): """Allow users to pay fees on a request""" def setUp(self): self.foia = FOIARequestFactory() UserFactory(username='******') def test_make_payment(self): """The request should accept payments for request fees.""" user = self.foia.user amount = 100.0 comm = self.foia.pay(user, amount) self.foia.refresh_from_db() eq_(self.foia.status, 'submitted') eq_(self.foia.date_processing, datetime.date.today()) ok_(comm, 'The function should return a communication.') task = SnailMailTask.objects.filter(communication=comm).first() ok_(task, 'A snail mail task should be created.') eq_(task.user, user) eq_(task.amount, amount)
def test_success_bad_data(self): """Test a post to the success view with missing data""" foia = FOIARequestFactory() request_factory = RequestFactory() request = request_factory.post( reverse('fine-uploader-success'), {'foia_id': foia.pk}, ) request.user = foia.user response = views.success(request) eq_(response.status_code, 400)
def test_attachments(self): """Test a message with an attachment""" try: foia = FOIARequestFactory() to_ = '*****@*****.**' % foia.get_mail_id() 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.files.count(), 1) nose.tools.eq_(foia.files.first().ffile.name, file_path) finally: foia.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_get_foia(self): """Try getting the detail page for a FOIA Request with an unread notification.""" agency = AgencyFactory() foia = FOIARequestFactory(agency=agency) view = FOIARequestDetail.as_view() # Create a notification for the request action = new_action(agency, 'completed', target=foia) notification = notify(self.user, action)[0] ok_(not notification.read, 'The notification should be unread.') # Try getting the view as the user response = http_get_response(foia.get_absolute_url(), view, self.user, idx=foia.pk, slug=foia.slug, jidx=foia.jurisdiction.pk, jurisdiction=foia.jurisdiction.slug) eq_(response.status_code, 200, 'The view should response 200 OK.') # Check that the notification has been read. notification.refresh_from_db() ok_(notification.read, 'The notification should be marked as read.')