Ejemplo n.º 1
0
    def test_upload_to_existing_project_valid_metadata_error(self):
        """
        Test that an error is raised if there's an issue updating a valid metadata file.
        """
        # Mock a server error for when a put request is made.
        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)

        sleep(30)
        shared_upload_function_gitlab(self)

        # Verify the new repo exists on the PresQT Resource Collection endpoint.
        url = reverse('resource_collection', kwargs={'target_name': 'gitlab'})
        project_id = self.client.get(
            url, **{'HTTP_PRESQT_SOURCE_TOKEN': GITLAB_UPLOAD_TEST_USER_TOKEN}).json()['resources'][0]['id']
        shutil.rmtree(self.ticket_path)

        # Now I'll make an explicit call to our metadata function with a mocked server error and ensure
        # it is raising an exception.
        with patch('requests.put') as mock_request:
            mock_request.return_value = mock_req
            # Attempt to update the metadata, but the server is down!
            self.assertRaises(PresQTError, gitlab_upload_metadata, self.token, project_id,
                              {"context": {}, "allKeywords": [], "actions": []})

        # Delete upload folder and project
        delete_gitlab_project(project_id, GITLAB_UPLOAD_TEST_USER_TOKEN)
Ejemplo n.º 2
0
    def test_success_202_empty_folder(self):
        """
        If an empty directory is included in the uploaded project, we want to ensure the user is
        made aware.
        """
        # 202 when uploading a new top level repo
        sleep(30)
        shared_upload_function_gitlab(self)

        # Verify the new repo exists on the PresQT Resource Collection endpoint.
        url = reverse('resource_collection', kwargs={'target_name': 'gitlab'})
        project_id = self.client.get(
            url, **{'HTTP_PRESQT_SOURCE_TOKEN': GITLAB_UPLOAD_TEST_USER_TOKEN}).json()['resources'][0]['id']
        shutil.rmtree(self.ticket_path)

        self.file = 'presqt/api_v1/tests/resources/upload/Empty_Directory_Bag.zip'
        self.resources_ignored = ['/Egg/Empty_Folder']
        self.resource_id = project_id
        self.url = reverse('resource', kwargs={
                           'target_name': 'gitlab', 'resource_id': self.resource_id})
        sleep(30)
        shared_upload_function_gitlab(self)

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

        # Delete upload folder and project
        delete_gitlab_project(project_id, GITLAB_UPLOAD_TEST_USER_TOKEN)
Ejemplo n.º 3
0
    def test_success_202_upload_extra_metadata(self):
        """
        Return a 202 when a file is uploading.
        """
        self.file = 'presqt/api_v1/tests/resources/upload/Upload_Extra_Metadata.zip'
        # 202 when uploading a new top level repo
        shared_upload_function_gitlab(self)

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

        # Check that the description was added to the project
        project_info = requests.get(
            'https://gitlab.com/api/v4/projects/{}'.format(
                response_json['resources'][0]['id'])).json()
        self.assertEqual(project_info['description'],
                         "There's so many eggs in here.")

        # Delete upload folder and project
        shutil.rmtree(self.ticket_path)
        delete_gitlab_project(response_json['resources'][0]['id'],
                              GITLAB_UPLOAD_TEST_USER_TOKEN)
Ejemplo n.º 4
0
    def test_failed_upload_to_existing_project(self):
        # Mock a server error for when a put request is made.
        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)

        # 202 when uploading a new top level repo
        sleep(30)
        shared_upload_function_gitlab(self)

        # Verify the new repo exists on the PresQT Resource Collection endpoint.
        url = reverse('resource_collection', kwargs={'target_name': 'gitlab'})
        project_id = self.client.get(
            url, **{'HTTP_PRESQT_SOURCE_TOKEN': GITLAB_UPLOAD_TEST_USER_TOKEN}).json()['resources'][0]['id']
        self.resource_id = project_id
        shutil.rmtree(self.ticket_path)

        # Now I'll make an explicit call to our metadata function with a mocked server error and ensure
        # it is raising an exception.
        with patch('requests.post') as mock_request:
            mock_request.return_value = mock_req

            # Upload to the newly created project
            self.url = reverse('resource', kwargs={
                               'target_name': 'gitlab', 'resource_id': project_id})
            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 = hash_tokens(self.token)
            self.ticket_path = 'mediafiles/uploads/{}'.format(ticket_number)

            # Verify status code and message
            self.assertEqual(response.status_code, 202)
            self.assertEqual(
                response.data['message'], 'The server is processing the request.')

            # Verify process_info file status is 'in_progress' initially
            process_info = read_file('{}/process_info.json'.format(self.ticket_path), True)
            self.assertEqual(process_info['status'], 'in_progress')

            # Wait until the spawned off process finishes in the background to do further validation
            process_wait(process_info, self.ticket_path)

            # Verify process_info.json file data
            process_info = read_file('{}/process_info.json'.format(self.ticket_path), True)

            self.assertEqual(process_info['status'], 'failed')
            self.assertEqual(process_info['message'], "Upload failed with a status code of 500")
            self.assertEqual(process_info['status_code'], 400)

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

            # Delete GitLab Project
            delete_gitlab_project(project_id, GITLAB_UPLOAD_TEST_USER_TOKEN)
Ejemplo n.º 5
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_gitlab(self)
        url = reverse('resource_collection', kwargs={'target_name': 'gitlab'})
        response_json = self.client.get(
            url, **{
                'HTTP_PRESQT_SOURCE_TOKEN': GITLAB_UPLOAD_TEST_USER_TOKEN
            }).json()

        # On the project that was just created, we need to get the contents of the metadata file.
        metadata_link = 'https://gitlab.com/api/v4/projects/{}/repository/files/PRESQT_FTS_METADATA%2Ejson?ref=master'.format(
            response_json['resources'][0]['id'])

        # Get the metadata json
        response = requests.get(
            metadata_link, headers={"Private-Token": "{}".format(self.token)})
        metadata_file = json.loads(base64.b64decode(
            response.json()['content']))

        # Action metadata
        self.assertEqual(metadata_file['actions'][0]['actionType'],
                         'resource_upload')
        self.assertEqual(metadata_file['actions'][0]['sourceTargetName'],
                         'Local Machine')
        self.assertEqual(metadata_file['actions'][0]['destinationTargetName'],
                         'gitlab')
        self.assertEqual(metadata_file['actions'][0]['destinationUsername'],
                         'Prometheus-Upload')

        # File metadata
        self.assertEqual(
            metadata_file['actions'][0]['files']['created'][0]['title'],
            'Screen Shot 2019-07-15 at 3.26.49 PM.png')
        self.assertEqual(
            metadata_file['actions'][0]['files']['created'][0]['sourcePath'],
            '/NewProject/funnyfunnyimages/Screen Shot 2019-07-15 at 3.26.49 PM.png'
        )
        self.assertEqual(
            metadata_file['actions'][0]['files']['created'][0]
            ['destinationHashes'], {
                'sha256':
                '6d33275234b28d77348e4e1049f58b95a485a7a441684a9eb9175d01c7f141ea'
            })

        # Delete upload folder
        shutil.rmtree(self.ticket_path)
        delete_gitlab_project(response_json['resources'][0]['id'],
                              GITLAB_UPLOAD_TEST_USER_TOKEN)
Ejemplo n.º 6
0
    def test_error_upload_to_file(self):
        """
        Test that we will get an error when attempting to upload to a file.
        """
        sleep(30)
        shared_upload_function_gitlab(self)

        # Verify the new repo exists on the PresQT Resource Collection endpoint.
        url = reverse('resource_collection', kwargs={'target_name': 'gitlab'})
        project_id = self.client.get(
            url, **{'HTTP_PRESQT_SOURCE_TOKEN': GITLAB_UPLOAD_TEST_USER_TOKEN}).json()['resources'][0]['id']
        shutil.rmtree(self.ticket_path)

        # Upload to existing repo
        self.resource_id = '{}:funnyfunnyimages%2FScreen Shot 2019-07-15 at 3%2E26%2E49 PM%2Epng'.format(
            project_id)
        self.url = reverse('resource', kwargs={
                           'target_name': 'gitlab', 'resource_id': self.resource_id})
        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 = hash_tokens(self.token)
        self.ticket_path = 'mediafiles/uploads/{}'.format(ticket_number)

        # Verify status code and message
        self.assertEqual(response.status_code, 202)
        self.assertEqual(
            response.data['message'], 'The server is processing the request.')

        # Verify process_info file status is 'in_progress' initially
        process_info = read_file('{}/process_info.json'.format(self.ticket_path), True)
        self.assertEqual(process_info['status'], 'in_progress')

        # Wait until the spawned off process finishes in the background to do further validation
        process_wait(process_info, self.ticket_path)

        # Verify process_info.json file data
        process_info = read_file('{}/process_info.json'.format(self.ticket_path), True)
        self.assertEqual(process_info['status'], 'failed')
        self.assertEqual(
            process_info['message'], 'Resource with id, {}, belongs to a file.'.format(self.resource_id))
        self.assertEqual(process_info['status_code'], 400)

        shutil.rmtree(self.ticket_path)

        # Delete upload folder and project
        delete_gitlab_project(project_id, GITLAB_UPLOAD_TEST_USER_TOKEN)
Ejemplo n.º 7
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_gitlab(self)

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

        # Delete upload folder and project
        shutil.rmtree(self.ticket_path)
        delete_gitlab_project(response_json['resources'][0]['id'],
                              GITLAB_UPLOAD_TEST_USER_TOKEN)
Ejemplo n.º 8
0
    def test_upload_to_existing_project_invalid_metadata_error(self):
        """
        Test that an error is raised if there's an issue updating an invalid metadata file.
        """
        # Mock a server error for when a post request is made.
        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)

        sleep(30)
        shared_upload_function_gitlab(self)

        # Verify the new repo exists on the PresQT Resource Collection endpoint.
        url = reverse('resource_collection', kwargs={'target_name': 'gitlab'})
        project_id = self.client.get(
            url, **{'HTTP_PRESQT_SOURCE_TOKEN': GITLAB_UPLOAD_TEST_USER_TOKEN}).json()['resources'][0]['id']
        shutil.rmtree(self.ticket_path)

        put_url = "https://gitlab.com/api/v4/projects/{}/repository/files/PRESQT_FTS_METADATA%2Ejson".format(
            project_id)

        # Update metadata to be invalid for testing purposes.
        metadata_dict = json.dumps({"bad_metadata": "metadata"}, indent=4).encode('utf-8')
        encoded_file = base64.b64encode(metadata_dict)

        data = {"branch": "master",
                "commit_message": "Made it bad...",
                "encoding": "base64",
                "content": encoded_file}

        requests.put(put_url, headers={'Private-Token': GITLAB_UPLOAD_TEST_USER_TOKEN}, data=data)

        # Now I'll make an explicit call to our metadata function with a mocked server error and ensure
        # it is raising an exception.
        with patch('requests.post') as mock_request:
            mock_request.return_value = mock_req
            # Attempt to update the metadata, but the server is down!
            self.assertRaises(PresQTError, gitlab_upload_metadata, self.token, project_id,
                              {"context": {}, "actions": []})

        # Delete upload folder and project
        delete_gitlab_project(project_id, GITLAB_UPLOAD_TEST_USER_TOKEN)
Ejemplo n.º 9
0
    def test_try_upload_same_duplicate(self):
        sleep(30)
        shared_upload_function_gitlab(self)

        # Verify the new repo exists on the PresQT Resource Collection endpoint.
        url = reverse('resource_collection', kwargs={'target_name': 'gitlab'})
        project_id = self.client.get(
            url, **{'HTTP_PRESQT_SOURCE_TOKEN': GITLAB_UPLOAD_TEST_USER_TOKEN}).json()['resources'][0]['id']
        self.resource_id = project_id
        shutil.rmtree(self.ticket_path)

        self.url = reverse('resource', kwargs={
                           'target_name': 'gitlab', 'resource_id': self.resource_id})
        sleep(30)
        shared_upload_function_gitlab(self)

        shutil.rmtree(self.ticket_path)

        self.duplicate_action = 'update'
        self.url = reverse('resource', kwargs={
                           'target_name': 'gitlab', 'resource_id': self.resource_id})
        self.resources_ignored = [
            "/NewProject/funnyfunnyimages/Screen Shot 2019-07-15 at 3.26.49 PM.png"]
        sleep(30)
        shared_upload_function_gitlab(self)
        shutil.rmtree(self.ticket_path)

        # Delete upload folder and project
        delete_gitlab_project(project_id, GITLAB_UPLOAD_TEST_USER_TOKEN)
Ejemplo n.º 10
0
    def test_duplicate_update(self):
        self.file = 'presqt/api_v1/tests/resources/upload/ProjectSingleFileToUpload.zip'
        sleep(30)
        shared_upload_function_gitlab(self)

        # Verify the new repo exists on the PresQT Resource Collection endpoint.
        url = reverse('resource_collection', kwargs={'target_name': 'gitlab'})
        project_id = self.client.get(
            url, **{'HTTP_PRESQT_SOURCE_TOKEN': GITLAB_UPLOAD_TEST_USER_TOKEN}).json()['resources'][0]['id']
        shutil.rmtree(self.ticket_path)

        self.file = 'presqt/api_v1/tests/resources/upload/ProjectSingleDuplicateFileToUpload.zip'
        self.resource_id = project_id
        self.duplicate_action = 'update'
        self.resources_updated = ['/Screen Shot 2019-07-15 at 3.51.13 PM.png']
        self.url = reverse('resource', kwargs={
                           'target_name': 'gitlab', 'resource_id': self.resource_id})
        sleep(30)
        shared_upload_function_gitlab(self)
        shutil.rmtree(self.ticket_path)

        # Delete upload folder and project
        delete_gitlab_project(project_id, GITLAB_UPLOAD_TEST_USER_TOKEN)
Ejemplo n.º 11
0
    def test_upload_to_project_with_invalid_metadata(self):
        """
        If a project has invalid metadata, we need to change the name and create a new metadata file.
        """
        sleep(30)
        shared_upload_function_gitlab(self)

        # Verify the new repo exists on the PresQT Resource Collection endpoint.
        url = reverse('resource_collection', kwargs={'target_name': 'gitlab'})
        project_id = self.client.get(
            url, **{'HTTP_PRESQT_SOURCE_TOKEN': GITLAB_UPLOAD_TEST_USER_TOKEN}).json()['resources'][0]['id']
        shutil.rmtree(self.ticket_path)

        put_url = "https://gitlab.com/api/v4/projects/{}/repository/files/PRESQT_FTS_METADATA%2Ejson".format(
            project_id)

        # Update metadata to be invalid for testing purposes.
        metadata_dict = json.dumps({"bad_metadata": "metadata"}, indent=4).encode('utf-8')
        encoded_file = base64.b64encode(metadata_dict)

        data = {"branch": "master",
                "commit_message": "Made it bad...",
                "encoding": "base64",
                "content": encoded_file}

        requests.put(put_url, headers={'Private-Token': GITLAB_UPLOAD_TEST_USER_TOKEN}, data=data)

        self.resource_id = project_id
        self.url = reverse('resource', kwargs={
                           'target_name': 'gitlab', 'resource_id': self.resource_id})
        sleep(30)
        shared_upload_function_gitlab(self)

        shutil.rmtree(self.ticket_path)
        # Delete upload folder and project
        delete_gitlab_project(project_id, GITLAB_UPLOAD_TEST_USER_TOKEN)
Ejemplo n.º 12
0
    def test_success_upload_to_existing_containers(self):
        """
        Test that we can successfully upload to existing containers in Gitlab.
        """
        sleep(30)
        shared_upload_function_gitlab(self)

        # Verify the new repo exists on the PresQT Resource Collection endpoint.
        url = reverse('resource_collection', kwargs={'target_name': 'gitlab'})
        project_id = self.client.get(
            url, **{'HTTP_PRESQT_SOURCE_TOKEN': GITLAB_UPLOAD_TEST_USER_TOKEN}).json()['resources'][0]['id']
        shutil.rmtree(self.ticket_path)

        # Upload to existing repo
        self.resource_id = project_id
        self.url = reverse('resource', kwargs={'target_name': 'gitlab', 'resource_id': project_id})
        sleep(30)
        shared_upload_function_gitlab(self)
        shutil.rmtree(self.ticket_path)

        # Attempt to upload a duplicate resource
        self.resource_id = project_id
        self.resources_ignored = [
            '/NewProject/funnyfunnyimages/Screen Shot 2019-07-15 at 3.26.49 PM.png']
        self.url = reverse('resource', kwargs={'target_name': 'gitlab', 'resource_id': project_id})
        sleep(30)
        shared_upload_function_gitlab(self)
        shutil.rmtree(self.ticket_path)

        # Upload to existing folder
        self.resource_id = '{}:funnyfunnyimages'.format(project_id)
        self.success_message = "Upload successful."
        self.resources_ignored = []
        self.url = reverse('resource', kwargs={
                           'target_name': 'gitlab', 'resource_id': self.resource_id})
        sleep(30)
        shared_upload_function_gitlab(self)
        shutil.rmtree(self.ticket_path)

        # Delete upload folder and project
        delete_gitlab_project(project_id, GITLAB_UPLOAD_TEST_USER_TOKEN)