Example #1
0
 def test_csv_parsed(self, header_row, open_ended_schema, subjects_list,
                     registration_provider, valid_row):
     test_csv = write_csv(header_row, {'Title': open_ended_schema._id},
                          valid_row)
     upload = BulkRegistrationUpload(test_csv, registration_provider._id)
     assert upload.is_validated is False
     upload.validate()
     parsed = upload.get_parsed()
     assert upload.is_validated is True
     assert upload.errors == []
     assert 'registrations' in parsed
     assert 'csv_raw' in parsed['registrations'][0]
     assert 'csv_parsed' in parsed['registrations'][0]
     assert_parsed(
         parsed['registrations'][0]['csv_parsed'], {
             'Title':
             'Test title',
             'Description':
             'Test description',
             'Admin Contributors': [{
                 'full_name': 'jane doe',
                 'email': '*****@*****.**'
             }, {
                 'full_name': 'joan doe',
                 'email': '*****@*****.**'
             }],
             'Read-Write Contributors': [],
             'Read-Only Contributors': [],
             'Bibliographic Contributors': [{
                 'full_name': 'jane doe',
                 'email': '*****@*****.**'
             }, {
                 'full_name': 'joan doe',
                 'email': '*****@*****.**'
             }],
             'Category':
             'project',
             'Affiliated Institutions': [],
             'License': {
                 'name': 'No license',
                 'required_fields': {
                     'year': '2030',
                     'copyright_holders':
                     ['jane doe', 'john doe', 'joan doe']
                 }
             },
             'Subjects':
             subjects_list,
             'Tags': [],
             'Project GUID':
             '',
             'External ID':
             '',
             'summary':
             'Test study',
         })
     test_csv.close()
Example #2
0
 def test_required_schema_question_errors(self, header_row,
                                          open_ended_schema,
                                          registration_provider, valid_row):
     missing_required_response = {'summary': ''}
     test_row = make_row({**valid_row, **missing_required_response})
     test_csv = write_csv(header_row, {'Title': open_ended_schema._id},
                          test_row)
     upload = BulkRegistrationUpload(test_csv, registration_provider._id)
     upload.validate()
     assert hasattr(upload, 'errors') is True
     assert_errors(upload.errors, {'summary': 'invalidResponse'})
     test_csv.close()
Example #3
0
    def test_invalid_headers_validation_error(self, open_ended_schema,
                                              registration_provider,
                                              valid_row):
        header_row = [
            'Title', 'description', 'Admin Contributors',
            'Read-Write Contributors', 'Read-Only Contributors',
            'Bibliographic Contributors', 'Category',
            'Affiliated Institutions', 'license', 'Subjects', 'Tags',
            'Project GUID', 'External ID', 'Uploader', 'Summary'
        ]
        test_csv = write_csv(header_row, {'Title': open_ended_schema._id})
        expected_missing_headers = ['Description', 'summary', 'License']
        expected_invalid_headers = [
            'Uploader', 'license', 'description', 'Summary'
        ]

        with pytest.raises(InvalidHeadersError) as exc_info:
            BulkRegistrationUpload(test_csv, registration_provider._id)

        actual_invalid_headers = exc_info.value.args[0]['invalid_headers']
        actual_missing_headers = exc_info.value.args[0]['missing_headers']

        assert actual_invalid_headers.sort() == expected_invalid_headers.sort()
        assert actual_missing_headers.sort() == expected_missing_headers.sort()
        test_csv.close()
Example #4
0
    def test_invalid_schema_id(self, registration_provider, header_row,
                               valid_row):
        test_csv = write_csv(header_row, {'Title': ''}, valid_row)

        with pytest.raises(NotFound) as exc_info:
            BulkRegistrationUpload(test_csv, registration_provider._id)
        assert str(exc_info.value.detail) == 'Schema with id "" was not found'
        test_csv.close()
Example #5
0
 def test_missing_required_metadata_errors(self, header_row,
                                           registration_provider,
                                           open_ended_schema):
     missing_required_metadata = {
         'Title': '',
         'Description': '',
         'License': '',
         'Subjects': '',
         'Admin Contributors': '',
         'Bibliographic Contributors': ''
     }
     test_row = make_row(missing_required_metadata)
     test_csv = write_csv(header_row, {'Title': open_ended_schema._id},
                          test_row)
     upload = BulkRegistrationUpload(test_csv, registration_provider._id)
     assert upload.is_validated is False
     upload.validate()
     assert upload.is_validated is True
     assert hasattr(upload, 'errors') is True
     assert_errors(
         upload.errors, {
             'Title':
             METADATA_FIELDS['Title']['error_type']['missing'],
             'Description':
             METADATA_FIELDS['Description']['error_type']['missing'],
             'License':
             METADATA_FIELDS['License']['error_type']['missing'],
             'Subjects':
             METADATA_FIELDS['Subjects']['error_type']['missing'],
             'Admin Contributors':
             METADATA_FIELDS['Admin Contributors']['error_type']['missing'],
             'Bibliographic Contributors':
             METADATA_FIELDS['Bibliographic Contributors']['error_type']
             ['missing'],
         })
     test_csv.close()
Example #6
0
    def test_file_upload_not_supported_validation_error(
            self, header_row, open_ended_schema, registration_provider,
            valid_row):
        file_input_block = open_ended_schema.schema_blocks.get(
            block_type='file-input')
        file_input_block.required = True
        file_input_block.display_text = 'N/A'
        file_input_block.help_text = 'N/A'
        file_input_block.example_text = 'N/A'
        file_input_block.save()
        test_csv = write_csv(header_row, {'Title': open_ended_schema._id},
                             valid_row)

        with pytest.raises(FileUploadNotSupportedError) as exc_info:
            BulkRegistrationUpload(test_csv, registration_provider._id)

        assert exc_info.value.args[0] == 'File uploads are not supported'
        test_csv.close()
Example #7
0
    def test_duplicate_headers_validation_error(self, open_ended_schema,
                                                registration_provider,
                                                valid_row):
        header_row = [
            'Title', 'Description', 'Admin Contributors',
            'Read-Write Contributors', 'Read-Only Contributors',
            'Bibliographic Contributors', 'Category',
            'Affiliated Institutions', 'License', 'Subjects', 'Subjects',
            'Tags', 'Project GUID', 'External ID', 'summary', 'summary'
        ]
        test_csv = write_csv(header_row, {'Title': open_ended_schema._id},
                             valid_row)

        with pytest.raises(DuplicateHeadersError) as exc_info:
            BulkRegistrationUpload(test_csv, registration_provider._id)

        assert exc_info.value.args[0]['duplicate_headers'] == [
            'Subjects', 'summary'
        ]
        test_csv.close()
Example #8
0
    def put(self, request, *args, **kwargs):
        provider_id = kwargs['provider_id']
        provider = get_object_or_error(RegistrationProvider, provider_id,
                                       request)
        if not provider.allow_bulk_uploads:
            return JsonResponse(
                {'errors': [{
                    'type': 'bulkUploadNotAllowed'
                }]},
                status=405,
                content_type='application/vnd.api+json; application/json',
            )
        user_id = self.request.user._id
        file_size_limit = BULK_SETTINGS['DEFAULT_BULK_LIMIT'] * 10000
        file_obj = request.data['file']

        if file_obj.size > file_size_limit:
            return JsonResponse(
                {'errors': [{
                    'type': 'sizeExceedsLimit'
                }]},
                status=413,
                content_type='application/vnd.api+json; application/json',
            )

        if file_obj.content_type != 'text/csv':
            return JsonResponse(
                {'errors': [{
                    'type': 'invalidFileType'
                }]},
                status=413,
                content_type='application/vnd.api+json; application/json',
            )

        file_md5 = self.get_hash(file_obj)
        if RegistrationBulkUploadJob.objects.filter(
                payload_hash=file_md5).exists():
            return JsonResponse(
                {'errors': [{
                    'type': 'bulkUploadJobExists'
                }]},
                status=409,
                content_type='application/vnd.api+json; application/json',
            )
        try:
            upload = BulkRegistrationUpload(file_obj, provider_id)
            upload.validate()
            errors = upload.errors
        except InvalidHeadersError as e:
            invalid_headers = [
                str(detail) for detail in e.detail['invalid_headers']
            ]
            missing_headers = [
                str(detail) for detail in e.detail['missing_headers']
            ]
            return JsonResponse(
                {
                    'errors': [{
                        'type': 'invalidColumnId',
                        'invalidHeaders': invalid_headers,
                        'missingHeaders': missing_headers
                    }]
                },
                status=400,
                content_type='application/vnd.api+json; application/json',
            )
        except DuplicateHeadersError as e:
            duplicate_headers = [
                str(detail) for detail in e.detail['duplicate_headers']
            ]
            return JsonResponse(
                {
                    'errors': [{
                        'type': 'duplicateColumnId',
                        'duplicateHeaders': duplicate_headers
                    }]
                },
                status=400,
                content_type='application/vnd.api+json; application/json',
            )
        except FileUploadNotSupportedError:
            return JsonResponse(
                {'errors': [{
                    'type': 'fileUploadNotSupported'
                }]},
                status=400,
                content_type='application/vnd.api+json; application/json',
            )
        except NotFound:
            return JsonResponse(
                {'errors': [{
                    'type': 'invalidSchemaId'
                }]},
                status=404,
                content_type='application/vnd.api+json; application/json',
            )

        if errors:
            return JsonResponse(
                {'errors': errors},
                status=400,
                content_type='application/vnd.api+json; application/json',
            )
        parsed = upload.get_parsed()
        enqueue_task(
            prepare_for_registration_bulk_creation.s(file_md5,
                                                     user_id,
                                                     provider_id,
                                                     parsed,
                                                     dry_run=False))
        return Response(status=204)