Exemple #1
0
    def test_location_file_upload(self, mock_api_client):
        self.login()
        self.setup_mock_get_authenticated_api_session(mock_api_client)

        file_data, expected_data = generate_testable_location_data()
        expected_calls = [expected_data]

        with responses.RequestsMock() as rsps, silence_logger(
                level=logging.WARNING):
            mock_prison_response(rsps)
            rsps.add(rsps.POST,
                     api_url('/prisoner_locations/actions/delete_inactive/'))
            rsps.add(rsps.POST, api_url('/prisoner_locations/'))
            rsps.add(rsps.POST,
                     api_url('/prisoner_locations/actions/delete_old/'))
            response = self.client.post(
                reverse('location_file_upload'),
                {'location_file': get_csv_data_as_file(file_data)})

            for call in rsps.calls:
                if call.request.url == api_url('/prisoner_locations/'):
                    self.assertEqual(json.loads(call.request.body.decode()),
                                     expected_calls.pop())

        if expected_calls:
            self.fail('Not all location data was uploaded')

        self.assertRedirects(response, reverse('location_file_upload'))
Exemple #2
0
    def test_location_file_valid_excel_format(self):
        file_data, _ = generate_testable_location_data(excel_csv=True)

        request = self.make_request(get_csv_data_as_file(file_data))
        with responses.RequestsMock() as rsps:
            mock_prison_response(rsps)
            form = LocationFileUploadForm(request.POST, request.FILES, request=request)
            self.assertTrue(form.is_valid())
Exemple #3
0
    def test_location_file_short_row_length_invalid(self):
        file_data, _ = generate_testable_location_data(
            extra_rows=['A1234GY,Smith,John,2/9/1997 00:00']
        )

        request = self.make_request(get_csv_data_as_file(file_data))
        with responses.RequestsMock() as rsps:
            mock_prison_response(rsps)
            form = LocationFileUploadForm(request.POST, request.FILES, request=request)
            self.assertFalse(form.is_valid())

        self.assertEqual(
            form.errors['location_file'],
            ['The file has the wrong number of columns']
        )
Exemple #4
0
    def test_location_file_without_valid_rows(self):
        file_data, _ = generate_testable_location_data(length=0, extra_rows=[
            'A1234ZZ,Smith,John,2/9/1997,TRN',
            'A1235ZZ,Smith,Fred,2/9/1997,ZCH',
        ])

        request = self.make_request(get_csv_data_as_file(file_data))
        with responses.RequestsMock() as rsps:
            mock_prison_response(rsps)
            form = LocationFileUploadForm(request.POST, request.FILES, request=request)
            self.assertFalse(form.is_valid())

        self.assertEqual(
            form.errors['location_file'],
            ['The uploaded report contains no valid prisoner locations']
        )
Exemple #5
0
    def test_location_file_batch_upload(self, mock_api_client):
        self.setup_mock_get_authenticated_api_session(mock_api_client)

        file_data, expected_data = generate_testable_location_data(length=50)
        expected_calls = [
            expected_data[40:50],
            expected_data[30:40],
            expected_data[20:30],
            expected_data[10:20],
            expected_data[0:10]
        ]

        request = self.make_request(get_csv_data_as_file(file_data))

        with responses.RequestsMock() as rsps, silence_logger(level=logging.WARNING):
            mock_prison_response(rsps)
            form = LocationFileUploadForm(request.POST, request.FILES, request=request)
            self.assertTrue(form.is_valid())

            rsps.add(
                rsps.POST,
                api_url('/prisoner_locations/actions/delete_inactive/')
            )
            rsps.add(
                rsps.POST,
                api_url('/prisoner_locations/')
            )
            rsps.add(
                rsps.POST,
                api_url('/prisoner_locations/actions/delete_old/')
            )
            form.update_locations()

            for call in rsps.calls:
                if call.request.url == api_url('/prisoner_locations/'):
                    self.assertEqual(
                        json.loads(call.request.body.decode()),
                        expected_calls.pop()
                    )

        if expected_calls:
            self.fail('Not all location data was uploaded')
Exemple #6
0
    def test_location_file_with_ignored_prisons(self, mock_api_client):
        self.setup_mock_get_authenticated_api_session(mock_api_client)

        file_data, expected_data = generate_testable_location_data(length=20, extra_rows=[
            'A1234ZZ,Smith,John,2/9/1997,TRN',
            'A1235ZZ,Smith,Fred,2/9/1997,ZCH',
        ])
        expected_calls = [expected_data]

        request = self.make_request(get_csv_data_as_file(file_data))

        with responses.RequestsMock() as rsps, silence_logger(level=logging.WARNING):
            mock_prison_response(rsps)
            form = LocationFileUploadForm(request.POST, request.FILES, request=request)
            self.assertTrue(form.is_valid())

            rsps.add(
                rsps.POST,
                api_url('/prisoner_locations/actions/delete_inactive/')
            )
            rsps.add(
                rsps.POST,
                api_url('/prisoner_locations/')
            )
            rsps.add(
                rsps.POST,
                api_url('/prisoner_locations/actions/delete_old/')
            )
            form.update_locations()

            for call in rsps.calls:
                if call.request.url == api_url('/prisoner_locations/'):
                    self.assertEqual(
                        json.loads(call.request.body.decode()),
                        expected_calls.pop()
                    )

        if expected_calls:
            self.fail('Not all location data was uploaded')
Exemple #7
0
    def test_location_file_with_ignored_prisons(self, mock_api_client):
        self.login()
        self.setup_mock_get_authenticated_api_session(mock_api_client)

        file_data, expected_data = generate_testable_location_data(
            length=20,
            extra_rows=[
                'A1234ZZ,Smith,John,2/9/1997,TRN',
                'A1235ZZ,Smith,Fred,2/9/1997,ZCH',
            ])
        expected_calls = [expected_data]

        with responses.RequestsMock() as rsps, silence_logger(
                level=logging.WARNING):
            mock_prison_response(rsps)
            rsps.add(rsps.POST,
                     api_url('/prisoner_locations/actions/delete_inactive/'))
            rsps.add(rsps.POST, api_url('/prisoner_locations/'))
            rsps.add(rsps.POST,
                     api_url('/prisoner_locations/actions/delete_old/'))
            response = self.client.post(
                reverse('location_file_upload'),
                {'location_file': get_csv_data_as_file(file_data)},
                follow=True)

            for call in rsps.calls:
                if call.request.url == api_url('/prisoner_locations/'):
                    self.assertEqual(json.loads(call.request.body.decode()),
                                     expected_calls.pop())

        if expected_calls:
            self.fail('Not all location data was uploaded')

        self.assertRedirects(response, reverse('location_file_upload'))
        response_content = response.content.decode()
        self.assertIn('Ignored 1 prisoner in transfer', response_content)
        self.assertIn('Ignored 1 prisoner in unsupported prison',
                      response_content)
        self.assertIn('NOMIS code: ZCH', response_content)
Exemple #8
0
    def test_location_file_upload_api_error_displays_message(
            self, mock_api_client):
        self.login()
        self.setup_mock_get_authenticated_api_session(mock_api_client)

        api_error_message = 'prison not found'
        response_content = ('[{"prison": ["%s"]}]' %
                            api_error_message).encode()

        file_data, _ = generate_testable_location_data()

        with responses.RequestsMock() as rsps, silence_logger():
            mock_prison_response(rsps)
            rsps.add(rsps.POST,
                     api_url('/prisoner_locations/actions/delete_inactive/'))
            rsps.add(rsps.POST,
                     api_url('/prisoner_locations/'),
                     status=400,
                     body=response_content)
            response = self.client.post(
                reverse('location_file_upload'),
                {'location_file': get_csv_data_as_file(file_data)})

        self.assertContains(response, api_error_message)