def test_bucket_is_downloaded(self, aws_session_mock):
        """ download object in bucket """
        aws_session_mock.return_value.check_bucket_exists.return_value = True
        aws_session_mock.return_value.download_object_from_bucket.return_value = False

        filename = 'aaa.txt'
        bucket_name = 'aarrrp'
        dest_option = '--destination-path'
        destination_path = str(os.getcwd())

        f = io.StringIO()
        with redirect_stdout(f):
            download_main([
                self.command_name, filename, bucket_name, dest_option,
                destination_path
            ])

        self.assertIn('downloading object {0} ...'.format(filename),
                      f.getvalue())

        destination_path = os.path.join(destination_path, filename)
        aws_session_mock.return_value.download_object_from_bucket.assert_called_once(
        )
        aws_session_mock.return_value.download_object_from_bucket.assert_called_with(
            filename, bucket_name, destination_path)
    def test_bucket_does_not_exist(self, aws_session_mock):
        """ bucket does not exist """
        aws_session_mock.return_value.check_bucket_exists.return_value = False

        filename = 'aaa.txt'
        bucket_name = 'aarrrp'

        with self.assertLogs('download_from_s3', level='INFO') as f:
            with self.assertRaises(SystemExit):
                download_main([self.command_name, filename, bucket_name])

        self.assertIn(f'Bucket \'{bucket_name}\' does not exist', f.output[0])
    def test_bucket_does_not_exist(self, aws_session_mock):
        """ bucket does not exist """
        aws_session_mock.return_value.check_bucket_exists.return_value = False

        filename = 'aaa.txt'
        bucket_name = 'aarrrp'

        f = io.StringIO()
        with redirect_stdout(f):
            with self.assertRaises(SystemExit):
                download_main([self.command_name, filename, bucket_name])

        self.assertIn('Bucket \'{0}\' does not exist'.format(bucket_name),
                      f.getvalue())
    def test_path_is_not_valid(self):
        """ path is not valid """
        filename = 'aaa.txt'
        bucket_name = 'aarrrp'
        dest_option = '--destination-path'
        destination_path = 'asdasd-sf-sf'

        f = io.StringIO()
        with self.assertLogs('download_from_s3', level='INFO') as f:
            with self.assertRaises(SystemExit):
                download_main([
                    self.command_name, filename, bucket_name, dest_option,
                    destination_path
                ])

        self.assertIn(f'Path \'{destination_path}\' is not valid', f.output[0])
    def test_path_is_not_valid(self):
        """ path is not valid """
        filename = 'aaa.txt'
        bucket_name = 'aarrrp'
        dest_option = '--destination-path'
        destination_path = 'asdasd-sf-sf'

        f = io.StringIO()
        with redirect_stdout(f):
            with self.assertRaises(SystemExit):
                download_main([
                    self.command_name, filename, bucket_name, dest_option,
                    destination_path
                ])

        self.assertIn('Path \'{0}\' is not valid'.format(destination_path),
                      f.getvalue())
    def test_bucket_raise_error_when_is_downloading(self, aws_session_mock):
        """ download object in bucket """
        operation_name = 'download'
        error_response = dict(Error=dict(Code=403, Message='forbidden'))
        aws_session_mock.return_value.check_bucket_exists.return_value = True
        aws_session_mock.return_value.download_object_from_bucket.side_effect = ClientError(
            error_response, operation_name)

        filename = 'aaa.txt'
        bucket_name = 'aarrrp'

        with self.assertLogs('download_from_s3', level='INFO') as f:
            download_main([self.command_name, filename, bucket_name])

        expected_answer = f"An error occurred ({error_response['Error']['Code']}) when calling the download operation: {error_response['Error']['Message']}"
        self.assertIn(expected_answer, f.output[1])

        aws_session_mock.return_value.download_object_from_bucket.assert_called_once(
        )
        aws_session_mock.return_value.download_object_from_bucket.assert_called_with(
            filename, bucket_name, filename)
    def test_bucket_is_downloaded(self, aws_session_mock):
        """ download object in bucket """
        aws_session_mock.return_value.check_bucket_exists.return_value = True
        aws_session_mock.return_value.download_object_from_bucket.return_value = False

        filename = 'aaa.txt'
        bucket_name = 'aarrrp'
        dest_option = '--destination-path'
        destination_path = str(os.getcwd())

        with self.assertLogs('download_from_s3', level='INFO') as f:
            download_main([
                self.command_name, filename, bucket_name, dest_option,
                destination_path
            ])

        self.assertIn(f'downloading object {filename} ...', f.output[0])

        destination_path = os.path.join(destination_path, filename)
        aws_session_mock.return_value.download_object_from_bucket.assert_called_once(
        )
        aws_session_mock.return_value.download_object_from_bucket.assert_called_with(
            filename, bucket_name, destination_path)
    def test_bucket_raise_error_when_is_downloading(self, aws_session_mock):
        """ download object in bucket """
        operation_name = 'download'
        error_response = dict(Error=dict(Code=403, Message='forbidden'))
        aws_session_mock.return_value.check_bucket_exists.return_value = True
        aws_session_mock.return_value.download_object_from_bucket.side_effect = ClientError(
            error_response, operation_name)

        filename = 'aaa.txt'
        bucket_name = 'aarrrp'

        f = io.StringIO()
        with redirect_stdout(f):
            download_main([self.command_name, filename, bucket_name])

        expected_answer = 'An error occurred ({0}) when calling the download operation: {1}'.format(
            error_response['Error']['Code'],
            error_response['Error']['Message'])
        self.assertIn(expected_answer, f.getvalue())

        aws_session_mock.return_value.download_object_from_bucket.assert_called_once(
        )
        aws_session_mock.return_value.download_object_from_bucket.assert_called_with(
            filename, bucket_name, filename)
    def test_with_one_param(self):
        filename = 'aaa.txt'

        with self.assertRaises(SystemExit):
            download_main([self.command_name, filename])
 def test_without_params(self):
     with self.assertRaises(SystemExit):
         download_main([self.command_name])