Exemple #1
0
 def test_dropbox_folder_list_returns_error_if_invalid_path(
         self, mock_metadata):
     mock_error = mock.Mock()
     mock_metadata.side_effect = ApiError('', mock_error, '', '')
     url = self.project.api_url_for('dropbox_folder_list',
                                    folder_id='/fake_path')
     with mock.patch.object(type(self.node_settings), 'has_auth', True):
         res = self.app.get(url, auth=self.user.auth, expect_errors=True)
     assert res.status_code == http.BAD_REQUEST
Exemple #2
0
 def test_dropbox_file_uploads_new_file_if_there_is_no_in_dropbox(
     self,
     files_upload_mock,
     files_get_metadata_mock,
     getsize_mock,
 ):
     files_get_metadata_mock.side_effect = ApiError(123, 'not_found',
                                                    'qwerty', '234')
     getsize_mock.return_value = 101
     dropbox_file("filename")
     self.assertTrue(files_upload_mock.assert_called)
Exemple #3
0
    def test_caught_error_on_files_delete(self, dropbox_constructor_mock):
        """
        Test that path lookup ApiErrors raised by Dropbox are caught

        :param MagicMock dropbox_constructor_mock:
                                                mock for dropbox constructor
        """
        dropbox_mock = Mock()
        dropbox_constructor_mock.return_value = dropbox_mock
        dropbox_mock.files_delete_v2.side_effect = ApiError(
            '', DeleteError('path_lookup', LookupError('not_found')),
            'myerrormessage', '')

        dropbox_uploader = DropboxUploader(None)
        dropbox_uploader.initialize()
        dropbox_uploader.upload('mybinarydata', 'myphoto.png')
        dropbox_mock.files_upload.assert_called_with(
            'mybinarydata', '/photos-picker/photo0.png')
Exemple #4
0
    def test_not_caught_error_on_files_delete(self, dropbox_constructor_mock):
        """
        Test that an ApiError raised by Dropbox client is not caught
        if it's not a path lookup error

        :param MagicMock dropbox_constructor_mock:
                                                mock for dropbox constructor
        """
        dropbox_mock = Mock()
        dropbox_constructor_mock.return_value = dropbox_mock
        dropbox_mock.files_delete_v2.side_effect = ApiError(
            '', DeleteError('other'), 'myerrormessage', '')

        with self.assertRaises(ApiError) as cm:
            dropbox_uploader = DropboxUploader(None)
            dropbox_uploader.initialize()

        self.assertEqual("myerrormessage", cm.exception.user_message_text)
Exemple #5
0
    def request(self,
                route,
                namespace,
                request_arg,
                request_binary,
                timeout=None):
        """
        Makes a request to the Dropbox API and in the process validates that
        the route argument and result are the expected data types. The
        request_arg is converted to JSON based on the arg_data_type. Likewise,
        the response is deserialized from JSON and converted to an object based
        on the {result,error}_data_type.

        :param host: The Dropbox API host to connect to.
        :param route: The route to make the request to.
        :type route: :class:`stone.backends.python_rsrc.stone_base.Route`
        :param request_arg: Argument for the route that conforms to the
            validator specified by route.arg_type.
        :param request_binary: String or file pointer representing the binary
            payload. Use None if there is no binary payload.
        :param Optional[float] timeout: Maximum duration in seconds
            that client will wait for any single packet from the
            server. After the timeout the client will give up on
            connection. If `None`, will use default timeout set on
            Dropbox object.  Defaults to `None`.
        :return: The route's result.
        """

        self.check_and_refresh_access_token()

        host = route.attrs['host'] or 'api'
        route_name = namespace + '/' + route.name
        if route.version > 1:
            route_name += '_v{}'.format(route.version)
        route_style = route.attrs['style'] or 'rpc'
        serialized_arg = stone_serializers.json_encode(route.arg_type,
                                                       request_arg)

        if (timeout is None and route == files.list_folder_longpoll):
            # The client normally sends a timeout value to the
            # longpoll route. The server will respond after
            # <timeout> + random(0, 90) seconds. We increase the
            # socket timeout to the longpoll timeout value plus 90
            # seconds so that we don't cut the server response short
            # due to a shorter socket timeout.
            # NB: This is done here because base.py is auto-generated
            timeout = request_arg.timeout + 90

        res = self.request_json_string_with_retry(host,
                                                  route_name,
                                                  route_style,
                                                  serialized_arg,
                                                  request_binary,
                                                  timeout=timeout)
        decoded_obj_result = json.loads(res.obj_result)
        if isinstance(res, RouteResult):
            returned_data_type = route.result_type
            obj = decoded_obj_result
        elif isinstance(res, RouteErrorResult):
            returned_data_type = route.error_type
            obj = decoded_obj_result['error']
            user_message = decoded_obj_result.get('user_message')
            user_message_text = user_message and user_message.get('text')
            user_message_locale = user_message and user_message.get('locale')
        else:
            raise AssertionError('Expected RouteResult or RouteErrorResult, '
                                 'but res is %s' % type(res))

        deserialized_result = stone_serializers.json_compat_obj_decode(
            returned_data_type, obj, strict=False)

        if isinstance(res, RouteErrorResult):
            raise ApiError(res.request_id, deserialized_result,
                           user_message_text, user_message_locale)
        elif route_style == self._ROUTE_STYLE_DOWNLOAD:
            return (deserialized_result, res.http_resp)
        else:
            return deserialized_result
 def mocked_session_append_error(
     self: Any, f: bytes, session_id: str, offset: int
 ) -> ApiError:
     raise ApiError("", "", "", "")
 def mocked_files_upload(self: Any, f: bytes, remote_path: str) -> ApiError:
     raise ApiError("", "", "", "")
Exemple #8
0
def launch_api_error(monkeypatch: Any, exception: Exception) -> None:
    def mock_api_exception(*args: List, **kwargs: Dict) -> None:
        raise exception

    monkeypatch.setattr(Dropbox, "files_list_folder", mock_api_exception)
    monkeypatch.setattr(Dropbox, "files_download_to_file", mock_api_exception)
    monkeypatch.setattr(dropbox, "Dropbox", mock_api_exception)"""


@pytest.mark.parametrize(
    "exception, job_status",
    [
        (
            ApiError(
                request_id="id",
                user_message_text="",
                user_message_locale="",
                error=None,
            ),
            JobStatus.IMPORTING_ERROR_DATA_UNREACHABLE,
        ),
        (
            AuthError(request_id="id", error=None),
            JobStatus.IMPORTING_ERROR_AUTHORIZATION_REQUIRED,
        ),
    ],
)
def test_dropbox_importer_api_error(
    monkeypatch: Any,
    db: Session,
    basic_job: dict,
    exception: Exception,
Exemple #9
0
class DropBoxTest(TestCase):
    def setUp(self, *args):
        self.storage = dropbox.DropBoxStorage('foo')

    def test_no_access_token(self, *args):
        with self.assertRaises(ImproperlyConfigured):
            dropbox.DropBoxStorage(None)

    @mock.patch('dropbox.Dropbox.files_delete', return_value=FILE_METADATA)
    def test_delete(self, *args):
        self.storage.delete('foo')

    @mock.patch('dropbox.Dropbox.files_get_metadata',
                return_value=FILE_METADATA)
    def test_exists(self, *args):
        exists = self.storage.exists('foo')
        self.assertTrue(exists)

    @mock.patch('dropbox.Dropbox.files_get_metadata',
                side_effect=ApiError(None, None, None, None))
    def test_not_exists(self, *args):
        exists = self.storage.exists('bar')
        self.assertFalse(exists)

    @mock.patch('dropbox.Dropbox.files_list_folder',
                return_value=LIST_FOLDER_RESULT)
    def test_listdir(self, *args):
        dirs, files = self.storage.listdir('/')
        self.assertGreater(len(dirs), 0)
        self.assertGreater(len(files), 0)
        self.assertEqual(dirs[0], 'bar')
        self.assertEqual(files[0], 'foo.txt')

    @mock.patch('dropbox.Dropbox.files_get_metadata',
                return_value=FILE_METADATA)
    def test_size(self, *args):
        size = self.storage.size('foo')
        self.assertEqual(size, FILE_METADATA.size)

    @mock.patch('dropbox.Dropbox.files_get_metadata',
                return_value=FILE_METADATA)
    def test_modified_time(self, *args):
        mtime = self.storage.modified_time('foo')
        self.assertEqual(mtime, FILE_DATE)

    @mock.patch('dropbox.Dropbox.files_get_metadata',
                return_value=FILE_METADATA)
    def test_accessed_time(self, *args):
        mtime = self.storage.accessed_time('foo')
        self.assertEqual(mtime, FILE_DATE)

    def test_open(self, *args):
        obj = self.storage._open('foo')
        self.assertIsInstance(obj, File)

    @mock.patch('dropbox.Dropbox.files_upload', return_value=FILE_METADATA)
    def test_save(self, files_upload, *args):
        self.storage._save('foo', File(BytesIO(b'bar'), 'foo'))
        self.assertTrue(files_upload.called)

    @mock.patch('dropbox.Dropbox.files_upload')
    @mock.patch('dropbox.Dropbox.files_upload_session_finish')
    @mock.patch('dropbox.Dropbox.files_upload_session_append_v2')
    @mock.patch('dropbox.Dropbox.files_upload_session_start',
                return_value=mock.MagicMock(session_id='foo'))
    def test_chunked_upload(self, start, append, finish, upload):
        large_file = File(BytesIO(b'bar' * self.storage.CHUNK_SIZE), 'foo')
        self.storage._save('foo', large_file)
        self.assertTrue(start.called)
        self.assertTrue(append.called)
        self.assertTrue(finish.called)
        self.assertFalse(upload.called)

    @mock.patch('dropbox.Dropbox.files_get_temporary_link',
                return_value=FILE_MEDIA_FIXTURE)
    def test_url(self, *args):
        url = self.storage.url('foo')
        self.assertEqual(url, FILE_MEDIA_FIXTURE.link)

    def test_formats(self, *args):
        self.storage = dropbox.DropBoxStorage('foo')
        files = self.storage._full_path('')
        self.assertEqual(files, self.storage._full_path('/'))
        self.assertEqual(files, self.storage._full_path('.'))