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')
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')
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')
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 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()
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)
def load_data(data): """ Loads data from each yaml file into the database. """ # Load the agency name = data['name'] slug = Agency.slug_for(name) a, created = Agency.objects.get_or_create(slug=slug, name=name) # Load the agency-specific values load_agency_fields(a, data) # If the agency only has a single department the contactable fields if len(data['departments']) == 1: dept_rec = data['departments'][0] contactable_fields(a, dept_rec) a.save() add_request_time_statistics(data, a) # Load agency offices if len(data['departments']) > 1: for dept_rec in data['departments']: # If top-level=True office is saved as agency if dept_rec.get('top_level'): sub_agency_name = dept_rec['name'] sub_agency_slug = Agency.slug_for(sub_agency_name) sub_agency, created = Agency.objects.get_or_create( slug=sub_agency_slug, name=sub_agency_name) sub_agency.parent = a load_agency_fields(sub_agency, dept_rec) contactable_fields(sub_agency, dept_rec) sub_agency.save() add_request_time_statistics(dept_rec, sub_agency) else: # Just an office office_name = dept_rec['name'] office_slug = Office.slug_for(office_name) full_slug = slug + '--' + office_slug o, created = Office.objects.get_or_create(agency=a, slug=full_slug) o.office_slug = office_slug o.name = office_name contactable_fields(o, dept_rec) o.save() add_request_time_statistics(dept_rec, a, o)
def load_data(data): """ Loads data from each yaml file into the database. """ # Load the agency name = data['name'] slug = Agency.slug_for(name) a, created = Agency.objects.get_or_create(slug=slug, name=name) # Load the agency-specific values load_agency_fields(a, data) # If the agency only has a single department the contactable fields if len(data['departments']) == 1: dept_rec = data['departments'][0] contactable_fields(a, dept_rec) a.save() add_request_time_statistics(data, a) # Load agency offices if len(data['departments']) > 1: for dept_rec in data['departments']: # If top-level=True office is saved as agency if dept_rec.get('top_level'): sub_agency_name = dept_rec['name'] sub_agency_slug = Agency.slug_for(sub_agency_name) sub_agency, created = Agency.objects.get_or_create( slug=sub_agency_slug, name=sub_agency_name) sub_agency.parent = a load_agency_fields(sub_agency, dept_rec) contactable_fields(sub_agency, dept_rec) sub_agency.save() add_request_time_statistics(dept_rec, sub_agency) else: # Just an office office_name = dept_rec['name'] office_slug = Office.slug_for(office_name) full_slug = slug + '--' + office_slug o, created = Office.objects.get_or_create( agency=a, slug=full_slug) o.office_slug = office_slug o.name = office_name contactable_fields(o, dept_rec) o.save() add_request_time_statistics(dept_rec, a, o)
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)
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)
def process_yamls(folder): # only load yaml files for item in iglob(folder + "/*.yaml"): data_file = os.path.join(folder, item) data = yaml.load(open(data_file)) # Agencies name = data['name'] slug = Agency.slug_for(name) a, created = Agency.objects.get_or_create(slug=slug, name=name) a.abbreviation = data['abbreviation'] a.description = data.get('description') a.keywords = data.get('keywords') a.common_requests = data.get('common_requests', []) a.no_records_about = data.get('no_records_about', []) # Only has a single, main branch/office if len(data['departments']) == 1: dept_rec = data['departments'][0] contactable_fields(a, dept_rec) a.save() add_request_time_statistics(data, a) # Offices if len(data['departments']) > 1: for dept_rec in data['departments']: if dept_rec.get('top_level'): # This is actually an agency sub_agency_name = dept_rec['name'] sub_agency_slug = Agency.slug_for(sub_agency_name) sub_agency, created = Agency.objects.get_or_create( slug=sub_agency_slug, name=sub_agency_name) sub_agency.parent = a abbreviation = build_abbreviation(sub_agency_name) sub_agency.abbreviation = abbreviation sub_agency.description = dept_rec.get('description') sub_agency.keyword = dept_rec.get('keywords') sub_agency.common_requests = dept_rec.get( 'common_requests', []) sub_agency.no_records_about = dept_rec.get( 'no_records_about', []) contactable_fields(sub_agency, dept_rec) sub_agency.save() add_request_time_statistics(dept_rec, sub_agency) else: # Just an office office_name = dept_rec['name'] office_slug = Office.slug_for(office_name) full_slug = slug + '--' + office_slug o, created = Office.objects.get_or_create( agency=a, slug=full_slug) o.office_slug = office_slug o.name = office_name contactable_fields(o, dept_rec) o.save() add_request_time_statistics(dept_rec, a, o)
def load_data(data): """ Loads data from each yaml file into the database. """ # Agencies name = data['name'] slug = Agency.slug_for(name) a, created = Agency.objects.get_or_create(slug=slug, name=name) a.abbreviation = data['abbreviation'] a.description = data.get('description') a.keywords = data.get('keywords') a.common_requests = data.get('common_requests', []) a.no_records_about = data.get('no_records_about', []) # Only has a single, main branch/office if len(data['departments']) == 1: dept_rec = data['departments'][0] contactable_fields(a, dept_rec) a.save() add_request_time_statistics(data, a) # Offices if len(data['departments']) > 1: for dept_rec in data['departments']: if dept_rec.get('top_level'): # This is actually an agency sub_agency_name = dept_rec['name'] sub_agency_slug = Agency.slug_for(sub_agency_name) sub_agency, created = Agency.objects.get_or_create( slug=sub_agency_slug, name=sub_agency_name) sub_agency.parent = a abbreviation = dept_rec.get('abbreviation') if not abbreviation: abbreviation = build_abbreviation(sub_agency_name) sub_agency.abbreviation = abbreviation sub_agency.description = dept_rec.get('description') sub_agency.keywords = dept_rec.get('keywords') sub_agency.common_requests = dept_rec.get( 'common_requests', []) sub_agency.no_records_about = dept_rec.get( 'no_records_about', []) contactable_fields(sub_agency, dept_rec) sub_agency.save() add_request_time_statistics(dept_rec, sub_agency) else: # Just an office office_name = dept_rec['name'] office_slug = Office.slug_for(office_name) full_slug = slug + '--' + office_slug o, created = Office.objects.get_or_create(agency=a, slug=full_slug) o.office_slug = office_slug o.name = office_name contactable_fields(o, dept_rec) o.save() add_request_time_statistics(dept_rec, a, o)
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)))
def process_yamls(folder): # only load yaml files for item in iglob(folder + "/*.yaml"): data_file = os.path.join(folder, item) data = yaml.load(open(data_file)) # Agencies name = data['name'] slug = Agency.slug_for(name) a, created = Agency.objects.get_or_create(slug=slug, name=name) a.abbreviation = data['abbreviation'] a.description = data.get('description') a.keywords = data.get('keywords') a.common_requests = data.get('common_requests', []) a.no_records_about = data.get('no_records_about', []) # Only has a single, main branch/office if len(data['departments']) == 1: dept_rec = data['departments'][0] contactable_fields(a, dept_rec) a.save() add_request_time_statistics(data, a) # Offices if len(data['departments']) > 1: for dept_rec in data['departments']: if dept_rec.get('top_level'): # This is actually an agency sub_agency_name = dept_rec['name'] sub_agency_slug = Agency.slug_for(sub_agency_name) sub_agency, created = Agency.objects.get_or_create( slug=sub_agency_slug, name=sub_agency_name) sub_agency.parent = a abbreviation = build_abbreviation(sub_agency_name) sub_agency.abbreviation = abbreviation sub_agency.description = dept_rec.get('description') sub_agency.keyword = dept_rec.get('keywords') sub_agency.common_requests = dept_rec.get( 'common_requests', []) sub_agency.no_records_about = dept_rec.get( 'no_records_about', []) contactable_fields(sub_agency, dept_rec) sub_agency.save() add_request_time_statistics(dept_rec, sub_agency) else: # Just an office office_name = dept_rec['name'] office_slug = Office.slug_for(office_name) full_slug = slug + '--' + office_slug o, created = Office.objects.get_or_create(agency=a, slug=full_slug) o.office_slug = office_slug o.name = office_name contactable_fields(o, dept_rec) o.save() add_request_time_statistics(dept_rec, a, o)