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)
class TaskListViewPOSTTests(TestCase): """Tests POST requests to the Task list view""" # we have to get the task again if we want to see the updated value def setUp(self): self.url = reverse('response-task-list') self.view = ResponseTaskList.as_view() self.user = factories.UserFactory(is_staff=True) self.task = ResponseTaskFactory() def test_post_resolve_task(self): data = {'resolve': 'truthy', 'task': self.task.pk} http_post_response(self.url, self.view, data, self.user) self.task.refresh_from_db() eq_( self.task.resolved, True, 'Tasks should be resolved by posting the task ID with a "resolve" request.' ) eq_(self.task.resolved_by, self.user, 'Task should record the logged in user who resolved it.') def test_post_do_not_resolve_task(self): self.client.post(self.url, {'task': self.task.pk}) self.task.refresh_from_db() eq_(self.task.resolved, False, 'Tasks should not be resolved when no "resolve" data is POSTed.')
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)
def test_post_set_code(self): """Setting the scan code should save it to the response and request, then resolve task, and unhide to communication. """ task = ResponseTaskFactory(scan=True, communication__hidden=True) code = "RES" data = {"code": code, "set_foia": True, "task": task.pk, "save": True} http_post_response(self.url, self.view, data, self.user) task.refresh_from_db() task.communication.refresh_from_db() task.communication.foia.refresh_from_db() eq_(task.communication.communication, CODES[code][2]) eq_(task.communication.status, CODES[code][1]) eq_(task.communication.foia.status, CODES[code][1]) eq_(task.resolved, True) assert_false(task.communication.hidden)
class ResponseTaskListViewTests(TestCase): """Tests ResponseTask-specific POST handlers""" def setUp(self): self.user = factories.UserFactory(is_staff=True) self.url = reverse('response-task-list') self.view = ResponseTaskList.as_view() self.task = ResponseTaskFactory() def test_get_list(self): """Staff users should be able to view a list of tasks.""" response = http_get_response(self.url, self.view, self.user) eq_(response.status_code, 200) def test_get_single(self): """Staff users should be able to view a single task""" url = reverse('response-task', kwargs={'pk': self.task.pk}) response = http_get_response(url, self.view, self.user) eq_(response.status_code, 200) def test_post_set_price(self): """Setting the price should update the price on the response's request.""" price = 1 foia = self.task.communication.foia logging.info(foia.agency) data = {'status': 'done', 'price': price, 'task': self.task.pk} http_post_response(self.url, self.view, data, self.user) self.task.refresh_from_db() foia.refresh_from_db() eq_(foia.price, float(price), 'The price on the FOIA should be set.') ok_(self.task.resolved, 'Setting the price should resolve the task.') def test_post_set_status(self): """Setting the status should save it to the response and request, then resolve task.""" status_change = 'done' data = { 'status': status_change, 'set_foia': True, 'task': self.task.pk } http_post_response(self.url, self.view, data, self.user) self.task.refresh_from_db() self.task.communication.refresh_from_db() self.task.communication.foia.refresh_from_db() comm_status = self.task.communication.status foia_status = self.task.communication.foia.status eq_(comm_status, status_change, 'The status change should be saved to the communication.') eq_(foia_status, status_change, 'The status of the FOIA should be set.') eq_(self.task.resolved, True, 'Setting the status should resolve the task') def test_post_set_comm_status(self): """Setting the status on just the communication should not influence its request.""" status_change = 'done' existing_foia_status = self.task.communication.foia.status data = { 'status': status_change, 'set_foia': False, 'task': self.task.pk } http_post_response(self.url, self.view, data, self.user) self.task.refresh_from_db() self.task.communication.refresh_from_db() self.task.communication.foia.refresh_from_db() comm_status = self.task.communication.status foia_status = self.task.communication.foia.status eq_(comm_status, status_change, 'The status change should be saved to the communication.') eq_(foia_status, existing_foia_status, 'The status of the FOIA should not be changed.') eq_(self.task.resolved, True, 'Settings the status should resolve the task.') def test_post_tracking_number(self): """Setting the tracking number should save it to the response's request.""" new_tracking_id = 'ABC123OMGWTF' data = { 'tracking_number': new_tracking_id, 'status': 'done', 'task': self.task.pk } http_post_response(self.url, self.view, data, self.user) self.task.refresh_from_db() self.task.communication.refresh_from_db() self.task.communication.foia.refresh_from_db() foia_tracking = self.task.communication.foia.tracking_id eq_( foia_tracking, new_tracking_id, 'The new tracking number should be saved to the associated request.' ) ok_(self.task.resolved, 'Setting the tracking number should resolve the task') def test_post_move(self): """Moving the response should save it to a new request.""" other_foia = factories.FOIARequestFactory() starting_date = self.task.communication.date data = {'move': other_foia.id, 'status': 'done', 'task': self.task.pk} http_post_response(self.url, self.view, data, self.user) self.task.refresh_from_db() self.task.communication.refresh_from_db() ending_date = self.task.communication.date eq_(self.task.communication.foia, other_foia, 'The response should be moved to a different FOIA.') ok_(self.task.resolved, 'Moving the status should resolve the task') eq_(starting_date, ending_date, 'Moving the communication should not change its date.') def test_post_move_multiple(self): """Moving the response to multiple requests modify all the requests.""" other_foias = [ factories.FOIARequestFactory(), factories.FOIARequestFactory(), factories.FOIARequestFactory() ] move_to_ids = ', '.join([str(foia.id) for foia in other_foias]) change_status = 'done' change_tracking = 'DEADBEEF' data = { 'move': move_to_ids, 'status': change_status, 'tracking_number': change_tracking, 'task': self.task.pk } http_post_response(self.url, self.view, data, self.user) for foia in other_foias: foia.refresh_from_db() eq_(change_tracking, foia.tracking_id, 'Tracking should update for each request in move list.') def test_terrible_data(self): """Posting awful data shouldn't cause everything to collapse.""" data = { 'move': 'omglol, howru', 'status': 'notastatus', 'tracking_number': ['wtf'], 'task': self.task.pk } response = http_post_response(self.url, self.view, data, self.user) ok_(response) def test_foia_integrity(self): """ Updating a request through a task should maintain integrity of that request's data. This is in response to issue #387. """ # first saving a comm foia = self.task.communication.foia num_comms = foia.communications.count() save_foia_comm(foia, 'Testman', 'Just testing, u no') eq_(foia.communications.count(), num_comms + 1, 'Should add a new communication to the FOIA.') num_comms = foia.communications.count() # next try resolving the task with a tracking number set data = { 'resolve': 'true', 'tracking_number': u'12345', 'task': self.task.pk } http_post_response(self.url, self.view, data, self.user) foia.refresh_from_db() eq_( foia.communications.count(), num_comms, 'The number of communications should not have changed from before.' )
class ResponseTaskListViewTests(TestCase): """Tests ResponseTask-specific POST handlers""" def setUp(self): password = "******" self.user = UserFactory(is_staff=True, password=password) self.url = reverse("response-task-list") self.view = ResponseTaskList.as_view() self.task = ResponseTaskFactory() self.client.login(username=self.user.username, password=password) def test_get_list(self): """Staff users should be able to view a list of tasks.""" response = http_get_response(self.url, self.view, self.user) eq_(response.status_code, 200) @tag("slow") def _test_n_plus_one_query(self): """Should make the same number of SQL queries regardless of amount of data""" n_plus_one_query(self.client, self.url, ResponseTaskFactory) def test_get_single(self): """Staff users should be able to view a single task""" url = reverse("response-task", kwargs={"pk": self.task.pk}) response = http_get_response(url, self.view, self.user) eq_(response.status_code, 200) def test_post_set_price(self): """Setting the price should update the price on the response's request.""" price = 1 foia = self.task.communication.foia logging.info(foia.agency) data = {"status": "done", "price": price, "task": self.task.pk, "save": True} http_post_response(self.url, self.view, data, self.user) self.task.refresh_from_db() foia.refresh_from_db() eq_(foia.price, float(price), "The price on the FOIA should be set.") ok_(self.task.resolved, "Setting the price should resolve the task.") def test_post_set_status(self): """Setting the status should save it to the response and request, then resolve task. """ status_change = "done" data = { "status": status_change, "set_foia": True, "task": self.task.pk, "save": True, } http_post_response(self.url, self.view, data, self.user) self.task.refresh_from_db() self.task.communication.refresh_from_db() self.task.communication.foia.refresh_from_db() comm_status = self.task.communication.status foia_status = self.task.communication.foia.status eq_( comm_status, status_change, "The status change should be saved to the communication.", ) eq_(foia_status, status_change, "The status of the FOIA should be set.") eq_(self.task.resolved, True, "Setting the status should resolve the task") def test_post_set_code(self): """Setting the scan code should save it to the response and request, then resolve task, and unhide to communication. """ task = ResponseTaskFactory(scan=True, communication__hidden=True) code = "RES" data = {"code": code, "set_foia": True, "task": task.pk, "save": True} http_post_response(self.url, self.view, data, self.user) task.refresh_from_db() task.communication.refresh_from_db() task.communication.foia.refresh_from_db() eq_(task.communication.communication, CODES[code][2]) eq_(task.communication.status, CODES[code][1]) eq_(task.communication.foia.status, CODES[code][1]) eq_(task.resolved, True) assert_false(task.communication.hidden) def test_post_set_comm_status(self): """Setting the status on just the communication should not influence its request.""" status_change = "done" existing_foia_status = self.task.communication.foia.status data = { "status": status_change, "set_foia": False, "task": self.task.pk, "save": True, } http_post_response(self.url, self.view, data, self.user) self.task.refresh_from_db() self.task.communication.refresh_from_db() self.task.communication.foia.refresh_from_db() comm_status = self.task.communication.status foia_status = self.task.communication.foia.status eq_( comm_status, status_change, "The status change should be saved to the communication.", ) eq_( foia_status, existing_foia_status, "The status of the FOIA should not be changed.", ) eq_(self.task.resolved, True, "Settings the status should resolve the task.") def test_post_tracking_number(self): """Setting the tracking number should save it to the response's request.""" new_tracking_id = "ABC123OMGWTF" data = { "tracking_number": new_tracking_id, "status": "done", "task": self.task.pk, "save": True, } http_post_response(self.url, self.view, data, self.user) self.task.refresh_from_db() self.task.communication.refresh_from_db() self.task.communication.foia.refresh_from_db() foia_tracking = self.task.communication.foia.current_tracking_id() eq_( foia_tracking, new_tracking_id, "The new tracking number should be saved to the associated request.", ) ok_(self.task.resolved, "Setting the tracking number should resolve the task") def test_post_move(self): """Moving the response should save it to a new request.""" other_foia = FOIARequestFactory() starting_date = self.task.communication.datetime data = { "move": other_foia.id, "status": "done", "task": self.task.pk, "save": True, } http_post_response(self.url, self.view, data, self.user) self.task.refresh_from_db() self.task.communication.refresh_from_db() ending_date = self.task.communication.datetime eq_( self.task.communication.foia, other_foia, "The response should be moved to a different FOIA.", ) ok_(self.task.resolved, "Moving the status should resolve the task") eq_( starting_date, ending_date, "Moving the communication should not change its date.", ) def test_post_move_multiple(self): """Moving the response to multiple requests modify all the requests.""" other_foias = [FOIARequestFactory(), FOIARequestFactory(), FOIARequestFactory()] move_to_ids = ", ".join([str(foia.id) for foia in other_foias]) change_status = "done" change_tracking = "DEADBEEF" data = { "move": move_to_ids, "status": change_status, "tracking_number": change_tracking, "task": self.task.pk, "save": True, } http_post_response(self.url, self.view, data, self.user) for foia in other_foias: foia.refresh_from_db() eq_( change_tracking, foia.current_tracking_id(), "Tracking should update for each request in move list.", ) def test_terrible_data(self): """Posting awful data shouldn't cause everything to collapse.""" data = { "move": "omglol, howru", "status": "notastatus", "tracking_number": ["wtf"], "task": self.task.pk, } response = http_post_response(self.url, self.view, data, self.user) ok_(response) def test_foia_integrity(self): """ Updating a request through a task should maintain integrity of that request's data. This is in response to issue #387. """ # first saving a comm foia = self.task.communication.foia num_comms = foia.communications.count() foia.create_out_communication( from_user=foia.user, text="Just testing", user=foia.user ) eq_( foia.communications.count(), num_comms + 1, "Should add a new communication to the FOIA.", ) num_comms = foia.communications.count() # next try resolving the task with a tracking number set data = {"resolve": "true", "tracking_number": "12345", "task": self.task.pk} http_post_response(self.url, self.view, data, self.user) foia.refresh_from_db() eq_( foia.communications.count(), num_comms, "The number of communications should not have changed from before.", )