示例#1
0
    def test_upload_large_file(self):
        self.drive.config = drive_config.DriveConfig({'max_put_size_bytes': 2})
        session_url = 'https://foo/bar/accept_data'
        input = io.BytesIO(b'12345')
        output = io.BytesIO()
        expected_ranges = ['0-1/5', '2-3/5', '4-4/5']
        response_dict = {
            'uploadUrl': session_url,
            'expirationDateTime': '2020-01-01T00:00:00.0Z',
            'nextExpectedRanges': ['0-']
        }
        with requests_mock.Mocker() as mock:
            def create_session(request, context):
                body = request.json()['item']
                self.assertEqual('test', body['name'])
                self.assertEqual(options.NameConflictBehavior.RENAME, body['@name.conflictBehavior'])
                context.status_code = codes.ok
                return response_dict

            def accept_data(request, context):
                self.assertEqual(expected_ranges.pop(0), request.headers['Content-Range'])
                self.assertLessEqual(len(request.body), self.drive.config.max_put_size_bytes)
                output.write(request.body)
                context.status_code = codes.accepted
                return response_dict

            mock.post(self.drive.get_item_uri(item_id='123', item_path=None) + ':/test:/upload.createSession',
                      json=create_session)
            mock.put(session_url, json=accept_data)
            self.drive.upload_file('test', data=input, size=5, parent_id='123',
                                   conflict_behavior=options.NameConflictBehavior.RENAME)
            self.assertEqual(input.getvalue(), output.getvalue())
示例#2
0
    def test_download_small_file(self):
        self.drive.config = drive_config.DriveConfig({'max_get_size_bytes': 10})
        data = b'12345'
        output = io.BytesIO()
        with requests_mock.Mocker() as mock:
            def callback(request, context):
                self.assertNotIn('Range', request.headers)
                context.status_code = codes.ok
                return data

            mock.get(self.drive.get_item_uri(item_id='123', item_path=None) + '/content', content=callback)
            self.drive.download_file(file=output, size=len(data), item_id='123')
            self.assertEqual(data, output.getvalue())
示例#3
0
 def test_path_filter(self):
     """
     Test if the path filter object is properly instantiated.
     """
     config = drive_config.DriveConfig({
         'ignore_files': [
             os.path.dirname(sys.modules['tests'].__file__) +
             '/data/ignore_list.txt',
             '/tmp/foo'  # bad path should not crash the program
         ]
     })
     filter = config.path_filter
     self.assertIsInstance(filter, path_filter.PathFilter)
     self.assertTrue(filter.should_ignore('/foo'))
     self.assertTrue(filter.should_ignore('/a.swp'))
示例#4
0
 def test_set_default_config(self):
     """
     Test both setting default config and differential dumping / loading.
     """
     drive_config.DriveConfig.set_default_config(self.conf)
     data = dict(self.data)
     data['ignore_files'] = set(data['ignore_files'])
     data['ignore_files'].add('/q')
     data['proxies'] = {'sock5': '1.2.3.4:5'}
     conf2 = drive_config.DriveConfig(data)
     dump2 = conf2.dump()
     self.assertDictEqual({'ignore_files': ['/q']}, dump2)
     conf3 = drive_config.DriveConfig.load(dump2)
     for k in self.data:
         self.assertEqual(getattr(conf2, k), getattr(conf3, k))
示例#5
0
    def test_download_large_file(self):
        self.drive.config = drive_config.DriveConfig({'max_get_size_bytes': 2})
        in_data = b'12345'
        output = io.BytesIO()
        expected_ranges = ['0-1', '2-3', '4-4']
        with requests_mock.Mocker() as mock:
            def callback(request, context):
                self.assertIn('Range', request.headers)
                r = expected_ranges.pop(0)
                self.assertEqual('bytes=' + r, request.headers['Range'])
                f, t = request.headers['Range'].split('=', 1)[1].split('-')
                context.status_code = codes.partial
                return in_data[int(f): int(t) + 1]

            mock.get(self.drive.get_item_uri(item_id='123', item_path=None) + '/content', content=callback)
            self.drive.download_file(file=output, size=len(in_data), item_id='123')
        self.assertEqual(in_data, output.getvalue())
示例#6
0
    def test_upload_small_file(self):
        self.drive.config = drive_config.DriveConfig({'max_put_size_bytes': 10})
        in_fd = io.BytesIO(b'12345')
        with requests_mock.Mocker() as mock:
            def callback(request, context):
                name = request.path_url.split('/')[-2][:-1]
                self.assertEqual('test', name)
                self.assertEqual(5, len(request.body.getvalue()))
                qs = request.path_url.split('?', 1)[1]
                self.assertEqual('@name.conflictBehavior=' + options.NameConflictBehavior.FAIL, qs)
                data = {'id': 'abc', 'name': name, 'size': 5, 'file': {}}
                context.status_code = codes.created
                return data

            mock.put(self.drive.get_item_uri('123', None) + ':/test:/content', json=callback)
            item = self.drive.upload_file('test', data=in_fd, size=5, parent_id='123',
                                          conflict_behavior=options.NameConflictBehavior.FAIL)
            self.assertIsInstance(item, items.OneDriveItem)
示例#7
0
 def test_serialize(self):
     dump = self.conf.dump()
     new_conf = drive_config.DriveConfig(dump)
     for k, v in self.data.items():
         self.assertEqual(v, getattr(new_conf, k))
示例#8
0
 def test_append_default_values(self):
     del self.data['max_get_size_bytes']
     conf = drive_config.DriveConfig(self.data)
     self.assertEqual(
         drive_config.DriveConfig.DEFAULT_VALUES['max_get_size_bytes'],
         conf.max_get_size_bytes)
示例#9
0
class TestDriveConfig(unittest.TestCase):
    data = get_data('drive_config.json')
    conf = drive_config.DriveConfig(data)

    def test_parse(self):
        assert_factory.assert_dict_equals_attributes(self, self.data,
                                                     self.conf)

    def test_append_default_values(self):
        del self.data['max_get_size_bytes']
        conf = drive_config.DriveConfig(self.data)
        self.assertEqual(
            drive_config.DriveConfig.DEFAULT_VALUES['max_get_size_bytes'],
            conf.max_get_size_bytes)

    def test_serialize(self):
        dump = self.conf.dump()
        new_conf = drive_config.DriveConfig(dump)
        for k, v in self.data.items():
            self.assertEqual(v, getattr(new_conf, k))

    def test_set_default_config(self):
        """
        Test both setting default config and differential dumping / loading.
        """
        drive_config.DriveConfig.set_default_config(self.conf)
        data = dict(self.data)
        data['ignore_files'] = set(data['ignore_files'])
        data['ignore_files'].add('/q')
        data['proxies'] = {'sock5': '1.2.3.4:5'}
        conf2 = drive_config.DriveConfig(data)
        dump2 = conf2.dump()
        self.assertDictEqual({'ignore_files': ['/q']}, dump2)
        conf3 = drive_config.DriveConfig.load(dump2)
        for k in self.data:
            self.assertEqual(getattr(conf2, k), getattr(conf3, k))

    def test_path_filter(self):
        """
        Test if the path filter object is properly instantiated.
        """
        config = drive_config.DriveConfig({
            'ignore_files': [
                os.path.dirname(sys.modules['tests'].__file__) +
                '/data/ignore_list.txt',
                '/tmp/foo'  # bad path should not crash the program
            ]
        })
        filter = config.path_filter
        self.assertIsInstance(filter, path_filter.PathFilter)
        self.assertTrue(filter.should_ignore('/foo'))
        self.assertTrue(filter.should_ignore('/a.swp'))

    def test_add_ignore_file(self):
        path = '/foo/bar'
        self.assertNotIn(
            path, drive_config.DriveConfig.DEFAULT_VALUES['ignore_files'])
        config = drive_config.DriveConfig.default_config()
        config.ignore_files.add(path)
        self.assertNotIn(
            path, drive_config.DriveConfig.DEFAULT_VALUES['ignore_files'])
        self.assertIn(path, config.ignore_files)
        d = config.dump()
        c = drive_config.DriveConfig.load(d)
        self.assertIn(path, c.ignore_files)
示例#10
0
def get_sample_drive_object(data=get_data('drive.json')):
    d = drives.DriveObject(
            root=get_sample_drive_root(), data=data, config=drive_config.DriveConfig(get_data('drive_config.json')))
    return d