Beispiel #1
0
    def test_upload_to_sftp_server_with_different_name_and_location(self, sftp_connection_constructor_mock):
        file_path_to_upload = 'a{0}path{0}to{0}upload{0}'.format(os.sep)
        new_file_path_to_upload = 'a{0}new{0}path{0}to{0}upload{0}file{0}'.format(os.sep)
        new_file_name = 'with_arbitrary_date_2001_01_01.txt' 

        expected_destination_path = "%s%s%s" % (self.__butcket_to_test, os.sep, new_file_path_to_upload + new_file_name)
        expected_uploaded_path = 'sftp://%s:%s@%s:%s/%s' % (self.user,
                                                            self.password,
                                                            self.host,
                                                            self.port,
                                                            expected_destination_path.replace('\\', '/'))

        sftp_connection = FileSystem('sftp', self.__login_arguments)
        file_loaded_url = sftp_connection.upload(
            self.__butcket_to_test, file_path_to_upload + self.__file_name_to_test, new_file_path_to_upload + new_file_name)

        sftp_connection_instance_mock = sftp_connection_constructor_mock.return_value
        sftp_connection_constructor_mock.assert_called_with(self.host,
                                                            username=self.user,
                                                            password=self.password)

        sftp_connection_instance_mock.put.assert_called_with(
            file_path_to_upload + self.__file_name_to_test, expected_destination_path + self.__file_name_to_test)
        
        self.assertEqual(expected_uploaded_path, file_loaded_url)
    def test_upload_to_google_cloud(self, mock_google_connection_service):
        a_google_key_path = 'a/path/to/key.json'
        destination_to_upload = 'a/folder/to_upload/'
        user_config = {'google_cloud_storage_key_path':a_google_key_path}
        mock_blob_manager = self.__prepare_google_mock(mock_google_connection_service)
        
        file_system = FileSystem('google', user_config)
        file_system.upload(self.__butcket_to_test, self.__file_name_to_test, destination_to_upload + self.__file_name_to_test)

        mock_google_connection_service.assert_called_with(a_google_key_path)
        mock_blob_manager.upload_from_filename.assert_called_with(self.__file_name_to_test)
Beispiel #3
0
    def test_download_from_sftp_server(self, sftp_connection_constructor_mock):
        destination = 'a/folder/to_download/'
        remote_path_to_download = "%s%s%s" % (
            self.__butcket_to_test, os.sep, self.__file_name_to_test)
        sftp_connection = FileSystem('sftp', self.__login_arguments)
        sftp_connection.download(self.__butcket_to_test, self.__file_name_to_test, destination)

        sftp_connection_instance_mock = sftp_connection_constructor_mock.return_value

        sftp_connection_constructor_mock.assert_called_with(self.host,
                                                            username=self.user,
                                                            password=self.password)

        sftp_connection_instance_mock.get.assert_called_with(
            remote_path_to_download, destination)
Beispiel #4
0
    def tests_ftp_with_tls_download_file(self, mock_writer,
                                         mock_ftp_constructor):
        expected_ftp_command = 'RETR %s%s%s' % (self.__butcket_to_test, os.sep,
                                                self.__file_name_to_test)
        mock_ftp_instance = self.__prepare_mock_ftp_instance(
            mock_ftp_constructor.return_value)
        mock_ftp_instance.retrbinary.return_value = True

        self.__file_system = FileSystem('ftp_tls', self.__login_arguments)
        self.__file_system.download(self.__butcket_to_test,
                                    self.__file_name_to_test, 'a_destination')

        mock_ftp_constructor.assert_called_with(self.host)
        mock_ftp_instance.login.assert_called_with(self.user, self.password)
        mock_ftp_instance.retrbinary.assert_called_with(
            expected_ftp_command, mock_writer.return_value.write)
Beispiel #5
0
    def tests_ftp_download_file(self, mock_writer, mock_ftp_constructor):
        self.tls_secure = False
        self.__prepare_enviroment()

        expected_ftp_command = 'RETR %s%s%s' % (self.__butcket_to_test, os.sep,
                                                self.__file_name_to_test)
        mock_ftp_instance = mock_ftp_constructor.return_value
        mock_ftp_instance.retrbinary.return_value = True

        self.__file_system = FileSystem('ftp', self.__login_arguments)
        self.__file_system.download(self.__butcket_to_test,
                                    self.__file_name_to_test, 'a_destination')

        mock_ftp_constructor.assert_called_with(host='localhost',
                                                user='******',
                                                passwd='my_password')
        mock_ftp_instance.retrbinary.assert_called_with(
            expected_ftp_command, mock_writer.return_value.write)
Beispiel #6
0
    def tests_ftp_with_tls_upload_file(self, mock_reader,
                                       mock_ftp_constructor):
        destination_file = 'a_destination_file_name.txt'
        expected_ftp_command = 'STOR %s%s%s' % (self.__butcket_to_test, os.sep,
                                                destination_file)
        expected_uploaded_path = 'ftp://%s:%s@%s:%s/%s/%s' % (
            self.user, self.password, self.host, self.port,
            self.__butcket_to_test, destination_file)
        mock_ftp_instance = self.__prepare_mock_ftp_instance(
            mock_ftp_constructor.return_value)
        mock_ftp_instance.storbinary.return_value = True

        self.__file_system = FileSystem('ftp_tls')
        file_loaded_url = self.__file_system.upload(self.__butcket_to_test,
                                                    self.__file_name_to_test,
                                                    destination_file)

        mock_ftp_constructor.assert_called_with(self.host)
        mock_ftp_instance.login.assert_called_with(self.user, self.password)
        mock_ftp_instance.storbinary.assert_called_with(
            expected_ftp_command, mock_reader.return_value)
        self.assertEqual(expected_uploaded_path, file_loaded_url)
Beispiel #7
0
    def tests_ftp_upload_file(self, mock_reader, mock_ftp_constructor):
        self.tls_secure = False
        self.__prepare_enviroment()

        destination_file = 'a_destination_file_name.txt'
        expected_ftp_command = 'STOR %s%s%s' % (self.__butcket_to_test, os.sep,
                                                destination_file)
        expected_uploaded_path = 'ftp://%s:%s@%s:%s/%s/%s' % (
            self.user, self.password, self.host, self.port,
            self.__butcket_to_test, destination_file)
        mock_ftp_instance = mock_ftp_constructor.return_value
        mock_ftp_instance.storbinary.return_value = True

        self.__file_system = FileSystem('ftp', self.__login_arguments)
        file_loaded_url = self.__file_system.upload(self.__butcket_to_test,
                                                    self.__file_name_to_test,
                                                    destination_file)

        mock_ftp_constructor.assert_called_with(host='localhost',
                                                user='******',
                                                passwd='my_password')
        mock_ftp_instance.storbinary.assert_called_with(
            expected_ftp_command, mock_reader.return_value)
        self.assertEqual(expected_uploaded_path, file_loaded_url)
Beispiel #8
0
class TestFtpProvider(TestCase):
    def setUp(self, *args, **kwargs):
        self.host = 'localhost'
        self.port = 21
        self.user = '******'
        self.password = '******'
        self.tls_secure = True
        self.__prepare_enviroment()

        super(TestFtpProvider, self).setUp()

    @mock.patch('cloud.storage.providers.ftp_provider.FTP_TLS', autospec=True)
    @mock.patch('builtins.open', create=True)
    def tests_ftp_with_tls_download_file(self, mock_writer,
                                         mock_ftp_constructor):
        expected_ftp_command = 'RETR %s%s%s' % (self.__butcket_to_test, os.sep,
                                                self.__file_name_to_test)
        mock_ftp_instance = self.__prepare_mock_ftp_instance(
            mock_ftp_constructor.return_value)
        mock_ftp_instance.retrbinary.return_value = True

        self.__file_system = FileSystem('ftp_tls', self.__login_arguments)
        self.__file_system.download(self.__butcket_to_test,
                                    self.__file_name_to_test, 'a_destination')

        mock_ftp_constructor.assert_called_with(self.host)
        mock_ftp_instance.login.assert_called_with(self.user, self.password)
        mock_ftp_instance.retrbinary.assert_called_with(
            expected_ftp_command, mock_writer.return_value.write)

    @mock.patch('cloud.storage.providers.ftp_provider.FTP_TLS', autospec=True)
    @mock.patch('builtins.open', create=True)
    def tests_ftp_with_tls_upload_file(self, mock_reader,
                                       mock_ftp_constructor):
        destination_file = 'a_destination_file_name.txt'
        expected_ftp_command = 'STOR %s%s%s' % (self.__butcket_to_test, os.sep,
                                                destination_file)
        expected_uploaded_path = 'ftp://%s:%s@%s:%s/%s/%s' % (
            self.user, self.password, self.host, self.port,
            self.__butcket_to_test, destination_file)
        mock_ftp_instance = self.__prepare_mock_ftp_instance(
            mock_ftp_constructor.return_value)
        mock_ftp_instance.storbinary.return_value = True

        self.__file_system = FileSystem('ftp_tls')
        file_loaded_url = self.__file_system.upload(self.__butcket_to_test,
                                                    self.__file_name_to_test,
                                                    destination_file)

        mock_ftp_constructor.assert_called_with(self.host)
        mock_ftp_instance.login.assert_called_with(self.user, self.password)
        mock_ftp_instance.storbinary.assert_called_with(
            expected_ftp_command, mock_reader.return_value)
        self.assertEqual(expected_uploaded_path, file_loaded_url)

    @mock.patch('cloud.storage.providers.ftp_provider.FTP', autospec=True)
    @mock.patch('builtins.open', create=True)
    def tests_ftp_upload_file(self, mock_reader, mock_ftp_constructor):
        self.tls_secure = False
        self.__prepare_enviroment()

        destination_file = 'a_destination_file_name.txt'
        expected_ftp_command = 'STOR %s%s%s' % (self.__butcket_to_test, os.sep,
                                                destination_file)
        expected_uploaded_path = 'ftp://%s:%s@%s:%s/%s/%s' % (
            self.user, self.password, self.host, self.port,
            self.__butcket_to_test, destination_file)
        mock_ftp_instance = mock_ftp_constructor.return_value
        mock_ftp_instance.storbinary.return_value = True

        self.__file_system = FileSystem('ftp', self.__login_arguments)
        file_loaded_url = self.__file_system.upload(self.__butcket_to_test,
                                                    self.__file_name_to_test,
                                                    destination_file)

        mock_ftp_constructor.assert_called_with(host='localhost',
                                                user='******',
                                                passwd='my_password')
        mock_ftp_instance.storbinary.assert_called_with(
            expected_ftp_command, mock_reader.return_value)
        self.assertEqual(expected_uploaded_path, file_loaded_url)

    @mock.patch('cloud.storage.providers.ftp_provider.FTP', autospec=True)
    @mock.patch('builtins.open', create=True)
    def tests_ftp_download_file(self, mock_writer, mock_ftp_constructor):
        self.tls_secure = False
        self.__prepare_enviroment()

        expected_ftp_command = 'RETR %s%s%s' % (self.__butcket_to_test, os.sep,
                                                self.__file_name_to_test)
        mock_ftp_instance = mock_ftp_constructor.return_value
        mock_ftp_instance.retrbinary.return_value = True

        self.__file_system = FileSystem('ftp', self.__login_arguments)
        self.__file_system.download(self.__butcket_to_test,
                                    self.__file_name_to_test, 'a_destination')

        mock_ftp_constructor.assert_called_with(host='localhost',
                                                user='******',
                                                passwd='my_password')
        mock_ftp_instance.retrbinary.assert_called_with(
            expected_ftp_command, mock_writer.return_value.write)

    def __prepare_enviroment(self):
        self.__butcket_to_test = 'a_bucket'
        self.__file_name_to_test = 'a_file.txt'
        self.__login_arguments = {
            'host': self.host,
            'port': self.port,
            'user': self.user,
            'password': self.password,
            'tls': self.tls_secure
        }

    def __prepare_mock_ftp_instance(self, mock_ftp_instance):
        mock_ftp_instance.login.return_value = True
        mock_ftp_instance.prot_p.return_value = True

        return mock_ftp_instance