Exemplo n.º 1
0
    def test_office_save(self):
        agency = Agency(name='Department of Commerce')
        agency.save()

        office = Office(name='Commerce Is FUNdamental', agency=agency)
        office.save()
        retrieved = Office.objects.get(pk=office.pk)

        self.assertEqual(retrieved.office_slug, 'commerce-is-fundamental')
        self.assertEqual(
            retrieved.slug, 'department-of-commerce--commerce-is-fundamental')
Exemplo n.º 2
0
class RequestSwitchTests(TestCase):
    fixtures = ['agencies_test.json', 'offices_test.json']

    def reload_urls(self):
        """ The flag the turns of the requests API is in urls.py, so we have to
        reload the URLs in Django to make tests. This reloads those tests. """

        if settings.ROOT_URLCONF in sys.modules:
            reload(sys.modules[settings.ROOT_URLCONF])
            import_module(settings.ROOT_URLCONF)

    def setUp(self):
        self.emails = ["*****@*****.**", "*****@*****.**"]
        self.agency = Agency(name='Agency With Offices',
                             zip_code=20404,
                             emails=self.emails)
        self.agency.save()
        self.office = Office(agency=self.agency,
                             name='Office 1',
                             zip_code=20404,
                             emails=self.emails)
        self.office.save()

        # Turn off SHOW_WEBFORM
        settings.SHOW_WEBFORM = False
        self.reload_urls()

    def tearDown(self):
        # Turn on SHOW_WEBFORM
        settings.SHOW_WEBFORM = True
        self.reload_urls()

    def test_api_off(self):
        """ The request API should not the available. """

        requester_email = "*****@*****.**"
        response = self.client.post("/api/request/",
                                    content_type='application/json',
                                    data=json.dumps({
                                        'agency': self.agency.slug,
                                        'email': requester_email,
                                        'first_name': "FOIA",
                                        'last_name': "Requester",
                                        'body': "A new request"
                                    }))
        self.assertEqual(404, response.status_code)
Exemplo n.º 3
0
class RequestFormTests(SimpleTestCase):
    def setUp(self):
        self.emails = ["*****@*****.**", "*****@*****.**"]

        self.agency = Agency(name='Agency With Offices',
                             zip_code=20404,
                             emails=self.emails)
        self.agency.save()

        self.office = Office(agency=self.agency,
                             name='Office 1',
                             zip_code=20404,
                             emails=self.emails)
        self.office.save()

        self.office2 = Office(agency=self.agency,
                              name='Office 2',
                              zip_code=20404,
                              emails=self.emails)
        self.office2.save()

        self.agency2 = Agency(name='Agency Without Offices',
                              zip_code=20009,
                              emails=self.emails)
        self.agency2.save()

        self.requester = Requester.objects.create(first_name='Alice',
                                                  last_name='Bobson',
                                                  email='*****@*****.**')
        self.request = FOIARequest.objects.create(
            requester=self.requester,
            office=self.office,
            date_end=date.today(),
            request_body='All the cheese')

    # destroy them all
    def tearDown(self):
        for model in [FOIARequest, Requester, Office, Agency]:
            model.objects.all().delete()

    def test_request_form_successful(self):
        """The agency name should be present in the request form"""
        response = self.client.get(
            reverse('form', kwargs={'slug': self.agency.slug}))
        self.assertContains(response, self.agency.name)

    def test_request_form_404(self):
        """Should get a 404 if requesting an agency that doesn't exist"""
        response = self.client.get(
            reverse('form', kwargs={'slug': 'does-not-exist'}))
        self.assertEqual(404, response.status_code)

    def test_contact_landing_404(self):
        """Verify that non-existing agency/offices cause 404s"""
        response = self.client.get(
            reverse('contact_landing', kwargs={'slug': 'sssss'}))
        self.assertEqual(response.status_code, 404)
        response = self.client.get(
            reverse('contact_landing', kwargs={'slug': 'sss--ss'}))
        self.assertEqual(response.status_code, 404)

    def test_contact_landing_success(self):
        """If loading an agency or a top-level office, we should see agency
        name. If an office, we should not see peer offices."""

        list_fingerprint = "Make your FOIA request directly"
        list_fingerprint += " to the most relevant group or component"

        response = self.client.get(
            reverse('contact_landing', kwargs={'slug': self.agency.slug}))
        self.assertContains(response, self.agency.name)
        self.assertContains(response, self.office.name)
        self.assertContains(response, self.office2.name)
        self.assertContains(response, list_fingerprint)

        response = self.client.get(
            reverse('contact_landing', kwargs={'slug': self.office.slug}))
        self.assertContains(response, self.agency.name)
        self.assertContains(response, self.office.name)
        self.assertNotContains(response, self.office2.name)
        self.assertNotContains(response, list_fingerprint)

        response = self.client.get(
            reverse('contact_landing', kwargs={'slug': self.agency2.slug}))
        self.assertContains(response, self.agency2.name)
        self.assertNotContains(response, self.office.name)
        self.assertNotContains(response, self.office2.name)
        self.assertNotContains(response, list_fingerprint)

    def test_about(self):
        """The /about/ page should load without errors."""
        response = self.client.get(reverse('about'))
        self.assertEqual(response.status_code, 200)
Exemplo n.º 4
0
class RequestFormTests(SimpleTestCase):
    def setUp(self):
        self.emails = ["*****@*****.**", "*****@*****.**"]

        self.agency = Agency(
            name='Agency With Offices', zip_code=20404, emails=self.emails)
        self.agency.save()

        self.office = Office(
            agency=self.agency, name='Office 1', zip_code=20404,
            emails=self.emails)
        self.office.save()

        self.office2 = Office(
            agency=self.agency, name='Office 2', zip_code=20404,
            emails=self.emails)
        self.office2.save()

        self.agency2 = Agency(
            name='Agency Without Offices', zip_code=20009, emails=self.emails)
        self.agency2.save()

        self.requester = Requester.objects.create(
            first_name='Alice', last_name='Bobson', email='*****@*****.**')
        self.request = FOIARequest.objects.create(
            requester=self.requester, office=self.office,
            date_end=date.today(), request_body='All the cheese')

    # destroy them all
    def tearDown(self):
        for model in [FOIARequest, Requester, Office, Agency]:
            model.objects.all().delete()

    def test_request_form_successful(self):
        """The agency name should be present in the request form"""
        response = self.client.get(reverse(
            'form', kwargs={'slug': self.agency.slug}))
        self.assertContains(response, self.agency.name)

    def test_request_form_404(self):
        """Should get a 404 if requesting an agency that doesn't exist"""
        response = self.client.get(reverse(
            'form', kwargs={'slug': 'does-not-exist'}))
        self.assertEqual(404, response.status_code)

    def test_request_success(self):
        """Request should be retrieved and displayed"""
        response = self.client.get(reverse(
            'success', kwargs={'id': self.request.id}))
        self.assertContains(response, self.requester.email)
        self.assertContains(response, self.agency.name)

    def test_request_success_404(self):
        """Should get a 404 if trying to get a success page for a request
        which doesn't exist"""
        response = self.client.get(reverse(
            'success', kwargs={'id': 9999999999}))
        self.assertEqual(404, response.status_code)

    def test_contact_landing_404(self):
        """Verify that non-existing agency/offices cause 404s"""
        response = self.client.get(reverse(
            'contact_landing', kwargs={'slug': 'sssss'}))
        self.assertEqual(response.status_code, 404)
        response = self.client.get(
            reverse('contact_landing', kwargs={'slug': 'sss--ss'}))
        self.assertEqual(response.status_code, 404)

    def test_contact_landing_success(self):
        """If loading an agency or a top-level office, we should see agency
        name. If an office, we should not see peer offices."""

        list_fingerprint = "Make your FOIA request directly"
        list_fingerprint += " to the most relevant group or component"

        response = self.client.get(reverse(
            'contact_landing', kwargs={'slug': self.agency.slug}))
        self.assertContains(response, self.agency.name)
        self.assertContains(response, self.office.name)
        self.assertContains(response, self.office2.name)
        self.assertContains(response, list_fingerprint)

        response = self.client.get(reverse(
            'contact_landing', kwargs={'slug': self.office.slug}))
        self.assertContains(response, self.agency.name)
        self.assertContains(response, self.office.name)
        self.assertNotContains(response, self.office2.name)
        self.assertNotContains(response, list_fingerprint)

        response = self.client.get(reverse(
            'contact_landing', kwargs={'slug': self.agency2.slug}))
        self.assertContains(response, self.agency2.name)
        self.assertNotContains(response, self.office.name)
        self.assertNotContains(response, self.office2.name)
        self.assertNotContains(response, list_fingerprint)

    def test_learn(self):
        """The /learn/ page should load without errors."""
        response = self.client.get(reverse('learn'))
        self.assertEqual(response.status_code, 200)

    def test_about(self):
        """The /about/ page should load without errors."""
        response = self.client.get(reverse('about'))
        self.assertEqual(response.status_code, 200)
Exemplo n.º 5
0
class FOIARequestTests(TestCase):
    fixtures = ['agencies_test.json', 'offices_test.json']

    def setUp(self):
        self.emails = ["*****@*****.**", "*****@*****.**"]
        self.agency = Agency(name='Agency With Offices',
                             zip_code=20404,
                             emails=self.emails)
        self.agency.save()
        self.office = Office(agency=self.agency,
                             name='Office 1',
                             zip_code=20404,
                             emails=self.emails)
        self.office.save()

    def test_create_request(self):
        """ Submit a simple, minimal FOIA request to the API and expect it to
        succeed.  """

        c = Client()

        data = {
            'agency': 'department-of-commerce',
            'first_name': 'Joe',
            'last_name': 'Public',
            'email': '*****@*****.**',
            'body': 'I want documents about palm trees.'
        }

        data_string = json.dumps(data)
        response = c.post('/api/request/',
                          content_type="application/json",
                          data=data_string)
        self.assertEqual(201, response.status_code)

    def test_create_request_no_email(self):
        """ Try to submit a request to an agency that has no online form, and
        no email address. This should fail with the appropriate HTTP status
        code. """

        a = Agency(name="Broadcasting Board of Governors", slug="broadcasting")
        a.save()

        c = Client()

        data = {
            'agency': 'broadcasting',
            'first_name': 'Joe',
            'last_name': 'Public',
            'email': '*****@*****.**',
            'body': 'I want documents about palm trees.'
        }
        data_string = json.dumps(data)
        response = c.post('/api/request/',
                          content_type="application.json",
                          data=data_string)
        self.assertEqual(400, response.status_code)

    def test_submit_request_to_agency(self):
        requester_email = "*****@*****.**"
        self.assertEqual(0,
                         len(Requester.objects.filter(email=requester_email)))

        response = self.client.post("/api/request/",
                                    content_type='application/json',
                                    data=json.dumps({
                                        'agency': self.agency.slug,
                                        'email': requester_email,
                                        'first_name': "FOIA",
                                        'last_name': "Requester",
                                        'body': "A new request",
                                    }))

        self.assertEqual(201, response.status_code, response.content)
        data = helpers.json_from(response)
        self.assertTrue(data.get('tracking_id') is not None)
        self.assertEqual('O', data.get('status'))

        request_id = data['tracking_id']
        foia_request = FOIARequest.objects.get(pk=request_id)
        self.assertTrue(foia_request is not None)
        self.assertEqual(foia_request.agency, self.agency)
        self.assertEqual(1,
                         len(Requester.objects.filter(email=requester_email)))
        self.assertEqual(requester_email, foia_request.requester.email)

    def test_submit_request_to_office(self):
        requester_email = "*****@*****.**"
        self.assertEqual(0,
                         len(Requester.objects.filter(email=requester_email)))

        response = self.client.post("/api/request/",
                                    content_type='application/json',
                                    data=json.dumps({
                                        'agency':
                                        self.office.agency.slug,
                                        'office':
                                        self.office.office_slug,
                                        'email':
                                        requester_email,
                                        'first_name':
                                        "FOIA",
                                        'last_name':
                                        "Requester",
                                        'body':
                                        "A new request",
                                    }))

        self.assertEqual(201, response.status_code, response.content)
        data = helpers.json_from(response)
        self.assertTrue(data.get('tracking_id') is not None)
        self.assertEqual('O', data.get('status'))

        request_id = data['tracking_id']
        foia_request = FOIARequest.objects.get(pk=request_id)
        self.assertTrue(foia_request is not None)
        self.assertEqual(foia_request.office, self.office)
        self.assertEqual(1,
                         len(Requester.objects.filter(email=requester_email)))
        self.assertEqual(requester_email, foia_request.requester.email)

    def test_submit_invalid_request(self):
        requester_email = "*****@*****.**"
        self.assertEqual(0,
                         len(Requester.objects.filter(email=requester_email)))

        response = self.client.post("/api/request/",
                                    content_type='application/json',
                                    data=json.dumps({
                                        'agency': "not-a-valid-agency",
                                        'email': requester_email,
                                        'first_name': "FOIA",
                                        'last_name': "Requester",
                                        'body': "A new request",
                                    }))

        self.assertEqual(404, response.status_code)
        self.assertEqual(0,
                         len(Requester.objects.filter(email=requester_email)))