Ejemplo n.º 1
0
    def test_presqt_fts_metadata_extra(self):
        """
        Check that the extra metadata is added to the deposition
        """
        self.file = 'presqt/api_v1/tests/resources/upload/Upload_Extra_Metadata.zip'
        self.project_title = 'Extra Eggs'
        # 202 when uploading a new top level repo
        shared_upload_function_osf(self)

        # On the project that was just created, we need to get the contents of the metadata file.
        metadata_helper = requests.get(
            'https://zenodo.org/api/deposit/depositions',
            params=self.auth_params).json()
        for project in metadata_helper:
            if project['title'] == self.project_title:
                url = project['links']['self']
                get_info = requests.get(url, params=self.auth_params).json()
                break
        self.assertEqual(get_info['metadata']['description'],
                         "There's so many eggs in here.")
        self.assertEqual(get_info['metadata']['creators'][0]['name'],
                         "Egg Eggs")
        self.assertEqual(get_info['metadata']['title'], "Extra Eggs")

        requests.delete(url, params=self.auth_params)

        # Delete upload folder
        shutil.rmtree(self.ticket_path)
Ejemplo n.º 2
0
    def test_presqt_fts_metadata(self):
        """
        Check that the PRESQT_FTS_METADATA is created and what we expect.
        """
        # 202 when uploading a new top level repo
        shared_upload_function_osf(self)

        # On the project that was just created, we need to get the contents of the metadata file.
        metadata_helper = requests.get(
            'https://zenodo.org/api/deposit/depositions',
            params=self.auth_params).json()
        for project in metadata_helper:
            if project['title'] == self.project_title:
                get_files = requests.get(project['links']['files'],
                                         params=self.auth_params).json()
                for file in get_files:
                    if file['filename'] == 'PRESQT_FTS_METADATA.json':
                        response = requests.get(file['links']['download'],
                                                params=self.auth_params)
                        metadata_file = json.loads(response.content)
        # Action metadata
        self.assertEqual(metadata_file['actions'][0]['actionType'],
                         'resource_upload')
        self.assertEqual(metadata_file['actions'][0]['sourceTargetName'],
                         'Server Created Zip')
        self.assertEqual(metadata_file['actions'][0]['destinationTargetName'],
                         'zenodo')
        self.assertEqual(metadata_file['actions'][0]['destinationUsername'],
                         None)

        # Delete upload folder
        shutil.rmtree(self.ticket_path)
Ejemplo n.º 3
0
    def test_uploading_to_exisitng_container_ignore(self):
        """
        If there is an ignored file from Zenodo when uploading to an existing container, we want 
        to make the user aware.
        """
        # 202 when uploading a new top level repo
        shared_upload_function_osf(self)

        # Verify the new repo exists on the PresQT Resource Collection endpoint.
        url = reverse('resource_collection', kwargs={'target_name': 'zenodo'})
        response_json = self.client.get(
            url, **{
                'HTTP_PRESQT_SOURCE_TOKEN': ZENODO_TEST_USER_TOKEN
            }).json()['resources']

        repo_name_list = [repo['title'] for repo in response_json]
        self.assertIn(self.project_title, repo_name_list)

        # Delete upload folder
        shutil.rmtree(self.ticket_path)

        self.resource_id = [
            resource['id'] for resource in response_json
            if resource['title'] == self.project_title
        ][0]
        self.duplicate_action = 'ignore'
        self.url = reverse('resource',
                           kwargs={
                               'target_name': 'zenodo',
                               'resource_id': self.resource_id
                           })
        self.file = 'presqt/api_v1/tests/resources/upload/ProjectSingleFileToUpload.zip'

        self.headers['HTTP_PRESQT_FILE_DUPLICATE_ACTION'] = 'ignore'
        response = self.client.post(self.url,
                                    {'presqt-file': open(self.file, 'rb')},
                                    **self.headers)

        ticket_path = 'mediafiles/jobs/{}'.format(self.ticket_number)

        # Wait until the spawned off process finishes in the background
        # to do validation on the resulting files
        process_info = read_file('{}/process_info.json'.format(ticket_path),
                                 True)
        while process_info['resource_upload']['status'] == 'in_progress':
            try:
                process_info = read_file(
                    '{}/process_info.json'.format(ticket_path), True)
            except json.decoder.JSONDecodeError:
                # Pass while the process_info file is being written to
                pass

        upload_job_response = self.client.get(response.data['upload_job'],
                                              **self.headers)
        self.assertEqual(upload_job_response.data['status_code'], '200')
        self.assertEqual(upload_job_response.data['resources_ignored'],
                         ['/NewProject/NewProject.presqt.zip'])

        # Delete the upload folder
        shutil.rmtree(ticket_path)
Ejemplo n.º 4
0
    def test_success_202_upload(self):
        """
        Return a 202 when a file is uploading.
        """
        # 202 when uploading a new top level repo
        shared_upload_function_osf(self)

        # Verify the new repo exists on the PresQT Resource Collection endpoint.
        url = reverse('resource_collection', kwargs={'target_name': 'zenodo'})
        response_json = self.client.get(
            url, **{
                'HTTP_PRESQT_SOURCE_TOKEN': ZENODO_TEST_USER_TOKEN
            }).json()['resources']

        repo_name_list = [repo['title'] for repo in response_json]
        self.assertIn(self.project_title, repo_name_list)

        # Delete upload folder
        shutil.rmtree(self.ticket_path)

        # 202 when uploading to an existing project.
        self.resource_id = [
            resource['id'] for resource in response_json
            if resource['title'] == self.project_title
        ][0]
        self.duplicate_action = 'ignore'
        self.url = reverse('resource',
                           kwargs={
                               'target_name': 'zenodo',
                               'resource_id': self.resource_id
                           })
        self.file = 'presqt/api_v1/tests/resources/upload/SingleFileDuplicate.zip'
        shared_upload_function_osf(self)

        # Ensure there are two actions in the metadata.
        metadata_helper = requests.get(
            'https://zenodo.org/api/deposit/depositions/{}/files'.format(
                self.resource_id),
            params=self.auth_params).json()
        for file in metadata_helper:
            if file['filename'] == 'PRESQT_FTS_METADATA.json':
                response = requests.get(file['links']['download'],
                                        params=self.auth_params)
                metadata_file = json.loads(response.content)

        # Verify there are multiple actions
        self.assertEqual(len(metadata_file['actions']), 2)

        # Delete upload folder
        shutil.rmtree(self.ticket_path)
Ejemplo n.º 5
0
    def test_invalid_metadata_existing_on_zenodo(self):
        """
        If there is bad metadata on Zenodo already, we want to rename it and upload the good stuff.
        """
        # 202 when uploading a new top level repo
        shared_upload_function_osf(self)
        # Verify the new repo exists on the PresQT Resource Collection endpoint.
        url = reverse('resource_collection', kwargs={'target_name': 'zenodo'})
        response_json = self.client.get(
            url, **{
                'HTTP_PRESQT_SOURCE_TOKEN': ZENODO_TEST_USER_TOKEN
            }).json()['resources']
        self.resource_id = [
            resource['id'] for resource in response_json
            if resource['title'] == self.project_title
        ][0]
        # Delete upload folder
        shutil.rmtree(self.ticket_path)

        # Muddy up the existing metadata file
        metadata_helper = requests.get(
            'https://zenodo.org/api/deposit/depositions/{}/files'.format(
                self.resource_id),
            params=self.auth_params).json()
        for file in metadata_helper:
            if file['filename'] == 'PRESQT_FTS_METADATA.json':
                # Delete old metadata file
                requests.delete(file['links']['self'], params=self.auth_params)
        bad_metadata = {"no": "good"}
        data = {'name': 'PRESQT_FTS_METADATA.json'}
        metadata_bytes = json.dumps(bad_metadata, indent=4).encode('utf-8')
        files = {'file': metadata_bytes}

        # Make the request
        response = requests.post(
            'https://zenodo.org/api/deposit/depositions/{}/files'.format(
                self.resource_id),
            params=self.auth_params,
            data=data,
            files=files)

        # 202 when uploading to an existing project.
        self.duplicate_action = 'ignore'
        self.url = reverse('resource',
                           kwargs={
                               'target_name': 'zenodo',
                               'resource_id': self.resource_id
                           })
        self.file = 'presqt/api_v1/tests/resources/upload/SingleFileDuplicate.zip'
        shared_upload_function_osf(self)

        # Ensure there are two actions in the metadata.
        metadata_helper = requests.get(
            'https://zenodo.org/api/deposit/depositions/{}/files'.format(
                self.resource_id),
            params=self.auth_params).json()
        for file in metadata_helper:
            if file['filename'] == 'INVALID_PRESQT_FTS_METADATA.json':
                response = requests.get(file['links']['download'],
                                        params=self.auth_params)
                metadata_file = json.loads(response.content)

        # Verify there are multiple actions
        self.assertEqual(metadata_file, {'no': 'good'})

        # Delete upload folder
        shutil.rmtree(self.ticket_path)
Ejemplo n.º 6
0
    def test_error_uploading_to_exisitng_container(self):
        """
        If there is an error response from Zenodo when uploading to an existing container, we want 
        to mkae the user aware.
        """
        # 202 when uploading a new top level repo
        shared_upload_function_osf(self)

        # Verify the new repo exists on the PresQT Resource Collection endpoint.
        url = reverse('resource_collection', kwargs={'target_name': 'zenodo'})
        response_json = self.client.get(
            url, **{
                'HTTP_PRESQT_SOURCE_TOKEN': ZENODO_TEST_USER_TOKEN
            }).json()

        repo_name_list = [repo['title'] for repo in response_json]
        self.assertIn(self.project_title, repo_name_list)

        # Delete upload folder
        shutil.rmtree(self.ticket_path)

        # *Error* when uploading to an existing project.
        class MockResponse:
            def __init__(self, json_data, status_code):
                self.json_data = json_data
                self.status_code = status_code

        mock_req = MockResponse({'error': 'The server is down.'}, 500)

        self.resource_id = [
            resource['id'] for resource in response_json
            if resource['title'] == self.project_title
        ][0]
        self.duplicate_action = 'ignore'
        self.url = reverse('resource',
                           kwargs={
                               'target_name': 'zenodo',
                               'resource_id': self.resource_id
                           })
        self.file = 'presqt/api_v1/tests/resources/upload/ProjectSingleFileToUpload.zip'

        with patch('requests.post') as fake_post:
            fake_post.return_value = mock_req

            self.headers[
                'HTTP_PRESQT_FILE_DUPLICATE_ACTION'] = self.duplicate_action
            response = self.client.post(self.url,
                                        {'presqt-file': open(self.file, 'rb')},
                                        **self.headers)

            ticket_number = response.data['ticket_number']
            ticket_path = 'mediafiles/uploads/{}'.format(ticket_number)

            # Wait until the spawned off process finishes in the background
            # to do validation on the resulting files
            process_info = read_file(
                '{}/process_info.json'.format(ticket_path), True)
            while process_info['status'] == 'in_progress':
                try:
                    process_info = read_file(
                        '{}/process_info.json'.format(ticket_path), True)
                except json.decoder.JSONDecodeError:
                    # Pass while the process_info file is being written to
                    pass

            upload_job_response = self.client.get(response.data['upload_job'],
                                                  **self.headers)
            self.assertEqual(upload_job_response.data['status_code'], 400)
            self.assertEqual(
                upload_job_response.data['message'],
                'Zenodo returned an error trying to upload NewProject.presqt.zip'
            )

            # Delete the upload folder
            shutil.rmtree(ticket_path)