Пример #1
0
    def test_success_full_code_repo(self):
        """
        Return a 200 along with a zip file of the repo and assosciated files requested.
        """
        resource_id = '209373160'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('download_job', kwargs={'ticket_number': self.ticket_number,
                                              'response_format': 'zip'})
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))
        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename={}_download_{}.zip'.format(self.target_name, resource_id))
        # Verify content type
        self.assertEqual(response._headers['content-type'][1], 'application/zip')
        # Verify the number of resources in the zip is correct
        self.assertEqual(len(zip_file.namelist()), 83)

        # GitHub does not provide file hashes, and thus we can't properly check fixity.
        with zip_file.open('{}_download_{}/data/fixity_info.json'.format(self.target_name, resource_id)) as fixityfile:
            zip_json = json.load(fixityfile)
            for file_fixity in zip_json:
                self.assertEqual(file_fixity['fixity_details'],
                                 "Either a Source Hash was not provided or the source hash algorithm is not supported.")

        # Delete corresponding folder
        shutil.rmtree('mediafiles/downloads/{}'.format(self.ticket_number))
Пример #2
0
    def test_success_item_multiple_files(self):
        """
        Return a 200 along with a zip file of the item and assosciated files requested.
        """
        resource_id = 'dj52w379504'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('download_job', kwargs={'ticket_number': self.ticket_number})
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))
        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename={}_download_{}.zip'.format(self.target_name, resource_id))
        # Verify content type
        self.assertEqual(response._headers['content-type'][1], 'application/zip')
        # Verify the number of resources in the zip is correct
        self.assertEqual(len(zip_file.namelist()), 14)

        # Verify the custom hash_file information is correct
        with zip_file.open('{}_download_{}/data/fixity_info.json'.format(self.target_name, resource_id)) as fixityfile:
            zip_json = json.load(fixityfile)
            for file_fixity in zip_json:
                self.assertEqual(file_fixity['fixity'], True)
                self.assertEqual(file_fixity['fixity_details'],
                                 'Source Hash and PresQT Calculated hash matched.')
                self.assertEqual(file_fixity['hash_algorithm'], 'md5')
                self.assertEqual(file_fixity['presqt_hash'], file_fixity['source_hash'])

        # Run the files through the fixity checker again to make sure they downloaded correctly
        with zip_file.open('{}_download_{}/data{}'.format(
                self.target_name, resource_id, zip_json[0]['path'])) as myfile:
            temp_file = myfile.read()
            resource_dict = {
                "file": temp_file,
                "hashes": {'md5': zip_json[0]['presqt_hash']},
                "title": 'f',
                "path": '{}_download_{}/data{}'.format(self.target_name, resource_id, zip_json[0]['path']),
                "metadata": {}
            }
            fixity, fixity_match = download_fixity_checker(resource_dict)
            self.assertEqual(fixity['fixity'], True)
        with zip_file.open('{}_download_{}/data{}'.format(
                self.target_name, resource_id, zip_json[1]['path'])) as myfile:
            temp_file = myfile.read()
            resource_dict = {
                "file": temp_file,
                "hashes": {'md5': zip_json[1]['presqt_hash']},
                "title": 'f',
                "path": '{}_download_{}/data{}'.format(self.target_name, resource_id, zip_json[1]['path']),
                "metadata": {}
            }
            fixity, fixity_match = download_fixity_checker(resource_dict)
            self.assertEqual(fixity['fixity'], True)

        # Delete corresponding folder
        shutil.rmtree('mediafiles/downloads/{}'.format(self.ticket_number))
Пример #3
0
    def test_success_download_unowned_public_repo(self):
        """
        Return a 200 along with a zip file of the unowned public repo requested.
        """
        resource_id = '248'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('download_job', kwargs={'ticket_number': self.ticket_number,
                                              'response_format': 'zip'})
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))
        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename={}_download_{}.zip'.format(self.target_name, resource_id))
        # Verify content type
        self.assertEqual(response._headers['content-type'][1], 'application/zip')
        # Verify the number of resources in the zip is correct
        # self.assertEqual(len(zip_file.namelist()), 13)

        # Verify the fixity file is empty as there was nothing to check.
        with zip_file.open('github_download_{}/data/fixity_info.json'.format(resource_id)) as fixityfile:
            zip_json = json.load(fixityfile)
            self.assertEqual(len(zip_json), 20)

        file_path = "{}_download_{}/data/rorem/lib/rorem/distribution.rb".format(self.target_name,
                                                                                 resource_id)
        # Verify that the file exists
        self.assertIn(file_path, zip_file.namelist())

        # Delete corresponding folder
        shutil.rmtree('mediafiles/downloads/{}'.format(self.ticket_number))
Пример #4
0
    def test_success_202(self):
        """
        Return a 202 if the resource has not finished being prepared on the server.
        """
        shared_call_get_resource_zip(self, self.resource_id)

        # Update the fixity_info.json to say the resource hasn't finished processing
        write_file(self.process_info_path, self.initial_process_info, True)

        url = reverse('download_job',
                      kwargs={'ticket_number': self.ticket_number})
        response = self.client.get(url, **self.header)

        # Verify the status code and content
        self.assertEqual(response.status_code, 202)
        self.assertEqual(
            response.data, {
                'message': 'Download is being processed on the server',
                'status_code': None
            })

        # Verify the status of the process_info file is 'in_progress'
        process_info = read_file(self.process_info_path, True)
        self.assertEqual(process_info['status'], 'in_progress')

        # Delete corresponding folder
        shutil.rmtree('mediafiles/downloads/{}'.format(self.ticket_number))
Пример #5
0
    def test_success_file(self):
        """
        Return a 200 along with a zip file of the file requested.
        """
        resource_id = '209373160:album_uploader%2F__pycache__%2F__init__%2Ecpython-36%2Epyc'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('job_status', kwargs={'action': 'download',
                                            'response_format': 'zip'})
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))

        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename={}_download_{}.zip'.format(self.target_name, resource_id))
        # Verify content type
        self.assertEqual(response._headers['content-type'][1], 'application/zip')
        # Verify the number of resources in the zip is correct
        self.assertEqual(len(zip_file.namelist()), 13)

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))
Пример #6
0
    def test_success_download_private_project(self):
        """
        Return a 200 along with a zip file of the private project requested.
        """
        resource_id = '83375'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('job_status',
                      kwargs={
                          'action': 'download',
                          'response_format': 'zip'
                      })
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))
        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename={}_download_{}.zip'.format(
                self.target_name, resource_id))
        # Verify content type
        self.assertEqual(response._headers['content-type'][1],
                         'application/zip')
        # Verify the number of resources in the zip is correct
        self.assertEqual(len(zip_file.namelist()), 15)

        # Verify the fixity file is empty as there was nothing to check.
        with zip_file.open('figshare_download_{}/fixity_info.json'.format(
                resource_id)) as fixityfile:
            zip_json = json.load(fixityfile)
            self.assertEqual(len(zip_json), 1)

        with zip_file.open(
                'figshare_download_{}/PRESQT_FTS_METADATA.json'.format(
                    resource_id)) as metadatafile:
            metadata = json.load(metadatafile)
        # Make sure the results of extra are what we expect
        self.assertEqual(metadata['extra_metadata']['description'],
                         "This is actually just eggs.")
        self.assertEqual(metadata['extra_metadata']['title'], 'Hello World')
        self.assertEqual(metadata['extra_metadata']['creators'],
                         [{
                             'first_name': 'Prometheus',
                             'last_name': 'Test',
                             'ORCID': None
                         }])

        file_path = "{}_download_{}/data/Hello World/Ecoute/ecoute.png".format(
            self.target_name, resource_id)
        # Verify that the folder exists
        self.assertIn(file_path, zip_file.namelist())

        count_of_file_references = zip_file.namelist().count(file_path)
        self.assertEqual(count_of_file_references, 1)

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(hash_tokens(self.token)))
Пример #7
0
    def test_success_download_private_repo(self):
        """
        Return a 200 along with a zip file of the private project requested.
        """
        resource_id = '17990894'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('job_status',
                      kwargs={
                          'action': 'download',
                          'response_format': 'zip'
                      })
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))
        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename={}_download_{}.zip'.format(
                self.target_name, resource_id))
        # Verify content type
        self.assertEqual(response._headers['content-type'][1],
                         'application/zip')
        # Verify the number of resources in the zip is correct
        self.assertEqual(len(zip_file.namelist()), 14)

        # Verify the fixity file is empty as there was nothing to check.
        with zip_file.open('gitlab_download_{}/fixity_info.json'.format(
                resource_id)) as fixityfile:
            zip_json = json.load(fixityfile)
            self.assertEqual(len(zip_json), 2)

        with zip_file.open(
                'gitlab_download_{}/PRESQT_FTS_METADATA.json'.format(
                    resource_id)) as metadatafile:
            metadata = json.load(metadatafile)
            self.assertEqual(metadata['extra_metadata']['description'],
                             "Welcome to the show, kid.")
            self.assertEqual(metadata['extra_metadata']['title'],
                             'Test Project')

        file_path = "{}_download_{}/data/Test Project/README.md".format(
            self.target_name, resource_id)
        # Verify that the folder exists
        self.assertIn(file_path, zip_file.namelist())

        # # Verify there is only one entry that contains this folder
        count_of_file_references = zip_file.namelist().count(file_path)
        self.assertEqual(count_of_file_references, 1)

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))
Пример #8
0
    def test_success_download_project(self):
        """
        Return a 200 along with a zip file of the private repo requested.
        """
        resource_id = '3525310'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('download_job',
                      kwargs={
                          'ticket_number': self.ticket_number,
                          'response_format': 'zip'
                      })
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))
        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename={}_download_{}.zip'.format(
                self.target_name, resource_id))
        # Verify content type
        self.assertEqual(response._headers['content-type'][1],
                         'application/zip')
        # Verify the number of resources in the zip is correct
        self.assertEqual(len(zip_file.namelist()), 14)

        # Verify the fixity file has the two file entries
        with zip_file.open('zenodo_download_{}/data/fixity_info.json'.format(
                resource_id)) as fixityfile:
            zip_json = json.load(fixityfile)
            self.assertEqual(len(zip_json), 2)

        file_path_one = "{}_download_{}/data/Test PresQT Project/1900s_Cat.jpg".format(
            self.target_name, resource_id)
        file_path_two = "{}_download_{}/data/Test PresQT Project/asdf.png".format(
            self.target_name, resource_id)
        # Verify that the files exists
        self.assertIn(file_path_one, zip_file.namelist())
        self.assertIn(file_path_two, zip_file.namelist())

        # Verify there is only two entry that contains this folder
        count_of_file_references = zip_file.namelist().count(file_path_one)
        self.assertEqual(count_of_file_references, 1)

        count_of_file_references = zip_file.namelist().count(file_path_two)
        self.assertEqual(count_of_file_references, 1)

        # Delete corresponding folder
        shutil.rmtree('mediafiles/downloads/{}'.format(self.ticket_number))
Пример #9
0
    def test_error_400_bad_action(self):
        """
        Return a 400 if the 'action' query parameter is bad
        """
        shared_call_get_resource_zip(self, self.resource_id)

        header = {}
        url = reverse('job_status', kwargs={'action': 'bad_action'})
        response = self.client.get(url, **header)

        # Verify the status code and content
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data['error'],
                         "PresQT Error: 'bad_action' is not a valid acton.")
Пример #10
0
    def test_success_download_public_project(self):
        """
        Return a 200 along with a zip file of the private repo requested.
        """
        resource_id = '2441380'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('job_status', kwargs={'action': 'download',
                                            'response_format': 'zip'})
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))
        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename={}_download_{}.zip'.format(self.target_name, resource_id))
        # Verify content type
        self.assertEqual(response._headers['content-type'][1], 'application/zip')
        # Verify the number of resources in the zip is correct
        self.assertEqual(len(zip_file.namelist()), 14)

        # Verify the fixity file has the one file entry
        with zip_file.open('zenodo_download_{}/fixity_info.json'.format(resource_id)) as fixityfile:
            zip_json = json.load(fixityfile)
            self.assertEqual(len(zip_json), 1)

        with zip_file.open('zenodo_download_{}/PRESQT_FTS_METADATA.json'.format(resource_id)) as metadatafile:
            extra_metadata = json.load(metadatafile)['extra_metadata']
            self.assertEqual(extra_metadata['title'], "A Curious Egg")
            self.assertEqual(extra_metadata['description'], 'n/a')
            self.assertEqual(extra_metadata['creators'],
                             [{'first_name': 'Barnard,', 'last_name': 'E. E.', 'ORCID': None}])
            self.assertEqual(extra_metadata['license'], 'CC0-1.0')
            self.assertEqual(extra_metadata['related_identifiers'],
                             [{'type':'doi', 'id':'10.1126/science.os-2.49.256'}])
            self.assertEqual(extra_metadata['publication_date'], '1881-05-28')

        file_path = "{}_download_{}/data/A Curious Egg/article.pdf".format(
            self.target_name, resource_id)
        # Verify that the files exists
        self.assertIn(file_path, zip_file.namelist())

        # Verify there is only two entry that contains this folder
        count_of_file_references = zip_file.namelist().count(file_path)
        self.assertEqual(count_of_file_references, 1)

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))
Пример #11
0
    def test_success_file_bad_project(self):
        """
        Return a 200 along with a zip file of the file requested.
        """
        resource_id = '9328472398472398472398423798432984723984723984:album_uploader%2F__pycache__%2F__init__%2Ecpython-36%2Epyc'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('job_status', kwargs={'action': 'download',
                                            'response_format': 'zip'})
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 500)

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))
Пример #12
0
    def test_success_file_bad_file_path(self):
        """
        Return a 200 along with a zip file of the file requested.
        """
        resource_id = '209373160:bad%2Ffile%2Fpath%2E.py'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('job_status', kwargs={'action': 'download',
                                            'response_format': 'zip'})
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 500)

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))
Пример #13
0
    def test_success_200_zip(self):
        """
        Return a 200 along with a zip file of the resource requested.
        """
        shared_call_get_resource_zip(self, self.resource_id)

        url = reverse('job_status', kwargs={'action': 'download', 'response_format': 'zip'})
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))
        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename=osf_download_{}.zip'.format(self.resource_id))
        # Verify content type
        self.assertEqual(response._headers['content-type'][1], 'application/zip')
        # Verify the number of resources in the zip is correct
        self.assertEqual(len(zip_file.namelist()), 13)

        # Verify the custom hash_file information is correct
        with zip_file.open('osf_download_{}/fixity_info.json'.format(self.resource_id)) as fixityfile:
            zip_json = json.load(fixityfile)[0]
            self.assertEqual(zip_json['fixity'], True)
            self.assertEqual(zip_json['fixity_details'],
                             'Source Hash and PresQT Calculated hash matched.')
            self.assertIn(zip_json['hash_algorithm'], ['sha256', 'md5'])
            self.assertEqual(zip_json['presqt_hash'], self.hashes[zip_json['hash_algorithm']])

        # Run the file through the fixity checker again to make sure it downloaded correctly
        with zip_file.open('osf_download_{}/data/22776439564_7edbed7e10_o.jpg'.format(self.resource_id)) as myfile:
            temp_file = myfile.read()
            resource_dict = {
                "file": temp_file,
                "hashes": self.hashes,
                "title": '22776439564_7edbed7e10_o.jpg',
                "path": 'osf_download_{}/data/22776439564_7edbed7e10_o.jpg'.format(self.resource_id),
                "metadata": {}
            }
            fixity, fixity_match = download_fixity_checker(resource_dict)
            self.assertEqual(fixity['fixity'], True)

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))

        # Ensure no email was sent for this request as no email was provided.
        self.assertEqual(len(mail.outbox), 0)
Пример #14
0
    def test_error_404(self):
        """
        Return a 404 if the ticket_number provided is not a valid ticket number.
        """
        shared_call_get_resource_zip(self, self.resource_id)

        url = reverse('download_job', kwargs={'ticket_number': 'bad_ticket'})
        response = self.client.get(url, **self.header)

        # Verify the status code and content
        self.assertEqual(response.status_code, 404)
        self.assertEqual(response.data['error'],
                         "PresQT Error: Invalid ticket number, 'bad_ticket'.")

        # Delete corresponding folder
        shutil.rmtree('mediafiles/downloads/{}'.format(self.ticket_number))
Пример #15
0
    def test_error_500_404_resource_not_found(self):
        """
        Return a 500 if the Resource._download_resource() function running on the server gets a 404 error
        """
        self.resource_id = 'bad_id'
        shared_call_get_resource_zip(self, self.resource_id)

        url = reverse('job_status', kwargs={'action': 'download'})
        response = self.client.get(url, **self.header)

        self.assertEqual(response.status_code, 500)
        self.assertEqual(response.data['message'],
                         "Resource with id 'bad_id' not found for this user.")

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))
Пример #16
0
    def test_error_500_410_gone(self):
        """
        Return a 500 if the Resource._download_resource() function running on the server gets a 410 error
        """
        self.resource_id = '5cd989c5f8214b00188af9b5'
        shared_call_get_resource_zip(self, self.resource_id)

        url = reverse('job_status', kwargs={'action': 'download'})
        response = self.client.get(url, **self.header)

        self.assertEqual(response.status_code, 500)
        self.assertEqual(response.data['message'],
                         "The requested resource is no longer available.")

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))
Пример #17
0
    def test_error_500_403_unauthorized_item_resource(self):
        """
        Return a 500 if the Resource._download_resource() function running on the server gets a 403 error
        """
        self.resource_id = '5cd98c2cf244ec0020e4d9d1'
        shared_call_get_resource_zip(self, self.resource_id)

        url = reverse('job_status', kwargs={'action': 'download'})
        response = self.client.get(url, **self.header)

        self.assertEqual(response.status_code, 500)
        self.assertEqual(response.data['message'],
                         "User does not have access to this resource with the token provided.")

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))
Пример #18
0
    def test_error_500_404_resource_not_found(self):
        """
        Return a 500 if the Resource._download_resource() function running on the server gets a 404 error
        """
        self.resource_id = 'bad_id'
        shared_call_get_resource_zip(self, self.resource_id)

        url = reverse('download_job', kwargs={'ticket_number': self.ticket_number})
        response = self.client.get(url, **self.header)

        self.assertEqual(response.status_code, 500)
        self.assertEqual(response.data,
                         {'message': "Resource not found.",
                          'status_code': 404})

        # Delete corresponding folder
        shutil.rmtree('mediafiles/downloads/{}'.format(self.ticket_number))
Пример #19
0
    def test_error_500_403_unauthorized_item_resource(self):
        """
        Return a 500 if the Resource._download_resource() function running on the server gets a 403 error
        """
        self.resource_id = 'ns064458c6g'
        shared_call_get_resource_zip(self, self.resource_id)

        url = reverse('download_job', kwargs={'ticket_number': self.ticket_number})
        response = self.client.get(url, **self.header)

        self.assertEqual(response.status_code, 500)
        self.assertEqual(response.data,
                         {'message': "User does not have access to this resource with the token provided.",
                          'status_code': 403})

        # Delete corresponding folder
        shutil.rmtree('mediafiles/downloads/{}'.format(self.ticket_number))
Пример #20
0
    def test_error_500_401_token_invalid(self):
        """
        Return a 500 if the Resource._download_resource() method running on the server gets a 401 error
        """
        self.header = {'HTTP_PRESQT_SOURCE_TOKEN': '1234'}
        self.token = '1234'
        shared_call_get_resource_zip(self, self.resource_id)

        url = reverse('job_status', kwargs={'action': 'download'})
        response = self.client.get(url, **self.header)

        self.assertEqual(response.status_code, 500)
        self.assertEqual(response.data['message'],
                         "Token is invalid. Response returned a 401 status code.")

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))
Пример #21
0
    def test_error_400(self):
        """
        Return a 400 if the 'presqt-source-token' is missing in the headers
        """
        shared_call_get_resource_zip(self, self.resource_id)

        header = {}
        url = reverse('job_status', kwargs={'action': 'download'})
        response = self.client.get(url, **header)

        # Verify the status code and content
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data['error'],
                         "PresQT Error: 'presqt-source-token' missing in the request headers.")

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))
Пример #22
0
    def test_success_empty_item(self):
        """
        Return a 200 along with a zip file of the empty item requested.
        """
        resource_id = 'hq37vm4432z'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('download_job',
                      kwargs={'ticket_number': self.ticket_number})
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))
        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename={}_download_{}.zip'.format(
                self.target_name, resource_id))

        # Verify content type
        self.assertEqual(response._headers['content-type'][1],
                         'application/zip')
        # Verify the number of resources in the zip is correct
        self.assertEqual(len(zip_file.namelist()), 13)

        # Verify the fixity file is empty as there was nothing to check.
        with zip_file.open(
                'curate_nd_download_{}/data/fixity_info.json'.format(
                    resource_id)) as fixityfile:
            zip_json = json.load(fixityfile)
            self.assertEqual(len(zip_json), 0)

        empty_folder_path = "{}_download_{}/data/No Files/".format(
            self.target_name, resource_id)
        # Verify that the empty folder exists
        self.assertIn(empty_folder_path, zip_file.namelist())

        # Verify there is only one entry that contains this folder
        count_of_empty_folder_references = zip_file.namelist().count(
            empty_folder_path)
        self.assertEqual(count_of_empty_folder_references, 1)

        # Delete corresponding folder
        shutil.rmtree('mediafiles/downloads/{}'.format(self.ticket_number))
Пример #23
0
    def test_success_download_public_file(self):
        """
        Return a 200 along with a zip file of the public file requested.
        """
        resource_id = '7c2a7648-44ec-4f17-98a1-b1736761d59b'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('download_job',
                      kwargs={
                          'ticket_number': self.ticket_number,
                          'response_format': 'zip'
                      })
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))
        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename={}_download_{}.zip'.format(
                self.target_name, resource_id))
        # Verify content type
        self.assertEqual(response._headers['content-type'][1],
                         'application/zip')
        # Verify the number of resources in the zip is correct
        self.assertEqual(len(zip_file.namelist()), 13)

        # Verify the fixity file has the one file entry
        with zip_file.open('zenodo_download_{}/data/fixity_info.json'.format(
                resource_id)) as fixityfile:
            zip_json = json.load(fixityfile)
            self.assertEqual(len(zip_json), 1)

        file_path = "{}_download_{}/data/article.pdf".format(
            self.target_name, resource_id)
        # Verify that the file exists
        self.assertIn(file_path, zip_file.namelist())

        # Verify there is only one entry that contains this file
        count_of_file_references = zip_file.namelist().count(file_path)
        self.assertEqual(count_of_file_references, 1)

        # Delete corresponding folder
        shutil.rmtree('mediafiles/downloads/{}'.format(self.ticket_number))
Пример #24
0
    def test_success_400_bad_format(self):
        """
        Return a 400 if the given response_format is bad.
        """
        shared_call_get_resource_zip(self, '5cd98510f244ec001fe5632f')

        url = reverse('job_status', kwargs={'action': 'download',
                                            'response_format': 'bad_format'})
        response = self.client.patch(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 400)

        # Verify the status code and data
        self.assertEqual(response.data['error'],
                         'PresQT Error: bad_format is not a valid format for this endpoint.')

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))
Пример #25
0
    def test_success_download_public_file(self):
        """
        Return a 200 along with a zip file of the private article requested.
        """
        resource_id = '82529:12541559:23316914'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('job_status',
                      kwargs={
                          'action': 'download',
                          'response_format': 'zip'
                      })
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))
        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename={}_download_{}.zip'.format(
                self.target_name, resource_id))
        # Verify content type
        self.assertEqual(response._headers['content-type'][1],
                         'application/zip')
        # Verify the number of resources in the zip is correct
        self.assertEqual(len(zip_file.namelist()), 13)

        # Verify the fixity file is empty as there was nothing to check.
        with zip_file.open('figshare_download_{}/fixity_info.json'.format(
                resource_id)) as fixityfile:
            zip_json = json.load(fixityfile)
            self.assertEqual(len(zip_json), 1)

        file_path = "{}_download_{}/data/PPFP Choices_Charurat_IRB7462_Facility Assessment Tool_V6_28 Feb.doc".format(
            self.target_name, resource_id)
        # Verify that the folder exists
        self.assertIn(file_path, zip_file.namelist())

        count_of_file_references = zip_file.namelist().count(file_path)
        self.assertEqual(count_of_file_references, 1)

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(hash_tokens(self.token)))
Пример #26
0
    def test_success_download_public_project(self):
        """
        Return a 200 along with a zip file of the public project requested.
        """
        resource_id = '82718'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('job_status',
                      kwargs={
                          'action': 'download',
                          'response_format': 'zip'
                      })
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))
        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename={}_download_{}.zip'.format(
                self.target_name, resource_id))
        # Verify content type
        self.assertEqual(response._headers['content-type'][1],
                         'application/zip')
        # Verify the number of resources in the zip is correct
        self.assertEqual(len(zip_file.namelist()), 72)

        # Verify the fixity file is empty as there was nothing to check.
        with zip_file.open('figshare_download_{}/fixity_info.json'.format(
                resource_id)) as fixityfile:
            zip_json = json.load(fixityfile)
            self.assertEqual(len(zip_json), 57)

        file_path = "{}_download_{}/data/ASFV alignment/Multiple sequence alignment for identification of divergent selection of MGF505/MGF360/MGF505-2R-4R.aln.fasta".format(
            self.target_name, resource_id)
        # Verify that the folder exists
        self.assertIn(file_path, zip_file.namelist())

        count_of_file_references = zip_file.namelist().count(file_path)
        self.assertEqual(count_of_file_references, 1)

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(hash_tokens(self.token)))
Пример #27
0
    def test_success_200_json(self):
        """
        Return a 200 along with a zip file of the resource requested.
        """
        self.header['HTTP_PRESQT_EMAIL_OPT_IN'] = '*****@*****.**'
        shared_call_get_resource_zip(self, self.resource_id)

        url = reverse('job_status', kwargs={'action': 'download', 'response_format': 'json'})
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        # Verify the status code and data
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['message'], 'Download successful.')
        self.assertEqual(response.data['failed_fixity'], [])

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))
Пример #28
0
    def test_error_400(self):
        """
        Return a 400 if the 'presqt-source-token' is missing in the headers
        """
        shared_call_get_resource_zip(self, '5cd98510f244ec001fe5632f')

        header = {}
        url = reverse('download_job',
                      kwargs={'ticket_number': self.ticket_number})
        response = self.client.patch(url, **header)

        # Verify the status code and content
        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.data['error'],
            "PresQT Error: 'presqt-source-token' missing in the request headers."
        )

        # Delete corresponding folder
        shutil.rmtree('mediafiles/downloads/{}'.format(self.ticket_number))
Пример #29
0
    def test_error_401(self):
        """
        Return a 401 if the 'presqt-source-token' provided in the header does not match
        the 'presqt-source-token' in the process_info file.
        """
        shared_call_get_resource_zip(self, self.resource_id)

        url = reverse('download_job',
                      kwargs={'ticket_number': self.ticket_number})
        headers = {'HTTP_PRESQT_SOURCE_TOKEN': '1234'}
        response = self.client.get(url, **headers)

        # Verify the status code and content
        self.assertEqual(response.status_code, 401)
        self.assertEqual(
            response.data['error'],
            "PresQT Error: Header 'presqt-source-token' does not match the 'presqt-source-token' "
            "for this server process.")

        # Delete corresponding folder
        shutil.rmtree('mediafiles/downloads/{}'.format(self.ticket_number))
Пример #30
0
    def test_success_download_single_file(self):
        """
        Return a 200 along with a zip file of the single file requested.
        """
        resource_id = '17993268:README%2Emd'
        shared_call_get_resource_zip(self, resource_id)

        url = reverse('job_status',
                      kwargs={
                          'action': 'download',
                          'response_format': 'zip'
                      })
        response = self.client.get(url, **self.header)
        # Verify the status code
        self.assertEqual(response.status_code, 200)

        zip_file = zipfile.ZipFile(io.BytesIO(response.content))
        # Verify the name of the zip file
        self.assertEquals(
            response._headers['content-disposition'][1],
            'attachment; filename={}_download_{}.zip'.format(
                self.target_name, resource_id))
        # Verify content type
        self.assertEqual(response._headers['content-type'][1],
                         'application/zip')
        # Verify the number of resources in the zip is correct
        # self.assertEqual(len(zip_file.namelist()), 13)

        # Verify the fixity file is empty as there was nothing to check.
        with zip_file.open('gitlab_download_{}/fixity_info.json'.format(
                resource_id)) as fixityfile:
            zip_json = json.load(fixityfile)
            self.assertEqual(len(zip_json), 1)

        file_path = 'gitlab_download_17993268:README%2Emd/data/README.md'
        # # Verify that the file exists
        self.assertIn(file_path, zip_file.namelist())

        # Delete corresponding folder
        shutil.rmtree('mediafiles/jobs/{}'.format(self.ticket_number))