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 setUp(self): self.agency = AgencyFactory() self.url = self.agency.get_absolute_url() self.view = detail self.user = UserFactory() self.kwargs = { "jurisdiction": self.agency.jurisdiction.slug, "jidx": self.agency.jurisdiction.id, "slug": self.agency.slug, "idx": self.agency.id, }
def test_list(self): """The list should only contain approved agencies""" approved_agency = AgencyFactory() unapproved_agency = AgencyFactory(status="pending") response = http_get_response(reverse("agency-list"), AgencyList.as_view()) agency_list = response.context_data["object_list"] ok_(approved_agency in agency_list, "Approved agencies should be listed.") ok_( unapproved_agency not in agency_list, "Unapproved agencies should not be listed.", )
def setUp(self): self.agency = AgencyFactory() self.url = self.agency.get_absolute_url() self.view = detail self.user = UserFactory() self.kwargs = { 'jurisdiction': self.agency.jurisdiction.slug, 'jidx': self.agency.jurisdiction.id, 'slug': self.agency.slug, 'idx': self.agency.id }
def test_list(self): """The list should only contain approved agencies""" approved_agency = AgencyFactory() unapproved_agency = AgencyFactory(status='pending') response = http_get_response(reverse('agency-list'), AgencyList.as_view()) agency_list = response.context_data['object_list'] ok_(approved_agency in agency_list, 'Approved agencies should be listed.') ok_(unapproved_agency not in agency_list, 'Unapproved agencies should not be listed.')
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, ]
def test_foia_create(self): """Test creating a FOIA through the API""" attachment_url = "http://www.example.com/attachment.txt" self.mocker.get( attachment_url, headers={"Content-Type": "text/plain"}, text="Attachment content here", ) agency = AgencyFactory() user = UserFactory.create(membership__organization__number_requests=5) Token.objects.create(user=user) data = { "jurisdiction": agency.jurisdiction.pk, "agency": agency.pk, "document_request": "The document", "title": "Title", "attachments": [attachment_url], } headers = { "content-type": "application/json", "HTTP_AUTHORIZATION": "Token %s" % user.auth_token, } response = self.client.post(reverse("api-foia-list"), json.dumps(data), content_type="application/json", **headers) eq_(response.status_code, 201, response) eq_(len(response.json()["Requests"]), 1)
def test_calc_return_requests(self): """Test calculating the return requests""" composer = FOIAComposerFactory( status='submitted', agencies=AgencyFactory.create_batch(6), num_org_requests=1, num_monthly_requests=2, num_reg_requests=3, user__profile__num_requests=5, user__profile__monthly_requests=10, user__profile__organization=OrganizationFactory(num_requests=100), ) values = [ (7, 4, 2, 1), (6, 3, 2, 1), (5, 3, 2, 0), (4, 3, 1, 0), (3, 3, 0, 0), (2, 2, 0, 0), (1, 1, 0, 0), ] for total, reg, monthly, org in values: eq_( composer._calc_return_requests(total), { 'regular': reg, 'monthly': monthly, 'org': org, }, )
def test_foia_create(self): """Test creating a FOIA through the API""" attachment_url = 'http://www.example.com/attachment.txt' self.mocker.get( attachment_url, headers={'Content-Type': 'text/plain'}, text='Attachment content here', ) agency = AgencyFactory() user = UserFactory.create(membership__organization__number_requests=5) Token.objects.create(user=user) data = { 'jurisdiction': agency.jurisdiction.pk, 'agency': agency.pk, 'document_request': 'The document', 'title': 'Title', 'attachments': [attachment_url], } headers = { 'content-type': 'application/json', 'HTTP_AUTHORIZATION': 'Token %s' % user.auth_token, } response = self.client.post(reverse('api-foia-list'), json.dumps(data), content_type='application/json', **headers) eq_(response.status_code, 201, response)
def test_post_update_composer(self): """Test submitting a composer""" composer = FOIAComposerFactory( status='started', user__profile__num_requests=4, ) agency = AgencyFactory() data = { 'title': 'Title', 'requested_docs': 'ABC', 'agencies': agency.pk, 'action': 'submit', } request = self.request_factory.post( reverse('foia-draft', kwargs={ 'idx': composer.pk }), data, ) request.user = composer.user request = mock_middleware(request) response = UpdateComposer.as_view()(request, idx=composer.pk) eq_(response.status_code, 302) composer.refresh_from_db() ok_(composer.status, 'submitted')
def setUp(self): agency = AgencyFactory() self.owner = UserFactory() self.follower = UserFactory() self.request = FOIARequestFactory(composer__user=self.owner, agency=agency) follow(self.follower, self.request) self.action = new_action(agency, "completed", target=self.request)
def test_agency_get_proxy_info(self): """Test an agencies get_proxy_info method""" agency_ = AgencyFactory() proxy_info = agency_.get_proxy_info() eq_(proxy_info['proxy'], False) eq_(proxy_info['missing_proxy'], False) assert_not_in('from_user', proxy_info) assert_not_in('warning', proxy_info) proxy_placeholder = UserFactory(username='******') agency_ = AgencyFactory(requires_proxy=True) proxy_info = agency_.get_proxy_info() eq_(proxy_info['proxy'], True) eq_(proxy_info['missing_proxy'], True) eq_(proxy_info['from_user'], proxy_placeholder) assert_in('warning', proxy_info) proxy = UserFactory( profile__acct_type='proxy', profile__state=agency_.jurisdiction.legal.abbrev, ) proxy_info = agency_.get_proxy_info() eq_(proxy_info['proxy'], True) eq_(proxy_info['missing_proxy'], False) eq_(proxy_info['from_user'], proxy) assert_in('warning', proxy_info)
def setUp(self): """Create some existing jurisdictions and agencies for the tests""" local = LocalJurisdictionFactory() state = local.parent federal = state.parent self.cia = AgencyFactory( name="Central Intelligence Agency", jurisdiction=federal, email=None, fax=None, ) self.governor = AgencyFactory( name="Governor's Office", jurisdiction=state, ) self.police = AgencyFactory( name="Boston Police Department", jurisdiction=local, )
def setUp(self): self.agency = AgencyFactory() self.form = AgencyForm( { 'name': self.agency.name, 'jurisdiction': self.agency.jurisdiction.pk, 'portal_type': 'other', }, instance=self.agency, )
def setUp(self): self.agency = AgencyFactory() self.form = AgencyForm( { "name": self.agency.name, "jurisdiction": self.agency.jurisdiction.pk, "portal_type": "other", }, instance=self.agency, )
def test_send_notification(self): """The email should send if there are notifications.""" # generate an action on an actor the user follows agency = AgencyFactory() foia = FOIARequestFactory(agency=agency) action = new_action(agency, "completed", target=foia) notify(self.user, action) # generate the email, which should contain the generated action email = self.digest(user=self.user, interval=self.interval) eq_(email.activity["count"], 1, "There should be activity.") eq_(email.send(), 1, "The email should send.")
def test_reject(self): replacement = AgencyFactory() existing_foia = FOIARequestFactory(agency=self.agency) self.task.reject(replacement) existing_foia.refresh_from_db() eq_(self.task.agency.status, 'rejected', 'Rejecting a new agency should leave it unapproved.') eq_( existing_foia.agency, replacement, 'The replacement agency should receive the rejected agency\'s requests.' )
def setUp(self): AgencyFactory() ArticleFactory() CrowdsourceResponseFactory() FOIARequestFactory() FlaggedTaskFactory() NewAgencyTaskFactory() OrphanTaskFactory() QuestionFactory() ResponseTaskFactory() SnailMailTaskFactory() UserFactory()
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, } UserFactory(username="******")
def test_digest_follow_requests(self): """Digests should include information on requests I follow.""" # generate an action on a request the user owns other_user = UserFactory() foia = FOIARequestFactory(composer__user=other_user) agency = AgencyFactory() action = new_action(agency, "rejected", target=foia) notify(self.user, action) # generate the email, which should contain the generated action email = self.digest(user=self.user, interval=self.interval) eq_(email.activity["count"], 1, "There should be activity.") eq_(email.send(), 1, "The email should send.")
def setUp(self): self.user = UserFactory() self.title = 'Test Request' self.request_language = 'Lorem ipsum' self.agency = AgencyFactory() self.jurisdiction = self.agency.jurisdiction self.foi = factories.FoiaMachineRequestFactory( user=self.user, title=self.title, request_language=self.request_language, jurisdiction=self.jurisdiction, )
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 } UserFactory(username='******')
def test_post_replace(self): """Rejecting the agency with a replacement agency""" replacement = AgencyFactory() self.client.post( self.url, { 'replace': True, 'task': self.task.pk, 'replace_agency': replacement.pk, 'replace_jurisdiction': replacement.jurisdiction.pk, }) updated_task = NewAgencyTask.objects.get(pk=self.task.pk) eq_(updated_task.agency.status, 'rejected') eq_(updated_task.resolved, True)
def test_contact_info_anonymous(self): """Test the contact_info ajax view""" agency = AgencyFactory() request = RequestFactory().get( reverse("agency-contact-info", kwargs={"idx": agency.pk}) ) request = mock_middleware(request) request.user = AnonymousUser() response = contact_info(request, agency.pk) eq_(response.status_code, 200) data = json.loads(response.content) eq_(data["type"], "email")
def test_contact_info(self): """Test the contact_info ajax view""" agency = AgencyFactory() request = RequestFactory().get( reverse("agency-contact-info", kwargs={"idx": agency.pk}) ) request = mock_middleware(request) request.user = ProfessionalUserFactory() response = contact_info(request, agency.pk) eq_(response.status_code, 200) data = json.loads(response.content) eq_(data["email"], str(agency.email))
def setUp(self): """Set up tests""" self.agency1 = AgencyFactory( fax__phone__number='1-987-654-3211', email__email__email='*****@*****.**', other_emails='[email protected], [email protected]') self.agency2 = AgencyFactory(fax__phone__number='987.654.3210', ) self.agency3 = AgencyFactory(email=None, )
def test_calc_return_requests(self): """Test calculating the return requests""" composer = FOIAComposerFactory( status="submitted", agencies=AgencyFactory.create_batch(6), num_monthly_requests=2, num_reg_requests=3, ) values = [(6, 4, 2), (5, 3, 2), (4, 3, 1), (3, 3, 0), (2, 2, 0), (1, 1, 0)] for total, reg, monthly in values: eq_( composer._calc_return_requests(total), {"regular": reg, "monthly": monthly}, )
def setUp(self): self.agencies = AgencyFactory.create_batch(6) self.organization = OrganizationFactory(num_requests=100) self.composer = FOIAComposerFactory( status='submitted', agencies=self.agencies, num_org_requests=1, num_monthly_requests=2, num_reg_requests=3, user__profile__num_requests=5, user__profile__monthly_requests=10, user__profile__organization=self.organization, ) self.task = MultiRequestTask.objects.create(composer=self.composer)
def setUp(self): self.agencies = AgencyFactory.create_batch(6) self.composer = FOIAComposerFactory( status="submitted", agencies=self.agencies, num_monthly_requests=2, num_reg_requests=3, ) self.task = MultiRequestTask.objects.create(composer=self.composer) self.mocker = requests_mock.Mocker() mock_squarelet(self.mocker) self.mocker.start() self.addCleanup(self.mocker.stop)
def test_boilerplate(self): """Test the boilerplate ajax view""" agencies = AgencyFactory.create_batch(2) request = RequestFactory().get( reverse("agency-boilerplate"), {"agencies": [a.pk for a in agencies]} ) request = mock_middleware(request) request.user = UserFactory(profile__full_name="John Doe") response = boilerplate(request) eq_(response.status_code, 200) data = json.loads(response.content) assert_in("{ law name }", data["intro"]) assert_in("{ days }", data["outro"]) assert_in("John Doe", data["outro"])