Tests#

Base test case and Paramiko mock utilities for core-ftp unit tests.

class core_ftp.tests.base.BaseFtpTestCase(*args, **kwargs)[source]#

Bases: BaseTestCase

Base class for Test Cases related to FTP connections.

This class provides comprehensive mocking infrastructure for SFTP testing by patching Paramiko’s transport and client operations. It simulates realistic SFTP behavior using local filesystem operations.

Mock Behavior:
  • Transport operations: Fully mocked with no real network connections.

  • SFTP client operations: Mapped to local filesystem equivalents.

  • Directory operations: Virtual directory tracking with get_cwd/chdir.

  • File operations: Actual file I/O using local test resources.

  • Authentication: Bypassed with successful mock responses.

Usage:
>>> class MyFtpTest(BaseFtpTestCase):
...     def test_upload(self):
...         client = SftpClient("host", user="user", password="pass")
...         client.connect()  # Uses mocked transport
...         client.upload_file("local.txt", "remote.txt")  # Uses mock
Test Isolation:
  • No real network connections made.

  • Working directory changes are virtual only.

  • Clean setup/teardown of all mocks.

  • Safe for parallel test execution.

init_transport_mock = None#
connect_transport_mock = None#
from_private_key_mock = None#
close_transport_mock = None#
from_transport_mock = None#
init_transport_patcher = <unittest.mock._patch object>#
connect_transport_patcher = <unittest.mock._patch object>#
from_transport_patcher = <unittest.mock._patch object>#
from_private_key_patcher = <unittest.mock._patch object>#
close_transport_patcher = <unittest.mock._patch object>#
_cwd = ''#
_root_path = ''#
classmethod setUpClass() None[source]#

Sets up class-level mocks for all test methods.

Initializes and starts all Paramiko patches to intercept SSH/SFTP operations. Establishes the virtual filesystem root and configures mock return values for successful connection simulation.

Mock Setup:
  • Transport initialization: Returns None (bypasses real network setup)

  • Transport connection: Returns None (bypasses authentication)

  • Private key loading: Returns None (bypasses key file reading)

  • Transport closing: Returns None (bypasses connection cleanup)

  • SFTP client creation: Returns configured mock client

Virtual Environment:
  • Sets root path to current working directory

  • Initializes virtual current working directory as empty

classmethod tearDownClass() None[source]#

Cleans up all mock patches after test completion.

Stops all Paramiko patches to restore original functionality and prevent mock leakage to other test classes.

Cleanup Operations:
  • Stops transport initialization patch

  • Stops transport connection patch

  • Stops private key loading patch

  • Stops SFTP client creation patch

  • Stops transport closing patch

classmethod get_client_mock()[source]#

Creates and configures the mock SFTP client.

Returns a Mock object that simulates Paramiko’s SFTPClient behavior by mapping SFTP operations to local filesystem operations.

Returns:

Configured mock SFTP client

Return type:

Mock

classmethod get_cwd()[source]#

Simulates SFTP getcwd() operation.

Returns the virtual current working directory or defaults to the test resources directory.

Returns:

Current working directory path

Return type:

str

classmethod chdir(remote_path: str)[source]#

Simulates SFTP chdir() operation with virtual directory tracking.

Changes only the virtual working directory without affecting the real filesystem working directory.

Parameters:

remote_path (str) – Path to change to

classmethod list_dir_attr(remote_path: str)[source]#

Simulates SFTP listdir_attr() operation.

Lists files in the specified local directory and wraps each filename in an SFTPAttributes object to match Paramiko’s API.

Parameters:

remote_path (str) – Directory path to list

Returns:

Generator of SFTPAttributes objects

Return type:

Iterator[SFTPAttributes]

static get(_remote_path: str, local_path: str, **_kwargs)[source]#

Simulates SFTP get() operation (file download).

Creates a local file with test content to simulate downloading a file from the remote server.

Parameters:
  • _remote_path (str) – Path to remote file (unused in mock)

  • local_path (str) – Local path where file will be created

  • _kwargs – Additional arguments (unused in mock)

static put(file_path, *args, **kwargs)[source]#

Simulates SFTP put() operation (file upload).

Stub method for upload operations. Override in test subclasses if specific upload behavior testing is needed.

Parameters:
  • file_path – Local file path to upload

  • args – Additional arguments

  • kwargs – Additional keyword arguments

static put_fo(file_like_object, remote_path, *args, **kwargs)[source]#

Simulates SFTP putfo() operation (file-like object upload).

Stub method for file-like object upload operations. Override in test subclasses if specific upload behavior testing is needed.

Parameters:
  • file_like_object – File-like object to upload

  • remote_path – Remote path for upload

  • args – Additional arguments

  • kwargs – Additional keyword arguments

static rmdir(path: str)[source]#

Simulates SFTP rmdir() operation (directory removal).

Stub method for directory removal operations. Override in test subclasses if specific deletion behavior testing is needed.

Parameters:

path (str) – Directory path to remove

static remove(path: str)[source]#

Simulates SFTP remove() operation (file removal).

Stub method for file removal operations. Override in test subclasses if specific deletion behavior testing is needed.

Parameters:

path (str) – File path to remove

_classSetupFailed = False#
_class_cleanups = []#